145 lines
5.0 KiB
Plaintext
145 lines
5.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2021 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"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",
|
|
"metadata": {},
|
|
"source": [
|
|
"# simple_min_cost_flow_program"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/graph/simple_min_cost_flow_program.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/simple_min_cost_flow_program.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",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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",
|
|
"\"\"\"From Bradley, H. and M., '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 data]\n",
|
|
"# Define four parallel arrays: sources, destinations, capacities,\n",
|
|
"# and unit costs between each pair. For instance, the arc from node 0\n",
|
|
"# to node 1 has a capacity of 15.\n",
|
|
"start_nodes = [0, 0, 1, 1, 1, 2, 2, 3, 4]\n",
|
|
"end_nodes = [1, 2, 2, 3, 4, 3, 4, 4, 2]\n",
|
|
"capacities = [15, 8, 20, 4, 10, 15, 4, 20, 5]\n",
|
|
"unit_costs = [4, 4, 2, 2, 6, 1, 3, 2, 3]\n",
|
|
"\n",
|
|
"# Define an array of supplies at each node.\n",
|
|
"supplies = [20, 0, 0, -5, -15]\n",
|
|
"# [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",
|
|
"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",
|
|
"# [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",
|
|
"# [END print_solution]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|