Update notebooks...

This commit is contained in:
Corentin Le Molgat
2021-12-06 10:19:50 +01:00
parent 07127d463c
commit 949ff20da4
260 changed files with 4680 additions and 2141 deletions

View File

@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "864533b2",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
]
},
{
"cell_type": "markdown",
"id": "1a7c5cdc",
"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": "0b189f3b",
"metadata": {},
"source": [
"# assignment_linear_assignment"
]
},
{
"cell_type": "markdown",
"id": "2c0a4294",
"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_linear_assignment.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_linear_assignment.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": "a5afaeea",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03440dda",
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c35d019",
"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",
"\"\"\"Solve assignment problem using linear assignment solver.\"\"\"\n",
"# [START import]\n",
"from ortools.graph import pywrapgraph\n",
"# [END import]\n",
"\n",
"\n",
"\"\"\"Linear Sum Assignment example.\"\"\"\n",
"# [START solver]\n",
"assignment = pywrapgraph.LinearSumAssignment()\n",
"# [END solver]\n",
"\n",
"# [START data]\n",
"costs = [\n",
" [90, 76, 75, 70],\n",
" [35, 85, 55, 65],\n",
" [125, 95, 90, 105],\n",
" [45, 110, 95, 115],\n",
"]\n",
"num_workers = len(costs)\n",
"num_tasks = len(costs[0])\n",
"# [END data]\n",
"\n",
"# [START constraints]\n",
"for worker in range(num_workers):\n",
" for task in range(num_tasks):\n",
" if costs[worker][task]:\n",
" assignment.AddArcWithCost(worker, task, costs[worker][task])\n",
"# [END constraints]\n",
"\n",
"# [START solve]\n",
"status = assignment.Solve()\n",
"# [END solve]\n",
"\n",
"# [START print_solution]\n",
"if status == assignment.OPTIMAL:\n",
" print(f'Total cost = {assignment.OptimalCost()}\\n')\n",
" for i in range(0, assignment.NumNodes()):\n",
" print(f'Worker {i} assigned to task {assignment.RightMate(i)}.' +\n",
" f' Cost = {assignment.AssignmentCost(i)}')\n",
"elif status == assignment.INFEASIBLE:\n",
" print('No assignment is possible.')\n",
"elif status == assignment.POSSIBLE_OVERFLOW:\n",
" print(\n",
" 'Some input costs are too large and may cause an integer overflow.')\n",
"# [END print_solution]\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,166 @@
{
"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
}

View File

@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3428c9a9",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
]
},
{
"cell_type": "markdown",
"id": "2be137e6",
"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": "9b3b2a72",
"metadata": {},
"source": [
"# balance_min_flow"
]
},
{
"cell_type": "markdown",
"id": "be0daab2",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/graph/balance_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/balance_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": "5232c307",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02ae9020",
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e668f0e8",
"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",
"\"\"\"Assignment with teams of workers.\"\"\"\n",
"# [START import]\n",
"from ortools.graph import pywrapgraph\n",
"# [END import]\n",
"\n",
"\n",
"\"\"\"Solving an Assignment with teams of worker.\"\"\"\n",
"# [START solver]\n",
"min_cost_flow = pywrapgraph.SimpleMinCostFlow()\n",
"# [END solver]\n",
"\n",
"# [START data]\n",
"# Define the directed graph for the flow.\n",
"team_a = [1, 3, 5]\n",
"team_b = [2, 4, 6]\n",
"\n",
"start_nodes = ([0, 0] + [11, 11, 11] + [12, 12, 12] + [\n",
" 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6\n",
"] + [7, 8, 9, 10])\n",
"end_nodes = ([11, 12] + team_a + team_b + [\n",
" 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8,\n",
" 9, 10\n",
"] + [13, 13, 13, 13])\n",
"capacities = ([2, 2] + [1, 1, 1] + [1, 1, 1] + [\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",
"] + [1, 1, 1, 1])\n",
"costs = ([0, 0] + [0, 0, 0] + [0, 0, 0] + [\n",
" 90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, 60,\n",
" 105, 80, 75, 45, 65, 110, 95\n",
"] + [0, 0, 0, 0])\n",
"\n",
"source = 0\n",
"sink = 13\n",
"tasks = 4\n",
"# Define an array of supplies at each node.\n",
"supplies = [tasks, 0, 0, 0, 0, 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(0, len(start_nodes)):\n",
" min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i],\n",
" end_nodes[i], capacities[i],\n",
" costs[i])\n",
"\n",
"# Add node supplies.\n",
"for i in range(0, 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",
" min_cost_flow.Solve()\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 intermediate, or into sink.\n",
" if (min_cost_flow.Tail(arc) != source and\n",
" min_cost_flow.Tail(arc) != 11 and\n",
" min_cost_flow.Tail(arc) != 12 and\n",
" min_cost_flow.Head(arc) != sink):\n",
"\n",
" # Arcs in the solution will have a flow value of 1.\n",
" # There start and end nodes 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
}

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "2bf9b567",
"id": "18a3f79d",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
@@ -10,7 +10,7 @@
},
{
"cell_type": "markdown",
"id": "7be013fd",
"id": "7a932855",
"metadata": {},
"source": [
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
@@ -28,7 +28,7 @@
},
{
"cell_type": "markdown",
"id": "5557bcfd",
"id": "55cdfccb",
"metadata": {},
"source": [
"# simple_max_flow_program"
@@ -36,7 +36,7 @@
},
{
"cell_type": "markdown",
"id": "8351f994",
"id": "b2f3a9e5",
"metadata": {},
"source": [
"<table align=\"left\">\n",
@@ -51,7 +51,7 @@
},
{
"cell_type": "markdown",
"id": "9db72a49",
"id": "00ac5383",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
@@ -60,7 +60,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c56ffb9c",
"id": "3d88e57a",
"metadata": {},
"outputs": [],
"source": [
@@ -70,7 +70,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4a75ec9d",
"id": "8f0c4a47",
"metadata": {},
"outputs": [],
"source": [
@@ -95,20 +95,21 @@
"\n",
"\n",
"\"\"\"MaxFlow simple interface example.\"\"\"\n",
"# [START solver]\n",
"# Instantiate a SimpleMaxFlow solver.\n",
"max_flow = pywrapgraph.SimpleMaxFlow()\n",
"# [END solver]\n",
"\n",
"# [START data]\n",
"# Define three parallel arrays: start_nodes, end_nodes, and the capacities\n",
"# between each pair. For instance, the arc from node 0 to node 1 has a\n",
"# capacity of 20.\n",
"\n",
"start_nodes = [0, 0, 0, 1, 1, 2, 2, 3, 3]\n",
"end_nodes = [1, 2, 3, 2, 4, 3, 4, 2, 4]\n",
"capacities = [20, 30, 10, 40, 30, 10, 20, 5, 20]\n",
"# [END data]\n",
"\n",
"# Instantiate a SimpleMaxFlow solver.\n",
"# [START constraints]\n",
"max_flow = pywrapgraph.SimpleMaxFlow()\n",
"# Add each arc.\n",
"for arc in zip(start_nodes, end_nodes, capacities):\n",
" max_flow.AddArcWithCapacity(arc[0], arc[1], arc[2])\n",
@@ -116,19 +117,24 @@
"\n",
"# [START solve]\n",
"# Find the maximum flow between node 0 and node 4.\n",
"if max_flow.Solve(0, 4) == max_flow.OPTIMAL:\n",
" print('Max flow:', max_flow.OptimalFlow())\n",
" print('')\n",
" print(' Arc Flow / Capacity')\n",
" for i in range(max_flow.NumArcs()):\n",
" print('%1s -> %1s %3s / %3s' %\n",
" (max_flow.Tail(i), max_flow.Head(i), max_flow.Flow(i),\n",
" max_flow.Capacity(i)))\n",
" print('Source side min-cut:', max_flow.GetSourceSideMinCut())\n",
" print('Sink side min-cut:', max_flow.GetSinkSideMinCut())\n",
"else:\n",
" print('There was an issue with the max flow input.')\n",
"status = max_flow.Solve(0, 4)\n",
"# [END solve]\n",
"\n",
"# [START print_solution]\n",
"if status != max_flow.OPTIMAL:\n",
" print('There was an issue with the max flow input.')\n",
" print(f'Status: {status}')\n",
" exit(1)\n",
"print('Max flow:', max_flow.OptimalFlow())\n",
"print('')\n",
"print(' Arc Flow / Capacity')\n",
"for i in range(max_flow.NumArcs()):\n",
" print('%1s -> %1s %3s / %3s' %\n",
" (max_flow.Tail(i), max_flow.Head(i), max_flow.Flow(i),\n",
" max_flow.Capacity(i)))\n",
"print('Source side min-cut:', max_flow.GetSourceSideMinCut())\n",
"print('Sink side min-cut:', max_flow.GetSinkSideMinCut())\n",
"# [END print_solution]\n",
"\n"
]
}

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "548055c9",
"id": "6ecdbdef",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
@@ -10,7 +10,7 @@
},
{
"cell_type": "markdown",
"id": "78e3c68d",
"id": "203490ab",
"metadata": {},
"source": [
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
@@ -28,7 +28,7 @@
},
{
"cell_type": "markdown",
"id": "eb074b2a",
"id": "34841db6",
"metadata": {},
"source": [
"# simple_min_cost_flow_program"
@@ -36,7 +36,7 @@
},
{
"cell_type": "markdown",
"id": "2f4c8a44",
"id": "c976ad61",
"metadata": {},
"source": [
"<table align=\"left\">\n",
@@ -51,7 +51,7 @@
},
{
"cell_type": "markdown",
"id": "880541ef",
"id": "bdb0b306",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
@@ -60,7 +60,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1b3b1dd5",
"id": "fe956f50",
"metadata": {},
"outputs": [],
"source": [
@@ -70,7 +70,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ecd66125",
"id": "6492b611",
"metadata": {},
"outputs": [],
"source": [
@@ -88,13 +88,18 @@
"# See the License for the specific language governing permissions and\n",
"# limitations under the License.\n",
"# [START program]\n",
"\"\"\"From Bradley, H. and M., 'Applied Mathematical Programming', figure 8.1.\"\"\"\n",
"\"\"\"From Bradley, Hax and Maganti, 'Applied Mathematical Programming', figure 8.1.\"\"\"\n",
"# [START import]\n",
"from ortools.graph import pywrapgraph\n",
"# [END import]\n",
"\n",
"\n",
"\"\"\"MinCostFlow simple interface example.\"\"\"\n",
"# [START solver]\n",
"# Instantiate a SimpleMinCostFlow solver.\n",
"min_cost_flow = pywrapgraph.SimpleMinCostFlow()\n",
"# [END solver]\n",
"\n",
"# [START data]\n",
"# Define four parallel arrays: sources, destinations, capacities,\n",
"# and unit costs between each pair. For instance, the arc from node 0\n",
@@ -109,37 +114,34 @@
"# [END data]\n",
"\n",
"# [START constraints]\n",
"# Instantiate a SimpleMinCostFlow solver.\n",
"min_cost_flow = pywrapgraph.SimpleMinCostFlow()\n",
"\n",
"# Add each arc.\n",
"for arc in zip(start_nodes, end_nodes, capacities, unit_costs):\n",
" min_cost_flow.AddArcWithCapacityAndUnitCost(arc[0], arc[1], arc[2],\n",
" arc[3])\n",
"\n",
"# Add node supplies.\n",
"# Add node supply.\n",
"for count, supply in enumerate(supplies):\n",
" min_cost_flow.SetNodeSupply(count, supply)\n",
"# [END constraints]\n",
"\n",
"# [START solve]\n",
"# Find the min cost flow.\n",
"solve_status = min_cost_flow.Solve()\n",
"status = min_cost_flow.Solve()\n",
"# [END solve]\n",
"\n",
"# [START print_solution]\n",
"if solve_status == min_cost_flow.OPTIMAL:\n",
" print('Minimum cost: ', min_cost_flow.OptimalCost())\n",
" print('')\n",
" print(' Arc Flow / Capacity Cost')\n",
" for i in range(min_cost_flow.NumArcs()):\n",
" cost = min_cost_flow.Flow(i) * min_cost_flow.UnitCost(i)\n",
" print('%1s -> %1s %3s / %3s %3s' %\n",
" (min_cost_flow.Tail(i), min_cost_flow.Head(i),\n",
" min_cost_flow.Flow(i), min_cost_flow.Capacity(i), cost))\n",
"else:\n",
" print('Solving the min cost flow problem failed. Solver status: ',\n",
" solve_status)\n",
"if status != min_cost_flow.OPTIMAL:\n",
" print('There was an issue with the min cost flow input.')\n",
" print(f'Status: {status}')\n",
" exit(1)\n",
"print('Minimum cost: ', min_cost_flow.OptimalCost())\n",
"print('')\n",
"print(' Arc Flow / Capacity Cost')\n",
"for i in range(min_cost_flow.NumArcs()):\n",
" cost = min_cost_flow.Flow(i) * min_cost_flow.UnitCost(i)\n",
" print('%1s -> %1s %3s / %3s %3s' %\n",
" (min_cost_flow.Tail(i), min_cost_flow.Head(i),\n",
" min_cost_flow.Flow(i), min_cost_flow.Capacity(i), cost))\n",
"# [END print_solution]\n",
"\n"
]