Files
ortools-clone/examples/notebook/graph/assignment_min_flow.ipynb
2025-12-19 15:01:21 +01:00

159 lines
4.9 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_min_flow"
]
},
{
"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_min_flow.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_min_flow.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",
"Linear assignment example."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.graph.python import min_cost_flow\n",
"\n",
"\n",
"\n",
"def main():\n",
" \"\"\"Solving an Assignment Problem with MinCostFlow.\"\"\"\n",
" # Instantiate a SimpleMinCostFlow solver.\n",
" smcf = min_cost_flow.SimpleMinCostFlow()\n",
"\n",
" # Define the directed graph for the flow.\n",
" start_nodes = (\n",
" [0, 0, 0, 0] + [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4] + [5, 6, 7, 8]\n",
" )\n",
" end_nodes = (\n",
" [1, 2, 3, 4] + [5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8] + [9, 9, 9, 9]\n",
" )\n",
" capacities = (\n",
" [1, 1, 1, 1] + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] + [1, 1, 1, 1]\n",
" )\n",
" costs = (\n",
" [0, 0, 0, 0]\n",
" + [90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115]\n",
" + [0, 0, 0, 0]\n",
" )\n",
"\n",
" source = 0\n",
" sink = 9\n",
" tasks = 4\n",
" supplies = [tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks]\n",
"\n",
" # Add each arc.\n",
" for start_node, end_node, capacity, cost in zip(\n",
" start_nodes, end_nodes, capacities, costs\n",
" ):\n",
" smcf.add_arc_with_capacity_and_unit_cost(start_node, end_node, capacity, cost)\n",
" # Add node supplies.\n",
" for idx, supply in enumerate(supplies):\n",
" smcf.set_node_supply(idx, supply)\n",
"\n",
" # Find the minimum cost flow between node 0 and node 10.\n",
" status = smcf.solve()\n",
"\n",
" if status == smcf.OPTIMAL:\n",
" print(f\"Total cost = {smcf.optimal_cost()}\")\n",
" for arc in range(smcf.num_arcs()):\n",
" # Can ignore arcs leading out of source or into sink.\n",
" if smcf.tail(arc) != source and smcf.head(arc) != sink:\n",
"\n",
" # Arcs in the solution have a flow value of 1. Their start and end nodes\n",
" # give an assignment of worker to task.\n",
" if smcf.flow(arc) > 0:\n",
" print(\n",
" f\"Worker {smcf.tail(arc)} assigned to task {smcf.head(arc)}. \"\n",
" f\"Cost = {smcf.unit_cost(arc)}\"\n",
" )\n",
" else:\n",
" print(\"There was an issue with the min cost flow input.\")\n",
" print(f\"Status: {status}\")\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}