python rewrite
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Copyright 2010-2013 Google
|
||||
# Copyright 2010-2014 Google
|
||||
# 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
|
||||
@@ -14,13 +14,14 @@
|
||||
"""MaxFlow and MinCostFlow examples."""
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.graph import pywrapgraph
|
||||
|
||||
|
||||
def MaxFlow():
|
||||
"""MaxFlow simple interface example."""
|
||||
print('MaxFlow on a simple network.')
|
||||
print 'MaxFlow on a simple network.'
|
||||
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]
|
||||
@@ -29,17 +30,17 @@ def MaxFlow():
|
||||
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:
|
||||
print('Total flow %i/%i' % (max_flow.OptimalFlow(), expected_total_flow))
|
||||
print 'Total flow', max_flow.OptimalFlow(), '/', expected_total_flow
|
||||
for i in range(max_flow.NumArcs()):
|
||||
print('From source %d to target %d: %d / %d' % (
|
||||
print 'From source %d to target %d: %d / %d' % (
|
||||
max_flow.Tail(i),
|
||||
max_flow.Head(i),
|
||||
max_flow.Flow(i),
|
||||
max_flow.Capacity(i)))
|
||||
print('Source side min-cut:', max_flow.GetSourceSideMinCut())
|
||||
print('Sink side min-cut:', max_flow.GetSinkSideMinCut())
|
||||
max_flow.Capacity(i))
|
||||
print 'Source side min-cut:', max_flow.GetSourceSideMinCut()
|
||||
print 'Sink side min-cut:', max_flow.GetSinkSideMinCut()
|
||||
else:
|
||||
print('There was an issue with the max flow input.')
|
||||
print 'There was an issue with the max flow input.'
|
||||
|
||||
|
||||
def MinCostFlow():
|
||||
@@ -48,7 +49,7 @@ def MinCostFlow():
|
||||
Note that this example is actually a linear sum assignment example and will
|
||||
be more efficiently solved with the pywrapgraph.LinearSumAssignement class.
|
||||
"""
|
||||
print('MinCostFlow on 4x4 matrix.')
|
||||
print 'MinCostFlow on 4x4 matrix.'
|
||||
num_sources = 4
|
||||
num_targets = 4
|
||||
costs = [[90, 75, 75, 80],
|
||||
@@ -66,15 +67,15 @@ def MinCostFlow():
|
||||
min_cost_flow.SetNodeSupply(num_sources + node, -1)
|
||||
status = min_cost_flow.Solve()
|
||||
if status == min_cost_flow.OPTIMAL:
|
||||
print('Total flow %i/%i' % (min_cost_flow.OptimalCost(), expected_cost))
|
||||
print 'Total flow', min_cost_flow.OptimalCost(), '/', expected_cost
|
||||
for i in range(0, min_cost_flow.NumArcs()):
|
||||
if min_cost_flow.Flow(i) > 0:
|
||||
print('From source %d to target %d: cost %d' % (
|
||||
print 'From source %d to target %d: cost %d' % (
|
||||
min_cost_flow.Tail(i),
|
||||
min_cost_flow.Head(i) - num_sources,
|
||||
min_cost_flow.UnitCost(i)))
|
||||
min_cost_flow.UnitCost(i))
|
||||
else:
|
||||
print('There was an issue with the min cost flow input.')
|
||||
print 'There was an issue with the min cost flow input.'
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
|
||||
Reference in New Issue
Block a user