-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAuctionAlgorithm.h
474 lines (406 loc) · 12.1 KB
/
AuctionAlgorithm.h
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* AuctionAlgorithm.h
*
* Created on: 30.07.2013
* Author: fb
*/
#ifndef AUCTIONALGORITHM_H_
#define AUCTIONALGORITHM_H_
#include <eigen/Eigen/Core>
#define __AUCTION_EPSILON_MULTIPLIER 1e-5
#define __AUCTION_INF 1e8
//#define __AUCTION_OMIT_ZEROS
#define __AUCTION_ZERO 0.
template<typename Scalar = double>
class Auction
{
private:
Auction() = delete;
virtual ~Auction(){}
public:
typedef Eigen::Matrix<Scalar, -1, -1> WeightMatrix;
/**
* solver modes for association problem
*/
enum APSolvingMode
{
MAXIMIZATION = 1, MINIMIZATION = 2
};
/**
* represents an undirected edge between node x and y with weight v
*/
struct Edge
{
public:
Edge() : x(0), y(0), v(0) {}
Edge(const size_t x, const size_t y, const Scalar v) :
x(x), y(y), v(v) {}
size_t x;
size_t y;
Scalar v;
};
/**
* vector of edges
*/
typedef std::vector<Edge> Edges;
/**
* vector of scalars (prices, profits, ...)
*/
typedef std::vector<Scalar> Scalars;
/**
* vector of bools for row/column-locking
*/
typedef std::vector<bool> Locks;
/**
* vector of indices
*/
typedef std::vector<size_t> indices;
static const Edges solve(const Eigen::Matrix<Scalar, -1, -1> & a)
{
const size_t rows = a.rows();
const size_t cols = a.cols();
Locks lockedRows(a.rows(), false);
Locks lockedCols(a.cols(), false);
Edges E;
Scalar lambda = .0;
Scalar epsilon = __AUCTION_EPSILON_MULTIPLIER / a.cols();
// condition 3: initially set p_j >= lambda
Scalars prices(cols, 0.), profits(rows, 1.); // p-Vector (1 to j) = p_j
do
{
// Step 1 (forward auction cycle):
// Execute iterations of the forward auction algorithm until at least one
// more person becomes assigned. If there is an unassigned person left, go
// to step 2; else go to step 3.
while (forward(a, E, prices, profits, lockedRows, lockedCols,
lambda, epsilon))
;
if (!allPersonsAssigned(lockedRows))
{
// Step 2 (reverse auction cycle):
// Execute several iterations of the reverse auction algorithm until at least
// one more object becomes assigned or until we have p_j <= lambda for all
// unassigned objects. If there is an unassigned person left, go to step 1
// else go to step 3
while (!reverse(a, E, prices, profits, lockedRows, lockedCols,
lambda, epsilon)
|| !unassignedObjectsLTlambda(lockedCols, prices,
lambda))
; // reverse auction
}
if (allPersonsAssigned(lockedRows))
{
// Step 3 (reverse auction):
// Execute successive iterations of the reverse auction algorithm until the
// algorithm terminates with p_j <= lambda for all unassigned objects j
while (true)
{
reverse(a, E, prices, profits, lockedRows, lockedCols,
lambda, epsilon);
if (unassignedObjectsLTlambda(lockedCols, prices, lambda))
break;
}
break;
}
} while (true);
return E;
}
private:
/**
* forward cycle of auction algorithm
* @param a weight matrix (nxm)
* @param S assignment matrix (nxm)
* @param prices prices per object (m)
* @param profits profits per person (n)
* @param lambda bidding threshold lambda
* @param epsilon bidding increment
* @return true if assignment was made, false otherwise
*/
static bool forward(const Eigen::Matrix<Scalar, -1, -1> & a, Edges & E,
Scalars & prices, Scalars & profits, Locks & lockedRows,
Locks & lockedCols, Scalar & lambda, Scalar & epsilon)
{
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << "forwarding ..." << std::endl;
#endif
const size_t rows = a.rows();
const size_t cols = a.cols();
bool assignmentFound = false;
for (size_t i = 0; i < rows; i++) // for the i-th row/person
{
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << "examining row " << i << std::endl;
#endif
bool assignmentInThisIterationFound = false;
// person already assigned?
if (lockedRows[i])
continue;
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << "row " << i << " not locked!" << std::endl;
#endif
// find an unassigned person i, its best object j_i
// j_i = argmax {a_ij - p_j} for j in A(i) ( A(i) are the set of edges of the i-th row )
// if a(i,j) = 0. it is not a valid edge
size_t j_i = 0;
// v_i = max { a_ij - p_j} for j in A(i) // maximum profit for person i
// v_i was already found = v_i
// w_i = max { a_ij - p_j} for j in A(i) and j != j_i // second best profit
// if j_i is the only entry in A(i), w_i = - inf // there's no second best profit
Scalar w_i = -__AUCTION_INF, v_i = -__AUCTION_INF, a_i_ji = 0.; // = max { a_ij - p_j}
// find maximum profit i.e. j_i = arg max { a_ij - p_j} and second best
for (size_t j = 0; j < cols; j++) // for the j-th column
{
const Scalar aij = a(i,j);
#ifndef __AUCTION_OMIT_ZEROS
if ( aij == __AUCTION_ZERO ) continue;
#endif
const Scalar diff = aij - prices[j];
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << " col " << j << " diff = " << diff << std::endl;
#endif
if (diff > v_i)
{
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << " diff > v_i !" << std::endl;
#endif
// if there already was an entry found, this is the second best
if (assignmentInThisIterationFound)
w_i = v_i;
v_i = diff;
j_i = j;
a_i_ji = aij;
assignmentInThisIterationFound = true;
}
if (diff > w_i && j_i != j)
w_i = diff;
// if no entry is bigger than v_i, check if there's still a bigger second best entry
}
// no possible assignment found?
if (!assignmentInThisIterationFound)
{
lockedRows[i] = true; // if no assignment found in this row, there is no arc ...
continue;
}
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << "i = " << i << " - j_i = " << j_i << " - v_i = " << v_i
<< " - w_i = " << w_i << " - a_ij = " << a_i_ji << std::endl;
#endif
assignmentInThisIterationFound = false;
// std::cout << "assignment found .." << std::endl;
const Scalar bid = a_i_ji - w_i + epsilon;
// P_i = w_i - E
profits[i] = w_i - epsilon; // set new profit for person
// prices(j_i) = max(lambda, a(i,j_i) - w(i) + epsilon)
// if lambda <= a_ij - w_i + E, add (i, j_i) to S
if (lambda <= bid)
{
prices[j_i] = bid;
// assignment was made, so lock row and col
lockedRows[i] = true;
lockedCols[j_i] = true;
bool newEdge = true;
// if j_i was assigned to different i' to begin, remove (i', j_i) from S
for (auto & e : E)
if (e.y == j_i) // change edge
{
lockedRows[e.x] = false; // unlock row i'
newEdge = false;
e.x = i;
e.v = a_i_ji;
break;
}
if (newEdge)
{
Edge e;
e.x = i;
e.y = j_i;
e.v = a_i_ji;
E.push_back(e);
#ifdef __AUCTION_DEBUG
__A_FORWARD_LOG << "adding edge (" << i << ", " << j_i << ")" << std::endl;
#endif
}
assignmentInThisIterationFound = true;
}
else
{
prices[j_i] = lambda;
assignmentInThisIterationFound = false;
}
if (assignmentInThisIterationFound)
assignmentFound = true;
}
return assignmentFound;
}
/**
* reverse cycle of auction algorithm
* @param a weight matrix (nxm)
* @param S assignment matrix (nxm)
* @param prices prices per object (m)
* @param profits profits per person (n)
* @param lambda bidding threshold lambda
* @param epsilon bidding increment
* @return true if assignment was made, false otherwise
*/
static bool reverse(const Eigen::Matrix<Scalar, -1, -1> & a, Edges & E,
Scalars & prices, Scalars & profits, Locks & lockedRows,
Locks & lockedCols, Scalar & lambda, const Scalar & epsilon)
{
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "reversing ..." << std::endl;
#endif
const size_t rows = a.rows();
const size_t cols = a.cols();
bool assignmentFound = false;
for (size_t j = 0; j < cols; j++) // for the j-th column (objects)
{
bool assignmentInThisIterationFound = false;
// object already assigned, p_j > lambda ?
if (lockedCols[j])
continue;
if (!(prices[j] > lambda))
continue;
// Find an unassigned object j with p_j > lambda, its best person i_j
// i_j = argmax {a_ij - profits[i]) für i aus B(j) (PI !!!)
size_t i_j = 0;
//g_j = max {a_ij - P_i} for i in B(j) and i != i_j
// if j_i is the only entry in B(j), g_j = - inf ( g_j < b_j)
//b_j = max {a_ij - P_i} for i in B(j)
Scalar b_j = -__AUCTION_INF, g_j = -__AUCTION_INF, a_ij_j = 0.;
// find maximum profit i.e. j_i = arg max { a_ij - p_j} and second best
for (size_t i = 0; i < rows; i++) // for the j-th column
{
const Scalar aij = a(i, j);
#ifndef __AUCTION_OMIT_ZEROS
if ( aij == __AUCTION_ZERO ) continue;
#endif
const Scalar diff = aij - profits[i];
if (diff > b_j)
{
// if there already was an entry found, this is the second best
if (assignmentInThisIterationFound)
g_j = b_j;
b_j = diff;
i_j = i;
a_ij_j = aij;
assignmentInThisIterationFound = true;
}
if (diff > g_j && i_j != i)
g_j = diff;
}
// no assignment found
if (!assignmentInThisIterationFound)
{
lockedCols[j] = true;
continue;
}
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "j = " << j << " i_j = " << i_j << " b_j = " << b_j << " g_j = " << g_j
<< " a_ij_j = " << a_ij_j
<< " p_j = " << prices[j] << " P_i = " << profits[i_j]<< std::endl;
#endif
assignmentInThisIterationFound = false;
//if b_j >= L + E, case 1:
if (b_j >= (lambda + epsilon))
{
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "b_j >= lambda + epsilon" << std::endl;
#endif
const Scalar diff = g_j - epsilon; // G_j - E
const Scalar max = lambda > diff ? lambda : diff; // max { L, G_j - E}
// p_j = max { L, G_j - E}
prices[j] = max;
// P_i_j = a_i_jj - max {L, G_j - E}
profits[i_j] = a_ij_j - max;
lockedRows[i_j] = true;
lockedCols[j] = true;
bool newEdge = true;
// if j_i was assigned to different i' to begin, remove (i', j_i) from S
for (auto & e : E)
if (e.x == i_j) // change edge
{
lockedCols[e.y] = false; // unlock row i'
newEdge = false;
e.y = j;
e.v = a_ij_j;
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "edges: " << E.size()
<< " changing edge ";
#endif
break;
}
if (newEdge)
{
Edge e;
e.x = i_j;
e.y = j;
e.v = a_ij_j;
E.push_back(e);
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "added edge " << E.size() << " ";
#endif
}
assignmentInThisIterationFound = true;
}
else // if B_j < L + E, case 2
{
// p_j = B_j - E
prices[j] = b_j - epsilon;
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "b_j < lambda + epsilon " << std::endl;
#endif
/** standard lambda scaling **/
size_t lowerThanLambda = 0;
Scalar newLambda = lambda;
// if the number of objectes k with p_k < lambda is bigger than (rows - cols)
for (size_t k = 0; k < cols; k++)
{
if (prices[k] < lambda) // p_k < lambda
{
lowerThanLambda++;
if (prices[k] < newLambda)
newLambda = prices[k];
}
}
// set new lambda
#ifdef __AUCTION_DEBUG
__A_REVERSE_LOG << "changing lambda from " << lambda << " to " << newLambda << std::endl;
#endif
if (lowerThanLambda >= (cols - rows))
lambda = newLambda;
assignmentInThisIterationFound = false;
}
if (assignmentInThisIterationFound)
assignmentFound = true;
}
return assignmentFound;
}
/**
* returns true if p_j <= lambda for all unassigned objects.
*
* @param c locked columns
* @param prices prices of objects
* @param lambda bidding threshold
* @return true if all prices of unassigned objects are below lambda, otherwise false
*/
static const bool unassignedObjectsLTlambda(const Locks & c,
const Scalars & prices, const Scalar lambda)
{
for (size_t j = 0; j < c.size(); ++j)
if (!c[j] && prices[j] > lambda)
return false;
return true;
}
/**
* check if all persons are assigned
* @return true if all persons are assigned, otherwise false
*/
static const bool allPersonsAssigned(const Locks & r)
{
for (size_t i = 0; i < r.size(); ++i)
if (!r[i])
return false;
return true;
}
};
#endif /* AUCTIONALGORITHM_H_ */