-
Notifications
You must be signed in to change notification settings - Fork 5
/
hungarian_test.go
168 lines (157 loc) · 4.17 KB
/
hungarian_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package hungarian_test
import (
"github.com/arthurkushman/go-hungarian"
"testing"
)
var testsMax = []struct {
m [][]float64
result map[int]map[int]float64
}{
{[][]float64{
{6, 2, 3, 4, 5},
{3, 8, 2, 8, 1},
{9, 9, 5, 4, 2},
{6, 7, 3, 4, 3},
{1, 2, 6, 4, 9},
}, map[int]map[int]float64{
0: {2: 3},
1: {3: 8},
2: {0: 9},
3: {1: 7},
4: {4: 9},
}},
}
func TestSolveMax(t *testing.T) {
for _, value := range testsMax {
for key, val := range hungarian.SolveMax(value.m) {
for k, v := range val {
if v != value.result[key][k] {
t.Fatalf("Want %f, got: %f", v, value.result[key][k])
}
}
}
}
}
var testsMin = []struct {
m [][]float64
}{
{[][]float64{
{6, 2, 3, 4, 5, 11, 3, 8},
{3, 8, 2, 8, 1, 12, 5, 4},
{7, 9, 5, 10, 2, 11, 6, 8},
{6, 7, 3, 4, 3, 5, 5, 3},
{1, 2, 6, 13, 9, 11, 3, 6},
{6, 2, 3, 4, 5, 11, 3, 8},
{4, 6, 8, 9, 7, 1, 5, 3},
{9, 1, 2, 5, 2, 7, 3, 8},
}},
}
func TestSolveMin(t *testing.T) {
data := make(map[int]float64)
for _, value := range testsMin {
for _, val := range hungarian.SolveMin(value.m) {
for k, v := range val {
if val, ok := data[k]; ok {
t.Fatalf("Repeated column %d: %f", k, val)
}
data[k] = v
}
}
}
}
func BenchmarkSolveMax8x8(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMax([][]float64{
{6, 2, 3, 4, 5, 11, 3, 8},
{3, 8, 2, 8, 1, 12, 5, 4},
{7, 9, 5, 10, 2, 11, 6, 8},
{6, 7, 3, 4, 3, 5, 5, 3},
{1, 2, 6, 13, 9, 11, 3, 6},
{6, 2, 3, 4, 5, 11, 3, 8},
{4, 6, 8, 9, 7, 1, 5, 3},
{9, 1, 2, 5, 2, 7, 3, 8},
})
}
}
func BenchmarkSolveMax10x10(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMax([][]float64{
{6, 2, 3, 4, 5, 11, 3, 8, 15, 18},
{3, 8, 2, 12, 33, 8, 1, 12, 5, 4},
{7, 9, 5, 11, 10, 2, 22, 11, 6, 8},
{6, 7, 3, 4, 32, 3, 5, 5, 23, 3},
{1, 2, 21, 6, 13, 9, 11, 3, 18, 6},
{6, 2, 17, 3, 4, 41, 5, 11, 3, 8},
{4, 6, 13, 8, 9, 7, 27, 1, 5, 3},
{9, 1, 2, 16, 5, 2, 7, 31, 3, 8},
{7, 1, 13, 8, 9, 4, 27, 6, 5, 3},
{9, 2, 6, 16, 5, 1, 7, 31, 3, 8},
})
}
}
func BenchmarkSolveMax12x12(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMax([][]float64{
{6, 2, 72, 3, 4, 5, 11, 3, 19, 8, 15, 18},
{3, 8, 2, 18, 12, 33, 8, 1, 34, 12, 5, 4},
{7, 9, 5, 11, 10, 51, 2, 22, 11, 6, 15, 8},
{6, 7, 3, 4, 32, 3, 5, 9, 5, 16, 23, 3},
{1, 12, 2, 21, 6, 13, 9, 11, 17, 3, 18, 6},
{6, 2, 16, 37, 17, 3, 4, 41, 5, 11, 3, 8},
{4, 15, 6, 13, 8, 9, 7, 19, 27, 1, 5, 3},
{9, 1, 73, 39, 2, 16, 5, 2, 7, 31, 3, 8},
{6, 2, 72, 3, 4, 5, 11, 3, 19, 8, 15, 18},
{3, 8, 2, 18, 12, 33, 8, 1, 34, 12, 5, 4},
{7, 9, 5, 11, 10, 51, 2, 22, 11, 6, 15, 8},
{6, 7, 3, 4, 32, 3, 5, 9, 5, 16, 23, 3},
})
}
}
func BenchmarkSolveMin8x8(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMin([][]float64{
{6, 2, 3, 4, 5, 11, 3, 8},
{3, 8, 2, 8, 1, 12, 5, 4},
{7, 9, 5, 10, 2, 11, 6, 8},
{6, 7, 3, 4, 3, 5, 5, 3},
{1, 2, 6, 13, 9, 11, 3, 6},
{6, 2, 3, 4, 5, 11, 3, 8},
{4, 6, 8, 9, 7, 1, 5, 3},
{9, 1, 2, 5, 2, 7, 3, 8},
})
}
}
func BenchmarkSolveMin10x10(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMin([][]float64{
{6, 2, 3, 4, 5, 11, 3, 8, 15, 18},
{3, 8, 2, 12, 33, 8, 1, 12, 5, 4},
{7, 9, 5, 11, 10, 2, 22, 11, 6, 8},
{6, 7, 3, 4, 32, 3, 5, 5, 23, 3},
{1, 2, 21, 6, 13, 9, 11, 3, 18, 6},
{6, 2, 17, 3, 4, 41, 5, 11, 3, 8},
{4, 6, 13, 8, 9, 7, 27, 1, 5, 3},
{9, 1, 2, 16, 5, 2, 7, 31, 3, 8},
{7, 1, 13, 8, 9, 4, 27, 6, 5, 3},
{9, 2, 6, 16, 5, 1, 7, 31, 3, 8},
})
}
}
func BenchmarkSolveMin12x12(b *testing.B) {
for i := 0; i < b.N; i++ {
hungarian.SolveMin([][]float64{
{6, 2, 72, 3, 4, 5, 11, 3, 19, 8, 15, 18},
{3, 8, 2, 18, 12, 33, 8, 1, 34, 12, 5, 4},
{7, 9, 5, 11, 10, 51, 2, 22, 11, 6, 15, 8},
{6, 7, 3, 4, 32, 3, 5, 9, 5, 16, 23, 3},
{1, 12, 2, 21, 6, 13, 9, 11, 17, 3, 18, 6},
{6, 2, 16, 37, 17, 3, 4, 41, 5, 11, 3, 8},
{4, 15, 6, 13, 8, 9, 7, 19, 27, 1, 5, 3},
{9, 1, 73, 39, 2, 16, 5, 2, 7, 31, 3, 8},
{6, 2, 72, 3, 4, 5, 11, 3, 19, 8, 15, 18},
{3, 8, 2, 18, 12, 33, 8, 1, 34, 12, 5, 4},
{7, 9, 5, 11, 10, 51, 2, 22, 11, 6, 15, 8},
{6, 7, 3, 4, 32, 3, 5, 9, 5, 16, 23, 3},
})
}
}