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