-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmunkres.py
More file actions
382 lines (341 loc) · 10.4 KB
/
munkres.py
File metadata and controls
382 lines (341 loc) · 10.4 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Pure-Numba Kuhn-Munkres realization of Hungarian algorithm for parallel processing.
# Adopted by Rustam Mussabayev from the original numba-munkres project.
# Original project is acceptable at https://github.com/hudl/numba-munkres
# and licensed under the Apache License, Version 2.0.
# See https://github.com/hudl/numba-munkres/blob/master/LICENSE.md for details.
# Copyright © 2008 Brian M. Clapper.
from numba import njit
import numpy as np
@njit
def pad_matrix(matrix):
r"""
Pad a possibly non-square matrix to make it square.
Parameters
----------
matrix : np.ndarray
A ``(n_rows, n_cols)`` matrix
Returns
-------
padded : np.ndarray
A padded ``(max(n_rows, n_cols), max(n_rows, n_cols))`` cost matrix
"""
matrix = np.asarray(matrix)
n_rows, n_cols = matrix.shape
n_dim = max(n_rows, n_cols)
padded = np.zeros((n_dim, n_dim))
padded[:n_rows, :n_cols] = matrix
return padded
@njit
def munkres(cost_matrix):
"""
Compute the indexes for the lowest-cost pairings between rows and columns
in the database. Returns a list of indexes where the row index is the list
position and column index is the list value.
Parameters
----------
cost_matrix : np.ndarray
The ``(n_rows, n_cols)`` cost matrix. If this cost matrix is not
square, it will be padded with zeros, via a call to ``pad_matrix()``.
Returns
-------
matches : np.ndarray
The ``(max(n_rows, n_cols)`` vector of rows to columns assignments
"""
n_rows, n_cols = cost_matrix.shape
C = pad_matrix(cost_matrix)
n = C.shape[0]
row_covered = np.full(n, False)
col_covered = np.full(n, False)
Z0_r = np.zeros(1)
Z0_c = np.zeros(1)
path = np.zeros((n*2, n*2))
matches = np.zeros((n, n))
args = (C, row_covered, col_covered, Z0_r, Z0_c, path, matches)
done = False
step = 1
while not done:
if 0 < step < 7:
if step == 1:
step = __step1(*args)
elif step == 2:
step = __step2(*args)
elif step == 3:
step = __step3(*args)
elif step == 4:
step = __step4(*args)
elif step == 5:
step = __step5(*args)
elif step == 6:
step = __step6(*args)
else:
done = True
mat = matches[:n_rows, :n_cols]
col_ind = np.full(n_rows, -1)
for i in range(n_rows):
col_ind[i] = np.argmax(mat[i])
return col_ind
@njit
def __step1(*args):
r"""
For each row of the matrix, find the smallest element and
subtract it from every element in its row. Go to Step 2.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
for i in range(n):
minval = C[i].min()
# Find the minimum value for this row and subtract that minimum
# from every element in the row.
for j in range(n):
C[i, j] -= minval
return 2
@njit
def __step2(*args):
r"""
Find a zero (Z) in the resulting matrix. If there is no starred
zero in its row or column, star Z. Repeat for each element in the
matrix. Go to Step 3.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
for i in range(n):
for j in range(n):
if (C[i, j] == 0) and not col_covered[j] and not row_covered[i]:
matches[i, j] = 1
col_covered[j] = True
row_covered[i] = True
break
col_covered[:] = False
row_covered[:] = False
return 3
@njit
def __step3(*args):
r"""
Cover each column containing a starred zero. If K columns are
covered, the starred zeros describe a complete set of unique
assignments. In this case, Go to DONE, otherwise, Go to Step 4.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
count = 0
for i in range(n):
for j in range(n):
if matches[i, j] == 1 and not col_covered[j]:
col_covered[j] = True
count += 1
if count >= n:
step = 7 # done
else:
step = 4
return step
@njit
def __step4(*args):
r"""
Find a noncovered zero and prime it. If there is no starred zero
in the row containing this primed zero, Go to Step 5. Otherwise,
cover this row and uncover the column containing the starred
zero. Continue in this manner until there are no uncovered zeros
left. Save the smallest uncovered value and Go to Step 6.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
step = 0
done = False
while not done:
row, col = __find_a_zero(*args)
if row < 0:
done = True
step = 6
else:
matches[row, col] = 2
star_col = __find_star_in_row(row, *args)
if star_col >= 0:
col = star_col
row_covered[row] = True
col_covered[col] = False
else:
done = True
Z0_r[0] = row
Z0_c[0] = col
step = 5
return step
@njit
def __step5(*args):
r"""
Construct a series of alternating primed and starred zeros as
follows. Let Z0 represent the uncovered primed zero found in Step 4.
Let Z1 denote the starred zero in the column of Z0 (if any).
Let Z2 denote the primed zero in the row of Z1 (there will always
be one). Continue until the series terminates at a primed zero
that has no starred zero in its column. Unstar each starred zero
of the series, star each primed zero of the series, erase all
primes and uncover every line in the matrix. Return to Step 3
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
count = 0
path[count, 0] = Z0_r[0]
path[count, 1] = Z0_c[0]
done = False
while not done:
row = __find_star_in_col(path[count, 1], *args)
if row >= 0:
count += 1
path[count, 0] = row
path[count, 1] = path[count-1, 1]
else:
done = True
if not done:
col = __find_prime_in_row(path[count, 0], *args)
count += 1
path[count, 0] = path[count-1, 0]
path[count, 1] = col
__convert_path(count, *args)
col_covered[:] = False
row_covered[:] = False
__erase_primes(*args)
return 3
@njit
def __step6(*args):
r"""
Add the value found in Step 4 to every element of each covered
row, and subtract it from every element of each uncovered column.
Return to Step 4 without altering any stars, primes, or covered
lines.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
minval = __find_smallest(*args)
events = 0 # track actual changes to matrix
for i in range(n):
for j in range(n):
if row_covered[i]:
C[i, j] += minval
events += 1
if not col_covered[j]:
C[i, j] -= minval
events += 1
if row_covered[i] and not col_covered[j]:
events -= 2 # change reversed, no real difference
if events == 0:
raise ValueError('Matrix cannot be solved!')
return 4
@njit
def __find_smallest(*args):
r"""
Find the smallest uncovered value in the matrix.
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
minval = np.inf
for i in range(n):
for j in range(n):
if not row_covered[i] and not col_covered[j]:
if minval > C[i, j]:
minval = C[i, j]
return minval
@njit
def __find_a_zero(*args):
r"""
Find the first uncovered element with value 0
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
row = -1
col = -1
i = 0
done = False
while not done:
j = 0
while True:
if C[i, j] == 0 and not row_covered[i] and not col_covered[j]:
row = i
col = j
done = True
j += 1
if j >= n:
break
i += 1
if i >= n:
done = True
return row, col
@njit
def __find_star_in_row(row, *args):
r"""
Find the first starred element in the specified row. Returns
the column index, or -1 if no starred element was found.
Parameters
----------
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
col = -1
for j in range(n):
if matches[row, j] == 1:
col = j
break
return col
@njit
def __find_star_in_col(col, *args):
r"""
Find the first starred element in the specified row. Returns
the row index, or -1 if no starred element was found.
Parameters
----------
col : int
The index of the column to look for the starred element in
Returns
-------
row : int
The starred element in `col`
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
row = -1
for i in range(n):
if matches[i, int(col)] == 1:
row = i
break
return row
@njit
def __find_prime_in_row(row, *args):
r"""
Find the first prime element in the specified row. Returns
the column index, or -1 if no starred element was found.
Parameters
----------
row : int
The index of the row to look for the prime element in
Returns
-------
col : int
The prime element in `row`
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
col = -1
for j in range(n):
if matches[int(row), j] == 2:
col = j
break
return col
@njit
def __convert_path(count, *args):
r"""
Reverse matches
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
for i in range(count+1):
if matches[int(path[i, 0]), int(path[i, 1])] == 1:
matches[int(path[i, 0]), int(path[i, 1])] = 0
else:
matches[int(path[i, 0]), int(path[i, 1])] = 1
@njit
def __erase_primes(*args):
r"""
Erase all prime markings
"""
C, row_covered, col_covered, Z0_r, Z0_c, path, matches = args
n = C.shape[0]
for i in range(n):
for j in range(n):
if matches[i, j] == 2:
matches[i, j] = 0