2014-07-09 11:17:29 +00:00
|
|
|
# Copyright 2010-2014 Google
|
2012-02-20 12:09:28 +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.
|
|
|
|
|
|
|
|
|
|
"""Test linear sum assignment on a 4x4 matrix.
|
|
|
|
|
|
|
|
|
|
Example taken from:
|
|
|
|
|
http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1]
|
|
|
|
|
modified so the optimum solution is unique.
|
|
|
|
|
"""
|
2016-01-14 18:59:36 +01:00
|
|
|
from __future__ import print_function
|
2012-02-20 12:09:28 +00:00
|
|
|
|
2014-06-13 10:03:03 +00:00
|
|
|
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.graph import pywrapgraph
|
2012-02-20 12:09:28 +00:00
|
|
|
|
|
|
|
|
|
2013-10-17 08:58:26 +00:00
|
|
|
def RunAssignmentOn4x4Matrix():
|
2012-02-20 12:09:28 +00:00
|
|
|
"""Test linear sum assignment on a 4x4 matrix.
|
|
|
|
|
"""
|
|
|
|
|
num_sources = 4
|
|
|
|
|
num_targets = 4
|
|
|
|
|
cost = [[90, 76, 75, 80],
|
|
|
|
|
[35, 85, 55, 65],
|
|
|
|
|
[125, 95, 90, 105],
|
|
|
|
|
[45, 110, 95, 115]]
|
|
|
|
|
expected_cost = cost[0][3] + cost[1][2] + cost[2][1] + cost[3][0]
|
|
|
|
|
|
2013-10-17 08:58:26 +00:00
|
|
|
assignment = pywrapgraph.LinearSumAssignment()
|
2014-05-22 20:13:16 +00:00
|
|
|
for source in range(0, num_sources):
|
2012-02-20 12:09:28 +00:00
|
|
|
for target in range(0, num_targets):
|
2013-10-17 08:58:26 +00:00
|
|
|
assignment.AddArcWithCost(source, target, cost[source][target])
|
|
|
|
|
|
|
|
|
|
solve_status = assignment.Solve()
|
|
|
|
|
if solve_status == assignment.OPTIMAL:
|
2016-01-14 18:59:36 +01:00
|
|
|
print('Successful solve.')
|
|
|
|
|
print('Total cost', assignment.OptimalCost(), '/', expected_cost)
|
2013-10-17 08:58:26 +00:00
|
|
|
for i in range(0, assignment.NumNodes()):
|
2016-01-14 18:59:36 +01:00
|
|
|
print('Left node %d assigned to right node %d with cost %d.' % (
|
2013-10-17 08:58:26 +00:00
|
|
|
i,
|
|
|
|
|
assignment.RightMate(i),
|
2016-01-14 18:59:36 +01:00
|
|
|
assignment.AssignmentCost(i)))
|
2013-10-17 08:58:26 +00:00
|
|
|
elif solve_status == assignment.INFEASIBLE:
|
2016-01-14 18:59:36 +01:00
|
|
|
print('No perfect matching exists.')
|
2013-10-17 08:58:26 +00:00
|
|
|
elif solve_status == assignment.POSSIBLE_OVERFLOW:
|
2016-01-14 18:59:36 +01:00
|
|
|
print('Some input costs are too large and may cause an integer overflow.')
|
2012-02-20 12:09:28 +00:00
|
|
|
|
|
|
|
|
|
2016-01-14 18:59:36 +01:00
|
|
|
def main():
|
2013-10-17 08:58:26 +00:00
|
|
|
RunAssignmentOn4x4Matrix()
|
2012-02-20 12:09:28 +00:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-12-09 14:49:52 +01:00
|
|
|
main()
|