Files
ortools-clone/examples/notebook/examples/integer_programming.ipynb
Corentin Le Molgat 27121a1068 Update examples/notebook
generated using ./tools/gen_all_notebook.sh
2020-03-04 14:34:33 +01:00

154 lines
6.8 KiB
Plaintext

{
"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",
"\"\"\"Integer programming examples that show how to use the APIs.\"\"\"\n",
"\n",
"from __future__ import print_function\n",
"\n",
"from ortools.linear_solver import pywraplp\n",
"\n",
"\n",
"def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type):\n",
" \"\"\"Example of simple integer program with natural language API.\"\"\"\n",
" solver = pywraplp.Solver('RunIntegerExampleNaturalLanguageAPI',\n",
" optimization_problem_type)\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('RunIntegerExampleCppStyleAPI',\n",
" optimization_problem_type)\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",
" solver.SetNumThreads(8)\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 Announce(solver, api_type):\n",
" print('---- Integer programming example with ' + solver + ' (' + api_type +\n",
" ') -----')\n",
"\n",
"\n",
"def RunAllIntegerExampleNaturalLanguageAPI():\n",
" if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('GLPK', 'natural language API')\n",
" RunIntegerExampleNaturalLanguageAPI(\n",
" pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('CBC', 'natural language API')\n",
" RunIntegerExampleNaturalLanguageAPI(\n",
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('SCIP', 'natural language API')\n",
" RunIntegerExampleNaturalLanguageAPI(\n",
" pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('GUROBI', 'natural language API')\n",
" RunIntegerExampleNaturalLanguageAPI(\n",
" pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('CPLEX', 'natural language API')\n",
" RunIntegerExampleNaturalLanguageAPI(\n",
" pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)\n",
"\n",
"\n",
"def RunAllIntegerExampleCppStyleAPI():\n",
" if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('GLPK', 'C++ style API')\n",
" RunIntegerExampleCppStyleAPI(\n",
" pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('CBC', 'C++ style API')\n",
" RunIntegerExampleCppStyleAPI(\n",
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('SCIP', 'C++ style API')\n",
" RunIntegerExampleCppStyleAPI(\n",
" pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('GUROBI', 'C++ style API')\n",
" RunIntegerExampleCppStyleAPI(\n",
" pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)\n",
" if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):\n",
" Announce('CPLEX', 'C++ style API')\n",
" RunIntegerExampleCppStyleAPI(\n",
" pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)\n",
"\n",
"\n",
"RunAllIntegerExampleNaturalLanguageAPI()\n",
"RunAllIntegerExampleCppStyleAPI()\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}