-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsspDense.c
404 lines (392 loc) · 10.3 KB
/
sspDense.c
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
/**
Michael Motro github.com/motrom/fastmurty 4/2/19
*/
#ifndef SPARSE
#include "sspDense.h"
WorkvarsforSSP allocateWorkvarsforSSP(int m, int n) {
WorkvarsforSSP workvars;
int totalsize = (sizeof(double) + sizeof(int)) * n;
workvars.distances = (double *) malloc(totalsize);
workvars.pathback = (int *)(workvars.distances + n);
workvars.n = n;
return workvars;
};
void deallocateWorkvarsforSSP(WorkvarsforSSP workvars) {
free(workvars.distances);
};
double SSP(double* c, Subproblem* prb, WorkvarsforSSP* workvars){
//const double inf = 1000000000;
double C = 0;
int cj, ri, i, i1, j, j1, minj, minmissi, mincol, rowidx, m, n;
double minval, val, minmissval, ui;
Solution sol = prb->solution;
#ifndef NDEBUG
const int loopescape = 10000;
int loopcounter = 0;
#endif
// reset y, v
for(cj=0; cj<prb->n; cj++){
j = prb->cols2use[cj];
sol.y[j] = -1;
sol.v[j] = 0;
}
// basic column reduction - basically running some rows in a convenient order
m = prb->m;
for(ri=prb->m-1; ri>=0; ri--){
i = prb->rows2use[ri];
rowidx = i * workvars->n;
minval = 0;
minj = -1;
for(cj=0; cj<prb->n; cj++){
j = prb->cols2use[cj];
val = c[rowidx+j] - sol.v[j];
if(val < minval){
minval = val;
minj = j;
}
}
if((minj==-1) || (sol.y[minj]==-1)){
// this row can be matched without conflicting previous matches
sol.x[i] = minj;
if (minj != -1) {
sol.y[minj] = i;
C += minval;
}
m--;
prb->rows2use[ri] = prb->rows2use[m];
prb->rows2use[m] = i;
}
}
for(ri=0; ri<m; ri++){
i1 = prb->rows2use[ri];
// shortest paths algorithm
n = prb->n;
rowidx = i1 * workvars->n;
for(cj=0; cj<n; cj++){
j = prb->cols2use[cj];
workvars->distances[j] = c[rowidx+j] - sol.v[j];
workvars->pathback[j] = i1;
}
minmissi = i1;
minmissval = 0;
for (;;) {
assert(loopcounter++ < loopescape);
minval = minmissval;
minj = -1;
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
val = workvars->distances[j];
if (val < minval) {
minj = j;
mincol = cj;
minval = val;
}
}
j = minj;
if (j == -1) break;
i = sol.y[j];
if (i == -1) break;
// this column should no longer be considered
sol.v[j] += minval;
n -= 1;
prb->cols2use[mincol] = prb->cols2use[n];
prb->cols2use[n] = j;
// update distances to other columns
rowidx = i * workvars->n;
ui = c[rowidx + j] - sol.v[j];
if (-ui < minmissval) {
minmissi = i;
minmissval = -ui;
}
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
val = c[rowidx + j] - sol.v[j] - ui;
if (val < workvars->distances[j]) {
workvars->distances[j] = val;
workvars->pathback[j] = i;
}
}
}
// travel back through shortest path
if (j == -1) {
i = minmissi;
j = sol.x[i];
sol.x[i] = -1;
}
while (i != i1) {
assert(loopcounter++ < loopescape);
i = workvars->pathback[j];
sol.y[j] = i;
j1 = j;
j = sol.x[i];
sol.x[i] = j1;
}
// update reductions
for (cj = n; cj < prb->n; cj++) {
sol.v[prb->cols2use[cj]] -= minval;
}
// update total cost
C += minval;
}
#ifndef NDEBUG
double eps_debug = 0.0000001;
for(ri = 0; ri < prb->m; ri++){
i = prb->rows2use[ri];
rowidx = i * workvars->n;
j = sol.x[i];
if (j == -1){
// check for positive slack on miss row
for(cj = 0; cj < prb->n; cj++){
j1 = prb->cols2use[cj];
assert(sol.y[j1] != i);
assert(c[rowidx + j1] - sol.v[j1] > -eps_debug);
}
} else {
assert(sol.y[j] == i);
ui = c[rowidx + j] - sol.v[j];
assert(ui < eps_debug);
// check for positive slack
for (cj = 0; cj < prb->n; cj++) {
j1 = prb->cols2use[cj];
assert(c[rowidx + j1] - sol.v[j1] - ui > -eps_debug);
}
}
}
for (cj = 0; cj < prb->n; cj++) {
j = prb->cols2use[cj];
assert(sol.v[j] < eps_debug);
if(sol.y[j] == -1){
assert(sol.v[j] > -eps_debug);
}
}
#endif
return C;
};
double spStep(double* c, Subproblem* prb, WorkvarsforSSP* workvars, double cost_bound) {
const double inf = 1000000000;
int cj, ri, i, i1, j, j1, minj, minmissi, minmissj, mincol, rowidx, n;
double minval, val, minmissval, ui, missing_cost;
bool missing, miss_unused, missing_from_row;
Solution sol = prb->solution;
#ifndef NDEBUG
const int loopescape = 10000;
int loopcounter = 0;
#endif
// which row and column are to be rematched
i1 = prb->rows2use[prb->m-1];
j1 = sol.x[i1];
rowidx = i1 * workvars->n;
// u not necessary to get solution, but gives accurate cost change
ui = 0;
if (j1 != -1) {
ui = c[rowidx + j1] - sol.v[j1];
}
n = prb->n;
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
if(prb->eliminateels[j]){
workvars->distances[j] = inf;
} else {
workvars->pathback[j] = i1;
workvars->distances[j] = c[rowidx + j] - sol.v[j] - ui;
}
}
minmissj = -1;
minmissi = i1;
if (prb->eliminatemiss) minmissval = inf; else minmissval = -ui;
miss_unused = true;
missing_from_row = false;
missing_cost = 0; // this is a dual cost on auxiliary columns
for (;;) {
assert(loopcounter++ < loopescape);
minval = minmissval;
minj = -2;
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
val = workvars->distances[j];
if (val < minval) {
minj = j;
minval = val;
mincol = cj;
}
}
if (minval > cost_bound) return inf; // early stopping
j = minj;
if (j == j1) {
break;
}
if (j == -2) {
if (!miss_unused) {
//if you got here again, costs must be really high
return inf;
}
// entry to missing zone : row was matched but is now missing
missing = true;
missing_from_row = true;
}
else {
i = sol.y[j];
// this column should no longer be considered
n -= 1;
prb->cols2use[mincol] = prb->cols2use[n];
prb->cols2use[n] = j;
if (i == -1) {
// entry to missing zone : col was missing but is now matched
if (miss_unused) {
minmissj = j;
missing = true;
missing_from_row = false;
}
else {
// already covered the missing zone, this is a dead end
continue;
}
} else {
missing = false;
}
}
if (missing) {
if (j1 == -1) {
j = -1;
break;
}
miss_unused = false;
missing_cost = minval;
minmissval = inf;
ui = -minval;
// exit from missing zone : row that was missing is matched
for (ri = 0; ri < prb->m; ri++) {
i = prb->rows2use[ri];
if (sol.x[i] == -1) {
rowidx = i * workvars->n;
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
val = c[rowidx + j] - sol.v[j] - ui;
if (val < workvars->distances[j]) {
workvars->distances[j] = val;
workvars->pathback[j] = i;
}
}
}
}
// exit from missing zone : col that was matched is missing
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
if (sol.y[j] != -1) {
val = -sol.v[j] - ui;
if (val < workvars->distances[j]) {
workvars->distances[j] = val;
workvars->pathback[j] = -1;
}
}
}
}
else {
rowidx = i * workvars->n;
ui = c[rowidx + j] - sol.v[j] - minval;
if (miss_unused & (-ui < minmissval)) {
minmissi = i;
minmissval = -ui;
}
for(cj=0; cj<n; cj++){
j = prb->cols2use[cj];
val = c[rowidx + j] - sol.v[j] - ui;
if (val < workvars->distances[j]) {
workvars->distances[j] = val;
workvars->pathback[j] = i;
}
}
}
}
// augment
// travel back through shortest path to find matches
i = i1 + 1; // any number that isn't i1
while (i != i1) {
assert(loopcounter++ < loopescape);
if (j == -1) {
// exit from missing zone : row was missing but is now matched
i = -1;
}
else {
i = workvars->pathback[j];
sol.y[j] = i;
}
if (i == -1) {
// exit from missing zone : column j was matched but is now missing
if (missing_from_row) {
// entry to missing zone : row was matched but is now missing
i = minmissi;
j = sol.x[i];
sol.x[i] = -1;
} else {
// entry to missing zone : col was missing but is now matched
j = minmissj;
}
} else {
j1 = j;
j = sol.x[i];
sol.x[i] = j1;
}
}
// updating of column reductions
if (miss_unused) {
missing_cost = minval;
}
for (cj = 0; cj < n; cj++) {
j = prb->cols2use[cj];
if (sol.y[j] == -1){
sol.v[j] = 0;
} else {
sol.v[j] = sol.v[j] + minval - missing_cost;
}
}
for (cj = n; cj < prb->n; cj++) {
j = prb->cols2use[cj];
if (sol.y[j] == -1) {
sol.v[j] = 0;
}
else {
sol.v[j] = sol.v[j] + workvars->distances[j] - missing_cost;
}
}
#ifndef NDEBUG
double eps_debug = 0.0000001;
for(ri = 0; ri < prb->m; ri++){
i = prb->rows2use[ri];
rowidx = i * workvars->n;
j = sol.x[i];
if (j == -1){
// check for positive slack on miss row
for(cj = 0; cj < prb->n; cj++){
j1 = prb->cols2use[cj];
assert(sol.y[j1] != i);
if (!(prb->eliminateels[j1] & (i==i1))){
assert(c[rowidx + j1] - sol.v[j1] > -eps_debug);
}
}
} else {
assert(sol.y[j] == i);
ui = c[rowidx + j] - sol.v[j];
if (!(prb->eliminatemiss & (i==i1))){
assert(ui < eps_debug);
}
// check for positive slack
for (cj = 0; cj < prb->n; cj++) {
j1 = prb->cols2use[cj];
if (!(prb->eliminateels[j1] & (i==i1))){
assert(c[rowidx + j1] - sol.v[j1] - ui > -eps_debug);
}
}
}
}
for (cj = 0; cj < prb->n; cj++) {
j = prb->cols2use[cj];
assert(sol.v[j] < eps_debug);
if(sol.y[j] == -1){
assert(sol.v[j] > -eps_debug);
}
}
#endif
return minval;
};
#endif