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

163 lines
5.5 KiB
Plaintext
Raw Normal View History

{
"cells": [
2020-09-27 16:03:47 +02:00
{
"cell_type": "markdown",
2021-12-06 10:19:50 +01:00
"id": "fd3017c4",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
2021-04-01 20:14:10 +02:00
"##### Copyright 2021 Google LLC."
2020-09-27 16:03:47 +02:00
]
},
{
"cell_type": "markdown",
2021-12-06 10:19:50 +01:00
"id": "582e7da9",
2020-09-27 16:03:47 +02:00
"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",
2021-12-06 10:19:50 +01:00
"id": "f4061fe7",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"# integer_programming_example"
]
},
{
"cell_type": "markdown",
2021-12-06 10:19:50 +01:00
"id": "d578fdc4",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/linear_solver/integer_programming_example.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/master/ortools/linear_solver/samples/integer_programming_example.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-06 10:19:50 +01:00
"id": "c4b8d234",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-06 10:19:50 +01:00
"id": "9f1cb762",
2020-09-27 16:03:47 +02:00
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-06 10:19:50 +01:00
"id": "2ccae1ba",
"metadata": {},
"outputs": [],
"source": [
2021-04-27 10:56:28 +02:00
"#!/usr/bin/env python3\n",
2021-04-02 10:08:51 +02:00
"# Copyright 2010-2021 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",
"# [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",
2020-09-27 16:03:47 +02:00
" # Create the mip solver with the SCIP backend.\n",
" solver = pywraplp.Solver.CreateSolver('SCIP')\n",
"\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,
2021-09-30 01:24:08 +02:00
"nbformat_minor": 5
}