11#include " HungarianAlg.h"
22
3- using namespace std ;
4-
53AssignmentProblemSolver::AssignmentProblemSolver ()
64{
75}
@@ -10,21 +8,21 @@ AssignmentProblemSolver::~AssignmentProblemSolver()
108{
119}
1210
13- double AssignmentProblemSolver::Solve (vector<vector<double >>& DistMatrix, vector<int >& Assignment,TMethod Method)
11+ double AssignmentProblemSolver::Solve (std:: vector<std:: vector<double >>& distMatrix,std:: vector<int >& Assignment,TMethod Method)
1412{
15- size_t N=DistMatrix .size (); // number of columns (tracks)
16- size_t M = DistMatrix [0 ].size (); // number of rows (measurements)
13+ size_t N=distMatrix .size (); // number of columns (tracks)
14+ size_t M = distMatrix [0 ].size (); // number of rows (measurements)
1715
1816 int *assignment =new int [N];
1917 double *distIn =new double [N*M];
2018
2119 double cost;
22- // Fill matrix with random numbers
20+ // Fill cv::Matrix with random numbers
2321 for (size_t i = 0 ; i<N; i++)
2422 {
2523 for (size_t j = 0 ; j<M; j++)
2624 {
27- distIn[i+N*j] = DistMatrix [i][j];
25+ distIn[i+N*j] = distMatrix [i][j];
2826 }
2927 }
3028 switch (Method)
@@ -62,7 +60,7 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
6260 bool *coveredColumns;
6361 bool *coveredRows;
6462 bool *starMatrix;
65- bool *newStarMatrix ;
63+ bool *newstarMatrix ;
6664 bool *primeMatrix;
6765
6866 int nOfElements;
@@ -77,14 +75,14 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
7775 assignment[row] = -1 ;
7876 }
7977
80- // Generate distance matrix
81- // and check matrix elements positiveness :)
78+ // Generate distance cv::Matrix
79+ // and check cv::Matrix elements positiveness :)
8280
8381 // Total elements number
8482 nOfElements = nOfRows * nOfColumns;
8583 // Memory allocation
8684 distMatrix = (double *)malloc (nOfElements * sizeof (double ));
87- // Pointer to last element
85+ // cv:: Pointer to last element
8886 distMatrixEnd = distMatrix + nOfElements;
8987
9088 //
@@ -93,7 +91,7 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
9391 value = distMatrixIn[row];
9492 if (value < 0 )
9593 {
96- cout << " All matrix elements have to be non-negative." << endl;
94+ std:: cout << " All cv::Matrix elements have to be non-negative." << std:: endl;
9795 }
9896 distMatrix[row] = value;
9997 }
@@ -103,7 +101,7 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
103101 coveredRows = (bool *)calloc (nOfRows, sizeof (bool ));
104102 starMatrix = (bool *)calloc (nOfElements, sizeof (bool ));
105103 primeMatrix = (bool *)calloc (nOfElements, sizeof (bool ));
106- newStarMatrix = (bool *)calloc (nOfElements, sizeof (bool )); /* used in step4 */
104+ newstarMatrix = (bool *)calloc (nOfElements, sizeof (bool )); /* used in step4 */
107105
108106 /* preliminary steps */
109107 if (nOfRows <= nOfColumns)
@@ -197,7 +195,7 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
197195 }
198196 }
199197 /* move to step 2b */
200- step2b (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
198+ step2b (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
201199 /* compute cost and remove invalid assignments */
202200 computeassignmentcost (assignment, cost, distMatrixIn, nOfRows);
203201 /* free allocated memory */
@@ -206,7 +204,7 @@ void AssignmentProblemSolver::assignmentoptimal(int *assignment, double *cost, d
206204 free (coveredRows);
207205 free (starMatrix);
208206 free (primeMatrix);
209- free (newStarMatrix );
207+ free (newstarMatrix );
210208 return ;
211209}
212210// --------------------------------------------------------------------------
@@ -246,7 +244,7 @@ void AssignmentProblemSolver::computeassignmentcost(int *assignment, double *cos
246244// --------------------------------------------------------------------------
247245//
248246// --------------------------------------------------------------------------
249- void AssignmentProblemSolver::step2a (int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
247+ void AssignmentProblemSolver::step2a (int *assignment, double *distMatrix, bool *starMatrix, bool *newstarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
250248{
251249 bool *starMatrixTemp, *columnEnd;
252250 int col;
@@ -265,13 +263,13 @@ void AssignmentProblemSolver::step2a(int *assignment, double *distMatrix, bool *
265263 }
266264 }
267265 /* move to step 3 */
268- step2b (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
266+ step2b (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
269267}
270268
271269// --------------------------------------------------------------------------
272270//
273271// --------------------------------------------------------------------------
274- void AssignmentProblemSolver::step2b (int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
272+ void AssignmentProblemSolver::step2b (int *assignment, double *distMatrix, bool *starMatrix, bool *newstarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
275273{
276274 int col, nOfCoveredColumns;
277275 /* count covered columns */
@@ -291,14 +289,14 @@ void AssignmentProblemSolver::step2b(int *assignment, double *distMatrix, bool *
291289 else
292290 {
293291 /* move to step 3 */
294- step3 (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
292+ step3 (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
295293 }
296294}
297295
298296// --------------------------------------------------------------------------
299297//
300298// --------------------------------------------------------------------------
301- void AssignmentProblemSolver::step3 (int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
299+ void AssignmentProblemSolver::step3 (int *assignment, double *distMatrix, bool *starMatrix, bool *newstarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
302300{
303301 bool zerosFound;
304302 int row, col, starCol;
@@ -325,7 +323,7 @@ void AssignmentProblemSolver::step3(int *assignment, double *distMatrix, bool *s
325323 if (starCol == nOfColumns) /* no starred zero found */
326324 {
327325 /* move to step 4 */
328- step4 (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
326+ step4 (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col);
329327 return ;
330328 }
331329 else
@@ -341,23 +339,23 @@ void AssignmentProblemSolver::step3(int *assignment, double *distMatrix, bool *s
341339 }
342340 }
343341 /* move to step 5 */
344- step5 (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
342+ step5 (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
345343}
346344
347345// --------------------------------------------------------------------------
348346//
349347// --------------------------------------------------------------------------
350- void AssignmentProblemSolver::step4 (int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim, int row, int col)
348+ void AssignmentProblemSolver::step4 (int *assignment, double *distMatrix, bool *starMatrix, bool *newstarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim, int row, int col)
351349{
352350 int n, starRow, starCol, primeRow, primeCol;
353351 int nOfElements = nOfRows*nOfColumns;
354352 /* generate temporary copy of starMatrix */
355353 for (n=0 ; n<nOfElements; n++)
356354 {
357- newStarMatrix [n] = starMatrix[n];
355+ newstarMatrix [n] = starMatrix[n];
358356 }
359357 /* star current zero */
360- newStarMatrix [row + nOfRows*col] = true ;
358+ newstarMatrix [row + nOfRows*col] = true ;
361359 /* find starred zero in current column */
362360 starCol = col;
363361 for (starRow=0 ; starRow<nOfRows; starRow++)
@@ -370,7 +368,7 @@ void AssignmentProblemSolver::step4(int *assignment, double *distMatrix, bool *s
370368 while (starRow<nOfRows)
371369 {
372370 /* unstar the starred zero */
373- newStarMatrix [starRow + nOfRows*starCol] = false ;
371+ newstarMatrix [starRow + nOfRows*starCol] = false ;
374372 /* find primed zero in current row */
375373 primeRow = starRow;
376374 for (primeCol=0 ; primeCol<nOfColumns; primeCol++)
@@ -381,7 +379,7 @@ void AssignmentProblemSolver::step4(int *assignment, double *distMatrix, bool *s
381379 }
382380 }
383381 /* star the primed zero */
384- newStarMatrix [primeRow + nOfRows*primeCol] = true ;
382+ newstarMatrix [primeRow + nOfRows*primeCol] = true ;
385383 /* find starred zero in current column */
386384 starCol = primeCol;
387385 for (starRow=0 ; starRow<nOfRows; starRow++)
@@ -397,20 +395,20 @@ void AssignmentProblemSolver::step4(int *assignment, double *distMatrix, bool *s
397395 for (n=0 ; n<nOfElements; n++)
398396 {
399397 primeMatrix[n] = false ;
400- starMatrix[n] = newStarMatrix [n];
398+ starMatrix[n] = newstarMatrix [n];
401399 }
402400 for (n=0 ; n<nOfRows; n++)
403401 {
404402 coveredRows[n] = false ;
405403 }
406404 /* move to step 2a */
407- step2a (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
405+ step2a (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
408406}
409407
410408// --------------------------------------------------------------------------
411409//
412410// --------------------------------------------------------------------------
413- void AssignmentProblemSolver::step5 (int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
411+ void AssignmentProblemSolver::step5 (int *assignment, double *distMatrix, bool *starMatrix, bool *newstarMatrix , bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim)
414412{
415413 double h, value;
416414 int row, col;
@@ -456,7 +454,7 @@ void AssignmentProblemSolver::step5(int *assignment, double *distMatrix, bool *s
456454 }
457455 }
458456 /* move to step 3 */
459- step3 (assignment, distMatrix, starMatrix, newStarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
457+ step3 (assignment, distMatrix, starMatrix, newstarMatrix , primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim);
460458}
461459
462460
@@ -469,7 +467,7 @@ void AssignmentProblemSolver::assignmentsuboptimal2(int *assignment, double *cos
469467 double value, minValue, *distMatrix;
470468
471469
472- /* make working copy of distance Matrix */
470+ /* make working copy of distance cv:: Matrix */
473471 nOfElements = nOfRows * nOfColumns;
474472 distMatrix = (double *)malloc (nOfElements * sizeof (double ));
475473 for (n=0 ; n<nOfElements; n++)
@@ -532,7 +530,7 @@ void AssignmentProblemSolver::assignmentsuboptimal1(int *assignment, double *cos
532530 double value, minValue, *distMatrix;
533531
534532
535- /* make working copy of distance Matrix */
533+ /* make working copy of distance cv:: Matrix */
536534 nOfElements = nOfRows * nOfColumns;
537535 distMatrix = (double *)malloc (nOfElements * sizeof (double ));
538536 for (n=0 ; n<nOfElements; n++)
@@ -760,34 +758,34 @@ void AssignmentProblemSolver::assignmentsuboptimal1(int *assignment, double *cos
760758// --------------------------------------------------------------------------
761759void main(void)
762760{
763- // Matrix size
761+ // cv:: Matrix size
764762 int N=8; // tracks
765763 int M=9; // detects
766764 // Random numbers generator initialization
767765 srand (time(NULL));
768- // Distance matrix N-th track to M-th detect.
769- vector< vector<double> > Cost(N,vector<double>(M));
770- // Fill matrix with random values
766+ // Distance cv::Matrix N-th track to M-th detect.
767+ std:: vector< std:: vector<double> > Cost(N,std:: vector<double>(M));
768+ // Fill cv::Matrix with random values
771769 for(int i=0; i<N; i++)
772770 {
773771 for(int j=0; j<M; j++)
774772 {
775773 Cost[i][j] = (double)(rand()%1000)/1000.0;
776- std::cout << Cost[i][j] << "\t";
774+ std::std:: cout << Cost[i][j] << "\t";
777775 }
778- std::cout << std::endl;
776+ std::std:: cout << std:: std::endl;
779777 }
780778
781779 AssignmentProblemSolver APS;
782780
783- vector<int> Assignment;
781+ std:: vector<int> Assignment;
784782
785- cout << APS.Solve(Cost,Assignment) << endl;
783+ std:: cout << APS.Solve(Cost,Assignment) << std:: endl;
786784
787785 // Output the result
788786 for(int x=0; x<N; x++)
789787 {
790- std::cout << x << ":" << Assignment[x] << "\t";
788+ std::std:: cout << x << ":" << Assignment[x] << "\t";
791789 }
792790
793791 getchar();
0 commit comments