146 lines
4.3 KiB
Plaintext
146 lines
4.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"metadata": {},
|
|
"source": [
|
|
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"you may not use this file except in compliance with the License.\n",
|
|
"You may obtain a copy of the License at\n",
|
|
"\n",
|
|
" http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"\n",
|
|
"Unless required by applicable law or agreed to in writing, software\n",
|
|
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"See the License for the specific language governing permissions and\n",
|
|
"limitations under the License.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# linear_programming_example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/linear_solver/linear_programming_example.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/main/ortools/linear_solver/samples/linear_programming_example.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Linear optimization example."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def LinearProgrammingExample():\n",
|
|
" \"\"\"Linear programming sample.\"\"\"\n",
|
|
" # Instantiate a Glop solver, naming it LinearExample.\n",
|
|
" solver = pywraplp.Solver.CreateSolver(\"GLOP\")\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" # Create the two variables and let them take on any non-negative value.\n",
|
|
" x = solver.NumVar(0, solver.infinity(), \"x\")\n",
|
|
" y = solver.NumVar(0, solver.infinity(), \"y\")\n",
|
|
"\n",
|
|
" print(\"Number of variables =\", solver.NumVariables())\n",
|
|
"\n",
|
|
" # Constraint 0: x + 2y <= 14.\n",
|
|
" solver.Add(x + 2 * y <= 14.0)\n",
|
|
"\n",
|
|
" # Constraint 1: 3x - y >= 0.\n",
|
|
" solver.Add(3 * x - y >= 0.0)\n",
|
|
"\n",
|
|
" # Constraint 2: x - y <= 2.\n",
|
|
" solver.Add(x - y <= 2.0)\n",
|
|
"\n",
|
|
" print(\"Number of constraints =\", solver.NumConstraints())\n",
|
|
"\n",
|
|
" # Objective function: 3x + 4y.\n",
|
|
" solver.Maximize(3 * x + 4 * y)\n",
|
|
"\n",
|
|
" # Solve the system.\n",
|
|
" print(f\"Solving with {solver.SolverVersion()}\")\n",
|
|
" status = solver.Solve()\n",
|
|
"\n",
|
|
" if status == pywraplp.Solver.OPTIMAL:\n",
|
|
" print(\"Solution:\")\n",
|
|
" print(f\"Objective value = {solver.Objective().Value():0.1f}\")\n",
|
|
" print(f\"x = {x.solution_value():0.1f}\")\n",
|
|
" print(f\"y = {y.solution_value():0.1f}\")\n",
|
|
" else:\n",
|
|
" print(\"The problem does not have an optimal solution.\")\n",
|
|
"\n",
|
|
" print(\"\\nAdvanced usage:\")\n",
|
|
" print(f\"Problem solved in {solver.wall_time():d} milliseconds\")\n",
|
|
" print(f\"Problem solved in {solver.iterations():d} iterations\")\n",
|
|
"\n",
|
|
"\n",
|
|
"LinearProgrammingExample()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|