145 lines
4.5 KiB
Plaintext
145 lines
4.5 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_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/integer_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/integer_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",
|
|
"Small example to illustrate solving a MIP problem."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def IntegerProgrammingExample():\n",
|
|
" \"\"\"Integer programming sample.\"\"\"\n",
|
|
" # Create the mip solver with the SCIP backend.\n",
|
|
" solver = pywraplp.Solver.CreateSolver(\"SCIP\")\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" # x, y, and z are non-negative integer variables.\n",
|
|
" x = solver.IntVar(0.0, solver.infinity(), \"x\")\n",
|
|
" y = solver.IntVar(0.0, solver.infinity(), \"y\")\n",
|
|
" z = solver.IntVar(0.0, solver.infinity(), \"z\")\n",
|
|
"\n",
|
|
" # 2*x + 7*y + 3*z <= 50\n",
|
|
" constraint0 = solver.Constraint(-solver.infinity(), 50)\n",
|
|
" constraint0.SetCoefficient(x, 2)\n",
|
|
" constraint0.SetCoefficient(y, 7)\n",
|
|
" constraint0.SetCoefficient(z, 3)\n",
|
|
"\n",
|
|
" # 3*x - 5*y + 7*z <= 45\n",
|
|
" constraint1 = solver.Constraint(-solver.infinity(), 45)\n",
|
|
" constraint1.SetCoefficient(x, 3)\n",
|
|
" constraint1.SetCoefficient(y, -5)\n",
|
|
" constraint1.SetCoefficient(z, 7)\n",
|
|
"\n",
|
|
" # 5*x + 2*y - 6*z <= 37\n",
|
|
" constraint2 = solver.Constraint(-solver.infinity(), 37)\n",
|
|
" constraint2.SetCoefficient(x, 5)\n",
|
|
" constraint2.SetCoefficient(y, 2)\n",
|
|
" constraint2.SetCoefficient(z, -6)\n",
|
|
"\n",
|
|
" # Maximize 2*x + 2*y + 3*z\n",
|
|
" objective = solver.Objective()\n",
|
|
" objective.SetCoefficient(x, 2)\n",
|
|
" objective.SetCoefficient(y, 2)\n",
|
|
" objective.SetCoefficient(z, 3)\n",
|
|
" objective.SetMaximization()\n",
|
|
"\n",
|
|
" # Solve the problem and print the solution.\n",
|
|
" solver.Solve()\n",
|
|
" # Print the objective value of the solution.\n",
|
|
" print(\"Maximum objective function value = %d\" % solver.Objective().Value())\n",
|
|
" print()\n",
|
|
" # Print the value of each variable in the solution.\n",
|
|
" for variable in [x, y, z]:\n",
|
|
" print(\"%s = %d\" % (variable.name(), variable.solution_value()))\n",
|
|
"\n",
|
|
"\n",
|
|
"IntegerProgrammingExample()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|