139 lines
4.1 KiB
Plaintext
139 lines
4.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2023 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_linear_sum_assignment"
|
|
]
|
|
},
|
|
{
|
|
"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/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",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<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",
|
|
"</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",
|
|
"Solve assignment problem using linear assignment solver."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"from ortools.graph.python import linear_sum_assignment\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" \"\"\"Linear Sum Assignment example.\"\"\"\n",
|
|
" assignment = linear_sum_assignment.SimpleLinearSumAssignment()\n",
|
|
"\n",
|
|
" 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",
|
|
"\n",
|
|
" # 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",
|
|
" np.arange(costs.shape[1]), np.arange(costs.shape[0])\n",
|
|
" )\n",
|
|
" start_nodes = start_nodes_unraveled.ravel()\n",
|
|
" end_nodes = end_nodes_unraveled.ravel()\n",
|
|
" arc_costs = costs.ravel()\n",
|
|
"\n",
|
|
" assignment.add_arcs_with_cost(start_nodes, end_nodes, arc_costs)\n",
|
|
"\n",
|
|
" status = assignment.solve()\n",
|
|
"\n",
|
|
" if status == assignment.OPTIMAL:\n",
|
|
" print(f\"Total cost = {assignment.optimal_cost()}\\n\")\n",
|
|
" for i in range(0, assignment.num_nodes()):\n",
|
|
" print(\n",
|
|
" f\"Worker {i} assigned to task {assignment.right_mate(i)}.\"\n",
|
|
" + f\" Cost = {assignment.assignment_cost(i)}\"\n",
|
|
" )\n",
|
|
" elif status == assignment.INFEASIBLE:\n",
|
|
" print(\"No assignment is possible.\")\n",
|
|
" elif status == assignment.POSSIBLE_OVERFLOW:\n",
|
|
" print(\"Some input costs are too large and may cause an integer overflow.\")\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|