-
Notifications
You must be signed in to change notification settings - Fork 1
/
SA.cpp
446 lines (335 loc) · 14.5 KB
/
SA.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
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
#include "SA.h"
bool SA::isFirstTime = true;
void SA::solve() {
//for (int i = 0; i < correspondingList.size(); i++) {
// std::cout << i << ": " << correspondingList[i] << std::endl;
//}
if (isFirstTime == true) {
isFirstTime = false;
srand(time(NULL));
}
// We have Ts, Te, numberOfIterations, Alpha
// Ts: The starting temperature
// Te: The ending temperature
// numberOfIterations: The number of iteration in one temperature
// Alpha: The decreasing rate
// Initialize some datas
std::vector<std::vector<int>> SAListOfEachDay; // It contains all the normal and fictive nodes, and (nRoutes - 1) -1 for boundaries of routes.
std::vector<std::vector<int>> bestSA;
std::vector<std::vector<int>> currentSA;
double bestScore = std::numeric_limits<double>::max();
double currentScore = std::numeric_limits<double>::max();
// Since we are doing SA, our algorithm will be running several times because nodes on different day can't swtich with each other.
// Generate initial datas, like initial SA.
int numberOfCorrespondingNode = nNormals + 1;
for (int day = 0; day < nDays; day++) {
SAListOfEachDay.push_back(std::vector<int>());
for (int j = 1; j < nNodes; j++) {
if (required[j][day] == 1) {
SAListOfEachDay[day].push_back(j);
}
}
std::vector<int> tempVectorForBoundary(nRoutes - 1, -1);
SAListOfEachDay[day].insert(SAListOfEachDay[day].end(), tempVectorForBoundary.begin(), tempVectorForBoundary.end());
}
//for (int day = 0; day < nDays; day++) {
// std::cout << "Day " << day << ": ";
// for (int i = 0; i < SAListOfEachDay[day].size(); i++) {
// std::cout << SAListOfEachDay[day][i] << " ";
// }
// std::cout << std::endl;
//}
//
for (double t = Ts; t >= Te; t = t * Alpha) {
for (int iterationNum = 0; iterationNum < numberOfIterations; iterationNum++) {
// Tweak the SA
tweakSolutionRandomly(SAListOfEachDay);
//for (int day = 0; day < nDays; day++) {
// std::cout << "Day " << day << ": ";
// for (int i = 0; i < SAListOfEachDay[day].size(); i++) {
// std::cout << SAListOfEachDay[day][i] << " ";
// }
// std::cout << std::endl;
//}
//if (iterationNum % 1000 == 0) {
// std::cout << iterationNum << " times" << std::endl;
//}
//temporary
//SAListOfEachDay[0] = {6, 7, 1, 20, 8, -1, 14, 19, 3, 9, -1, 11, 16, 13, 15, 10, -1, 18};
//SAListOfEachDay[1] = {6, 1, 8, -1, 19, -1, 11, 13, 10, -1, 12, 17, 2, 18};
//SAListOfEachDay[2] = {6, 7, 20, 8, -1, 14, 3, 9, -1, 16, 13, 15, 10, -1, 12, 17, 4, 5};
//SAListOfEachDay[0] = {6, -1, 1, 5, 3, -1, 4};
//SAListOfEachDay[1] = {4, -1, 2, 1, 5, -1, 7, 6};
//SAListOfEachDay[2] = {6, -1, 1, 3, -1};
//SAListOfEachDay[0] = {6, 14, -1, 8, 18, 15, 17, -1, 7, 19, 5, 20, -1, 16, 3, 11, 13, 10, 2, 12, 4};
//SAListOfEachDay[0] = {2, 18, 12, 8, 4, 16, -1, 6, 15, 7, 14, 5, 11, -1, 17, 9, -1, 13, 20, 1};
//SAListOfEachDay[0] = {6, 17, 9, -1, 11, -1, -1, 15, 8, 5, 13, 7};
//SAListOfEachDay[0] = {6, 16, 5, -1, 2, 11, 4, 9, 13, -1, 12, 1, 8, 19, -1, 7};
//SAListOfEachDay[1] = {6, 16, 20, 5, 14, -1, 2, 11, 9, -1, 12, 1, 8, 15, -1, 7, 10, 18, 17};
//SAListOfEachDay[2] = {6, 16, 20, 14, -1, 2, 11, 3, 4, 9, 13, -1, 12, 19, 15, -1, 10, 17};
simpleArrivalTimeCalculation(SAListOfEachDay);
//double violationScoreOfSimpleOne = getViolationScore(SAListOfEachDay, FACTOR_OF_VIOLATION);
// Adjust the arrival time and departure time to get the minimum violation.
//calculateObjective(SAListOfEachDay);
//double violationScoreOfMine = getViolationScore(SAListOfEachDay, FACTOR_OF_VIOLATION);
//if (violationScoreOfMine > violationScoreOfSimpleOne) {
// std::cout << "Simple: " << violationScoreOfSimpleOne << std::endl;
// std::cout << "Mine: " << violationScoreOfMine << std::endl;
// std::cout << "Oh no" << std::endl;
// exit(1);
//}
//std::cout << "calculateObjective done. " << std::endl;
//CheckConstraintsResult checkConstraintsResult = checkConstraints(SAListOfEachDay);
//std::cout << "checkConstraints done. " << std::endl;
//if (checkConstraintsResult.result == false) {
// printGraph(SAListOfEachDay, GRAPH_LIMIT);
// std::cout << "Violation detected ----------------------------------" << std::endl;
// for (std::string message : checkConstraintsResult.messages) {
// std::cout << message << std::endl;
// }
// std::cout << "-----------------------------------------------------" << std::endl;
// exit(1);
//}
//// Adjust the arrival time and departure time to get the minimum score. (Approximately)
//adjustDepartureTime(SAListOfEachDay);
//// Calculate and subtract the violation score
//int violationScore = calculateViolationScore(SAListOfEachDay, scaleOfViolationScore);
// Do Step 4 to minimize maximum arrival time differences.
// If it's feasible, do Step 5 to find the best local score using 2-opt algorithm.
// This one may cost too much time. In one of the references, there's a way to reduce the time it might take.
double newScore;// = getScore();
// Temporary:
newScore = getViolationScore(SAListOfEachDay, FACTOR_OF_VIOLATION);
if (newScore < 0) {
std::cout << "Error: violation < 0: " << std::endl;
std::cout << "Violation score: " << newScore << std::endl;
exit(1);
}
//CheckConstraintsResult checkConstraintsResult = checkConstraints(SAListOfEachDay);
////std::cout << "checkConstraints done. " << std::endl;
//if (checkConstraintsResult.result == false) {
// printGraph(SAListOfEachDay, GRAPH_LIMIT);
// std::cout << "After calculateObjective" << std::endl;
// std::cout << "Violation detected ----------------------------------" << std::endl;
// for (std::string message : checkConstraintsResult.messages) {
// std::cout << message << std::endl;
// }
// std::cout << "-----------------------------------------------------" << std::endl;
// exit(1);
//}
if (newScore == 0) {
//printGraph(SAListOfEachDay, GRAPH_LIMIT);
//newScore += getObjectiveScore(SAListOfEachDay);
CheckConstraintsResult checkConstraintsResult = checkConstraints(SAListOfEachDay);
//std::cout << "checkConstraints done. " << std::endl;
if (checkConstraintsResult.result == false) {
printGraph(SAListOfEachDay, GRAPH_LIMIT);
std::cout << "After calculateObjective" << std::endl;
std::cout << "Violation detected ----------------------------------" << std::endl;
for (std::string message : checkConstraintsResult.messages) {
std::cout << message << std::endl;
}
std::cout << "-----------------------------------------------------" << std::endl;
exit(1);
}
//printGraph(SAListOfEachDay, GRAPH_LIMIT);
adjustDepartureTime(SAListOfEachDay);
//newScore += getObjectiveScore(SAListOfEachDay);
checkConstraintsResult = checkConstraints(SAListOfEachDay);
//std::cout << "checkConstraints done. " << std::endl;
if (checkConstraintsResult.result == false) {
printGraph(SAListOfEachDay, GRAPH_LIMIT);
std::cout << "After adjustDepartureTime" << std::endl;
std::cout << "Violation detected ----------------------------------" << std::endl;
for (std::string message : checkConstraintsResult.messages) {
std::cout << message << std::endl;
}
std::cout << "-----------------------------------------------------" << std::endl;
exit(1);
}
improveTimeConsistency(SAListOfEachDay);
newScore += getObjectiveScore(SAListOfEachDay);
checkConstraintsResult = checkConstraints(SAListOfEachDay);
//std::cout << "checkConstraints done. " << std::endl;
if (checkConstraintsResult.result == false) {
printGraph(SAListOfEachDay, GRAPH_LIMIT);
std::cout << "After improveTimeConsistency" << std::endl;
std::cout << "Violation detected ----------------------------------" << std::endl;
for (std::string message : checkConstraintsResult.messages) {
std::cout << message << std::endl;
}
std::cout << "-----------------------------------------------------" << std::endl;
exit(1);
}
if (getViolationScore(SAListOfEachDay, FACTOR_OF_VIOLATION)) {
printGraph(SAListOfEachDay, GRAPH_LIMIT);
std::cout << "After ImproveTimeConssitenct" << std::endl;
std::cout << "Violation score is not zero" << std::endl;
exit(1);
}
//bool improved = improveTimeConsistency(SAListOfEachDay);
//if (improved) {
// calculateObjective(SAListOfEachDay);
// adjustDepartureTime(SAListOfEachDay);
// newScore = getObjectiveScore(SAListOfEachDay);
// CheckConstraintsResult checkConstraintsResult = checkConstraints(SAListOfEachDay);
// //std::cout << "checkConstraints done. " << std::endl;
// if (checkConstraintsResult.result == false) {
// printGraph(SAListOfEachDay, GRAPH_LIMIT);
// std::cout << "Violation detected ----------------------------------" << std::endl;
// for (std::string message : checkConstraintsResult.messages) {
// std::cout << message << std::endl;
// }
// std::cout << "-----------------------------------------------------" << std::endl;
// exit(1);
// }
//}
//else {
// calculateObjective(SAListOfEachDay);
// adjustDepartureTime(SAListOfEachDay);
//}
//std::cout << "Score: " << newScore << std::endl;
}
//std::cout << "Score: " << newScore << ", Best Score: " << bestScore << std::endl;
// Store it if it's the best one so far.
if (newScore < 0) {
std::cout << "Error: Total score < 0: " << std::endl;
std::cout << "Total score: " << newScore << std::endl;
exit(1);
}
if (newScore < bestScore) {
bestSA.assign(SAListOfEachDay.begin(), SAListOfEachDay.end());
bestScore = newScore;
//std::cout << "Best Score: " << newScore << std::endl;
//printGraph(SAListOfEachDay, GRAPH_LIMIT);
}
// Judge if it can replace the current one.
if (newScore < currentScore) {
currentSA.assign(SAListOfEachDay.begin(), SAListOfEachDay.end());
}
else {
if (getRandomDecimal() < exp(-(currentScore - newScore) / t)) {
currentSA.assign(SAListOfEachDay.begin(), SAListOfEachDay.end());
currentScore = newScore;
}
}
//printGraph(bestSA, GRAPH_LIMIT);
//std::cout << bestScore << std::endl;
//exit(1);
}
}
calculateObjective(bestSA);
if (getViolationScore(bestSA, FACTOR_OF_VIOLATION) == 0) {
adjustDepartureTime(bestSA);
}
//CheckConstraintsResult checkConstraintsResult = checkConstraints(bestSA);
//printGraph(bestSA, GRAPH_LIMIT);
//std::cout << "Best score: " << bestScore << std::endl;
//if (checkConstraintsResult.result == false) {
// std::cout << "Violation detected ----------------------------------" << std::endl;
// for (std::string message : checkConstraintsResult.messages) {
// std::cout << message << std::endl;
// }
// std::cout << "-----------------------------------------------------" << std::endl;
//}
bestSolution = std::vector<std::vector<int>>(bestSA);
}
void SA::tweakSolutionByInsertion(std::vector<std::vector<int>>& SAListOfEachDay) {
//int day = getRandomInteger(3);
for (int day = 0; day < nDays; day++) {
int positionToChoose = getRandomInteger(SAListOfEachDay[day].size());
int positionToInsert = getRandomInteger(SAListOfEachDay[day].size() - 1);
int tempValueForInsertion = SAListOfEachDay[day][positionToChoose];
SAListOfEachDay[day].erase(SAListOfEachDay[day].begin() + positionToChoose);
SAListOfEachDay[day].insert(SAListOfEachDay[day].begin() + positionToInsert, tempValueForInsertion);
}
}
void SA::tweakSolutionBySwap(std::vector<std::vector<int>>& SAListOfEachDay) {
//int day = getRandomInteger(3);
for (int day = 0; day < nDays; day++) {
int position1 = getRandomInteger(SAListOfEachDay[day].size());
int position2 = getRandomInteger(SAListOfEachDay[day].size());
std::swap(SAListOfEachDay[day][position1], SAListOfEachDay[day][position2]);
}
}
void SA::tweakSolutionByReversion(std::vector<std::vector<int>>& SAListOfEachDay) {
//int day = getRandomInteger(3);
for (int day = 0; day < nDays; day++) {
int position1 = getRandomInteger(SAListOfEachDay[day].size());
int position2 = getRandomInteger(SAListOfEachDay[day].size());
int middlePosition = (position1 + position2) / 2;
int totalOfTwoPositions = position1 + position2;
for (int i = std::min(position1, position2); i <= middlePosition; i++) {
std::swap(SAListOfEachDay[day][i], SAListOfEachDay[day][totalOfTwoPositions - i]);
}
}
}
void SA::tweakSolutionRandomly(std::vector<std::vector<int>>& SAListOfEachDay) {
int randomChoice = getRandomInteger(3);
if (randomChoice == 0) {
tweakSolutionByInsertion(SAListOfEachDay);
}
else if (randomChoice == 1) {
tweakSolutionBySwap(SAListOfEachDay);
}
else if (randomChoice == 2) {
tweakSolutionByReversion(SAListOfEachDay);
}
else {
std::cout << "Error: tweakSARandomly get value out of {0, 1, 2}" << std::endl;
exit(1);
}
}
int Smallest::getTheSmallest() {
return index[0];
}
void Smallest::increaseAValue(int _index, int _value) {
bool found = false;
for (int i = 0; i < quantity; i++) {
if (found == true) {
if (value[i] < value[i - 1]) {
std::swap(value[i], value[i - 1]);
}
else {
break;
}
}
if (_index == index[i]) {
found = true;
value[i] = _value;
}
}
}
void Smallest::resetAll(int _value) {
for (int i = 0; i < quantity; i++) {
value[i] = _value;
}
}
double SA::getTheBestScore() {
calculateObjective(bestSolution);
double violationScore = getViolationScore(bestSolution, FACTOR_OF_VIOLATION);
double objectiveScore = getObjectiveScore(bestSolution);
if (getViolationScore(bestSolution, FACTOR_OF_VIOLATION) == 0) {
adjustDepartureTime(bestSolution);
violationScore = getViolationScore(bestSolution, FACTOR_OF_VIOLATION);
objectiveScore = getObjectiveScore(bestSolution);
}
if (violationScore < 0) {
std::cout << "Error: violation < 0: " << std::endl;
std::cout << "Violation score: " << violationScore << std::endl;
exit(1);
}
if (objectiveScore < 0) {
std::cout << "Error: objectiveScore < 0: " << std::endl;
std::cout << "Objective score: " << objectiveScore << std::endl;
exit(1);
}
return violationScore + objectiveScore;
}
bool SA::checkIfTheBestSolutionIsValid() {
calculateObjective(bestSolution);
if (getViolationScore(bestSolution, FACTOR_OF_VIOLATION) == 0) {
return true;
}
return false;
}