Files
ortools-clone/examples/notebook/graph/assignment_linear_sum_assignment.ipynb

144 lines
4.2 KiB
Plaintext
Raw Permalink Normal View History

2021-12-06 10:19:50 +01:00
{
"cells": [
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "google",
2021-12-06 10:19:50 +01:00
"metadata": {},
"source": [
2025-02-04 18:04:03 +01:00
"##### Copyright 2025 Google LLC."
2021-12-06 10:19:50 +01:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "apache",
2021-12-06 10:19:50 +01: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-14 13:05:00 +01:00
"id": "basename",
2021-12-06 10:19:50 +01:00
"metadata": {},
"source": [
2022-03-03 21:21:33 +01:00
"# assignment_linear_sum_assignment"
2021-12-06 10:19:50 +01:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "link",
2021-12-06 10:19:50 +01:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/graph/assignment_linear_sum_assignment.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
2021-12-06 10:19:50 +01:00
"</td>\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://github.com/google/or-tools/blob/main/ortools/graph/samples/assignment_linear_sum_assignment.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
2021-12-06 10:19:50 +01:00
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "doc",
2021-12-06 10:19:50 +01: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-14 13:05:00 +01:00
"id": "install",
2021-12-06 10:19:50 +01:00
"metadata": {},
"outputs": [],
"source": [
"%pip install ortools"
2021-12-06 10:19:50 +01:00
]
},
2022-04-14 14:07:58 +02:00
{
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
2022-06-27 15:42:26 +02:00
"\n",
2022-04-14 14:07:58 +02:00
"Solve assignment problem using linear assignment solver."
]
},
2021-12-06 10:19:50 +01:00
{
"cell_type": "code",
"execution_count": null,
2021-12-14 13:05:00 +01:00
"id": "code",
2021-12-06 10:19:50 +01:00
"metadata": {},
"outputs": [],
"source": [
2022-06-27 15:42:26 +02:00
"import numpy as np\n",
"\n",
2022-11-02 10:12:37 +01:00
"from ortools.graph.python import linear_sum_assignment\n",
"\n",
2021-12-06 10:19:50 +01:00
"\n",
2025-12-19 15:01:21 +01:00
"\n",
2022-04-14 14:07:58 +02:00
"def main():\n",
" \"\"\"Linear Sum Assignment example.\"\"\"\n",
" assignment = linear_sum_assignment.SimpleLinearSumAssignment()\n",
"\n",
2023-07-13 14:34:43 +02:00
" costs = np.array(\n",
" [\n",
" [90, 76, 75, 70],\n",
" [35, 85, 55, 65],\n",
" [125, 95, 90, 105],\n",
" [45, 110, 95, 115],\n",
" ]\n",
" )\n",
2022-06-27 15:42:26 +02:00
"\n",
2022-11-02 10:12:37 +01:00
" # Let's transform this into 3 parallel vectors (start_nodes, end_nodes,\n",
" # arc_costs)\n",
" end_nodes_unraveled, start_nodes_unraveled = np.meshgrid(\n",
2023-07-13 14:34:43 +02:00
" np.arange(costs.shape[1]), np.arange(costs.shape[0])\n",
" )\n",
2022-06-27 15:42:26 +02:00
" start_nodes = start_nodes_unraveled.ravel()\n",
" end_nodes = end_nodes_unraveled.ravel()\n",
" arc_costs = costs.ravel()\n",
2022-04-14 14:07:58 +02:00
"\n",
2022-06-27 15:42:26 +02:00
" assignment.add_arcs_with_cost(start_nodes, end_nodes, arc_costs)\n",
2021-12-06 10:19:50 +01:00
"\n",
2022-04-14 14:07:58 +02:00
" status = assignment.solve()\n",
2021-12-06 10:19:50 +01:00
"\n",
2022-04-14 14:07:58 +02:00
" if status == assignment.OPTIMAL:\n",
2023-07-13 14:34:43 +02:00
" print(f\"Total cost = {assignment.optimal_cost()}\\n\")\n",
2022-04-14 14:07:58 +02:00
" for i in range(0, assignment.num_nodes()):\n",
2023-07-13 14:34:43 +02:00
" print(\n",
" f\"Worker {i} assigned to task {assignment.right_mate(i)}.\"\n",
" + f\" Cost = {assignment.assignment_cost(i)}\"\n",
" )\n",
2022-04-14 14:07:58 +02:00
" elif status == assignment.INFEASIBLE:\n",
2023-07-13 14:34:43 +02:00
" print(\"No assignment is possible.\")\n",
2022-04-14 14:07:58 +02:00
" elif status == assignment.POSSIBLE_OVERFLOW:\n",
2023-07-13 14:34:43 +02:00
" print(\"Some input costs are too large and may cause an integer overflow.\")\n",
2021-12-06 10:19:50 +01:00
"\n",
"\n",
2022-04-14 14:07:58 +02:00
"main()\n",
2021-12-06 10:19:50 +01:00
"\n"
]
}
],
2025-02-04 18:04:03 +01:00
"metadata": {
"language_info": {
"name": "python"
}
},
2021-12-06 10:19:50 +01:00
"nbformat": 4,
"nbformat_minor": 5
}