HungarianAlg.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #include "HungarianAlg.h"
  2. #include <limits>
  3. // --------------------------------------------------------------------------
  4. //
  5. // --------------------------------------------------------------------------
  6. AssignmentProblemSolver::AssignmentProblemSolver()
  7. {
  8. }
  9. // --------------------------------------------------------------------------
  10. //
  11. // --------------------------------------------------------------------------
  12. AssignmentProblemSolver::~AssignmentProblemSolver()
  13. {
  14. }
  15. // --------------------------------------------------------------------------
  16. //
  17. // --------------------------------------------------------------------------
  18. track_t AssignmentProblemSolver::Solve(
  19. const distMatrix_t& distMatrixIn,
  20. size_t nOfRows,
  21. size_t nOfColumns,
  22. std::vector<int>& assignment,
  23. TMethod Method
  24. )
  25. {
  26. assignment.resize(nOfRows, -1);
  27. track_t cost = 0;
  28. switch (Method)
  29. {
  30. case optimal:
  31. assignmentoptimal(assignment, cost, distMatrixIn, nOfRows, nOfColumns);
  32. break;
  33. case many_forbidden_assignments:
  34. assignmentsuboptimal1(assignment, cost, distMatrixIn, nOfRows, nOfColumns);
  35. break;
  36. case without_forbidden_assignments:
  37. assignmentsuboptimal2(assignment, cost, distMatrixIn, nOfRows, nOfColumns);
  38. break;
  39. }
  40. return cost;
  41. }
  42. // --------------------------------------------------------------------------
  43. // Computes the optimal assignment (minimum overall costs) using Munkres algorithm.
  44. // --------------------------------------------------------------------------
  45. void AssignmentProblemSolver::assignmentoptimal(assignments_t& assignment, track_t& cost, const distMatrix_t& distMatrixIn, size_t nOfRows, size_t nOfColumns)
  46. {
  47. // Generate distance cv::Matrix
  48. // and check cv::Matrix elements positiveness :)
  49. // Total elements number
  50. size_t nOfElements = nOfRows * nOfColumns;
  51. // Memory allocation
  52. track_t* distMatrix = (track_t *)malloc(nOfElements * sizeof(track_t));
  53. if (distMatrix == nullptr)
  54. {
  55. return;
  56. }
  57. // Pointer to last element
  58. track_t* distMatrixEnd = distMatrix + nOfElements;
  59. for (size_t row = 0; row < nOfElements; row++)
  60. {
  61. track_t value = distMatrixIn[row];
  62. assert(value >= 0);
  63. distMatrix[row] = value;
  64. }
  65. // Memory allocation
  66. bool* coveredColumns = (bool*)calloc(nOfColumns, sizeof(bool));
  67. bool* coveredRows = (bool*)calloc(nOfRows, sizeof(bool));
  68. bool* starMatrix = (bool*)calloc(nOfElements, sizeof(bool));
  69. bool* primeMatrix = (bool*)calloc(nOfElements, sizeof(bool));
  70. bool* newStarMatrix = (bool*)calloc(nOfElements, sizeof(bool)); /* used in step4 */
  71. /* preliminary steps */
  72. if (nOfRows <= nOfColumns)
  73. {
  74. for (size_t row = 0; row < nOfRows; row++)
  75. {
  76. /* find the smallest element in the row */
  77. track_t* distMatrixTemp = distMatrix + row;
  78. track_t minValue = *distMatrixTemp;
  79. distMatrixTemp += nOfRows;
  80. while (distMatrixTemp < distMatrixEnd)
  81. {
  82. track_t value = *distMatrixTemp;
  83. if (value < minValue)
  84. {
  85. minValue = value;
  86. }
  87. distMatrixTemp += nOfRows;
  88. }
  89. /* subtract the smallest element from each element of the row */
  90. distMatrixTemp = distMatrix + row;
  91. while (distMatrixTemp < distMatrixEnd)
  92. {
  93. *distMatrixTemp -= minValue;
  94. distMatrixTemp += nOfRows;
  95. }
  96. }
  97. /* Steps 1 and 2a */
  98. for (size_t row = 0; row < nOfRows; row++)
  99. {
  100. for (size_t col = 0; col < nOfColumns; col++)
  101. {
  102. if (distMatrix[row + nOfRows*col] == 0)
  103. {
  104. if (!coveredColumns[col])
  105. {
  106. starMatrix[row + nOfRows * col] = true;
  107. coveredColumns[col] = true;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. else /* if(nOfRows > nOfColumns) */
  115. {
  116. for (size_t col = 0; col < nOfColumns; col++)
  117. {
  118. /* find the smallest element in the column */
  119. track_t* distMatrixTemp = distMatrix + nOfRows*col;
  120. track_t* columnEnd = distMatrixTemp + nOfRows;
  121. track_t minValue = *distMatrixTemp++;
  122. while (distMatrixTemp < columnEnd)
  123. {
  124. track_t value = *distMatrixTemp++;
  125. if (value < minValue)
  126. {
  127. minValue = value;
  128. }
  129. }
  130. /* subtract the smallest element from each element of the column */
  131. distMatrixTemp = distMatrix + nOfRows*col;
  132. while (distMatrixTemp < columnEnd)
  133. {
  134. *distMatrixTemp++ -= minValue;
  135. }
  136. }
  137. /* Steps 1 and 2a */
  138. for (size_t col = 0; col < nOfColumns; col++)
  139. {
  140. for (size_t row = 0; row < nOfRows; row++)
  141. {
  142. if (distMatrix[row + nOfRows*col] == 0)
  143. {
  144. if (!coveredRows[row])
  145. {
  146. starMatrix[row + nOfRows*col] = true;
  147. coveredColumns[col] = true;
  148. coveredRows[row] = true;
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. for (size_t row = 0; row < nOfRows; row++)
  155. {
  156. coveredRows[row] = false;
  157. }
  158. }
  159. /* move to step 2b */
  160. step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, (nOfRows <= nOfColumns) ? nOfRows : nOfColumns);
  161. /* compute cost and remove invalid assignments */
  162. computeassignmentcost(assignment, cost, distMatrixIn, nOfRows);
  163. /* free allocated memory */
  164. free(distMatrix);
  165. free(coveredColumns);
  166. free(coveredRows);
  167. free(starMatrix);
  168. free(primeMatrix);
  169. free(newStarMatrix);
  170. return;
  171. }
  172. // --------------------------------------------------------------------------
  173. //
  174. // --------------------------------------------------------------------------
  175. void AssignmentProblemSolver::buildassignmentvector(assignments_t& assignment, bool *starMatrix, size_t nOfRows, size_t nOfColumns)
  176. {
  177. for (size_t row = 0; row < nOfRows; row++)
  178. {
  179. for (size_t col = 0; col < nOfColumns; col++)
  180. {
  181. if (starMatrix[row + nOfRows * col])
  182. {
  183. assignment[row] = static_cast<int>(col);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. // --------------------------------------------------------------------------
  190. //
  191. // --------------------------------------------------------------------------
  192. void AssignmentProblemSolver::computeassignmentcost(const assignments_t& assignment, track_t& cost, const distMatrix_t& distMatrixIn, size_t nOfRows)
  193. {
  194. for (size_t row = 0; row < nOfRows; row++)
  195. {
  196. const int col = assignment[row];
  197. if (col >= 0)
  198. {
  199. cost += distMatrixIn[row + nOfRows * col];
  200. }
  201. }
  202. }
  203. // --------------------------------------------------------------------------
  204. //
  205. // --------------------------------------------------------------------------
  206. void AssignmentProblemSolver::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)
  207. {
  208. bool *starMatrixTemp, *columnEnd;
  209. /* cover every column containing a starred zero */
  210. for (size_t col = 0; col < nOfColumns; col++)
  211. {
  212. starMatrixTemp = starMatrix + nOfRows * col;
  213. columnEnd = starMatrixTemp + nOfRows;
  214. while (starMatrixTemp < columnEnd)
  215. {
  216. if (*starMatrixTemp++)
  217. {
  218. coveredColumns[col] = true;
  219. break;
  220. }
  221. }
  222. }
  223. /* move to step 3 */
  224. step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
  225. }
  226. // --------------------------------------------------------------------------
  227. //
  228. // --------------------------------------------------------------------------
  229. void AssignmentProblemSolver::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)
  230. {
  231. /* count covered columns */
  232. size_t nOfCoveredColumns = 0;
  233. for (size_t col = 0; col < nOfColumns; col++)
  234. {
  235. if (coveredColumns[col])
  236. {
  237. nOfCoveredColumns++;
  238. }
  239. }
  240. if (nOfCoveredColumns == minDim)
  241. {
  242. /* algorithm finished */
  243. buildassignmentvector(assignment, starMatrix, nOfRows, nOfColumns);
  244. }
  245. else
  246. {
  247. /* move to step 3 */
  248. step3_5(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
  249. }
  250. }
  251. // --------------------------------------------------------------------------
  252. //
  253. // --------------------------------------------------------------------------
  254. void AssignmentProblemSolver::step3_5(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)
  255. {
  256. for (;;)
  257. {
  258. /* step 3 */
  259. bool zerosFound = true;
  260. while (zerosFound)
  261. {
  262. zerosFound = false;
  263. for (size_t col = 0; col < nOfColumns; col++)
  264. {
  265. if (!coveredColumns[col])
  266. {
  267. for (size_t row = 0; row < nOfRows; row++)
  268. {
  269. if ((!coveredRows[row]) && (distMatrix[row + nOfRows*col] == 0))
  270. {
  271. /* prime zero */
  272. primeMatrix[row + nOfRows*col] = true;
  273. /* find starred zero in current row */
  274. size_t starCol = 0;
  275. for (; starCol < nOfColumns; starCol++)
  276. {
  277. if (starMatrix[row + nOfRows * starCol])
  278. {
  279. break;
  280. }
  281. }
  282. if (starCol == nOfColumns) /* no starred zero found */
  283. {
  284. /* move to step 4 */
  285. step4(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
  286. return;
  287. }
  288. else
  289. {
  290. coveredRows[row] = true;
  291. coveredColumns[starCol] = false;
  292. zerosFound = true;
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. /* step 5 */
  301. track_t h = std::numeric_limits<track_t>::max();
  302. for (size_t row = 0; row < nOfRows; row++)
  303. {
  304. if (!coveredRows[row])
  305. {
  306. for (size_t col = 0; col < nOfColumns; col++)
  307. {
  308. if (!coveredColumns[col])
  309. {
  310. const track_t value = distMatrix[row + nOfRows*col];
  311. if (value < h)
  312. {
  313. h = value;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. /* add h to each covered row */
  320. for (size_t row = 0; row < nOfRows; row++)
  321. {
  322. if (coveredRows[row])
  323. {
  324. for (size_t col = 0; col < nOfColumns; col++)
  325. {
  326. distMatrix[row + nOfRows*col] += h;
  327. }
  328. }
  329. }
  330. /* subtract h from each uncovered column */
  331. for (size_t col = 0; col < nOfColumns; col++)
  332. {
  333. if (!coveredColumns[col])
  334. {
  335. for (size_t row = 0; row < nOfRows; row++)
  336. {
  337. distMatrix[row + nOfRows*col] -= h;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. // --------------------------------------------------------------------------
  344. //
  345. // --------------------------------------------------------------------------
  346. void AssignmentProblemSolver::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)
  347. {
  348. const size_t nOfElements = nOfRows * nOfColumns;
  349. /* generate temporary copy of starMatrix */
  350. for (size_t n = 0; n < nOfElements; n++)
  351. {
  352. newStarMatrix[n] = starMatrix[n];
  353. }
  354. /* star current zero */
  355. newStarMatrix[row + nOfRows*col] = true;
  356. /* find starred zero in current column */
  357. size_t starCol = col;
  358. size_t starRow = 0;
  359. for (; starRow < nOfRows; starRow++)
  360. {
  361. if (starMatrix[starRow + nOfRows * starCol])
  362. {
  363. break;
  364. }
  365. }
  366. while (starRow < nOfRows)
  367. {
  368. /* unstar the starred zero */
  369. newStarMatrix[starRow + nOfRows*starCol] = false;
  370. /* find primed zero in current row */
  371. size_t primeRow = starRow;
  372. size_t primeCol = 0;
  373. for (; primeCol < nOfColumns; primeCol++)
  374. {
  375. if (primeMatrix[primeRow + nOfRows * primeCol])
  376. {
  377. break;
  378. }
  379. }
  380. /* star the primed zero */
  381. newStarMatrix[primeRow + nOfRows*primeCol] = true;
  382. /* find starred zero in current column */
  383. starCol = primeCol;
  384. for (starRow = 0; starRow < nOfRows; starRow++)
  385. {
  386. if (starMatrix[starRow + nOfRows * starCol])
  387. {
  388. break;
  389. }
  390. }
  391. }
  392. /* use temporary copy as new starMatrix */
  393. /* delete all primes, uncover all rows */
  394. for (size_t n = 0; n < nOfElements; n++)
  395. {
  396. primeMatrix[n] = false;
  397. starMatrix[n] = newStarMatrix[n];
  398. }
  399. for (size_t n = 0; n < nOfRows; n++)
  400. {
  401. coveredRows[n] = false;
  402. }
  403. /* move to step 2a */
  404. step2a(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
  405. }
  406. // --------------------------------------------------------------------------
  407. // Computes a suboptimal solution. Good for cases without forbidden assignments.
  408. // --------------------------------------------------------------------------
  409. void AssignmentProblemSolver::assignmentsuboptimal2(assignments_t& assignment, track_t& cost, const distMatrix_t& distMatrixIn, size_t nOfRows, size_t nOfColumns)
  410. {
  411. /* make working copy of distance Matrix */
  412. const size_t nOfElements = nOfRows * nOfColumns;
  413. track_t* distMatrix = (track_t*)malloc(nOfElements * sizeof(track_t));
  414. for (size_t n = 0; n < nOfElements; n++)
  415. {
  416. distMatrix[n] = distMatrixIn[n];
  417. }
  418. /* recursively search for the minimum element and do the assignment */
  419. for (;;)
  420. {
  421. /* find minimum distance observation-to-track pair */
  422. track_t minValue = std::numeric_limits<track_t>::max();
  423. size_t tmpRow = 0;
  424. size_t tmpCol = 0;
  425. for (size_t row = 0; row < nOfRows; row++)
  426. {
  427. for (size_t col = 0; col < nOfColumns; col++)
  428. {
  429. const track_t value = distMatrix[row + nOfRows*col];
  430. if (value != std::numeric_limits<track_t>::max() && (value < minValue))
  431. {
  432. minValue = value;
  433. tmpRow = row;
  434. tmpCol = col;
  435. }
  436. }
  437. }
  438. if (minValue != std::numeric_limits<track_t>::max())
  439. {
  440. assignment[tmpRow] = static_cast<int>(tmpCol);
  441. cost += minValue;
  442. for (size_t n = 0; n < nOfRows; n++)
  443. {
  444. distMatrix[n + nOfRows*tmpCol] = std::numeric_limits<track_t>::max();
  445. }
  446. for (size_t n = 0; n < nOfColumns; n++)
  447. {
  448. distMatrix[tmpRow + nOfRows*n] = std::numeric_limits<track_t>::max();
  449. }
  450. }
  451. else
  452. {
  453. break;
  454. }
  455. }
  456. free(distMatrix);
  457. }
  458. // --------------------------------------------------------------------------
  459. // Computes a suboptimal solution. Good for cases with many forbidden assignments.
  460. // --------------------------------------------------------------------------
  461. void AssignmentProblemSolver::assignmentsuboptimal1(assignments_t& assignment, track_t& cost, const distMatrix_t& distMatrixIn, size_t nOfRows, size_t nOfColumns)
  462. {
  463. /* make working copy of distance Matrix */
  464. const size_t nOfElements = nOfRows * nOfColumns;
  465. track_t* distMatrix = (track_t *)malloc(nOfElements * sizeof(track_t));
  466. for (size_t n = 0; n < nOfElements; n++)
  467. {
  468. distMatrix[n] = distMatrixIn[n];
  469. }
  470. /* allocate memory */
  471. int* nOfValidObservations = (int *)calloc(nOfRows, sizeof(int));
  472. int* nOfValidTracks = (int *)calloc(nOfColumns, sizeof(int));
  473. /* compute number of validations */
  474. bool infiniteValueFound = false;
  475. bool finiteValueFound = false;
  476. for (size_t row = 0; row < nOfRows; row++)
  477. {
  478. for (size_t col = 0; col < nOfColumns; col++)
  479. {
  480. if (distMatrix[row + nOfRows*col] != std::numeric_limits<track_t>::max())
  481. {
  482. nOfValidTracks[col] += 1;
  483. nOfValidObservations[row] += 1;
  484. finiteValueFound = true;
  485. }
  486. else
  487. {
  488. infiniteValueFound = true;
  489. }
  490. }
  491. }
  492. if (infiniteValueFound)
  493. {
  494. if (!finiteValueFound)
  495. {
  496. /* free allocated memory */
  497. free(nOfValidObservations);
  498. free(nOfValidTracks);
  499. free(distMatrix);
  500. return;
  501. }
  502. bool repeatSteps = true;
  503. while (repeatSteps)
  504. {
  505. repeatSteps = false;
  506. /* step 1: reject assignments of multiply validated tracks to singly validated observations */
  507. for (size_t col = 0; col < nOfColumns; col++)
  508. {
  509. bool singleValidationFound = false;
  510. for (size_t row = 0; row < nOfRows; row++)
  511. {
  512. if (distMatrix[row + nOfRows * col] != std::numeric_limits<track_t>::max() && (nOfValidObservations[row] == 1))
  513. {
  514. singleValidationFound = true;
  515. break;
  516. }
  517. }
  518. if (singleValidationFound)
  519. {
  520. for (size_t nestedRow = 0; nestedRow < nOfRows; nestedRow++)
  521. if ((nOfValidObservations[nestedRow] > 1) && distMatrix[nestedRow + nOfRows * col] != std::numeric_limits<track_t>::max())
  522. {
  523. distMatrix[nestedRow + nOfRows * col] = std::numeric_limits<track_t>::max();
  524. nOfValidObservations[nestedRow] -= 1;
  525. nOfValidTracks[col] -= 1;
  526. repeatSteps = true;
  527. }
  528. }
  529. }
  530. /* step 2: reject assignments of multiply validated observations to singly validated tracks */
  531. if (nOfColumns > 1)
  532. {
  533. for (size_t row = 0; row < nOfRows; row++)
  534. {
  535. bool singleValidationFound = false;
  536. for (size_t col = 0; col < nOfColumns; col++)
  537. {
  538. if (distMatrix[row + nOfRows*col] != std::numeric_limits<track_t>::max() && (nOfValidTracks[col] == 1))
  539. {
  540. singleValidationFound = true;
  541. break;
  542. }
  543. }
  544. if (singleValidationFound)
  545. {
  546. for (size_t col = 0; col < nOfColumns; col++)
  547. {
  548. if ((nOfValidTracks[col] > 1) && distMatrix[row + nOfRows*col] != std::numeric_limits<track_t>::max())
  549. {
  550. distMatrix[row + nOfRows*col] = std::numeric_limits<track_t>::max();
  551. nOfValidObservations[row] -= 1;
  552. nOfValidTracks[col] -= 1;
  553. repeatSteps = true;
  554. }
  555. }
  556. }
  557. }
  558. }
  559. } /* while(repeatSteps) */
  560. /* for each multiply validated track that validates only with singly validated */
  561. /* observations, choose the observation with minimum distance */
  562. for (size_t row = 0; row < nOfRows; row++)
  563. {
  564. if (nOfValidObservations[row] > 1)
  565. {
  566. bool allSinglyValidated = true;
  567. track_t minValue = std::numeric_limits<track_t>::max();
  568. size_t tmpCol = 0;
  569. for (size_t col = 0; col < nOfColumns; col++)
  570. {
  571. const track_t value = distMatrix[row + nOfRows*col];
  572. if (value != std::numeric_limits<track_t>::max())
  573. {
  574. if (nOfValidTracks[col] > 1)
  575. {
  576. allSinglyValidated = false;
  577. break;
  578. }
  579. else if ((nOfValidTracks[col] == 1) && (value < minValue))
  580. {
  581. tmpCol = col;
  582. minValue = value;
  583. }
  584. }
  585. }
  586. if (allSinglyValidated)
  587. {
  588. assignment[row] = static_cast<int>(tmpCol);
  589. cost += minValue;
  590. for (size_t n = 0; n < nOfRows; n++)
  591. {
  592. distMatrix[n + nOfRows*tmpCol] = std::numeric_limits<track_t>::max();
  593. }
  594. for (size_t n = 0; n < nOfColumns; n++)
  595. {
  596. distMatrix[row + nOfRows*n] = std::numeric_limits<track_t>::max();
  597. }
  598. }
  599. }
  600. }
  601. // for each multiply validated observation that validates only with singly validated track, choose the track with minimum distance
  602. for (size_t col = 0; col < nOfColumns; col++)
  603. {
  604. if (nOfValidTracks[col] > 1)
  605. {
  606. bool allSinglyValidated = true;
  607. track_t minValue = std::numeric_limits<track_t>::max();
  608. size_t tmpRow = 0;
  609. for (size_t row = 0; row < nOfRows; row++)
  610. {
  611. const track_t value = distMatrix[row + nOfRows*col];
  612. if (value != std::numeric_limits<track_t>::max())
  613. {
  614. if (nOfValidObservations[row] > 1)
  615. {
  616. allSinglyValidated = false;
  617. break;
  618. }
  619. else if ((nOfValidObservations[row] == 1) && (value < minValue))
  620. {
  621. tmpRow = row;
  622. minValue = value;
  623. }
  624. }
  625. }
  626. if (allSinglyValidated)
  627. {
  628. assignment[tmpRow] = static_cast<int>(col);
  629. cost += minValue;
  630. for (size_t n = 0; n < nOfRows; n++)
  631. {
  632. distMatrix[n + nOfRows*col] = std::numeric_limits<track_t>::max();
  633. }
  634. for (size_t n = 0; n < nOfColumns; n++)
  635. {
  636. distMatrix[tmpRow + nOfRows*n] = std::numeric_limits<track_t>::max();
  637. }
  638. }
  639. }
  640. }
  641. } /* if(infiniteValueFound) */
  642. /* now, recursively search for the minimum element and do the assignment */
  643. for (;;)
  644. {
  645. /* find minimum distance observation-to-track pair */
  646. track_t minValue = std::numeric_limits<track_t>::max();
  647. size_t tmpRow = 0;
  648. size_t tmpCol = 0;
  649. for (size_t row = 0; row < nOfRows; row++)
  650. {
  651. for (size_t col = 0; col < nOfColumns; col++)
  652. {
  653. const track_t value = distMatrix[row + nOfRows*col];
  654. if (value != std::numeric_limits<track_t>::max() && (value < minValue))
  655. {
  656. minValue = value;
  657. tmpRow = row;
  658. tmpCol = col;
  659. }
  660. }
  661. }
  662. if (minValue != std::numeric_limits<track_t>::max())
  663. {
  664. assignment[tmpRow] = static_cast<int>(tmpCol);
  665. cost += minValue;
  666. for (size_t n = 0; n < nOfRows; n++)
  667. {
  668. distMatrix[n + nOfRows*tmpCol] = std::numeric_limits<track_t>::max();
  669. }
  670. for (size_t n = 0; n < nOfColumns; n++)
  671. {
  672. distMatrix[tmpRow + nOfRows*n] = std::numeric_limits<track_t>::max();
  673. }
  674. }
  675. else
  676. {
  677. break;
  678. }
  679. }
  680. /* free allocated memory */
  681. free(nOfValidObservations);
  682. free(nOfValidTracks);
  683. free(distMatrix);
  684. }