hungarian.c 11.3 KB
Newer Older
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
/**
 * @file
 * @brief   Solving the Minimum Assignment Problem using the Hungarian Method.
 */
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

35
#include "util.h"
36
37
#include "xmalloc.h"
#include "debug.h"
38
#include "error.h"
39
#include "hungarian.h"
40
#include "raw_bitset.h"
41

42
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
43

44
struct hungarian_problem_t {
45
46
47
48
49
50
51
52
53
	unsigned      num_rows;      /**< number of rows */
	unsigned      num_cols;      /**< number of columns */
	unsigned     *cost;          /**< the cost matrix */
	unsigned      max_cost;      /**< the maximal costs in the matrix */
	match_type_t  match_type;    /**< PERFECT or NORMAL matching */
	unsigned     *missing_left;  /**< bitset: left side nodes having no edge to
	                                  the right side */
	unsigned     *missing_right; /**< bitset: right side nodes having no edge to
	                              the left side */
54
55
};

56
57
static void hungarian_dump_f(FILE *f, const unsigned *cost,
                             unsigned num_rows, unsigned num_cols, int width)
58
{
59
	unsigned r, c;
60
61

	fprintf(f , "\n");
62
	for (r = 0; r < num_rows; r++) {
63
		fprintf(f, " [");
64
65
		for (c = 0; c < num_cols; c++) {
			fprintf(f, "%*u", width, cost[r*num_cols + c]);
66
67
68
69
70
71
		}
		fprintf(f, "]\n");
	}
	fprintf(f, "\n");
}

72
73
void hungarian_print_cost_matrix(hungarian_problem_t *p, int width)
{
Matthias Braun's avatar
Matthias Braun committed
74
	hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, width);
75
76
}

77
hungarian_problem_t *hungarian_new(unsigned num_rows, unsigned num_cols,
78
                                   match_type_t match_type)
79
{
80
	hungarian_problem_t *p = XMALLOCZ(hungarian_problem_t);
81

82
	FIRM_DBG_REGISTER(dbg, "firm.hungarian");
83
84
85
86
87

	/*
		Is the number of cols  not equal to number of rows ?
		If yes, expand with 0 - cols / 0 - cols
	*/
88
89
	num_rows = MAX(num_cols, num_rows);
	num_cols = num_rows;
90

91
92
	p->num_rows   = num_rows;
	p->num_cols   = num_cols;
93
94
95
96
97
98
99
100
	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) {
101
102
103
104
		p->missing_left  = rbitset_malloc(num_rows);
		p->missing_right = rbitset_malloc(num_cols);
		rbitset_set_all(p->missing_left,  num_rows);
		rbitset_set_all(p->missing_right, num_cols);
105
	}
106
107

	/* allocate space for cost matrix */
108
	p->cost = XMALLOCNZ(unsigned, num_rows * num_cols);
109
110
111
	return p;
}

112
113
void hungarian_prepare_cost_matrix(hungarian_problem_t *p,
                                   hungarian_mode_t mode)
114
{
115
	if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
116
117
118
119
		unsigned  r, c;
		unsigned  num_cols = p->num_cols;
		unsigned *cost     = p->cost;
		unsigned  max_cost = p->max_cost;
120
121
		for (r = 0; r < p->num_rows; r++) {
			for (c = 0; c < p->num_cols; c++) {
122
				cost[r*num_cols + c] = max_cost - cost[r*num_cols + c];
123
124
			}
		}
125
	} else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) {
126
		/* nothing to do */
127
	} else {
128
		panic("Unknown hungarian problem mode");
129
130
131
	}
}

132
void hungarian_add(hungarian_problem_t *p, unsigned left, unsigned right,
133
                   unsigned cost)
134
{
135
136
137
	assert(p->num_rows > left  && "Invalid row selected.");
	assert(p->num_cols > right && "Invalid column selected.");

138
139
	p->cost[left*p->num_cols + right] = cost;
	p->max_cost                       = MAX(p->max_cost, cost);
140
141

	if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
142
143
		rbitset_clear(p->missing_left, left);
		rbitset_clear(p->missing_right, right);
144
	}
145
146
}

147
void hungarian_remove(hungarian_problem_t *p, unsigned left, unsigned right)
148
{
149
150
151
	assert(p->num_rows > left  && "Invalid row selected.");
	assert(p->num_cols > right && "Invalid column selected.");

152
	p->cost[left*p->num_cols + right] = 0;
153
154

	if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
155
156
		rbitset_set(p->missing_left, left);
		rbitset_set(p->missing_right, right);
157
	}
158
159
}

160
161
void hungarian_free(hungarian_problem_t* p)
{
162
163
164
165
	free(p->missing_left);
	free(p->missing_right);
	free(p->cost);
	free(p);
166
167
}

168
int hungarian_solve(hungarian_problem_t* p, unsigned *assignment,
169
                    unsigned *final_cost, unsigned cost_threshold)
