-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabuSearchSolver.cpp
327 lines (248 loc) · 10.4 KB
/
TabuSearchSolver.cpp
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
/**
* @file TSPSolver.cpp
* @brief TSP solver (neighborhood search)
*/
#include "TabuSearchSolver.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <ctime>
#include <sys/time.h>
using namespace std;
std::string TabuSearchSolver::getSolverName() const {
char buffer[50];
sprintf(buffer, "\tTenure: %d, MaxIter: %d", mTabuLength, mMaxIteration);
std::string tmp = std::string(buffer);
if (ACmode) {
if (BestImprovement) {
return "Tabu Search AC - BI " + tmp;
} else {
return "Tabu Search AC - FI " + tmp;
}
} else {
if (BestImprovement) {
return "Tabu Search - BI " + tmp;
} else {
return "Tabu Search - FI " + tmp;
}
}
}
bool TabuSearchSolver::solve(const TSP& tsp, const TSPSolution& initSol, TSPSolution& bestSol) {
// clear previous use
//mTabuList.clear();
mTabuSet.clear();
mAdvTabuList.clear();
try {
bool stop = false;
int iter = 0;
//tabuList.reserve(tsp.n);
//initTabuList(tsp.n);
TSPSolution currSol(initSol);
double bestValue, currValue;
bestValue = currValue = currSol.evaluateObjectiveFunction(tsp);
TSPMove move;
clock_t currTime = clock();
while (!stop) {
iter++;
// for small instance of problem print the current solution
if (tsp.n < 20) {
currSol.print(std::cout);
std::cout << " (" << iter << ") value " << currValue << "\t(" << bestValue << ")";
}
//mAspiration = bestValue - currValue;
/*double costVar;
if (BestImprovement) {
costVar = findBestNeighbor(tsp, currSol, move);
} else {
costVar = findFirstBestNeighbor(tsp, currSol, move);
}*/
double bestNeighValue = currValue + findBestNeighbor(tsp, currSol, move); // costVar;
if (bestNeighValue >= tsp.infinite) {
cout << "\tmove: NO legal neighbour" << endl;
stop = true;
}
else {
// insertTabu(move);
if (mTabuLength != 0) {
// use only List
/*if (mTabuList.size() < mTabuLength) {
mTabuList.push_back(move);
} else {
mTabuList.pop_front(); // remove old elements
mTabuList.push_back(move);
}*/
// use only List + Set
if (mTabuSet.size() < mTabuLength) {
// get key from TSPMove
char buffer[50];
sprintf(buffer, "%u%u", move.from, move.to);
string moveKey = string(buffer);
mAdvTabuList.push_back(moveKey);
mTabuSet.insert(moveKey);
} else {
// get key from TSPMove
char buffer[50];
sprintf(buffer, "%u%u", move.from, move.to);
string moveKey = string(buffer);
//cout << "\tNew tabu move: " << moveKey << "\t" << mTabuSet.size() << endl;
// remove old key
string oldTabuKey = mAdvTabuList.front();
mAdvTabuList.pop_front();
mTabuSet.erase(oldTabuKey);
// add new key
mAdvTabuList.push_back(moveKey);
mTabuSet.insert(moveKey);
}
}
currSol = swap(currSol,move);
currValue = bestNeighValue;
if (currValue < bestValue - 0.01) { // TS: update incumbent (if better -with tolerance- solution found)
bestValue = currValue;
bestSol = currSol;
cout << " (" << iter << ") value " << currValue
<< "\tmove: " << move.from << " , " << move.to
<< "\tbetter solution" << std::endl;
}
// stopping criteria
/*if (iter > mMaxIteration) {
stop = true;
} else*/ if ((double)(clock() - currTime) / CLOCKS_PER_SEC > mMaxTime) {
stop = true;
}
//std::cout << "\tmove: " << move.from << " , " << move.to;
//std::cout << std::endl;
}
}
bestSol.iterations = iter;
return true;
}
catch(std::exception& e) {
std::cout << ">>>EXCEPTION: " << e.what() << std::endl;
return false;
}
}
TSPSolution& TabuSearchSolver::swap(TSPSolution& tspSol, const TSPMove& move) {
TSPSolution tmpSol(tspSol);
for ( int i = move.from ; i <= move.to ; ++i ) {
tspSol.sequence[i] = tmpSol.sequence[move.to-(i-move.from)];
}
return tspSol;
}
double TabuSearchSolver::findFirstBestNeighbor(const TSP& tsp , const TSPSolution& currSol, TSPMove& move) {
double bestCostVariation = tsp.infinite;
// N.B. intial and final position are fixed (initial/final node remains 0)
// Slice all the current solution vector
for (uint a = 1 ; a < currSol.sequence.size() - 2; a++) {
// prev node
int h = currSol.sequence[a-1];
// choose a starting node
int i = currSol.sequence[a];
for (uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++) {
int j = currSol.sequence[b]; // choose a finishing node
int l = currSol.sequence[b+1]; // prev node
// prof. implementation it discards other moves
/*if ( ( (currIter - tabuList[i] <= tabuLength) &&
(currIter - tabuList[j] <= tabuLength)) ) continue */
double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
+ tsp.cost[h][j] + tsp.cost[i][l];
if (isTabuMove(a, b) && !satisfiedAspirationCriteria(neighCostVariation)) {
// DEBUG
//std::cout << "\t" << "discard move: " << a << ", " << b << "\t";
}
else {
// DEBUG
/*if (isTabuMove(a, b) && satisfiedAspirationCriteria(neighCostVariation)) {
std::cout << "\t" << "in tabu but satisfied AC: " << a << ", " << b << "\t";
}*/
if (neighCostVariation < bestCostVariation) {
bestCostVariation = neighCostVariation;
move.from = a;
move.to = b;
double currentBestValueFound = currSol.evaluateObjectiveFunction(tsp);
// on first improvement exit
if (currentBestValueFound + bestCostVariation < currentBestValueFound) {
return bestCostVariation;
}
//std::cout << "\t" << "better move: " << a << ", " << b << "\t";
}
else {
//std::cout << "\t" << "no better move: " << a << ", " << b << "\t";
}
}
}
}
return bestCostVariation;
}
double TabuSearchSolver::findBestNeighbor( const TSP& tsp , const TSPSolution& currSol, TSPMove& move ) {
// Determine the NON-TABU *move* yielding the best 2-opt neigbor solution
double bestCostVariation = tsp.infinite;
// N.B. intial and final position are fixed (initial/final node remains 0)
// Slice all the current solution vector
for (uint a = 1 ; a < currSol.sequence.size() - 2; a++) {
// prev node
int h = currSol.sequence[a-1];
// choose a starting node
int i = currSol.sequence[a];
for (uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++) {
int j = currSol.sequence[b]; // choose a finishing node
int l = currSol.sequence[b+1]; // prev node
// prof. implementation it discards other moves
/*if ( ( (currIter - tabuList[i] <= tabuLength) &&
(currIter - tabuList[j] <= tabuLength)) ) continue */
double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
+ tsp.cost[h][j] + tsp.cost[i][l];
if (!isTabuMove(a, b) /*|| satisfiedAspirationCriteria(neighCostVariation)*/) {
if (neighCostVariation < bestCostVariation) {
bestCostVariation = neighCostVariation;
move.from = a;
move.to = b;
}
}
/*if (isTabuMove(a, b) && !satisfiedAspirationCriteria(neighCostVariation)) {
// DEBUG
//std::cout << "\t" << "discard move: " << a << ", " << b << "\t";
}
else {
// DEBUG
if (isTabuMove(a, b) && satisfiedAspirationCriteria(neighCostVariation)) {
std::cout << "\t" << "in tabu but satisfied AC: " << a << ", " << b << "\t";
}
if (neighCostVariation < bestCostVariation) {
bestCostVariation = neighCostVariation;
move.from = a;
move.to = b;
//std::cout << "\t" << "better move: " << a << ", " << b << "\t";
}
else {
//std::cout << "\t" << "no better move: " << a << ", " << b << "\t";
}
}*/
}
}
return bestCostVariation;
}
bool TabuSearchSolver::satisfiedAspirationCriteria(double neighbourCostVariation) const {
if (ACmode) {
// aspiration criteria implementation
//std::cout << std::endl << "Aspiration criteria";
return neighbourCostVariation < (mAspiration - 0.01);
} else {
return false; // default implementation
}
}
bool TabuSearchSolver::isTabuMove(int from, int to) {
/*std::list<TSPMove>::const_iterator it = mTabuList.begin();
while (it != mTabuList.end()) {
if ((*it).from == from && (*it).to == to)
return true;
it++;
}*/
char buffer[50];
sprintf(buffer, "%u%u", from, to);
string moveKey = string(buffer);
if (mTabuSet.find(moveKey) != mTabuSet.end()) {
return true;
}
return false;
}