2013-06-11 14:51:51 +00:00
|
|
|
# Copyright 2010-2013 Google
|
2011-03-16 15:32:09 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2013-06-11 14:51:51 +00:00
|
|
|
"""MaxFlow and MinCostFlow examples."""
|
2011-03-16 15:32:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
from google.apputils import app
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.graph import pywrapgraph
|
2011-03-16 15:32:09 +00:00
|
|
|
|
2013-06-11 14:51:51 +00:00
|
|
|
|
|
|
|
|
def MaxFlow():
|
|
|
|
|
"""MaxFlow simple interface example."""
|
2013-12-29 14:04:35 +00:00
|
|
|
print('MaxFlow on a simple network.')
|
2013-06-11 14:51:51 +00:00
|
|
|
tails = [0, 0, 0, 0, 1, 2, 3, 3, 4]
|
|
|
|
|
heads = [1, 2, 3, 4, 3, 4, 4, 5, 5]
|
|
|
|
|
capacities = [5, 8, 5, 3, 4, 5, 6, 6, 4]
|
|
|
|
|
expected_total_flow = 10
|
|
|
|
|
max_flow = pywrapgraph.SimpleMaxFlow()
|
|
|
|
|
for i in range(0, len(tails)):
|
|
|
|
|
max_flow.AddArcWithCapacity(tails[i], heads[i], capacities[i])
|
|
|
|
|
if max_flow.Solve(0, 5) == max_flow.OPTIMAL:
|
2013-12-29 14:15:25 +00:00
|
|
|
print('Total flow %i/%i' % (max_flow.OptimalFlow(), expected_total_flow))
|
2013-06-11 14:51:51 +00:00
|
|
|
for i in range(max_flow.NumArcs()):
|
2013-12-29 14:04:35 +00:00
|
|
|
print('From source %d to target %d: %d / %d' % (
|
2013-06-11 14:51:51 +00:00
|
|
|
max_flow.Tail(i),
|
|
|
|
|
max_flow.Head(i),
|
|
|
|
|
max_flow.Flow(i),
|
2013-12-29 14:04:35 +00:00
|
|
|
max_flow.Capacity(i)))
|
|
|
|
|
print('Source side min-cut:', max_flow.GetSourceSideMinCut())
|
|
|
|
|
print('Sink side min-cut:', max_flow.GetSinkSideMinCut())
|
2013-06-11 14:51:51 +00:00
|
|
|
else:
|
2013-12-29 14:04:35 +00:00
|
|
|
print('There was an issue with the max flow input.')
|
2011-03-16 15:32:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def MinCostFlow():
|
2013-06-11 14:51:51 +00:00
|
|
|
"""MinCostFlow simple interface example.
|
|
|
|
|
|
|
|
|
|
Note that this example is actually a linear sum assignment example and will
|
|
|
|
|
be more efficiently solved with the pywrapgraph.LinearSumAssignement class.
|
|
|
|
|
"""
|
2013-12-29 14:04:35 +00:00
|
|
|
print('MinCostFlow on 4x4 matrix.')
|
2011-03-16 15:32:09 +00:00
|
|
|
num_sources = 4
|
|
|
|
|
num_targets = 4
|
|
|
|
|
costs = [[90, 75, 75, 80],
|
|
|
|
|
[35, 85, 55, 65],
|
|
|
|
|
[125, 95, 90, 105],
|
|
|
|
|
[45, 110, 95, 115]]
|
|
|
|
|
expected_cost = 275
|
2013-06-11 14:51:51 +00:00
|
|
|
min_cost_flow = pywrapgraph.SimpleMinCostFlow()
|
2011-06-07 21:51:24 +00:00
|
|
|
for source in range(0, num_sources):
|
|
|
|
|
for target in range(0, num_targets):
|
2013-06-11 14:51:51 +00:00
|
|
|
min_cost_flow.AddArcWithCapacityAndUnitCost(
|
|
|
|
|
source, num_sources + target, 1, costs[source][target])
|
|
|
|
|
for node in range(0, num_sources):
|
|
|
|
|
min_cost_flow.SetNodeSupply(node, 1)
|
|
|
|
|
min_cost_flow.SetNodeSupply(num_sources + node, -1)
|
|
|
|
|
status = min_cost_flow.Solve()
|
|
|
|
|
if status == min_cost_flow.OPTIMAL:
|
2013-12-29 14:15:25 +00:00
|
|
|
print('Total flow %i/%i' % (min_cost_flow.OptimalCost(), expected_cost))
|
2013-06-11 14:51:51 +00:00
|
|
|
for i in range(0, min_cost_flow.NumArcs()):
|
|
|
|
|
if min_cost_flow.Flow(i) > 0:
|
2013-12-29 14:04:35 +00:00
|
|
|
print('From source %d to target %d: cost %d' % (
|
2013-06-11 14:51:51 +00:00
|
|
|
min_cost_flow.Tail(i),
|
|
|
|
|
min_cost_flow.Head(i) - num_sources,
|
2013-12-29 14:04:35 +00:00
|
|
|
min_cost_flow.UnitCost(i)))
|
2013-06-11 14:51:51 +00:00
|
|
|
else:
|
2013-12-29 14:04:35 +00:00
|
|
|
print('There was an issue with the min cost flow input.')
|
2011-03-16 15:32:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(unused_argv):
|
2013-06-11 14:51:51 +00:00
|
|
|
MaxFlow()
|
2011-03-16 15:32:09 +00:00
|
|
|
MinCostFlow()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run()
|