-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfbb.cpp
More file actions
221 lines (207 loc) · 8.06 KB
/
dfbb.cpp
File metadata and controls
221 lines (207 loc) · 8.06 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
#include <algorithm>
#include "dfbb.h"
extern unique_ptr<BRPPolicy> ubSolver;
extern bool verbose;
DFBB::DFBB(unsigned int UB, unsigned int timeLimit) {
UB_ = UB;
timeLimit_ = timeLimit;
}
shared_ptr<BRPState> DFBB::solve(const BRPState &initialState) const {
clock_t startTicks = 0;
if (timeLimit_ > 0) {
startTicks = clock();
}
//
BRPState currentState(initialState);
clock_t before = clock();
shared_ptr<BRPState> bestFound = ubSolver->solve(initialState);
cout << "Calculated UB in " << ((double)clock() - before) / CLOCKS_PER_SEC
<< " seconds" << endl;
unsigned int bestObj = min(UB_, bestFound->nRelocations());
//
cout << "Starting builtin depth-first branch-and-bound with LB = "
<< currentState.LB()
<< " and UB = " << bestObj << endl;
bool finished = solveSub(currentState, -1, bestFound, bestObj, startTicks);
if (! finished) {
cout << "DFBB: Time limit reached!" << endl;
}
return bestFound;
}
// returns false if time limit reached, true otherwise
// side effect: bestFound and bestObj are updated if a new better solution
// is found
bool DFBB::solveSub(BRPState ¤tState,
unsigned int lastRelocatedTo,
shared_ptr<BRPState> &bestFound,
unsigned int &bestObj,
const clock_t &startTicks) const {
// cout << "\t from subroutine, timeLimit_ = " << timeLimit_
// << endl;
// step 0: do we still have time?
if ( timeLimit_ > 0 &&
((double) clock() - startTicks) / CLOCKS_PER_SEC > timeLimit_){
return false;
}
// step 1: perform all possible retrievals
int nRetrievals = 0;
while (currentState.retrieveNext()) {
nRetrievals += 1;
lastRelocatedTo = -1;
}
// are we done?
// if yes: did we find a new best solution?
if (currentState.empty()) {
if (currentState.nRelocations() < bestObj) {
bestObj = currentState.nRelocations();
bestFound = make_shared<BRPState>(currentState);
}
} else if (currentState.nRelocations() + currentState.LB() >= bestObj) {
// if ( currentState.nRelocations() + currentState.LB2() < bestObj) {
// cout << currentState << endl;
// cout << "LB1 = " << currentState.LB1() << endl;
// cout << "LB2 = " << currentState.LB2() << endl;
// cout << "LB3 = " << currentState.LB3() << endl;
// }
;
} else { // step 2: branch and evaluate subtrees
// // special case: do we have depth+LB = UB - 1?
// // if yes: compute UB at this node
// if ( currentState.nRelocations() + currentState.LB() == bestObj - 1 ) {
// shared_ptr<BRPState> ns = ubSolver->solve(currentState);
// if ( ns->nRelocations() < bestObj ) {
// cout << "tralala" << endl;
// bestObj = ns->nRelocations();
// bestFound = ns;
// }
// }
// each possible relocation is a branch
// a branch is a <LB, from, to> tuple
vector<tuple<unsigned int, unsigned int, int> > branches;
int currentLB = currentState.LB1();
for (unsigned int sFrom=0; sFrom < currentState.W(); sFrom++) {
// only relocate from stacks with at least one item and which
// are not the last stack we relocated to
if ( sFrom != lastRelocatedTo &&
currentState.height(sFrom) > 0 ) {
// item being relocated
unsigned int item = currentState.top(sFrom);
// look-ahead part 1: difference on LB induced by relocating
// item from sFrom
int fromDiff = 0;
if (currentState.low(sFrom) < item) {
fromDiff = -1;
}
bool relocatedToEmpty = false;
// now try every destination stack
for (unsigned int sTo=0; sTo < currentState.W(); sTo++) {
if (sTo != sFrom && // do not relocate to same stack
currentState.height(sTo) < currentState.H()) {
// only relocate to an empty stack once to
// break symmetry
if (relocatedToEmpty &&
currentState.height(sTo) == 0) {
continue;
}
// look-ahead part 2: difference on LB induced by
// relocating item to sTo
int toDiff = 0;
if (currentState.low(sTo) < item) {
toDiff = 1;
}
int newBound = currentState.nRelocations() + 1 +
currentLB + fromDiff + toDiff;
// would that move be promising?
if (newBound < bestObj) {
branches.push_back(make_tuple(newBound,
sFrom, sTo));
}
}
}
}
}
// now that all branches are computed, sort them from most to least
// promising and try them all
sort(branches.begin(), branches.end());
bool keepGoing = true;
for (auto branch: branches) {
// 2.1: branch
currentState.relocate(get<1>(branch), get<2>(branch));
// 2.2: evaluate subtree
keepGoing = solveSub(currentState,
get<2>(branch),
bestFound,
bestObj,
startTicks);
// 2.3: cancel branching decision
currentState.undoLastMove();
// 2.4: it time limit has been reached, stop everything
if (! keepGoing) {
return false;
}
}
}
// step 3: cancel retrievals
while (nRetrievals > 0) {
currentState.undoLastMove();
nRetrievals -= 1;
}
return true;
}
shared_ptr<BRPState> DFBBLoop::solve(const BRPState &initialState) const {
clock_t startTicks = 0;
if (timeLimit_ > 0) {
startTicks = clock();
}
if ( verbose ) {
cout << "Starting DFBB-Loop with time limit = " << timeLimit_ << endl;
}
//
BRPState currentState(initialState);
shared_ptr<BRPState> bestFound = ubSolver->solve(initialState);
unsigned int UB = min(UB_, bestFound->nRelocations());
//
unsigned int LB = currentState.LB3();
if ( verbose ) {
cout << "Starting builtin DFBB-loop with LB = "
<< currentState.LB()
<< " and UB = " << UB << endl;
}
cout << "Starting builtin depth-first branch-and-bound with LB = "
<< currentState.LB()
<< " and UB = " << UB << endl;
if (LB == UB) {
if ( verbose ) {
cout << "Done at root node!!" << endl;
}
return bestFound;
} else {
unsigned int UBcur = LB;
while (UBcur < UB) {
unsigned int bestObj = UBcur + 1;
BRPState tmpState(initialState);
if ( verbose ) {
cout << "*** Trying with UB = " << bestObj << endl;
cout << "\t calling subroutine, timeLimit_ = " << timeLimit_
<< endl;
}
bool finished = solveSub(tmpState, -1, bestFound, bestObj,
startTicks);
if (! finished) {
cerr << "DFBB-Loop: Time limit reached!" << endl;
return bestFound;
}
if ( verbose ) {
cout << "\tcurrent best found: " << bestFound->nRelocations()
<< endl;
}
UB = min(UB, bestFound->nRelocations());
UBcur += 1;
}
return bestFound;
}
}
DFBBLoop::DFBBLoop(unsigned int UB, unsigned int timeLimit) {
UB_ = UB;
timeLimit_ = timeLimit;
}