@@ -0,0 +1,2172 @@
+# Copyright 2010-2011 Google%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0A%22%22%22Test linear sum assignment on a 4x4 matrix.%0A%0A   Example taken from:%0A   http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost%5B0%5D%5B1%5D%0A   modified so the optimum solution is unique.%0A%22%22%22%0A%0A%0A%0Afrom google.apputils import app%0Afrom graph import pywrapgraph%0A%0A%0Adef RunAssignmentOn4x4Matrix(forward_graph):%0A  %22%22%22Test linear sum assignment on a 4x4 matrix.%0A%0A  Arguments:%0A    forward_graph: if true uses a ForwardStarGraph or a StarGraph to model%0A                   the assignment problem.%0A  %22%22%22%0A  num_sources = 4%0A  num_targets = 4%0A  cost = %5B%5B90, 76, 75, 80%5D,%0A          %5B35, 85, 55, 65%5D,%0A          %5B125, 95, 90, 105%5D,%0A          %5B45, 110, 95, 115%5D%5D%0A  expected_cost = cost%5B0%5D%5B3%5D + cost%5B1%5D%5B2%5D + cost%5B2%5D%5B1%5D + cost%5B3%5D%5B0%5D%0A%0A  if forward_graph:%0A    graph = pywrapgraph.ForwardStarGraph(num_sources + num_targets,%0A                                         num_sources * num_targets)%0A    assignment = pywrapgraph.ForwardEbertLinearSumAssignment(graph, num_sources)%0A  else:%0A    graph = pywrapgraph.StarGraph(num_sources + num_targets,%0A                                  num_sources * num_targets)%0A    assignment = pywrapgraph.EbertLinearSumAssignment(graph, num_sources)%0A%0A  for source in range (0, num_sources):%0A    for target in range(0, num_targets):%0A      arc = graph.AddArc(source, num_sources + target)%0A      assignment.SetArcCost(arc, cost%5Bsource%5D%5Btarget%5D)%0A%0A  assignment.ComputeAssignment()%0A  total_cost = assignment.GetCost()%0A  print 'total cost', total_cost, '/', expected_cost%0A%0A%0Adef main(unused_argv):%0A  RunAssignmentOn4x4Matrix(True)%0A  RunAssignmentOn4x4Matrix(False)%0A%0Aif __name__ == '__main__':%0A  app.run()%0A

add linear assignment api python example