hungarian.c 10.5 KB
 Christian Würdig committed Sep 13, 2006 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ``````/******************************************************************** ******************************************************************** ** ** libhungarian by Cyrill Stachniss, 2004 ** ** Added and adapted to libFirm by Christian Wuerdig, 2006 ** ** Solving the Minimum Assignment Problem using the ** Hungarian Method. ** ** ** This file may be freely copied and distributed! ** ** ** Parts of the used code was originally provided by the ** "Stanford GraphGase", but I made changes to this code. ** As asked by the copyright node of the "Stanford GraphGase", ** I hereby proclaim that this file are *NOT* part of the ** "Stanford GraphGase" distrubition! ** ** This file is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ** PURPOSE. ** ******************************************************************** ********************************************************************/ `````` 27 28 29 30 31 ``````/** * @file * @brief Solving the Minimum Assignment Problem using the Hungarian Method. * @version \$Id\$ */ `````` Matthias Braun committed Oct 11, 2008 32 ``````#include "config.h" `````` Matthias Braun committed Dec 10, 2006 33 `````` `````` Christian Würdig committed Sep 13, 2006 34 35 36 37 38 39 40 41 ``````#include #include #include #include "irtools.h" #include "xmalloc.h" #include "debug.h" #include "obst.h" `````` Christian Würdig committed Sep 13, 2006 42 ``````#include "bitset.h" `````` Christian Würdig committed Sep 13, 2006 43 44 45 46 47 48 `````` #include "hungarian.h" #define INF (0x7FFFFFFF) struct _hungarian_problem_t { `````` Christian Würdig committed Sep 13, 2006 49 50 51 52 53 54 55 `````` int num_rows; /**< number of rows */ int num_cols; /**< number of columns */ int **cost; /**< the cost matrix */ int max_cost; /**< the maximal costs in the matrix */ int match_type; /**< PERFECT or NORMAL matching */ bitset_t *missing_left; /**< left side nodes having no edge to the right side */ bitset_t *missing_right; /**< right side nodes having no edge to the left side */ `````` Christian Würdig committed Sep 13, 2006 56 57 58 59 `````` struct obstack obst; DEBUG_ONLY(firm_dbg_module_t *dbg); }; `````` Christoph Mallon committed Feb 13, 2010 60 61 ``````static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) { `````` Christian Würdig committed Sep 13, 2006 62 63 64 65 66 67 68 69 70 71 72 73 74 `````` int i, j; fprintf(f , "\n"); for (i = 0; i < rows; i++) { fprintf(f, " ["); for (j = 0; j < cols; j++) { fprintf(f, "%*d", width, C[i][j]); } fprintf(f, "]\n"); } fprintf(f, "\n"); } `````` Christoph Mallon committed Feb 13, 2010 75 76 ``````void hungarian_print_cost_matrix(hungarian_problem_t *p, int width) { `````` Matthias Braun committed Mar 02, 2009 77 `````` hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, width); `````` Christian Würdig committed Sep 13, 2006 78 79 80 81 82 ``````} /** * Create the object and allocate memory for the data structures. */ `````` Christoph Mallon committed Feb 13, 2010 83 84 ``````hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) { `````` Christian Würdig committed Sep 13, 2006 85 `````` int i; `````` 86 `````` hungarian_problem_t *p = XMALLOCZ(hungarian_problem_t); `````` Christian Würdig committed Sep 13, 2006 87 88 89 90 91 92 93 94 95 96 97 98 `````` FIRM_DBG_REGISTER(p->dbg, "firm.hungarian"); /* Is the number of cols not equal to number of rows ? If yes, expand with 0 - cols / 0 - cols */ rows = MAX(cols, rows); cols = rows; obstack_init(&p->obst); `````` Christian Würdig committed Sep 13, 2006 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 `````` p->num_rows = rows; p->num_cols = cols; p->match_type = match_type; /* In case of normal matching, we have to keep track of nodes without edges to kill them in the assignment later. */ if (match_type == HUNGARIAN_MATCH_NORMAL) { p->missing_left = bitset_obstack_alloc(&p->obst, rows); p->missing_right = bitset_obstack_alloc(&p->obst, cols); bitset_set_all(p->missing_left); bitset_set_all(p->missing_right); } `````` Christian Würdig committed Sep 13, 2006 114 115 `````` /* allocate space for cost matrix */ `````` Christoph Mallon committed Sep 02, 2009 116 `````` p->cost = OALLOCNZ(&p->obst, int*, rows); `````` Christian Würdig committed Sep 13, 2006 117 `````` for (i = 0; i < p->num_rows; i++) `````` Christoph Mallon committed Sep 02, 2009 118 `````` p->cost[i] = OALLOCNZ(&p->obst, int, cols); `````` Christian Würdig committed Sep 13, 2006 119 120 121 122 123 124 125 `````` return p; } /** * Prepare the cost matrix. */ `````` Christoph Mallon committed Feb 13, 2010 126 127 ``````void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) { `````` Christian Würdig committed Sep 13, 2006 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 `````` int 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] = p->max_cost - p->cost[i][j]; } } } else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) { /* nothing to do */ } else fprintf(stderr, "Unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST.\n"); } /** * Set cost[left][right] to cost. */ `````` Christoph Mallon committed Feb 13, 2010 147 148 ``````void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) { `````` Christian Würdig committed Sep 13, 2006 149 150 `````` assert(p->num_rows > left && "Invalid row selected."); assert(p->num_cols > right && "Invalid column selected."); `````` Matthias Braun committed Jan 29, 2007 151 `````` assert(cost >= 0); `````` Christian Würdig committed Sep 13, 2006 152 153 154 `````` p->cost[left][right] = cost; p->max_cost = MAX(p->max_cost, cost); `````` Christian Würdig committed Sep 13, 2006 155 156 157 158 159 `````` if (p->match_type == HUNGARIAN_MATCH_NORMAL) { bitset_clear(p->missing_left, left); bitset_clear(p->missing_right, right); } `````` Christian Würdig committed Sep 13, 2006 160 161 162 163 164 ``````} /** * Set cost[left][right] to 0. */ `````` Christoph Mallon committed Feb 13, 2010 165 166 ``````void hungarian_remv(hungarian_problem_t *p, int left, int right) { `````` Christian Würdig committed Sep 13, 2006 167 168 169 170 `````` assert(p->num_rows > left && "Invalid row selected."); assert(p->num_cols > right && "Invalid column selected."); p->cost[left][right] = 0; `````` Christian Würdig committed Sep 13, 2006 171 172 173 174 175 `````` if (p->match_type == HUNGARIAN_MATCH_NORMAL) { bitset_set(p->missing_left, left); bitset_set(p->missing_right, right); } `````` Christian Würdig committed Sep 13, 2006 176 177 178 179 180 ``````} /** * Frees all allocated memory. */ `````` Christoph Mallon committed Feb 13, 2010 181 182 ``````void hungarian_free(hungarian_problem_t* p) { `````` Christian Würdig committed Sep 13, 2006 183 184 185 186 187 188 189 `````` obstack_free(&p->obst, NULL); xfree(p); } /** * Do the assignment. */ `````` Christoph Mallon committed Feb 13, 2010 190 191 ``````int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) { `````` Christian Würdig committed Sep 13, 2006 192 193 194 195 196 197 198 199 200 201 202 203 204 205 `````` int i, j, m, n, k, l, s, t, q, unmatched, cost; int *col_mate; int *row_mate; int *parent_row; int *unchosen_row; int *row_dec; int *col_inc; int *slack; int *slack_row; cost = 0; m = p->num_rows; n = p->num_cols; `````` 206 207 208 209 `````` col_mate = XMALLOCNZ(int, p->num_rows); unchosen_row = XMALLOCNZ(int, p->num_rows); row_dec = XMALLOCNZ(int, p->num_rows); slack_row = XMALLOCNZ(int, p->num_rows); `````` Christian Würdig committed Sep 13, 2006 210 `````` `````` 211 212 213 214 `````` row_mate = XMALLOCNZ(int, p->num_cols); parent_row = XMALLOCNZ(int, p->num_cols); col_inc = XMALLOCNZ(int, p->num_cols); slack = XMALLOCNZ(int, p->num_cols); `````` Christian Würdig committed Sep 13, 2006 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 `````` memset(assignment, -1, m * sizeof(assignment[0])); /* Begin subtract column minima in order to start with lots of zeros 12 */ DBG((p->dbg, LEVEL_1, "Using heuristic\n")); for (l = 0; l < n; ++l) { s = p->cost[0][l]; for (k = 1; k < m; ++k) { if (p->cost[k][l] < s) s = p->cost[k][l]; } cost += s; if (s != 0) { for (k = 0; k < m; ++k) p->cost[k][l] -= s; } } /* End subtract column minima in order to start with lots of zeros 12 */ /* Begin initial state 16 */ t = 0; for (l = 0; l < n; ++l) { row_mate[l] = -1; parent_row[l] = -1; col_inc[l] = 0; slack[l] = INF; } for (k = 0; k < m; ++k) { s = p->cost[k][0]; for (l = 1; l < n; ++l) { if (p->cost[k][l] < s) s = p->cost[k][l]; } row_dec[k] = s; for (l = 0; l < n; ++l) { if (s == p->cost[k][l] && row_mate[l] < 0) { col_mate[k] = l; row_mate[l] = k; DBG((p->dbg, LEVEL_1, "matching col %d == row %d\n", l, k)); goto row_done; } } col_mate[k] = -1; DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k)); unchosen_row[t++] = k; row_done: ; } /* End initial state 16 */ /* Begin Hungarian algorithm 18 */ if (t == 0) goto done; unmatched = t; while (1) { DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t)); q = 0; while (1) { while (q < t) { /* Begin explore node q of the forest 19 */ k = unchosen_row[q]; s = row_dec[k]; for (l = 0; l < n; ++l) { if (slack[l]) { int del = p->cost[k][l] - s + col_inc[l]; if (del < slack[l]) { if (del == 0) { if (row_mate[l] < 0) goto breakthru; slack[l] = 0; parent_row[l] = k; DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k)); unchosen_row[t++] = row_mate[l]; } else { slack[l] = del; slack_row[l] = k; } } } } /* End explore node q of the forest 19 */ q++; } /* Begin introduce a new zero into the matrix 21 */ s = INF; for (l = 0; l < n; ++l) { if (slack[l] && slack[l] < s) s = slack[l]; } for (q = 0; q < t; ++q) row_dec[unchosen_row[q]] += s; for (l = 0; l < n; ++l) { if (slack[l]) { slack[l] -= s; if (slack[l] == 0) { /* Begin look at a new zero 22 */ k = slack_row[l]; DBG((p->dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%d, %d]\n", s, k, l)); if (row_mate[l] < 0) { for (j = l + 1; j < n; ++j) { if (slack[j] == 0) col_inc[j] += s; } goto breakthru; } else { parent_row[l] = k; DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k)); unchosen_row[t++] = row_mate[l]; } /* End look at a new zero 22 */ } } else { col_inc[l] += s; } } /* End introduce a new zero into the matrix 21 */ } breakthru: /* Begin update the matching 20 */ DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t)); while (1) { j = col_mate[k]; col_mate[k] = l; row_mate[l] = k; DBG((p->dbg, LEVEL_1, "rematching col %d == row %d\n", l, k)); if (j < 0) break; k = parent_row[j]; l = j; } /* End update the matching 20 */ if (--unmatched == 0) goto done; /* Begin get ready for another stage 17 */ t = 0; for (l = 0; l < n; ++l) { parent_row[l] = -1; slack[l] = INF; } for (k = 0; k < m; ++k) { if (col_mate[k] < 0) { DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k)); unchosen_row[t++] = k; } } /* End get ready for another stage 17 */ } done: /* Begin double check the solution 23 */ for (k = 0; k < m; ++k) { for (l = 0; l < n; ++l) { if (p->cost[k][l] < row_dec[k] - col_inc[l]) return -1; } } for (k = 0; k < m; ++k) { l = col_mate[k]; if (l < 0 || p->cost[k][l] != row_dec[k] - col_inc[l]) return -2; } for (k = l = 0; l < n; ++l) { if (col_inc[l]) k++; } if (k > m) return -3; /* End double check the solution 23 */ /* End Hungarian algorithm 18 */ /* collect the assigned values */ for (i = 0; i < m; ++i) { `````` Christian Würdig committed Sep 25, 2006 415 416 417 418 `````` if (cost_threshold > 0 && p->cost[i][col_mate[i]] >= cost_threshold) assignment[i] = -1; /* remove matching having cost > threshold */ else assignment[i] = col_mate[i]; `````` Christian Würdig committed Sep 13, 2006 419 420 `````` } `````` Christian Würdig committed Sep 13, 2006 421 422 423 424 425 426 427 428 `````` /* In case of normal matching: remove impossible ones */ if (p->match_type == HUNGARIAN_MATCH_NORMAL) { for (i = 0; i < m; ++i) { if (bitset_is_set(p->missing_left, i) || bitset_is_set(p->missing_right, col_mate[i])) assignment[i] = -1; } } `````` Christian Würdig committed Sep 13, 2006 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 `````` for (k = 0; k < m; ++k) { for (l = 0; l < n; ++l) { p->cost[k][l] = p->cost[k][l] - row_dec[k] + col_inc[l]; } } for (i = 0; i < m; ++i) cost += row_dec[i]; for (i = 0; i < n; ++i) cost -= col_inc[i]; DBG((p->dbg, LEVEL_1, "Cost is %d\n", cost)); xfree(slack); xfree(col_inc); xfree(parent_row); xfree(row_mate); xfree(slack_row); xfree(row_dec); xfree(unchosen_row); xfree(col_mate); `````` Matthias Braun committed Sep 05, 2009 452 453 `````` if (final_cost != NULL) *final_cost = cost; `````` 454 455 `````` return 0; `````` Christian Würdig committed Sep 13, 2006 456 ``}``