-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminCostAlgorithm.cpp
398 lines (323 loc) · 11.3 KB
/
minCostAlgorithm.cpp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
minCostAssignment.cpp - this is an implementation of the Hungarian algorithm,
also known as the Kuhn-Munkres algorithm. The goal is to find a column for each
row in a given matrix such that the lowest possible sum is attained.
Code is based off the Hungarian algorithm implementation by Markus Buehren and Cong Ma.
*/
#include <stdlib.h>
#include <cfloat> // for DBL_MAX
#include <cmath> // for fabs()
#include "minCostAlgorithm.h"
//*****************************************************************************//
// A single function used to tie all others together and compute the minimum sum.
//*****************************************************************************//
double minCostAlgorithm::Solve(std::vector <std::vector<double> >& costMatrix, std::vector<int>& Assignment)
{
unsigned int nRows = costMatrix.size();
unsigned int nCols = costMatrix[0].size();
double *costMatrixIn = new double[nRows * nCols];
int *assignment = new int[nRows];
double cost = 0.0;
// Populate costMatrixIn with the contents of costMatrix.
// The goal is to store costMatrix in a one-dimensional form.
for (unsigned int i = 0; i < nRows; ++i)
for (unsigned int j = 0; j < nCols; ++j)
costMatrixIn[i + nRows * j] = costMatrix[i][j];
// Call actual assignment solving function
optimalAssignment(assignment, &cost, costMatrixIn, nRows, nCols);
Assignment.clear();
for (unsigned int r = 0; r < nRows; r++)
Assignment.push_back(assignment[r]);
delete[] costMatrixIn;
delete[] assignment;
return cost;
}
// Solve optimal solution using Hungarian algorithm.
void minCostAlgorithm::optimalAssignment(int *assignment, double *cost, double *costMatrixIn, int nOfRows, int nOfColumns)
{
double *costMatrix, *costMatrixTemp, *costMatrixEnd, *columnEnd, value, minValue;
bool *coveredColumns, *coveredRows, *starMatrix, *newStarMatrix, *primeMatrix;
int nOfElements, minDim, row, col;
// Initialize all of assignment to -1; starting state
*cost = 0;
for (row = 0; row<nOfRows; row++)
assignment[row] = -1;
// Construct a copy of cost matrix
// Confirm that no elements are negative
nOfElements = nOfRows * nOfColumns;
costMatrix = (double *)malloc(nOfElements * sizeof(double));
costMatrixEnd = costMatrix + nOfElements;
for (row = 0; row<nOfElements; row++)
{
value = costMatrixIn[row];
if (value < 0)
std::cerr << "All matrix elements have to be non-negative." << std::endl;
costMatrix[row] = value;
}
// Allocate memory for boolean arrays
coveredColumns = (bool *)calloc(nOfColumns, sizeof(bool));
coveredRows = (bool *)calloc(nOfRows, sizeof(bool));
starMatrix = (bool *)calloc(nOfElements, sizeof(bool));
primeMatrix = (bool *)calloc(nOfElements, sizeof(bool));
newStarMatrix = (bool *)calloc(nOfElements, sizeof(bool)); // used in step4
// Preliminary setup
if (nOfRows <= nOfColumns)
{
minDim = nOfRows;
for (row = 0; row<nOfRows; row++)
{
// Locate minimum element in each row
costMatrixTemp = costMatrix + row;
minValue = *costMatrixTemp;
costMatrixTemp += nOfRows;
while (costMatrixTemp < costMatrixEnd)
{
value = *costMatrixTemp;
if (value < minValue)
minValue = value;
costMatrixTemp += nOfRows;
}
// Subtract minimum element from each element in the row
costMatrixTemp = costMatrix + row;
while (costMatrixTemp < costMatrixEnd)
{
*costMatrixTemp -= minValue;
costMatrixTemp += nOfRows;
}
}
// For steps 1 and 2a
for (row = 0; row<nOfRows; row++)
for (col = 0; col<nOfColumns; col++)
if (fabs(costMatrix[row + nOfRows*col]) < DBL_EPSILON)
if (!coveredColumns[col])
{
starMatrix[row + nOfRows*col] = true;
coveredColumns[col] = true;
break;
}
}
else // Read: number of rows > number of columns
{
minDim = nOfColumns;
for (col = 0; col<nOfColumns; col++)
{
// find the smallest element in the column
costMatrixTemp = costMatrix + nOfRows*col;
columnEnd = costMatrixTemp + nOfRows;
minValue = *costMatrixTemp++;
while (costMatrixTemp < columnEnd)
{
value = *costMatrixTemp++;
if (value < minValue)
minValue = value;
}
// subtract the smallest element from each element of the column
costMatrixTemp = costMatrix + nOfRows*col;
while (costMatrixTemp < columnEnd)
*costMatrixTemp++ -= minValue;
}
// for steps 1 and 2a
for (col = 0; col<nOfColumns; col++)
for (row = 0; row<nOfRows; row++)
if (fabs(costMatrix[row + nOfRows*col]) < DBL_EPSILON)
if (!coveredRows[row])
{
starMatrix[row + nOfRows*col] = true;
coveredColumns[col] = true;
coveredRows[row] = true;
break;
}
for (row = 0; row<nOfRows; row++)
coveredRows[row] = false;
}
// Transition to step 2b
step2b(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
// Compute cost, remove invalid assignments
computeAssignmentCost(assignment, cost, costMatrixIn, nOfRows);
// Free memory
free(costMatrix);
free(coveredColumns);
free(coveredRows);
free(starMatrix);
free(primeMatrix);
free(newStarMatrix);
return;
}
/********************************************************/
void minCostAlgorithm::buildAssignmentVector(int *assignment, bool *starMatrix, int nOfRows, int nOfColumns)
{
int row, col;
for (row = 0; row < nOfRows; ++row)
for (col = 0; col < nOfColumns; ++col)
if (starMatrix[row + nOfRows*col])
{
#ifdef ONE_INDEXING
assignment[row] = col + 1; // MATLAB-Indexing
#else
assignment[row] = col;
#endif
break;
}
}
/********************************************************/
void minCostAlgorithm::computeAssignmentCost(int *assignment, double *cost, double *costMatrix, int nOfRows)
{
int row, col;
for (row = 0; row<nOfRows; ++row)
{
col = assignment[row];
if (col >= 0)
*cost += costMatrix[row + nOfRows*col];
}
}
/********************************************************/
void minCostAlgorithm::step2a(int *assignment, double *costMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
bool *starMatrixTemp, *columnEnd;
int col;
// Cover every column containing a starred zero
for (col = 0; col < nOfColumns; ++col)
{
starMatrixTemp = starMatrix + nOfRows*col;
columnEnd = starMatrixTemp + nOfRows;
while (starMatrixTemp < columnEnd){
if (*starMatrixTemp++)
{
coveredColumns[col] = true;
break;
}
}
}
// move to step 2b
step2b(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
/*******************************************************/
void minCostAlgorithm::step2b(int *assignment, double *costMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
// Count number of covered columns
int col, nOfCoveredColumns = 0;
for (col = 0; col < nOfColumns; ++col)
if (coveredColumns[col])
nOfCoveredColumns++;
if (nOfCoveredColumns == minDim)
{
// Finished, no more steps needed
buildAssignmentVector(assignment, starMatrix, nOfRows, nOfColumns);
}
else
{
// move to step 3
step3(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
}
/********************************************************/
void minCostAlgorithm::step3(int *assignment, double *costMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
bool zerosFound;
int row, col, starCol;
zerosFound = true;
while (zerosFound)
{
zerosFound = false;
for (col = 0; col < nOfColumns; ++col)
if (!coveredColumns[col])
for (row = 0; row < nOfRows; ++row)
if ((!coveredRows[row]) && (fabs(costMatrix[row + nOfRows*col]) < DBL_EPSILON))
{
// Prime zero
primeMatrix[row + nOfRows*col] = true;
// Find starred zero in current row
for (starCol = 0; starCol<nOfColumns; ++starCol)
if (starMatrix[row + nOfRows*starCol])
break;
if (starCol == nOfColumns) // No starred zero detected
{
// move to step 4
step4(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
return;
}
else
{
coveredRows[row] = true;
coveredColumns[starCol] = false;
zerosFound = true;
break;
}
}
}
// move to step 5
step5(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
/********************************************************/
void minCostAlgorithm::step4(int *assignment, double *costMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim, int row, int col)
{
int n, starRow, starCol, primeRow, primeCol;
int nOfElements = nOfRows*nOfColumns;
// Construct temporary copy of starMatrix
for (n = 0; n < nOfElements; ++n)
newStarMatrix[n] = starMatrix[n];
// Star current zero
newStarMatrix[row + nOfRows*col] = true;
// Find starred zero in current column
starCol = col;
for (starRow = 0; starRow < nOfRows; starRow++)
if (starMatrix[starRow + nOfRows*starCol])
break;
while (starRow<nOfRows)
{
// Unstar the starred zero
newStarMatrix[starRow + nOfRows*starCol] = false;
// Find primed zero in current row
primeRow = starRow;
for (primeCol = 0; primeCol < nOfColumns; ++primeCol)
if (primeMatrix[primeRow + nOfRows*primeCol])
break;
// Star the primed zero
newStarMatrix[primeRow + nOfRows*primeCol] = true;
// Find starred zero in current column
starCol = primeCol;
for (starRow = 0; starRow < nOfRows; ++starRow)
if (starMatrix[starRow + nOfRows*starCol])
break;
}
// Use temporary copy as the new starMatrix
// Delete all primes, uncover all rows
for (n = 0; n < nOfElements; ++n)
{
primeMatrix[n] = false;
starMatrix[n] = newStarMatrix[n];
}
for (n = 0; n < nOfRows; ++n)
coveredRows[n] = false;
// move to step 2a
step2a(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}
/********************************************************/
void minCostAlgorithm::step5(int *assignment, double *costMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
{
double h, value;
int row, col;
// Find the lowest uncovered element - h
h = DBL_MAX;
for (row = 0; row < nOfRows; ++row)
if (!coveredRows[row])
for (col = 0; col < nOfColumns; ++col)
if (!coveredColumns[col])
{
value = costMatrix[row + nOfRows*col];
if (value < h)
h = value;
}
// Add h to each covered row
for (row = 0; row < nOfRows; ++row)
if (coveredRows[row])
for (col = 0; col < nOfColumns; ++col)
costMatrix[row + nOfRows*col] += h;
// Subtract h from each uncovered column
for (col = 0; col < nOfColumns; ++col)
if (!coveredColumns[col])
for (row = 0; row < nOfRows; ++row)
costMatrix[row + nOfRows*col] -= h;
// move to step 3
step3(assignment, costMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
}