170
{
171
	int       result       = 0;
172
173
174
175
176
177
178
179
180
181
182
183
	unsigned  res_cost     = 0;
	unsigned  num_rows     = p->num_rows;
	unsigned  num_cols     = p->num_cols;
	unsigned *cost         = p->cost;
	unsigned *col_mate     = XMALLOCNZ(unsigned, num_rows);
	unsigned *row_mate     = XMALLOCNZ(unsigned, num_cols);
	unsigned *parent_row   = XMALLOCNZ(unsigned, num_cols);
	unsigned *unchosen_row = XMALLOCNZ(unsigned, num_rows);
	int      *row_dec      = XMALLOCNZ(int, num_rows);
	int      *col_inc      = XMALLOCNZ(int, num_cols);
	int      *slack        = XMALLOCNZ(int, num_cols);
	unsigned *slack_row    = XMALLOCNZ(unsigned, num_rows);
184
185
186
187
188
189
	unsigned  r;
	unsigned  c;
	unsigned  t;
	unsigned  unmatched;

	memset(assignment, -1, num_rows * sizeof(assignment[0]));
190
191

	/* Begin subtract column minima in order to start with lots of zeros 12 */
192
	DBG((dbg, LEVEL_1, "Using heuristic\n"));
193

194
	for (c = 0; c < num_cols; ++c) {
195
		unsigned col_mininum = cost[0*num_cols + c];
196

197
		for (r = 1; r < num_rows; ++r) {
198
199
			if (cost[r*num_cols + c] < col_mininum)
				col_mininum = cost[r*num_cols + c];
200
201
		}

202
		if (col_mininum == 0)
203
			continue;
204

205
		res_cost += col_mininum;
206
		for (r = 0; r < num_rows; ++r)
207
			cost[r*num_cols + c] -= col_mininum;
208
209
210
211
	}
	/* End subtract column minima in order to start with lots of zeros 12 */

	/* Begin initial state 16 */
212
	unmatched = 0;
213
214
215
216
217
	for (c = 0; c < num_cols; ++c) {
		row_mate[c]   = (unsigned) -1;
		parent_row[c] = (unsigned) -1;
		col_inc[c]    = 0;
		slack[c]      = INT_MAX;
218
219
	}

220
	for (r = 0; r < num_rows; ++r) {
221
		unsigned row_minimum = cost[r*num_cols + 0];
222

223
		for (c = 1; c < num_cols; ++c) {
224
225
			if (cost[r*num_cols + c] < row_minimum)
				row_minimum = cost[r*num_cols + c];
226
227
		}

228
		row_dec[r] = row_minimum;
229

230
		for (c = 0; c < num_cols; ++c) {
231
232
233
234
235
236
237
238
239
			if (cost[r*num_cols + c] != row_minimum)
				continue;
			if (row_mate[c] != (unsigned)-1)
				continue;

			col_mate[r] = c;
			row_mate[c] = r;
			DBG((dbg, LEVEL_1, "matching col %u == row %u\n", c, r));
			goto row_done;
240
241
		}

242
		col_mate[r] = (unsigned)-1;
243
244
		DBG((dbg, LEVEL_1, "node %u: unmatched row %u\n", unmatched, r));
		unchosen_row[unmatched++] = r;
245
246
247
248
249
row_done: ;
	}
	/* End initial state 16 */

	/* Begin Hungarian algorithm 18 */
250
	if (unmatched == 0)
251
252
		goto done;

253
	t = unmatched;
254
	for (;;) {
255
256
		unsigned q = 0;
		unsigned j;
257
		DBG((dbg, LEVEL_1, "Matched %u rows.\n", num_rows - t));
258

259
		for (;;) {
260
			int s;
261
262
			while (q < t) {
				/* Begin explore node q of the forest 19 */
263
264
				r = unchosen_row[q];
				s = row_dec[r];
265

266
267
				for (c = 0; c < num_cols; ++c) {
					if (slack[c]) {
268
						int del = cost[r*num_cols + c] - s + col_inc[c];
269

270
						if (del < slack[c]) {
271
							if (del == 0) {
272
								if (row_mate[c] == (unsigned)-1)
273
274
									goto breakthru;

275
276
								slack[c]      = 0;
								parent_row[c] = r;
277
								DBG((dbg, LEVEL_1, "node %u: row %u == col %u -- row %u\n", t, row_mate[c], c, r));
278
279
280
281
								unchosen_row[t++] = row_mate[c];
							} else {
								slack[c]     = del;
								slack_row[c] = r;
282
283
284
285
286
287
288
289
290
							}
						}
					}
				}
				/* End explore node q of the forest 19 */
				q++;
			}

			/* Begin introduce a new zero into the matrix 21 */
291
292
293
294
			s = INT_MAX;
			for (c = 0; c < num_cols; ++c) {
				if (slack[c] && slack[c] < s)
					s = slack[c];
295
296
297
298
299
			}

			for (q = 0; q < t; ++q)
				row_dec[unchosen_row[q]] += s;

300
301
302
303
			for (c = 0; c < num_cols; ++c) {
				if (slack[c]) {
					slack[c] -= s;
					if (slack[c] == 0) {
304
						/* Begin look at a new zero 22 */
305
						r = slack_row[c];
306
						DBG((dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%u, %u]\n", s, r, c));
307
308
						if (row_mate[c] == (unsigned)-1) {
							for (j = c + 1; j < num_cols; ++j) {
309
310
311
312
								if (slack[j] == 0)
									col_inc[j] += s;
							}
							goto breakthru;
313
314
						} else {
							parent_row[c] = r;
315
							DBG((dbg, LEVEL_1, "node %u: row %u == col %u -- row %u\n", t, row_mate[c], c, r));
316
							unchosen_row[t++] = row_mate[c];
317
318
319
						}
						/* End look at a new zero 22 */
					}
320
321
				} else {
					col_inc[c] += s;
322
323
324
325
326
327
				}
			}
			/* End introduce a new zero into the matrix 21 */
		}
breakthru:
		/* Begin update the matching 20 */
328
		DBG((dbg, LEVEL_1, "Breakthrough at node %u of %u.\n", q, t));
329
		for (;;) {
330
331
332
			j           = col_mate[r];
			col_mate[r] = c;
			row_mate[c] = r;
333

334
			DBG((dbg, LEVEL_1, "rematching col %u == row %u\n", c, r));
335
			if (j == (unsigned)-1)
336
337
				break;

338
339
			r = parent_row[j];
			c = j;
340
341
342
343
344
345
346
347
		}
		/* End update the matching 20 */

		if (--unmatched == 0)
			goto done;

		/* Begin get ready for another stage 17 */
		t = 0;
348
		for (c = 0; c < num_cols; ++c) {
349
			parent_row[c] = (unsigned) -1;
350
			slack[c]      = INT_MAX;
351
352
		}

353
354
		for (r = 0; r < num_rows; ++r) {
			if (col_mate[r] == (unsigned)-1) {
355
				DBG((dbg, LEVEL_1, "node %u: unmatched row %u\n", t, r));
356
				unchosen_row[t++] = r;
357
358
359
360
361
362
363
			}
		}
		/* End get ready for another stage 17 */
	}
done:

	/* Begin double check the solution 23 */
364
365
	for (r = 0; r < num_rows; ++r) {
		for (c = 0; c < num_cols; ++c) {
366
367
368
369
			if ((int) cost[r*num_cols + c] < row_dec[r] - col_inc[c]) {
				result = -1;
				goto ret;
			}
370
371
372
		}
	}

373
374
	for (r = 0; r < num_rows; ++r) {
		c = col_mate[r];
375
		if (c == (unsigned)-1
376
377
378
379
		    || cost[r*num_cols + c] != (unsigned) (row_dec[r] - col_inc[c])) {
		    result = -2;
		    goto ret;
		}
380
381
	}

382
383
384
	for (r = c = 0; c < num_cols; ++c) {
		if (col_inc[c])
			r++;
385
386
	}

387
388
389
390
	if (r > num_rows) {
		result = -3;
		goto ret;
	}
391
392
393
394
395
	/* End double check the solution 23 */

	/* End Hungarian algorithm 18 */

	/* collect the assigned values */
396
	for (r = 0; r < num_rows; ++r) {
397
398
		if (cost_threshold > 0
		    && cost[r*num_cols + col_mate[r]] >= cost_threshold)
399
			assignment[r] = -1; /* remove matching having cost > threshold */
Christian Würdig's avatar
Christian Würdig committed
400
		else
401
			assignment[r] = col_mate[r];
402
403
	}

404
405
	/* In case of normal matching: remove impossible ones */
	if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
406
		for (r = 0; r < num_rows; ++r) {
407
408
			if (rbitset_is_set(p->missing_left, r)
			        || rbitset_is_set(p->missing_right, col_mate[r]))
409
				assignment[r] = -1;
410
411
412
		}
	}

413
414
	for (r = 0; r < num_rows; ++r) {
		for (c = 0; c < num_cols; ++c) {
415
			cost[r*num_cols + c] = cost[r*num_cols + c] - row_dec[r] + col_inc[c];
416
417
418
		}
	}

419
	for (r = 0; r < num_rows; ++r)
420
		res_cost += row_dec[r];
421

422
	for (c = 0; c < num_cols; ++c)
423
		res_cost -= col_inc[c];
424

425
	DBG((dbg, LEVEL_1, "Cost is %d\n", res_cost));
426

427
ret:
428
	if (final_cost != NULL)
429
		*final_cost = res_cost;
430

431
432
433
434
435
436
437
438
439
440
	free(col_mate);
	free(row_mate);
	free(parent_row);
	free(unchosen_row);
	free(row_dec);
	free(col_inc);
	free(slack);
	free(slack_row);

	return result;
441
}