-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalbp1-simulated-annealing.cpp
343 lines (310 loc) · 9.51 KB
/
salbp1-simulated-annealing.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
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <utility>
#include <vector>
#include <chrono>
//Instance parameters
int numberOfTasks;
int cycleTime = 6;
std::vector<int> taskTimes;
std::vector<std::vector <int>> precedenceOrder;
//Simulated Annealing Parameters
double temperature = 1;
double initialTemperature = 1;
double temperatureLimit = 0.00001;
double decay = 0.9;
int iterations = 1000;
int restarts = 1;
long seed = 0;
//misc
bool verbose = false;
bool csv = false;
class Solution{
private:
std::vector<int> tasks;
long value;
public:
Solution();
explicit Solution(std::vector<int> tasks);
long getValue() const;
int evalSolution();
bool isValidPrecedence();
bool isValidTimes();
Solution neighbour();
void printSolution();
void printSimple(float duration);
void printCsv(float duration);
void generateInitialSolution();
};
Solution::Solution(){
generateInitialSolution();
value = evalSolution();
}
Solution::Solution(std::vector<int> tasks){
this->tasks = std::move(tasks);
value = evalSolution();
}
void Solution::generateInitialSolution(){
tasks = std::vector<int>((unsigned long)numberOfTasks);
int stationIndex = 0;
int stationTime = 0;
for (int i=0; i<numberOfTasks; i++) {
if (stationTime + taskTimes[i] <= cycleTime) {
stationTime += taskTimes[i];
tasks[i] = stationIndex;
} else {
stationTime = taskTimes[i];
tasks[i] = ++stationIndex;
}
}
}
int Solution::evalSolution(){
std::vector<int> v(tasks);
std::sort(v.begin(), v.end());
long count = std::unique(v.begin(), v.end()) - v.begin();
return (int) count;
}
bool Solution::isValidPrecedence(){
for (auto &i : precedenceOrder) {
if(tasks[i[0]] > tasks[i[1]]){
return false;
}
}
return true;
}
bool Solution::isValidTimes(){
std::vector<int> stationTimes(numberOfTasks, 0);
for (int i=0; i<numberOfTasks; i++) {
stationTimes[tasks[i]] += taskTimes[i];
if (stationTimes[tasks[i]] > cycleTime){
return false;
}
}
return true;
}
bool acceptWorseSolution(){
float zeroToOne = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/1.0));
return temperature > zeroToOne;
}
Solution Solution::neighbour(){
while(true) {
std::vector<int> newTasks(tasks);
long location = rand() % numberOfTasks;
long newStation = rand() % numberOfTasks;
newTasks[location] = (int) newStation;
Solution neigh(newTasks);
if (neigh.isValidPrecedence() && neigh.isValidTimes()) {
return neigh;
}
}
}
long Solution::getValue() const {
return value;
}
void readInstance() {
int currentValue;
std::cin >> numberOfTasks;
for(int i=0; i<numberOfTasks; i++){
std::cin >> currentValue;
taskTimes.push_back(currentValue);
}
while(std::cin >> currentValue){
if (currentValue == -1){
break;
}
int predecessor = currentValue;
int successor;
std::cin.ignore(1);
std::cin >> successor;
std::vector<int> precedence = {predecessor-1, successor-1};
precedenceOrder.push_back(precedence);
}
}
void printPrecedence(){
auto size = (long) ceil(sqrt(precedenceOrder.size()));
std::cout << "Precedence Order:" << std::endl;
int i=1;
for(auto item : precedenceOrder){
std::cout << "\t" << item[0]+1 << "->" << item[1]+1;
if (i % size == 0){
std::cout << std::endl;
} else {
std::cout << " ";
}
i++;
}
}
void printTimes(){
auto size = (long) ceil(sqrt(numberOfTasks));
std::cout << "Task times:";
for(int i=0; i<numberOfTasks; i++){
if (i % size == 0){
std::cout << std::endl;
}
std::cout << "\t(" << i+1 << ")" << taskTimes[i] << "u.t.";
}
}
void printInstance() {
std::cout << "################### Instance ###################\n";
std::cout << "Seed: " << seed << std::endl;
std::cout << "Number of tasks: " << numberOfTasks << std::endl;
std::cout << "Cycle time: " << cycleTime << std::endl;
printPrecedence();
std::cout << std::endl;
printTimes();
std::cout << std::endl;
}
void printExecution(float duration){
std::cout << "################### Execution ###################\n";
std::cout << "Duration: " << duration << " seconds.\n";
std::cout << "Parameters to reproduce this execution:" << std::endl;
std::cout << "\t-c " << cycleTime;
std::cout << " -t " << initialTemperature;
std::cout << std::fixed;
std::cout << " -l " << temperatureLimit;
std::cout << " -d " << decay;
std::cout << " -i " << iterations;
std::cout << " -r " << restarts;
std::cout << " -s " << seed;
if (verbose){
std::cout << " -v";
}
std::cout << std::endl;
}
void Solution::printSimple(float duration){
std::cout << "Seed: " << seed << std::endl;
std::cout << "Number of tasks: " << numberOfTasks << std::endl;
std::cout << "Cycle time: " << cycleTime << std::endl;
std::cout << "Best value: " << getValue() << std::endl;
std::cout << "Execution duration: " << duration << std::endl;
}
void Solution::printCsv(float duration){
std::cout << seed << "; " <<
numberOfTasks << "; " <<
cycleTime << "; " <<
iterations << "; " <<
restarts << "; " <<
getValue() << "; " <<
duration << ";";
}
void Solution::printSolution() {
std::cout << "\n################### Solution ###################\n";
std::cout << "Best value: " << getValue() << std::endl;
std::cout << "Used Stations: " << std::endl;
std::vector<std::vector<int> > stations((u_long)numberOfTasks);
for(int i=0; i<numberOfTasks; i++){
stations[tasks[i]].push_back(i);
}
int count = 0;
for(auto& station : stations){
if (station.size()){
std::cout << "\tStation " << ++count << " tasks:";
for(auto& task : station){
std::cout << " " << task+1 << ((&task==&station.back())?"\n":",");
}
}
}
std::cout << std::endl;
}
void getOptions(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "c:t:d:i:r:l:s:vp")) != -1) {
switch (opt) {
case 'c':
cycleTime = atoi(optarg);
break;
case 't':
temperature = atof(optarg);
initialTemperature = temperature;
break;
case 'r':
restarts = atoi(optarg);
break;
case 'l':
temperatureLimit = atof(optarg);
break;
case 'd':
decay = atof(optarg);
break;
case 'i':
iterations = atoi(optarg);
break;
case 's':
seed = atoi(optarg);
break;
case 'v':
verbose = true;
break;
case 'p':
csv = true;
break;
default:
std::cout << "Usage:" << argv[0] << "[OPTIONS]...\n";
std::cout << "\tOPTIONS:\n";
std::cout << "\t\t-c cycle_time\n";
std::cout << "\t\t-t temperature\n";
std::cout << "\t\t-l temperature_limit\n";
std::cout << "\t\t-d temperature_decay\n";
std::cout << "\t\t-i iterations\n";
std::cout << "\t\t-s seed\n";
std::cout << "\t\t-v (verbose)\n";
std::cout << "\t\t-p (csv)\n";
exit(EXIT_FAILURE);
break;
}
}
}
int main(int argc, char **argv) {
getOptions(argc, argv);
if (seed == 0) {
seed = (time(NULL));;
}
srand(static_cast<unsigned int>(seed));
readInstance();
std::chrono::high_resolution_clock::time_point timeBefore = std::chrono::high_resolution_clock::now();
Solution currentSolution;
Solution best = currentSolution;
for(int res = 0; res < restarts; res++){
long loopCount = 0;
while(temperature > temperatureLimit){
for(int i=0; i<iterations; i++){
Solution neighbour = currentSolution.neighbour();
if(neighbour.getValue() < currentSolution.getValue()){
currentSolution = neighbour;
} else {
if(acceptWorseSolution()){
currentSolution = neighbour;
}
}
if(neighbour.getValue() < best.getValue()){
best = neighbour;
}
}
temperature *= decay;
}
temperature = initialTemperature;
currentSolution = best;
}
std::chrono::high_resolution_clock::time_point timeAfter = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(timeAfter - timeBefore).count();
auto durationSec = duration/1000000.0;
//print outputs
if (csv){
best.printCsv(durationSec);
} else {
if (verbose){
printInstance();
best.printSolution();
printExecution(durationSec);
} else {
best.printSimple(durationSec);
}
}
return 0;
}