int hungarian_init(hungarian_problem_t* p, int** cost_matrix, int rows, int cols, int mode) { int i,j, org_cols, org_rows; int max_cost; max_cost = 0; org_cols = cols; org_rows = rows; // is the number of cols not equal to number of rows ? // if yes, expand with 0-cols / 0-cols rows = hungarian_imax(cols, rows); cols = rows; p->num_rows = rows; p->num_cols = cols; p->cost = (int**)calloc(rows,sizeof(int*)); hungarian_test_alloc(p->cost); p->assignment = (int**)calloc(rows,sizeof(int*)); hungarian_test_alloc(p->assignment); for(i=0; i<p->num_rows; i++) { p->cost[i] = (int*)calloc(cols,sizeof(int)); hungarian_test_alloc(p->cost[i]); p->assignment[i] = (int*)calloc(cols,sizeof(int)); hungarian_test_alloc(p->assignment[i]); for(j=0; j<p->num_cols; j++) { p->cost[i][j] = (i < org_rows && j < org_cols) ? cost_matrix[i][j] : 0; p->assignment[i][j] = 0; if (max_cost < p->cost[i][j]) max_cost = p->cost[i][j]; } } if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) { for(i=0; i<p->num_rows; i++) { for(j=0; j<p->num_cols; j++) { p->cost[i][j] = max_cost - p->cost[i][j]; } } } else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) { // nothing to do } else fprintf(stderr,"%s: unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST !\n", __FUNCTION__); return rows; }
int hungarian_init(hungarian_problem_t* p, int** cost_matrix, int rows, int cols, int mode) { int i,j, org_cols, org_rows; int max_cost; max_cost = 0; org_cols = cols; org_rows = rows; /* is the number of cols not equal to number of rows ? if yes, expand with 0-cols / 0-cols */ rows = hungarian_imax(cols, rows); cols = rows; p->num_rows = rows; p->num_cols = cols; p->cost = (double**)calloc(rows,sizeof(double*)); p->assignment = (int**)calloc(rows,sizeof(int*)); p->col_inc = (int*)calloc(cols,sizeof(int)); for(i=0; i<p->num_rows; i++) { p->cost[i] = (double*)calloc(cols,sizeof(double)); p->assignment[i] = (int*)calloc(cols,sizeof(int)); for(j=0; j<p->num_cols; j++) { p->cost[i][j] = (i < org_rows && j < org_cols) ? cost_matrix[i][j] : 0; p->assignment[i][j] = 0; if (max_cost < p->cost[i][j]) max_cost = p->cost[i][j]; } } if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) { for(i=0; i<p->num_rows; i++) { for(j=0; j<p->num_cols; j++) { p->cost[i][j] = max_cost - p->cost[i][j]; } } } else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) { /* nothing to do */ } else //mexPrintf("%s: unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST !\n"); return rows; }