Files
ortools-clone/examples/notebook/linear_solver/assignment_mb.ipynb
Corentin Le Molgat 4908fd2fb4 update python notebooks
2022-11-02 10:12:37 +01:00

155 lines
4.7 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": [
"# assignment_mb"
]
},
{
"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/assignment_mb.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/assignment_mb.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": [
"MIP example that solves an assignment problem."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.linear_solver.python import model_builder\n",
"\n",
"\n",
"def main():\n",
" # Data\n",
" costs = [\n",
" [90, 80, 75, 70],\n",
" [35, 85, 55, 65],\n",
" [125, 95, 90, 95],\n",
" [45, 110, 95, 115],\n",
" [50, 100, 90, 100],\n",
" ]\n",
" num_workers = len(costs)\n",
" num_tasks = len(costs[0])\n",
"\n",
" # Solver\n",
" # Create the model.\n",
" model = model_builder.ModelBuilder()\n",
"\n",
" # Variables\n",
" # x[i, j] is an array of 0-1 variables, which will be 1\n",
" # if worker i is assigned to task j.\n",
" x = {}\n",
" for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" x[i, j] = model.new_bool_var(f'x_{i}_{j}')\n",
"\n",
" # Constraints\n",
" # Each worker is assigned to at most 1 task.\n",
" for i in range(num_workers):\n",
" model.add(sum(x[i, j] for j in range(num_tasks)) <= 1)\n",
"\n",
" # Each task is assigned to exactly one worker.\n",
" for j in range(num_tasks):\n",
" model.add(sum(x[i, j] for i in range(num_workers)) == 1)\n",
"\n",
" # Objective\n",
" objective_expr = 0\n",
" for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" objective_expr += costs[i][j] * x[i, j]\n",
" model.minimize(objective_expr)\n",
"\n",
" # Create the solver with the CP-SAT backend, and solve the model.\n",
" solver = model_builder.ModelSolver('sat')\n",
" status = solver.solve(model)\n",
"\n",
" # Print solution.\n",
" if (status == model_builder.SolveStatus.OPTIMAL or\n",
" status == model_builder.SolveStatus.FEASIBLE):\n",
" print(f'Total cost = {solver.objective_value}\\n')\n",
" for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" # Test if x[i,j] is 1 (with tolerance for floating point arithmetic).\n",
" if solver.value(x[i, j]) > 0.5:\n",
" print(f'Worker {i} assigned to task {j}.' +\n",
" f' Cost: {costs[i][j]}')\n",
" else:\n",
" print('No solution found.')\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}