183 lines
5.6 KiB
Plaintext
183 lines
5.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 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_teams_mip"
|
|
]
|
|
},
|
|
{
|
|
"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_teams_mip.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_teams_mip.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",
|
|
"MIP example that solves an assignment problem."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" # Data\n",
|
|
" costs = [\n",
|
|
" [90, 76, 75, 70],\n",
|
|
" [35, 85, 55, 65],\n",
|
|
" [125, 95, 90, 105],\n",
|
|
" [45, 110, 95, 115],\n",
|
|
" [60, 105, 80, 75],\n",
|
|
" [45, 65, 110, 95],\n",
|
|
" ]\n",
|
|
" num_workers = len(costs)\n",
|
|
" num_tasks = len(costs[0])\n",
|
|
"\n",
|
|
" team1 = [0, 2, 4]\n",
|
|
" team2 = [1, 3, 5]\n",
|
|
" # Maximum total of tasks for any team\n",
|
|
" team_max = 2\n",
|
|
"\n",
|
|
" # Solver\n",
|
|
" # Create the mip solver with the SCIP backend.\n",
|
|
" solver = pywraplp.Solver.CreateSolver(\"SCIP\")\n",
|
|
" if not solver:\n",
|
|
" return\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 worker in range(num_workers):\n",
|
|
" for task in range(num_tasks):\n",
|
|
" x[worker, task] = solver.BoolVar(f\"x[{worker},{task}]\")\n",
|
|
"\n",
|
|
" # Constraints\n",
|
|
" # Each worker is assigned at most 1 task.\n",
|
|
" for worker in range(num_workers):\n",
|
|
" solver.Add(solver.Sum([x[worker, task] for task in range(num_tasks)]) <= 1)\n",
|
|
"\n",
|
|
" # Each task is assigned to exactly one worker.\n",
|
|
" for task in range(num_tasks):\n",
|
|
" solver.Add(solver.Sum([x[worker, task] for worker in range(num_workers)]) == 1)\n",
|
|
"\n",
|
|
" # Each team takes at most two tasks.\n",
|
|
" team1_tasks = []\n",
|
|
" for worker in team1:\n",
|
|
" for task in range(num_tasks):\n",
|
|
" team1_tasks.append(x[worker, task])\n",
|
|
" solver.Add(solver.Sum(team1_tasks) <= team_max)\n",
|
|
"\n",
|
|
" team2_tasks = []\n",
|
|
" for worker in team2:\n",
|
|
" for task in range(num_tasks):\n",
|
|
" team2_tasks.append(x[worker, task])\n",
|
|
" solver.Add(solver.Sum(team2_tasks) <= team_max)\n",
|
|
"\n",
|
|
" # Objective\n",
|
|
" objective_terms = []\n",
|
|
" for worker in range(num_workers):\n",
|
|
" for task in range(num_tasks):\n",
|
|
" objective_terms.append(costs[worker][task] * x[worker, task])\n",
|
|
" solver.Minimize(solver.Sum(objective_terms))\n",
|
|
"\n",
|
|
" # Solve\n",
|
|
" print(f\"Solving with {solver.SolverVersion()}\")\n",
|
|
" status = solver.Solve()\n",
|
|
"\n",
|
|
" # Print solution.\n",
|
|
" if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:\n",
|
|
" print(f\"Total cost = {solver.Objective().Value()}\\n\")\n",
|
|
" for worker in range(num_workers):\n",
|
|
" for task in range(num_tasks):\n",
|
|
" if x[worker, task].solution_value() > 0.5:\n",
|
|
" print(\n",
|
|
" f\"Worker {worker} assigned to task {task}.\"\n",
|
|
" + f\" Cost = {costs[worker][task]}\"\n",
|
|
" )\n",
|
|
" else:\n",
|
|
" print(\"No solution found.\")\n",
|
|
" print(f\"Time = {solver.WallTime()} ms\")\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|