参考地址:https://pypi.org/project/munkres/ (基于此源码修改 ,目前测试的12的test case均能通过)
python实现:munkres.py(最小权重匹配)
C实现:munkres_tests.c (最小权重匹配),max_munkres_test.c(最大权重匹配)
cd src
gcc -o max_munkres_test max_munkres_test.c -lm
./max_munkres_test
=== Test Case 1 ===
Cost matrix:
[-400.0000, -150.0000, -400.0000]
[-400.0000, -450.0000, -600.0000]
[-300.0000, -225.0000, -300.0000]
After Step 3:
Row covers: 0 0 0
Column covers: 1 0 1
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000
Column labels (ly): 225.0000 0.0000 225.0000
After Step 3:
Row covers: 0 0 0
Column covers: 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -400.0000
目标 1 匹配到观测 2,成本: -600.0000
目标 2 匹配到观测 1,成本: -225.0000
计算的总成本 = -1225.0000
预期的总成本 = -1225.0000
测试通过!
=== Test Case 2 ===
Cost matrix:
[-400.0000, -150.0000, -400.0000, -1.0000]
[-400.0000, -450.0000, -600.0000, -2.0000]
[-300.0000, -225.0000, -300.0000, -3.0000]
[0.0000, 0.0000, 0.0000, 0.0000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 -75.0000
Column labels (ly): 300.0000 0.0000 300.0000 0.0000
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -400.0000
目标 1 匹配到观测 2,成本: -600.0000
目标 2 匹配到观测 1,成本: -225.0000
计算的总成本 = -1225.0000
预期的总成本 = -1225.0000
测试通过!
=== Test Case 3 ===
Cost matrix:
[-10.0000, -10.0000, -8.0000]
[-9.0000, -8.0000, -1.0000]
[-9.0000, -7.0000, -4.0000]
After Step 3:
Row covers: 0 0 0
Column covers: 1 0 0
After Step 3:
Row covers: 0 0 0
Column covers: 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000
Column labels (ly): 6.0000 6.0000 0.0000
After Step 6:
Row labels (lx): -1.0000 0.0000 0.0000
Column labels (ly): 9.0000 6.0000 0.0000
After Step 3:
Row covers: 0 0 0
Column covers: 1 1 1
匹配结果:
目标 0 匹配到观测 2,成本: -8.0000
目标 1 匹配到观测 1,成本: -8.0000
目标 2 匹配到观测 0,成本: -9.0000
计算的总成本 = -25.0000
预期的总成本 = -25.0000
测试通过!
=== Test Case 4 ===
Cost matrix:
[-10.1000, -10.2000, -8.3000]
[-9.4000, -8.5000, -1.6000]
[-9.7000, -7.8000, -4.9000]
After Step 3:
Row covers: 0 0 0
Column covers: 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000
Column labels (ly): 5.7000 5.7000 0.0000
After Step 6:
Row labels (lx): -0.9000 0.0000 0.0000
Column labels (ly): 8.4000 5.7000 0.0000
After Step 3:
Row covers: 0 0 0
Column covers: 1 1 1
匹配结果:
目标 0 匹配到观测 2,成本: -8.3000
目标 1 匹配到观测 1,成本: -8.5000
目标 2 匹配到观测 0,成本: -9.7000
计算的总成本 = -26.5000
预期的总成本 = -26.5000
测试通过!
=== Test Case 5 ===
Cost matrix:
[-10.0000, -10.0000, -8.0000, -11.0000]
[-9.0000, -8.0000, -1.0000, -1.0000]
[-9.0000, -7.0000, -4.0000, -10.0000]
[0.0000, 0.0000, 0.0000, 0.0000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 0 1
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 -1.0000
Column labels (ly): 4.0000 0.0000 0.0000 4.0000
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 1,成本: -10.0000
目标 1 匹配到观测 0,成本: -9.0000
目标 2 匹配到观测 3,成本: -10.0000
计算的总成本 = -29.0000
预期的总成本 = -29.0000
测试通过!
=== Test Case 6 ===
Cost matrix:
[-10.0100, -10.0200, -8.0300, -11.0400]
[-9.0500, -8.0600, -1.0700, -1.0800]
[-9.0900, -7.1000, -4.1100, -10.1200]
[0.0000, 0.0000, 0.0000, 0.0000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 0 1
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 -0.9900
Column labels (ly): 3.9600 0.0000 0.0000 3.9600
After Step 6:
Row labels (lx): 0.0000 -0.0300 0.0000 -1.0200
Column labels (ly): 3.9600 0.0000 0.0000 4.0800
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 1,成本: -10.0200
目标 1 匹配到观测 0,成本: -9.0500
目标 2 匹配到观测 3,成本: -10.1200
计算的总成本 = -29.1900
预期的总成本 = -29.1900
测试通过!
=== Test Case 7 ===
Cost matrix:
[-4.0000, -5.0000, -6.0000, 0.0000]
[-1.0000, -9.0000, -12.0000, -11.0000]
[0.0000, -5.0000, -4.0000, 0.0000]
[-12.0000, -12.0000, -12.0000, -10.0000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 0.0000
Column labels (ly): 4.0000 4.0000 4.0000 0.0000
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 2,成本: -6.0000
目标 1 匹配到观测 3,成本: -11.0000
目标 2 匹配到观测 1,成本: -5.0000
目标 3 匹配到观测 0,成本: -12.0000
计算的总成本 = -34.0000
预期的总成本 = -34.0000
测试通过!
=== Test Case 8 ===
Cost matrix:
[-4.0010, -5.0020, -6.0030, 0.0000]
[-1.0040, -9.0050, -12.0060, -11.0070]
[0.0000, -5.0080, -4.0090, 0.0000]
[-12.0100, -12.0110, -12.0120, -10.0130]
After Step 3:
Row covers: 0 0 0 0
Column covers: 0 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 0.0000
Column labels (ly): 0.0000 0.0080 0.0080 0.0000
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 0
After Step 6:
Row labels (lx): 0.0000 0.0000 0.0000 0.0000
Column labels (ly): 3.9880 3.9960 3.9960 0.0000
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 2,成本: -6.0030
目标 1 匹配到观测 3,成本: -11.0070
目标 2 匹配到观测 1,成本: -5.0080
目标 3 匹配到观测 0,成本: -12.0100
计算的总成本 = -34.0280
预期的总成本 = -34.0280
测试通过!
=== Test Case 9 ===
Cost matrix:
[-1.0000, 0.0000, 0.0000, 0.0000]
[0.0000, -2.0000, 0.0000, 0.0000]
[0.0000, 0.0000, -3.0000, 0.0000]
[0.0000, 0.0000, 0.0000, -4.0000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -1.0000
目标 1 匹配到观测 1,成本: -2.0000
目标 2 匹配到观测 2,成本: -3.0000
目标 3 匹配到观测 3,成本: -4.0000
计算的总成本 = -10.0000
预期的总成本 = -10.0000
测试通过!
=== Test Case 10 ===
Cost matrix:
[-1.1000, 0.0000, 0.0000, 0.0000]
[0.0000, -2.2000, 0.0000, 0.0000]
[0.0000, 0.0000, -3.3000, 0.0000]
[0.0000, 0.0000, 0.0000, -4.4000]
After Step 3:
Row covers: 0 0 0 0
Column covers: 1 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -1.1000
目标 1 匹配到观测 1,成本: -2.2000
目标 2 匹配到观测 2,成本: -3.3000
目标 3 匹配到观测 3,成本: -4.4000
计算的总成本 = -11.0000
预期的总成本 = -11.0000
测试通过!
=== Test Case 11 ===
Cost matrix:
[-0.8768, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, -0.8997, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, 1.0000, -0.8312, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, 1.0000, 1.0000, -0.8771, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, -0.3786, -0.3098, 1.0000, -0.2441, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, -0.8956, -0.5149, 1.0000, 1.0000, 1.0000, 1.0000, -0.3389, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[1.0000, 1.0000, 1.0000, 1.0000, -0.8140, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
After Step 3:
Row covers: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Column covers: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
After Step 3:
Row covers: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Column covers: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -0.8768
目标 1 匹配到观测 1,成本: -0.8997
目标 2 匹配到观测 2,成本: -0.8312
目标 3 匹配到观测 21,成本: 1.0000
目标 4 匹配到观测 3,成本: -0.8771
目标 5 匹配到观测 6,成本: -0.8956
目标 6 匹配到观测 4,成本: -0.8140
计算的总成本 = -4.1944
预期的总成本 = -4.1944
测试通过!
=== Test Case 12 ===
Cost matrix:
[-0.8768, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, -0.8997, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
[1.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
After Step 3:
Row covers: 0 0 0 0 0 0 0
Column covers: 1 1 1 1 1 1 1
匹配结果:
目标 0 匹配到观测 0,成本: -0.8768
目标 1 匹配到观测 1,成本: -0.8997
计算的总成本 = -1.7765
预期的总成本 = -1.7765
测试通过!