12 std::vector<int>& assignment,
TMethod Method)
14 assignment.resize(nOfRows, -1);
39 const distMatrix_t& distMatrixIn,
size_t nOfRows,
size_t nOfColumns)
45 size_t nOfElements = nOfRows * nOfColumns;
49 track_t* distMatrixEnd = distMatrix + nOfElements;
51 for (
size_t row = 0; row < nOfElements; row++)
53 track_t value = distMatrixIn[row];
55 distMatrix[row] = value;
59 bool* coveredColumns = (
bool*)calloc(nOfColumns,
sizeof(
bool));
60 bool* coveredRows = (
bool*)calloc(nOfRows,
sizeof(
bool));
61 bool* starMatrix = (
bool*)calloc(nOfElements,
sizeof(
bool));
62 bool* primeMatrix = (
bool*)calloc(nOfElements,
sizeof(
bool));
63 bool* newStarMatrix = (
bool*)calloc(nOfElements,
sizeof(
bool));
66 if (nOfRows <= nOfColumns)
68 for (
size_t row = 0; row < nOfRows; row++)
71 track_t* distMatrixTemp = distMatrix + row;
72 track_t minValue = *distMatrixTemp;
73 distMatrixTemp += nOfRows;
74 while (distMatrixTemp < distMatrixEnd)
76 track_t value = *distMatrixTemp;
81 distMatrixTemp += nOfRows;
84 distMatrixTemp = distMatrix + row;
85 while (distMatrixTemp < distMatrixEnd)
87 *distMatrixTemp -= minValue;
88 distMatrixTemp += nOfRows;
92 for (
size_t row = 0; row < nOfRows; row++)
94 for (
size_t col = 0; col < nOfColumns; col++)
96 if (distMatrix[row + nOfRows * col] == 0)
98 if (!coveredColumns[col])
100 starMatrix[row + nOfRows * col] =
true;
101 coveredColumns[col] =
true;
110 for (
size_t col = 0; col < nOfColumns; col++)
113 track_t* distMatrixTemp = distMatrix + nOfRows * col;
114 track_t* columnEnd = distMatrixTemp + nOfRows;
115 track_t minValue = *distMatrixTemp++;
116 while (distMatrixTemp < columnEnd)
118 track_t value = *distMatrixTemp++;
119 if (value < minValue)
125 distMatrixTemp = distMatrix + nOfRows * col;
126 while (distMatrixTemp < columnEnd)
128 *distMatrixTemp++ -= minValue;
132 for (
size_t col = 0; col < nOfColumns; col++)
134 for (
size_t row = 0; row < nOfRows; row++)
136 if (distMatrix[row + nOfRows * col] == 0)
138 if (!coveredRows[row])
140 starMatrix[row + nOfRows * col] =
true;
141 coveredColumns[col] =
true;
142 coveredRows[row] =
true;
149 for (
size_t row = 0; row < nOfRows; row++)
151 coveredRows[row] =
false;
155 step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
156 nOfColumns, (nOfRows <= nOfColumns) ? nOfRows : nOfColumns);
161 free(coveredColumns);
174 for (
size_t row = 0; row < nOfRows; row++)
176 for (
size_t col = 0; col < nOfColumns; col++)
178 if (starMatrix[row + nOfRows * col])
180 assignment[row] =
static_cast<int>(col);
192 for (
size_t row = 0; row < nOfRows; row++)
194 const int col = assignment[row];
197 cost += distMatrixIn[row + nOfRows * col];
206 bool* newStarMatrix,
bool* primeMatrix,
bool* coveredColumns,
bool* coveredRows,
207 size_t nOfRows,
size_t nOfColumns,
size_t minDim)
209 bool* starMatrixTemp, *columnEnd;
211 for (
size_t col = 0; col < nOfColumns; col++)
213 starMatrixTemp = starMatrix + nOfRows * col;
214 columnEnd = starMatrixTemp + nOfRows;
215 while (starMatrixTemp < columnEnd)
217 if (*starMatrixTemp++)
219 coveredColumns[col] =
true;
225 step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
233 bool* newStarMatrix,
bool* primeMatrix,
bool* coveredColumns,
bool* coveredRows,
234 size_t nOfRows,
size_t nOfColumns,
size_t minDim)
237 size_t nOfCoveredColumns = 0;
238 for (
size_t col = 0; col < nOfColumns; col++)
240 if (coveredColumns[col])
245 if (nOfCoveredColumns == minDim)
253 step3(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
262 bool* newStarMatrix,
bool* primeMatrix,
bool* coveredColumns,
bool* coveredRows,
263 size_t nOfRows,
size_t nOfColumns,
size_t minDim)
265 bool zerosFound =
true;
269 for (
size_t col = 0; col < nOfColumns; col++)
271 if (!coveredColumns[col])
273 for (
size_t row = 0; row < nOfRows; row++)
275 if ((!coveredRows[row]) && (distMatrix[row + nOfRows * col] == 0))
278 primeMatrix[row + nOfRows * col] =
true;
281 for (; starCol < nOfColumns; starCol++)
283 if (starMatrix[row + nOfRows * starCol])
288 if (starCol == nOfColumns)
291 step4(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows,
292 nOfRows, nOfColumns, minDim, row, col);
297 coveredRows[row] =
true;
298 coveredColumns[starCol] =
false;
308 step5(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
316 bool* newStarMatrix,
bool* primeMatrix,
bool* coveredColumns,
bool* coveredRows,
317 size_t nOfRows,
size_t nOfColumns,
size_t minDim,
size_t row,
size_t col)
319 const size_t nOfElements = nOfRows * nOfColumns;
321 for (
size_t n = 0; n < nOfElements; n++)
323 newStarMatrix[n] = starMatrix[n];
326 newStarMatrix[row + nOfRows * col] =
true;
328 size_t starCol = col;
330 for (; starRow < nOfRows; starRow++)
332 if (starMatrix[starRow + nOfRows * starCol])
337 while (starRow < nOfRows)
340 newStarMatrix[starRow + nOfRows * starCol] =
false;
342 size_t primeRow = starRow;
344 for (; primeCol < nOfColumns; primeCol++)
346 if (primeMatrix[primeRow + nOfRows * primeCol])
352 newStarMatrix[primeRow + nOfRows * primeCol] =
true;
355 for (starRow = 0; starRow < nOfRows; starRow++)
357 if (starMatrix[starRow + nOfRows * starCol])
365 for (
size_t n = 0; n < nOfElements; n++)
367 primeMatrix[n] =
false;
368 starMatrix[n] = newStarMatrix[n];
370 for (
size_t n = 0; n < nOfRows; n++)
372 coveredRows[n] =
false;
375 step2a(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
383 bool* newStarMatrix,
bool* primeMatrix,
bool* coveredColumns,
bool* coveredRows,
384 size_t nOfRows,
size_t nOfColumns,
size_t minDim)
387 float h = std::numeric_limits<track_t>::max();
388 for (
size_t row = 0; row < nOfRows; row++)
390 if (!coveredRows[row])
392 for (
size_t col = 0; col < nOfColumns; col++)
394 if (!coveredColumns[col])
396 const float value = distMatrix[row + nOfRows * col];
406 for (
size_t row = 0; row < nOfRows; row++)
408 if (coveredRows[row])
410 for (
size_t col = 0; col < nOfColumns; col++)
412 distMatrix[row + nOfRows * col] += h;
417 for (
size_t col = 0; col < nOfColumns; col++)
419 if (!coveredColumns[col])
421 for (
size_t row = 0; row < nOfRows; row++)
423 distMatrix[row + nOfRows * col] -= h;
428 step3(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows,
436 const distMatrix_t& distMatrixIn,
size_t nOfRows,
size_t nOfColumns)
439 const size_t nOfElements = nOfRows * nOfColumns;
440 float* distMatrix = (
float*)malloc(nOfElements *
sizeof(
float));
441 for (
size_t n = 0; n < nOfElements; n++)
443 distMatrix[n] = distMatrixIn[n];
450 float minValue = std::numeric_limits<track_t>::max();
453 for (
size_t row = 0; row < nOfRows; row++)
455 for (
size_t col = 0; col < nOfColumns; col++)
457 const float value = distMatrix[row + nOfRows * col];
458 if (value != std::numeric_limits<track_t>::max() && (value < minValue))
467 if (minValue != std::numeric_limits<track_t>::max())
469 assignment[tmpRow] =
static_cast<int>(tmpCol);
471 for (
size_t n = 0; n < nOfRows; n++)
473 distMatrix[n + nOfRows * tmpCol] = std::numeric_limits<track_t>::max();
475 for (
size_t n = 0; n < nOfColumns; n++)
477 distMatrix[tmpRow + nOfRows * n] = std::numeric_limits<track_t>::max();
492 const distMatrix_t& distMatrixIn,
size_t nOfRows,
size_t nOfColumns)
495 const size_t nOfElements = nOfRows * nOfColumns;
496 float* distMatrix = (
float*)malloc(nOfElements *
sizeof(
float));
497 for (
size_t n = 0; n < nOfElements; n++)
499 distMatrix[n] = distMatrixIn[n];
503 int* nOfValidObservations = (
int*)calloc(nOfRows,
sizeof(
int));
504 int* nOfValidTracks = (
int*)calloc(nOfColumns,
sizeof(
int));
507 bool infiniteValueFound =
false;
508 bool finiteValueFound =
false;
509 for (
size_t row = 0; row < nOfRows; row++)
511 for (
size_t col = 0; col < nOfColumns; col++)
513 if (distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max())
515 nOfValidTracks[col] += 1;
516 nOfValidObservations[row] += 1;
517 finiteValueFound =
true;
521 infiniteValueFound =
true;
526 if (infiniteValueFound)
528 if (!finiteValueFound)
532 bool repeatSteps =
true;
539 for (
size_t col = 0; col < nOfColumns; col++)
541 bool singleValidationFound =
false;
542 for (
size_t row = 0; row < nOfRows; row++)
544 if (distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max() &&
545 (nOfValidObservations[row] == 1))
547 singleValidationFound =
true;
551 if (singleValidationFound)
553 for (
size_t row = 0; row < nOfRows; row++)
554 if ((nOfValidObservations[row] > 1) &&
555 distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max())
557 distMatrix[row + nOfRows * col] = std::numeric_limits<track_t>::max();
558 nOfValidObservations[row] -= 1;
559 nOfValidTracks[col] -= 1;
569 for (
size_t row = 0; row < nOfRows; row++)
571 bool singleValidationFound =
false;
572 for (
size_t col = 0; col < nOfColumns; col++)
574 if (distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max() && (nOfValidTracks[col] == 1))
576 singleValidationFound =
true;
581 if (singleValidationFound)
583 for (
size_t col = 0; col < nOfColumns; col++)
585 if ((nOfValidTracks[col] > 1) && distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max())
587 distMatrix[row + nOfRows * col] = std::numeric_limits<track_t>::max();
588 nOfValidObservations[row] -= 1;
589 nOfValidTracks[col] -= 1;
600 for (
size_t row = 0; row < nOfRows; row++)
602 if (nOfValidObservations[row] > 1)
604 bool allSinglyValidated =
true;
605 float minValue = std::numeric_limits<track_t>::max();
607 for (
size_t col = 0; col < nOfColumns; col++)
609 const float value = distMatrix[row + nOfRows * col];
610 if (value != std::numeric_limits<track_t>::max())
612 if (nOfValidTracks[col] > 1)
614 allSinglyValidated =
false;
617 else if ((nOfValidTracks[col] == 1) && (value < minValue))
625 if (allSinglyValidated)
627 assignment[row] =
static_cast<int>(tmpCol);
629 for (
size_t n = 0; n < nOfRows; n++)
631 distMatrix[n + nOfRows * tmpCol] = std::numeric_limits<track_t>::max();
633 for (
size_t n = 0; n < nOfColumns; n++)
635 distMatrix[row + nOfRows * n] = std::numeric_limits<track_t>::max();
643 for (
size_t col = 0; col < nOfColumns; col++)
645 if (nOfValidTracks[col] > 1)
647 bool allSinglyValidated =
true;
648 float minValue = std::numeric_limits<track_t>::max();
650 for (
size_t row = 0; row < nOfRows; row++)
652 const float value = distMatrix[row + nOfRows * col];
653 if (value != std::numeric_limits<track_t>::max())
655 if (nOfValidObservations[row] > 1)
657 allSinglyValidated =
false;
660 else if ((nOfValidObservations[row] == 1) && (value < minValue))
668 if (allSinglyValidated)
670 assignment[tmpRow] =
static_cast<int>(col);
672 for (
size_t n = 0; n < nOfRows; n++)
674 distMatrix[n + nOfRows * col] = std::numeric_limits<track_t>::max();
676 for (
size_t n = 0; n < nOfColumns; n++)
678 distMatrix[tmpRow + nOfRows * n] = std::numeric_limits<track_t>::max();
689 float minValue = std::numeric_limits<track_t>::max();
692 for (
size_t row = 0; row < nOfRows; row++)
694 for (
size_t col = 0; col < nOfColumns; col++)
696 const float value = distMatrix[row + nOfRows * col];
697 if (value != std::numeric_limits<track_t>::max() && (value < minValue))
706 if (minValue != std::numeric_limits<track_t>::max())
708 assignment[tmpRow] =
static_cast<int>(tmpCol);
710 for (
size_t n = 0; n < nOfRows; n++)
712 distMatrix[n + nOfRows * tmpCol] = std::numeric_limits<track_t>::max();
714 for (
size_t n = 0; n < nOfColumns; n++)
716 distMatrix[tmpRow + nOfRows * n] = std::numeric_limits<track_t>::max();
726 free(nOfValidObservations);
727 free(nOfValidTracks);
~AssignmentProblemSolver()
void assignmentsuboptimal1(assignments_t &assignment, track_t &cost, const distMatrix_t &distMatrixIn, size_t nOfRows, size_t nOfColumns)
void assignmentoptimal(assignments_t &assignment, track_t &cost, const distMatrix_t &distMatrixIn, size_t nOfRows, size_t nOfColumns)
void buildassignmentvector(assignments_t &assignment, bool *starMatrix, size_t nOfRows, size_t nOfColumns)
void step2a(assignments_t &assignment, track_t *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, size_t nOfRows, size_t nOfColumns, size_t minDim)
void computeassignmentcost(const assignments_t &assignment, track_t &cost, const distMatrix_t &distMatrixIn, size_t nOfRows)
AssignmentProblemSolver()
std::vector< track_t > distMatrix_t
void assignmentsuboptimal2(assignments_t &assignment, track_t &cost, const distMatrix_t &distMatrixIn, size_t nOfRows, size_t nOfColumns)
void step2b(assignments_t &assignment, track_t *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, size_t nOfRows, size_t nOfColumns, size_t minDim)
void step3(assignments_t &assignment, track_t *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, size_t nOfRows, size_t nOfColumns, size_t minDim)
void step4(assignments_t &assignment, track_t *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, size_t nOfRows, size_t nOfColumns, size_t minDim, size_t row, size_t col)
std::vector< int > assignments_t
void step5(assignments_t &assignment, track_t *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, size_t nOfRows, size_t nOfColumns, size_t minDim)
track_t Solve(const distMatrix_t &distMatrixIn, size_t nOfRows, size_t nOfColumns, assignments_t &assignment, TMethod Method=optimal)