161 lines
5.9 KiB
Plaintext
161 lines
5.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2021 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": [
|
|
"# pyflow_example"
|
|
]
|
|
},
|
|
{
|
|
"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/master/examples/notebook/examples/pyflow_example.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/examples/python/pyflow_example.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": "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": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"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",
|
|
"\"\"\"MaxFlow and MinCostFlow examples.\"\"\"\n",
|
|
"\n",
|
|
"from absl import app\n",
|
|
"from ortools.graph import pywrapgraph\n",
|
|
"\n",
|
|
"\n",
|
|
"def MaxFlow():\n",
|
|
" \"\"\"MaxFlow simple interface example.\"\"\"\n",
|
|
" print('MaxFlow on a simple network.')\n",
|
|
" tails = [0, 0, 0, 0, 1, 2, 3, 3, 4]\n",
|
|
" heads = [1, 2, 3, 4, 3, 4, 4, 5, 5]\n",
|
|
" capacities = [5, 8, 5, 3, 4, 5, 6, 6, 4]\n",
|
|
" expected_total_flow = 10\n",
|
|
" max_flow = pywrapgraph.SimpleMaxFlow()\n",
|
|
" for i in range(0, len(tails)):\n",
|
|
" max_flow.AddArcWithCapacity(tails[i], heads[i], capacities[i])\n",
|
|
" if max_flow.Solve(0, 5) == max_flow.OPTIMAL:\n",
|
|
" print('Total flow', max_flow.OptimalFlow(), '/', expected_total_flow)\n",
|
|
" for i in range(max_flow.NumArcs()):\n",
|
|
" print('From source %d to target %d: %d / %d' %\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",
|
|
"\n",
|
|
"\n",
|
|
"def MinCostFlow():\n",
|
|
" \"\"\"MinCostFlow simple interface example.\n",
|
|
"\n",
|
|
" Note that this example is actually a linear sum assignment example and will\n",
|
|
" be more efficiently solved with the pywrapgraph.LinearSumAssignement class.\n",
|
|
" \"\"\"\n",
|
|
" print('MinCostFlow on 4x4 matrix.')\n",
|
|
" num_sources = 4\n",
|
|
" num_targets = 4\n",
|
|
" costs = [[90, 75, 75, 80], [35, 85, 55, 65], [125, 95, 90, 105],\n",
|
|
" [45, 110, 95, 115]]\n",
|
|
" expected_cost = 275\n",
|
|
" min_cost_flow = pywrapgraph.SimpleMinCostFlow()\n",
|
|
" for source in range(0, num_sources):\n",
|
|
" for target in range(0, num_targets):\n",
|
|
" min_cost_flow.AddArcWithCapacityAndUnitCost(source,\n",
|
|
" num_sources + target, 1,\n",
|
|
" costs[source][target])\n",
|
|
" for node in range(0, num_sources):\n",
|
|
" min_cost_flow.SetNodeSupply(node, 1)\n",
|
|
" min_cost_flow.SetNodeSupply(num_sources + node, -1)\n",
|
|
" status = min_cost_flow.Solve()\n",
|
|
" if status == min_cost_flow.OPTIMAL:\n",
|
|
" print('Total flow', min_cost_flow.OptimalCost(), '/', expected_cost)\n",
|
|
" for i in range(0, min_cost_flow.NumArcs()):\n",
|
|
" if min_cost_flow.Flow(i) > 0:\n",
|
|
" print('From source %d to target %d: cost %d' %\n",
|
|
" (min_cost_flow.Tail(i), min_cost_flow.Head(i) -\n",
|
|
" num_sources, min_cost_flow.UnitCost(i)))\n",
|
|
" else:\n",
|
|
" print('There was an issue with the min cost flow input.')\n",
|
|
"\n",
|
|
"\n",
|
|
"MaxFlow()\n",
|
|
"MinCostFlow()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|