Files
ortools-clone/examples/notebook/graph/assignment_min_flow.ipynb
Corentin Le Molgat 949ff20da4 Update notebooks...
2021-12-06 10:19:50 +01:00

167 lines
5.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "981fd9e1",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
]
},
{
"cell_type": "markdown",
"id": "74b3e95d",
"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": "198ffa63",
"metadata": {},
"source": [
"# assignment_min_flow"
]
},
{
"cell_type": "markdown",
"id": "6e991c13",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/graph/assignment_min_flow.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/master/ortools/graph/samples/assignment_min_flow.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"id": "2e100055",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0be3b731",
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d21bf459",
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"# Copyright 2010-2021 Google LLC\n",
"# 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",
"# [START program]\n",
"\"\"\"Linear assignment example.\"\"\"\n",
"# [START import]\n",
"from ortools.graph import pywrapgraph\n",
"# [END import]\n",
"\n",
"\n",
"\"\"\"Solving an Assignment Problem with MinCostFlow.\"\"\"\n",
"# [START solver]\n",
"# Instantiate a SimpleMinCostFlow solver.\n",
"min_cost_flow = pywrapgraph.SimpleMinCostFlow()\n",
"# [END solver]\n",
"\n",
"# [START data]\n",
"# Define the directed graph for the flow.\n",
"start_nodes = [0, 0, 0, 0] + [\n",
" 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4\n",
"] + [5, 6, 7, 8]\n",
"end_nodes = [1, 2, 3, 4] + [5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8\n",
" ] + [9, 9, 9, 9]\n",
"capacities = [1, 1, 1, 1] + [\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n",
"] + [1, 1, 1, 1]\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",
"source = 0\n",
"sink = 9\n",
"tasks = 4\n",
"supplies = [tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks]\n",
"# [END data]\n",
"\n",
"# [START constraints]\n",
"# Add each arc.\n",
"for i in range(len(start_nodes)):\n",
" min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i],\n",
" end_nodes[i], capacities[i],\n",
" costs[i])\n",
"# Add node supplies.\n",
"for i in range(len(supplies)):\n",
" min_cost_flow.SetNodeSupply(i, supplies[i])\n",
"# [END constraints]\n",
"\n",
"# [START solve]\n",
"# Find the minimum cost flow between node 0 and node 10.\n",
"status = min_cost_flow.Solve()\n",
"# [END solve]\n",
"\n",
"# [START print_solution]\n",
"if status == min_cost_flow.OPTIMAL:\n",
" print('Total cost = ', min_cost_flow.OptimalCost())\n",
" print()\n",
" for arc in range(min_cost_flow.NumArcs()):\n",
" # Can ignore arcs leading out of source or into sink.\n",
" if min_cost_flow.Tail(arc) != source and min_cost_flow.Head(\n",
" 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 min_cost_flow.Flow(arc) > 0:\n",
" print('Worker %d assigned to task %d. Cost = %d' %\n",
" (min_cost_flow.Tail(arc), min_cost_flow.Head(arc),\n",
" min_cost_flow.UnitCost(arc)))\n",
"else:\n",
" print('There was an issue with the min cost flow input.')\n",
" print(f'Status: {status}')\n",
"# [END print_solution]\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}