-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglah.cpp
More file actions
525 lines (482 loc) · 19.6 KB
/
glah.cpp
File metadata and controls
525 lines (482 loc) · 19.6 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <algorithm>
#include <cmath>
#include "glah.h"
extern bool verbose;
// used as UB
shared_ptr<BRPState> JZW::solve(const BRPState &initialState) const {
BRPState state(initialState);
autoRetrieve(state);
while (! state.empty()) {
int cStar = state.next();
unsigned int sStar = state.stackForItem(cStar);
while (state.top(sStar) != cStar) {
int c = state.top(sStar);
// cout << state << endl;
// cout << "s* = " << sStar << endl;
// cout << "c* = " << cStar << endl;
// cout << "c = " << c << endl;
// look for stacks that can support c
int sPrime = -1;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) < state.H() &&
state.low(s) >= c ) {
if ( sPrime == -1 ||
state.low(s) < state.low(sPrime) ) {
sPrime = s;
}
}
}
// case where S1 is not empty
if (sPrime > -1) {
// cout << "S1 not empty!" << endl;
gapUtilize(state, sStar, sPrime);
// cout << "\trelocating from " << sStar
// << " to " << sPrime << endl;
state.relocate(sStar, sPrime);
} else {
// we look for an assistant stack which we call 'sa'
int sa = -1;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) >= 1 &&
state.top(s) <= state.f(s) &&
c <= state.f(s) ) {
for (int t=0; t < state.W(); t++) {
if ( t != sStar &&
t != s &&
state.height(t) < state.H() &&
state.low(t) >= state.top(s) ) {
if ( sPrime == -1 ||
state.top(s) > state.top(sPrime) ||
( state.top(s) >= state.top(sPrime) &&
state.low(t) < state.low(sa) ) ) {
sPrime = s;
sa = t;
}
}
}
}
} // case where S2 is not empty
if (sPrime > -1) {
// cout << "S2 not empty!" << endl;
int cPrime = state.top(sPrime);
gapUtilize(state, sPrime, sa);
// cout << "\trelocating from " << sPrime
// << " to " << sa << endl;
state.relocate(sPrime, sa);
gapUtilize(state, sStar, sPrime);
state.relocate(sStar, sPrime);
// cout << "\trelocating from " << sStar
// << " to " << sPrime << endl;
} else { // general case: we have to consider FT-BB relocations
// cout << "S1 and S2 both empty!" << endl;
int smallestAbove = state.smallestAbove(cStar);
vector<pair<int, int> > minAndStack;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) < state.H() ) {
minAndStack.push_back(make_pair(state.low(s), s));
}
}
sort(minAndStack.begin(), minAndStack.end());
// we want the stack with the largest min
sPrime = minAndStack.rbegin()->second;
// special case as in Jin et al.
if ( state.height(sPrime) == state.H() - 1 &&
c != smallestAbove ) {
sPrime = (minAndStack.rbegin() + 1)->second;
}
state.relocate(sStar, sPrime);
// cout << "\trelocating from " << sStar
// << " to " << sPrime << endl;
}
}
}
autoRetrieve(state);
}
return make_shared<BRPState>(state);
}
// returns the next relocation
pair<int, int> JZW::solveOnlyOne(const BRPState &initialState) const {
BRPState state(initialState);
autoRetrieve(state);
int cStar = state.next();
unsigned int sStar = state.stackForItem(cStar);
int c = state.top(sStar);
// look for stacks that can support c
int sPrime = -1;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) < state.H() &&
state.low(s) >= c ) {
if ( sPrime == -1 ||
state.low(s) < state.low(sPrime) ) {
sPrime = s;
}
}
}
// case where S1 is not empty
if (sPrime > -1) {
auto nextReloc = gapUtilizeOnlyOne(state, sStar, sPrime);
if ( nextReloc.first == -1 ) {
// case where gapUtilize would not perform any relocation
return make_pair(sStar, sPrime);
} else {
return nextReloc;
}
} else {
// we look for an assistant stack which we call 'sa'
int sa = -1;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) >= 1 &&
state.top(s) <= state.f(s) &&
c <= state.f(s) ) {
for (int t=0; t < state.W(); t++) {
if ( t != sStar &&
t != s &&
state.height(t) < state.H() &&
state.low(t) >= state.top(s) ) {
if ( sPrime == -1 ||
state.top(s) > state.top(sPrime) ||
( state.top(s) >= state.top(sPrime) &&
state.low(t) < state.low(sa) ) ) {
sPrime = s;
sa = t;
}
}
}
}
} // case where S2 is not empty
if (sPrime > -1) {
int cPrime = state.top(sPrime);
auto nextReloc = gapUtilizeOnlyOne(state, sPrime, sa);
if ( nextReloc.first == -1 ) {
return make_pair(sPrime, sa);
} else {
return nextReloc;
}
} else { // general case: we have to consider FT-BB relocations
// cout << "S1 and S2 both empty!" << endl;
int smallestAbove = state.smallestAbove(cStar);
vector<pair<int, int> > minAndStack;
for (int s=0; s < state.W(); s++) {
if ( s != sStar &&
state.height(s) < state.H() ) {
minAndStack.push_back(make_pair(state.low(s), s));
}
}
sort(minAndStack.begin(), minAndStack.end());
// we want the stack with the largest min
sPrime = minAndStack.rbegin()->second;
// special case as in Jin et al.
if ( state.height(sPrime) == state.H() - 1 &&
c != smallestAbove ) {
sPrime = (minAndStack.rbegin() + 1)->second;
}
return make_pair(sStar, sPrime);
}
}
}
// returns the next relocation that would be performed by gapUtilize,
// or <-1, -1> if it would not perform any relocation
pair<int, int> JZW::gapUtilizeOnlyOne(BRPState &state,
unsigned int s1,
unsigned int s2) const {
while ( state.height(s2) <= state.H() - 2 ) {
int s3 = -1;
for (int s=0; s < state.W(); s++) {
if ( s != s1 && s != s2 && state.height(s) > 1 &&
state.top(s) > state.f(s) &&
state.top(s1) <= state.top(s) &&
state.top(s) <= state.low(s2) ) {
if ( s3 == -1 || state.top(s) >= state.top(s3) ) {
s3 = s;
}
}
}
if ( s3 == -1 ) {
return make_pair(-1, -1);
} else {
return make_pair(s3, s2);
}
}
return make_pair(-1, -1);
}
GLAH::GLAH(unsigned int level) {
D_ = level;
// following the Jin et al. article
nFTBG_ = 5;
nNFBG_ = 5;
nFTBB_ = 3;
nNFBB_ = 3;
nGG_ = 1;
nGB_ = 1;
}
// as in the article
void JZW::gapUtilize(BRPState &state,
unsigned int s1, unsigned int s2) const {
// cout << "\tIn Gap-utilize with s1 = " << s1 << " and s2 = " << s2 << endl;
while ( state.height(s2) <= state.H() - 2 ) {
int s3 = -1;
for (int s=0; s < state.W(); s++) {
if ( s != s1 && s != s2 && state.height(s) > 1 &&
state.top(s) > state.f(s) &&
state.top(s1) <= state.top(s) &&
state.top(s) <= state.low(s2) ) {
if ( s3 == -1 || state.top(s) >= state.top(s3) ) {
s3 = s;
}
}
}
if ( s3 == -1 ) {
return;//break;
} else {
state.relocate(s3, s2);
// cout << "\t\trelocating from " << s3 << " to " << s2 << endl;
}
}
}
// wrapped by solve()
shared_ptr<BRPState> GLAH::greedy(const BRPState &initialState) const {
auto solBest = ubSolver_.solve(initialState);
// cout << "Initialised solBest, nRelocations = "
// << solBest->nRelocations() << endl;
// incumbent
BRPState Lcurr(initialState);
autoRetrieve(Lcurr);
while (! Lcurr.empty() ) {
if ( Lcurr.nRelocations() + Lcurr.LB() >= solBest->nRelocations() ) {
break;
}
// if (verbose) {
// cout << endl << endl << "++++++++++++++++++++++++++++++++++++++++++++" << endl;
// cout << "Lcurr.nRelocations() + Lcurr.LB() = "
// << Lcurr.nRelocations() + Lcurr.LB() << endl;
// cout << "solBest->nRelocations() = " << solBest->nRelocations() << endl;
// }
tuple<int, int, int> relo = lookAheadAdvice(Lcurr, solBest);
// cout << endl << "Look-ahead advice: " << get<0>(relo) << " --> " << get<1>(relo)
// << endl;
// no advice!
if ( get<0>(relo) == -1 ) {
break;
}
Lcurr.relocate( get<0>(relo), get<1>(relo) );
// exit(0);
// cout << "Relocations: " << endl;
// Lcurr.displaySolution();
// cout << endl << endl;
autoRetrieve( Lcurr );
}
return solBest;
}
// as in the article
// side-effect: solBest if updated if necessary
// (in the article, it is a global variable)
tuple<int, int, int> GLAH::lookAheadAdvice(const BRPState &state,
shared_ptr<BRPState> &solBest) const {
BRPState L0(state);
return treeSearch(0, L0, solBest);
}
// format: <from, to, cost>
// side-effect: solBest if updated if necessary
// (in the article, it is a global variable)
tuple<int, int, int> GLAH::treeSearch(int d,
const BRPState &Ld,
shared_ptr<BRPState> &solBest) const {
// if (verbose) {
// for (int i=0; i < d; i++) { cout << "\t"; }
// cout << "-*- ";
// }
// termination case 1
if ( Ld.nRelocations() + Ld.LB() >= solBest->nRelocations() ) {
// if (verbose) {
// cout << "Termination 1: unpromising" << endl;
// }
return make_tuple(-1, -1, -1);
} else if ( Ld.empty() || d == D_ ) { // termination case 2
auto solEva = ubSolver_.solve(Ld);
solEva->condenseJin();
if ( solBest->nRelocations() > solEva->nRelocations() ) {
solBest = solEva;
// if (!verbose) {
// cout << "New best solution: "
// << solEva->nRelocations() << " relocations" << endl;
// }
}
// if (verbose) {
// cout << "Termination 2: condensed solution with "
// << solEva->nRelocations() << " relocations" << endl;
// }
return make_tuple(-1, -1, solEva->nRelocations() );
} else { // general case
// if (verbose) {
// cout << "General case: generating subtrees" << endl;
// }
int bestFrom=-1, bestTo=-1, bestCost=pow(Ld.n(), 2);
// generate relocations here
vector<pair<int, int> > reloList = genReloList(Ld);
// now evaluate every child
for (auto relo: reloList) {
// cout << "Looking at relocation: " << relo.first << " --> "
// << relo.second << endl;
BRPState Lnext(Ld);
// if (verbose) {
// for (int i=0; i < d; i++) { cout << "\t"; }
// cout << " " << relo.first << " --> "
// << relo.second << endl;
// }
Lnext.relocate(relo.first, relo.second);
autoRetrieve(Lnext);
auto child = treeSearch( d + 1, Lnext, solBest );
if ( get<2>(child) != -1 && bestCost > get<2>(child) ) {
// if (verbose) {
// for (int i=0; i < d; i++) { cout << "\t"; }
// cout << " new best! old was " << bestCost
// << ", new is " << get<2>(child) << endl;
// }
bestFrom = relo.first;
bestTo = relo.second;
bestCost = get<2>(child);
}
}
return make_tuple(bestFrom, bestTo, bestCost);
}
}
// generate list of relocations for tree search
vector<pair<int, int> > GLAH::genReloList(const BRPState &state) const {
// cout << "Current state:" << endl << state << endl << endl;
// these tuples are of the form < score, sFrom, sTo > where the definition
// of score varies depending on the type of relocation
vector<tuple<int, int, int> >
ftbgRelocs, nfbgRelocs, ftbbRelocs, nfbbRelocs, ggRelocs, gbRelocs;
vector<pair<int, int> > finalRelocs;
// add next relocation of evaluate()
finalRelocs.push_back( ubSolver_.solveOnlyOne(state) );
// sort all Relocations in different lists
for (int s1=0; s1 < state.W(); s1++) {
if ( state.height(s1) == 0 ) {
continue;
}
for (int s2=0; s2 < state.W(); s2++) {
if ( s1 == s2 or state.height(s2) == state.H() ) {
continue;
}
// moving a well-placed item
if ( state.top(s1) == state.low(s1) ) {
// case 1: GG
if ( state.top(s1) <= state.low(s2) ) {
// score multiplied by -1 to keep sorting in ascending order
ggRelocs.push_back( make_tuple( - state.f(s1)
+ state.low(s2),
s1, s2 ) );
} else { // case 2: GB
// score multiplied by -1 to keep sorting in ascending order
gbRelocs.push_back( make_tuple( - state.f(s1)
- state.low(s2),
s1, s2 ) );
}
} else { // moving a badly placed item
// are we moving it to a well-placed location?
if ( state.top(s1) <= state.low(s2) ) { // xx-BG relocations
int score = state.low(s2) - state.top(s1);
if ( state.low(s1) == state.next() ) { // case 3: FT-BG
ftbgRelocs.push_back( make_tuple( score, s1, s2 ) );
} else { // case 4: NF-BG
nfbgRelocs.push_back( make_tuple( score, s1, s2 ) );
}
} else { // xx-BB relocations
int score = state.top(s1) - state.low(s2);
if ( state.low(s1) == state.next() ) { // case 5: FT-BB
ftbbRelocs.push_back( make_tuple( score, s1, s2 ) );
} else { // case 6: NF-BB
nfbbRelocs.push_back( make_tuple( score, s1, s2 ) );
}
}
}
}
}
// used multiple times in the following
int n;
vector<tuple<int, int, int> >::const_iterator it;
// FT-BG relocations
// cout << "Considering " << ftbgRelocs.size() << " FT-BG relocations:"
// << endl;
// for (auto r: ftbgRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(ftbgRelocs.begin(), ftbgRelocs.end());
n = min( ftbgRelocs.size(), nFTBG_ );
for ( it = ftbgRelocs.begin(); it != ftbgRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// NF-BG relocations
// cout << "Considering " << nfbgRelocs.size() << " NF-BG relocations:"
// << endl;
// for (auto r: nfbgRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(nfbgRelocs.begin(), nfbgRelocs.end());
n = min( nfbgRelocs.size(), nNFBG_ );
for ( it = nfbgRelocs.begin(); it != nfbgRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// FT-BB relocations
// cout << "Considering " << ftbbRelocs.size() << " FT-BB relocations:"
// << endl;
// for (auto r: ftbbRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(ftbbRelocs.begin(), ftbbRelocs.end());
n = min( ftbbRelocs.size(), nFTBB_ );
for ( it = ftbbRelocs.begin(); it != ftbbRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// NF-BB relocations
// cout << "Considering " << nfbbRelocs.size() << " NF-BB relocations:"
// << endl;
// for (auto r: nfbbRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(nfbbRelocs.begin(), nfbbRelocs.end());
n = min( nfbbRelocs.size(), nNFBB_ );
for ( it = nfbbRelocs.begin(); it != nfbbRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// GG relocations
// cout << "Considering " << ggRelocs.size() << " GG relocations:"
// << endl;
// for (auto r: ggRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(ggRelocs.begin(), ggRelocs.end());
n = min( ggRelocs.size(), nGG_ );
for ( it = ggRelocs.begin(); it != ggRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// GB relocations
// cout << "Considering " << gbRelocs.size() << " GB relocations:"
// << endl;
// for (auto r: gbRelocs) {
// cout << "\t" << get<0>(r) << "\t" << get<1>(r) << "\t" << get<2>(r)
// << endl;
// }
sort(gbRelocs.begin(), gbRelocs.end());
n = min( gbRelocs.size(), nGB_ );
for ( it = gbRelocs.begin(); it != gbRelocs.begin() + n; it++) {
finalRelocs.push_back( make_pair( get<1>(*it), get<2>(*it) ) );
}
// cout << "Final set of relocations:" << endl;
// for (auto it: finalRelocs) {
// cout << "\t" << it.first << " --> " << it.second << endl;
// }
// exit(8);
return finalRelocs;
}