test.cpp
Go to the documentation of this file.
00001 /********************************************************************
00002  ********************************************************************
00003  ** C++ class implementation of the Hungarian algorithm by David Schwarz, 2012
00004  **
00005  **
00006  ** O(n^3) implementation derived from libhungarian by Cyrill Stachniss, 2004
00007  **
00008  **
00009  ** Solving the Minimum Assignment Problem using the 
00010  ** Hungarian Method.
00011  **
00012  ** ** This file may be freely copied and distributed! **
00013  **
00014  **
00015  ** This file is distributed in the hope that it will be useful,
00016  ** but WITHOUT ANY WARRANTY; without even the implied 
00017  ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018  ** PURPOSE.  
00019  **
00020  ********************************************************************
00021  ********************************************************************/
00022 
00023 #include "hungarian.h"
00024 
00025 std::vector< std::vector<int> > array_to_matrix(int* m, int rows, int cols) {
00026   int i,j;
00027   std::vector< std::vector<int> > r;
00028   r.resize(rows, std::vector<int>(cols, 0));
00029 
00030   for(i=0;i<rows;i++)
00031   {
00032     for(j=0;j<cols;j++)
00033       r[i][j] = m[i*cols+j];
00034   }
00035   return r;
00036 }
00037 
00038 
00039 int main() {
00040 
00041   /* an example cost matrix */
00042   int r[3*3] =  {20,90,10,60,30,40,90,90, 120};
00043   std::vector< std::vector<int> > m = array_to_matrix(r,3,3);
00044 
00045   /* initialize the gungarian_problem using the cost matrix*/
00046   Hungarian hungarian(m , 3,3, HUNGARIAN_MODE_MINIMIZE_COST) ;
00047 
00048   //fprintf(stderr, "assignement matrix has a now a size %d rows and %d columns.\n\n",  hungarian.ro,matrix_size);
00049 
00050   /* some output */
00051   fprintf(stderr, "cost-matrix:");
00052   hungarian.print_cost();
00053 
00054   /* solve the assignement problem */
00055   hungarian.solve();
00056 
00057   /* some output */
00058   fprintf(stderr, "assignment:");
00059   hungarian.print_assignment();
00060 
00061 
00062   return 0;
00063 }
00064 


explorer
Author(s): Daniel Neuhold , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:53