198 lines
6.4 KiB
Plaintext
198 lines
6.4 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": [
|
|
"# integer_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/integer_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/integer_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": [
|
|
"\n",
|
|
"Integer 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(\n",
|
|
" \"---- Integer programming example with \" + solver + \" (\" + api_type + \") -----\"\n",
|
|
" )\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type):\n",
|
|
" \"\"\"Example of simple integer program with natural language API.\"\"\"\n",
|
|
"\n",
|
|
" solver = pywraplp.Solver.CreateSolver(optimization_problem_type)\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" Announce(optimization_problem_type, \"natural language API\")\n",
|
|
"\n",
|
|
" infinity = solver.infinity()\n",
|
|
" # x1 and x2 are integer non-negative variables.\n",
|
|
" x1 = solver.IntVar(0.0, infinity, \"x1\")\n",
|
|
" x2 = solver.IntVar(0.0, infinity, \"x2\")\n",
|
|
"\n",
|
|
" solver.Minimize(x1 + 2 * x2)\n",
|
|
" solver.Add(3 * x1 + 2 * x2 >= 17)\n",
|
|
"\n",
|
|
" SolveAndPrint(solver, [x1, x2])\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunIntegerExampleCppStyleAPI(optimization_problem_type):\n",
|
|
" \"\"\"Example of simple integer 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 and x2 are integer non-negative variables.\n",
|
|
" x1 = solver.IntVar(0.0, infinity, \"x1\")\n",
|
|
" x2 = solver.IntVar(0.0, infinity, \"x2\")\n",
|
|
"\n",
|
|
" # Minimize x1 + 2 * x2.\n",
|
|
" objective = solver.Objective()\n",
|
|
" objective.SetCoefficient(x1, 1)\n",
|
|
" objective.SetCoefficient(x2, 2)\n",
|
|
"\n",
|
|
" # 2 * x2 + 3 * x1 >= 17.\n",
|
|
" ct = solver.Constraint(17, infinity)\n",
|
|
" ct.SetCoefficient(x1, 3)\n",
|
|
" ct.SetCoefficient(x2, 2)\n",
|
|
"\n",
|
|
" SolveAndPrint(solver, [x1, x2])\n",
|
|
"\n",
|
|
"\n",
|
|
"def SolveAndPrint(solver, variable_list):\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",
|
|
" 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 branch-and-bound nodes\" % solver.nodes())\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunAllIntegerExampleNaturalLanguageAPI():\n",
|
|
" RunIntegerExampleNaturalLanguageAPI(\"GLPK\")\n",
|
|
" # Disabling due to ASAN errors with CBC.\n",
|
|
" # RunIntegerExampleNaturalLanguageAPI('CBC')\n",
|
|
" RunIntegerExampleNaturalLanguageAPI(\"SCIP\")\n",
|
|
" RunIntegerExampleNaturalLanguageAPI(\"SAT\")\n",
|
|
"\n",
|
|
"\n",
|
|
"def RunAllIntegerExampleCppStyleAPI():\n",
|
|
" RunIntegerExampleCppStyleAPI(\"GLPK\")\n",
|
|
" # Disabling due to ASAN errors with CBC.\n",
|
|
" # RunIntegerExampleCppStyleAPI('CBC')\n",
|
|
" RunIntegerExampleCppStyleAPI(\"SCIP\")\n",
|
|
" RunIntegerExampleCppStyleAPI(\"SAT\")\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" RunAllIntegerExampleNaturalLanguageAPI()\n",
|
|
" RunAllIntegerExampleCppStyleAPI()\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|