/////////////////////////////////////////////////////////////////////////////// // Hungarian.cpp: Implementation file for Class HungarianAlgorithm. // // This is a C++ wrapper with slight modification of a hungarian algorithm implementation by Markus Buehren. // The original implementation is a few mex-functions for use in MATLAB, found here: // http://www.mathworks.com/matlabcentral/fileexchange/6543-functions-for-the-rectangular-assignment-problem // // Both this code and the orignal code are published under the BSD license. // by Cong Ma, 2016 // #include "Hungarian.h" #include #include HungarianAlgorithm::HungarianAlgorithm(){} HungarianAlgorithm::~HungarianAlgorithm(){} //********************************************************// // A single function wrapper for solving assignment problem. //********************************************************// double HungarianAlgorithm::Solve(vector>& DistMatrix, vector& Assignment) { unsigned int nRows = DistMatrix.size(); unsigned int nCols = DistMatrix[0].size(); double *distMatrixIn = new double[nRows * nCols]; int *assignment = new int[nRows]; double cost = 0.0; // Fill in the distMatrixIn. Mind the index is "i + nRows * j". // Here the cost matrix of size MxN is defined as a double precision array of N*M elements. // In the solving functions matrices are seen to be saved MATLAB-internally in row-order. // (i.e. the matrix [1 2; 3 4] will be stored as a vector [1 3 2 4], NOT [1 2 3 4]). for (unsigned int i = 0; i < nRows; i++) for (unsigned int j = 0; j < nCols; j++) distMatrixIn[i + nRows * j] = DistMatrix[i][j]; // call solving function assignmentoptimal(assignment, &cost, distMatrixIn, nRows, nCols); Assignment.clear(); for (unsigned int r = 0; r < nRows; r++) Assignment.push_back(assignment[r]); delete[] distMatrixIn; delete[] assignment; return cost; } //********************************************************// // Solve optimal solution for assignment problem using Munkres algorithm, also known as Hungarian Algorithm. //********************************************************// void HungarianAlgorithm::assignmentoptimal(int *assignment, double *cost, double *distMatrixIn, int nOfRows, int nOfColumns) { double *distMatrix, *distMatrixTemp, *distMatrixEnd, *columnEnd, value, minValue; bool *coveredColumns, *coveredRows, *starMatrix, *newStarMatrix, *primeMatrix; int nOfElements, minDim, row, col; /* initialization */ *cost = 0; for (row = 0; row nOfColumns) */ { minDim = nOfColumns; for (col = 0; col= 0) *cost += distMatrix[row + nOfRows*col]; } } /********************************************************/ void HungarianAlgorithm::step2a(int *assignment, double *distMatrix, 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