196 lines
5.8 KiB
Plaintext
196 lines
5.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2022 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": [
|
|
"# max_flow_taha"
|
|
]
|
|
},
|
|
{
|
|
"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/contrib/max_flow_taha.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/examples/contrib/max_flow_taha.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",
|
|
"\n",
|
|
" Max flow problem in Google CP Solver.\n",
|
|
"\n",
|
|
" From Taha 'Introduction to Operations Research', Example 6.4-2\n",
|
|
"\n",
|
|
" Translated from the AMPL code at\n",
|
|
" http://taha.ineg.uark.edu/maxflo.txt\n",
|
|
"\n",
|
|
" Compare with the following model:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/max_flow_taha.mzn\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver('Max flow problem, Taha')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" n = 5\n",
|
|
" start = 0\n",
|
|
" end = n - 1\n",
|
|
"\n",
|
|
" nodes = list(range(n))\n",
|
|
"\n",
|
|
" # cost matrix\n",
|
|
" c = [[0, 20, 30, 10, 0], [0, 0, 40, 0, 30], [0, 0, 0, 10, 20],\n",
|
|
" [0, 0, 5, 0, 20], [0, 0, 0, 0, 0]]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" x = {}\n",
|
|
" for i in nodes:\n",
|
|
" for j in nodes:\n",
|
|
" x[i, j] = solver.IntVar(0, c[i][j], 'x[%i,%i]' % (i, j))\n",
|
|
"\n",
|
|
" x_flat = [x[i, j] for i in nodes for j in nodes]\n",
|
|
" out_flow = [solver.IntVar(0, 10000, 'out_flow[%i]' % i) for i in nodes]\n",
|
|
" in_flow = [solver.IntVar(0, 10000, 'in_flow[%i]' % i) for i in nodes]\n",
|
|
"\n",
|
|
" total = solver.IntVar(0, 10000, 'z')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" cost_sum = solver.Sum([x[start, j] for j in nodes if c[start][j] > 0])\n",
|
|
" solver.Add(total == cost_sum)\n",
|
|
"\n",
|
|
" for i in nodes:\n",
|
|
" in_flow_sum = solver.Sum([x[j, i] for j in nodes if c[j][i] > 0])\n",
|
|
" solver.Add(in_flow[i] == in_flow_sum)\n",
|
|
"\n",
|
|
" out_flow_sum = solver.Sum([x[i, j] for j in nodes if c[i][j] > 0])\n",
|
|
" solver.Add(out_flow[i] == out_flow_sum)\n",
|
|
"\n",
|
|
" # in_flow == out_flow\n",
|
|
" for i in nodes:\n",
|
|
" if i != start and i != end:\n",
|
|
" solver.Add(out_flow[i] - in_flow[i] == 0)\n",
|
|
"\n",
|
|
" s1 = [x[i, start] for i in nodes if c[i][start] > 0]\n",
|
|
" if len(s1) > 0:\n",
|
|
" solver.Add(solver.Sum([x[i, start] for i in nodes if c[i][start] > 0] == 0))\n",
|
|
"\n",
|
|
" s2 = [x[end, j] for j in nodes if c[end][j] > 0]\n",
|
|
" if len(s2) > 0:\n",
|
|
" solver.Add(solver.Sum([x[end, j] for j in nodes if c[end][j] > 0]) == 0)\n",
|
|
"\n",
|
|
" # objective: maximize total cost\n",
|
|
" objective = solver.Maximize(total, 1)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" db = solver.Phase(x_flat, solver.INT_VAR_DEFAULT, solver.ASSIGN_MAX_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db, [objective])\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" print('total:', total.Value())\n",
|
|
" print('in_flow:', [in_flow[i].Value() for i in nodes])\n",
|
|
" print('out_flow:', [out_flow[i].Value() for i in nodes])\n",
|
|
" for i in nodes:\n",
|
|
" for j in nodes:\n",
|
|
" print('%2i' % x[i, j].Value(), end=' ')\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('num_solutions:', num_solutions)\n",
|
|
" print('failures:', solver.Failures())\n",
|
|
" print('branches:', solver.Branches())\n",
|
|
" print('WallTime:', solver.WallTime(), 'ms')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|