1 #ifndef vnl_hungarian_algorithm_hxx_ 2 #define vnl_hungarian_algorithm_hxx_ 12 # include <vcl_msvc_warnings.h> 37 m_Cost.set_size( m_N, m_N);
38 m_Cost.fill( static_cast<T>(0) );
42 m_Cost.update( cost_in, 0, 0 );
53 typedef std::vector<bool>::iterator iter;
55 for ( iter i =
v.begin(); i != end; ++i )
91 while ( step != STEP_done )
162 m_M.set_size( m_N, m_N);
168 m_R_cov.assign(m_N,
false);
169 m_C_cov.assign(m_N,
false);
184 for (
unsigned i = 0; i < m_N; ++i )
187 for (
unsigned j = 1; j < m_N; ++j )
189 if ( mn > m_Cost(i,j) )
194 for (
unsigned j = 0; j < m_N; ++j )
215 for (
unsigned i = 0; i < m_N; ++i )
219 for (
unsigned j = 0; j < m_N; ++j )
221 if ( m_Cost(i,j) == 0 && ! m_C_cov[j] )
231 clear_vector( m_R_cov );
232 clear_vector( m_C_cov );
247 for (
unsigned j = 0; j < m_N; ++j )
249 for (
unsigned i = 0; i < m_N; ++i )
251 if ( m_M(i,j) == STAR )
282 m_Z0_r = m_Z0_c = (
unsigned int)(-1);
287 for (i = 0 ; i < m_N; ++i )
291 for ( j = 0; j < m_N; ++j )
294 std::cout << m_Cost(i,j) << std::endl;
296 if ( m_Cost(i,j) == 0 && ! m_C_cov[j] )
299 bool star_in_row =
false;
300 for (
unsigned j2 = 0; j2 < m_N; ++j2 )
302 if ( m_M(i,j2) == STAR )
348 std::vector<unsigned> rows, cols;
353 assert( m_M(i,j) == PRIME );
358 for ( i = 0; i < m_N; ++i )
360 if ( m_M(i,j) == STAR )
377 for ( j = 0; j < m_N; ++j )
379 if ( m_M(i,j) == PRIME )
391 for (
unsigned idx = 0; idx < rows.size(); ++idx )
393 unsigned i = rows[idx];
394 unsigned j = cols[idx];
395 if ( m_M(i,j) == STAR )
401 assert( m_M(i,j) == PRIME );
407 for (
unsigned i = 0; i < m_N; ++i )
409 for (
unsigned j = 0; j < m_N; ++j )
411 if ( m_M(i,j) == PRIME )
419 clear_vector( m_R_cov );
420 clear_vector( m_C_cov );
437 for (
unsigned i = 0; i < m_N; ++i )
441 for (
unsigned j = 0; j < m_N; ++j )
443 if ( ! m_C_cov[j] && m_Cost(i,j) < minval )
445 minval = m_Cost(i,j);
452 for (
unsigned i = 0; i < m_N; ++i )
454 for (
unsigned j = 0; j < m_N; ++j )
458 m_Cost(i,j) += minval;
462 m_Cost(i,j) -= minval;
481 std::vector<unsigned> assign( m_Cost_in.rows(), (
unsigned int)(-1) );
482 m_AssignmentVector = assign;
487 for (
unsigned j = 0; j < m_Cost_in.cols(); ++j )
489 for (
unsigned i = 0; i < m_Cost_in.rows(); ++i )
491 if ( m_M(i,j) == STAR )
493 m_AssignmentVector[i] = j;
494 m_TotalCost += m_Cost_in[i][j];
527 return m_AssignmentVector;
530 #undef VNL_HUNGARIAN_ALGORITHM_INSTANTIATE 531 #define VNL_HUNGARIAN_ALGORITHM_INSTANTIATE(T) \ 532 template class VNL_EXPORT vnl_hungarian_algorithm<T > 534 #endif // vnl_hungarian_algorithm_hxx_ unsigned int cols() const
Return the number of columns.
void Step_done()
Step done - Returns a vector containing the result of the assignment.
STEP_TYPE Step_4()
Step 4: Find a noncovered zero and prime it.
An ordinary mathematical matrix.
AssignmentVectorType GetAssignmentVector()
Returns the assignment vector.
T GetTotalCost()
Returns the total cost of the assignment.
STEP_TYPE Step_1()
Step 1 - For each row of the matrix, find the smallest element and subtract it from every element in ...
STEP_TYPE Step_6()
Step 6 -.
STEP_TYPE Step_5()
Step 5 - Construct a series of alternating primed and starred zeros as follows.
STEP_TYPE Step_3()
Step 3 - Cover each column containing a starred zero.
An ordinary mathematical matrix.
STEP_TYPE Step_0()
Step 0 - Make sure there are at least as many rows as columns in the cost matrix.
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
void StartAssignment()
Starts the assignment.
AssignmentMatrixType GetAssignmentMatrix()
Returns the assignment matrix.
std::vector< unsigned > AssignmentVectorType
void SetCostMatrix(vnl_matrix< T > const &)
Starts the assignment.
unsigned int rows() const
Return the number of rows.
STEP_TYPE Step_2()
Step 2 - Find a zero (Z) in the resulting matrix.
void clear_vector(std::vector< bool > &)
Sets all the values in the input bool vector to false.