Files
ortools-clone/examples/notebook/linear_solver/integer_programming_example.ipynb

95 lines
3.5 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2018 Google LLC\n",
"# 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",
"\"\"\"Small example to illustrate solving a MIP problem.\"\"\"\n",
"# [START program]\n",
"from __future__ import print_function\n",
"# [START import]\n",
"from ortools.linear_solver import pywraplp\n",
"# [END import]\n",
"\n",
"\n",
"def IntegerProgrammingExample():\n",
" \"\"\"Integer programming sample.\"\"\"\n",
" # [START solver]\n",
" # Create the mip solver with the CBC backend.\n",
" solver = pywraplp.Solver('IntegerProgrammingExample',\n",
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)\n",
" # [END solver]\n",
"\n",
" # [START variables]\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",
" # [END variables]\n",
"\n",
" # [START constraints]\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",
" # [END constraints]\n",
"\n",
" # [START objective]\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",
" # [END objective]\n",
"\n",
" # Solve the problem and print the solution.\n",
" # [START print_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",
" # [END print_solution]\n",
"\n",
"\n",
"IntegerProgrammingExample()\n",
"# [END program]\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}