219 lines
7.8 KiB
Plaintext
219 lines
7.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2022 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"
|
|
]
|
|
},
|
|
{
|
|
"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/examples/linear_programming.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/examples/python/linear_programming.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": [
|
|
"Linear programming examples that show how to use the APIs.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def Announce(solver, api_type):\n",
|
|
" print('---- Linear programming example with ' + solver + ' (' + api_type +\n",
|
|
" ') -----')\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):\n",
|
|
" \"\"\"Example of simple linear program with natural language API.\"\"\"\n",
|
|
" solver = pywraplp.Solver.CreateSolver(optimization_problem_type)\n",
|
|
"\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" Announce(optimization_problem_type, 'natural language API')\n",
|
|
"\n",
|
|
" infinity = solver.infinity()\n",
|
|
" # x1, x2 and x3 are continuous non-negative variables.\n",
|
|
" x1 = solver.NumVar(0.0, infinity, 'x1')\n",
|
|
" x2 = solver.NumVar(0.0, infinity, 'x2')\n",
|
|
" x3 = solver.NumVar(0.0, infinity, 'x3')\n",
|
|
"\n",
|
|
" solver.Maximize(10 * x1 + 6 * x2 + 4 * x3)\n",
|
|
" c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, 'ConstraintName0')\n",
|
|
" c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300)\n",
|
|
" sum_of_vars = sum([x1, x2, x3])\n",
|
|
" c2 = solver.Add(sum_of_vars <= 100.0, 'OtherConstraintName')\n",
|
|
"\n",
|
|
" SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != 'PDLP')\n",
|
|
" # Print a linear expression's solution value.\n",
|
|
" print('Sum of vars: %s = %s' % (sum_of_vars, sum_of_vars.solution_value()))\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunLinearExampleCppStyleAPI(optimization_problem_type):\n",
|
|
" \"\"\"Example of simple linear program with the C++ style API.\"\"\"\n",
|
|
" solver = pywraplp.Solver.CreateSolver(optimization_problem_type)\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" Announce(optimization_problem_type, 'C++ style API')\n",
|
|
"\n",
|
|
" infinity = solver.infinity()\n",
|
|
" # x1, x2 and x3 are continuous non-negative variables.\n",
|
|
" x1 = solver.NumVar(0.0, infinity, 'x1')\n",
|
|
" x2 = solver.NumVar(0.0, infinity, 'x2')\n",
|
|
" x3 = solver.NumVar(0.0, infinity, 'x3')\n",
|
|
"\n",
|
|
" # Maximize 10 * x1 + 6 * x2 + 4 * x3.\n",
|
|
" objective = solver.Objective()\n",
|
|
" objective.SetCoefficient(x1, 10)\n",
|
|
" objective.SetCoefficient(x2, 6)\n",
|
|
" objective.SetCoefficient(x3, 4)\n",
|
|
" objective.SetMaximization()\n",
|
|
"\n",
|
|
" # x1 + x2 + x3 <= 100.\n",
|
|
" c0 = solver.Constraint(-infinity, 100.0, 'c0')\n",
|
|
" c0.SetCoefficient(x1, 1)\n",
|
|
" c0.SetCoefficient(x2, 1)\n",
|
|
" c0.SetCoefficient(x3, 1)\n",
|
|
"\n",
|
|
" # 10 * x1 + 4 * x2 + 5 * x3 <= 600.\n",
|
|
" c1 = solver.Constraint(-infinity, 600.0, 'c1')\n",
|
|
" c1.SetCoefficient(x1, 10)\n",
|
|
" c1.SetCoefficient(x2, 4)\n",
|
|
" c1.SetCoefficient(x3, 5)\n",
|
|
"\n",
|
|
" # 2 * x1 + 2 * x2 + 6 * x3 <= 300.\n",
|
|
" c2 = solver.Constraint(-infinity, 300.0, 'c2')\n",
|
|
" c2.SetCoefficient(x1, 2)\n",
|
|
" c2.SetCoefficient(x2, 2)\n",
|
|
" c2.SetCoefficient(x3, 6)\n",
|
|
"\n",
|
|
" SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2],\n",
|
|
" optimization_problem_type != 'PDLP')\n",
|
|
"\n",
|
|
"\n",
|
|
"def SolveAndPrint(solver, variable_list, constraint_list, is_precise):\n",
|
|
" \"\"\"Solve the problem and print the solution.\"\"\"\n",
|
|
" print('Number of variables = %d' % solver.NumVariables())\n",
|
|
" print('Number of constraints = %d' % solver.NumConstraints())\n",
|
|
"\n",
|
|
" result_status = solver.Solve()\n",
|
|
"\n",
|
|
" # The problem has an optimal solution.\n",
|
|
" assert result_status == pywraplp.Solver.OPTIMAL\n",
|
|
"\n",
|
|
" # The solution looks legit (when using solvers others than\n",
|
|
" # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).\n",
|
|
" if is_precise:\n",
|
|
" assert solver.VerifySolution(1e-7, True)\n",
|
|
"\n",
|
|
" print('Problem solved in %f milliseconds' % solver.wall_time())\n",
|
|
"\n",
|
|
" # The objective value of the solution.\n",
|
|
" print('Optimal objective value = %f' % solver.Objective().Value())\n",
|
|
"\n",
|
|
" # The value of each variable in the solution.\n",
|
|
" for variable in variable_list:\n",
|
|
" print('%s = %f' % (variable.name(), variable.solution_value()))\n",
|
|
"\n",
|
|
" print('Advanced usage:')\n",
|
|
" print('Problem solved in %d iterations' % solver.iterations())\n",
|
|
" for variable in variable_list:\n",
|
|
" print('%s: reduced cost = %f' %\n",
|
|
" (variable.name(), variable.reduced_cost()))\n",
|
|
" activities = solver.ComputeConstraintActivities()\n",
|
|
" for i, constraint in enumerate(constraint_list):\n",
|
|
" print(('constraint %d: dual value = %f\\n'\n",
|
|
" ' activity = %f' %\n",
|
|
" (i, constraint.dual_value(), activities[constraint.index()])))\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" RunLinearExampleNaturalLanguageAPI('GLOP')\n",
|
|
" RunLinearExampleNaturalLanguageAPI('GLPK_LP')\n",
|
|
" RunLinearExampleNaturalLanguageAPI('CLP')\n",
|
|
" RunLinearExampleNaturalLanguageAPI('PDLP')\n",
|
|
"\n",
|
|
" RunLinearExampleCppStyleAPI('GLOP')\n",
|
|
" RunLinearExampleCppStyleAPI('GLPK_LP')\n",
|
|
" RunLinearExampleCppStyleAPI('CLP')\n",
|
|
" RunLinearExampleCppStyleAPI('PDLP')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|