-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
167 lines (133 loc) · 4.97 KB
/
main.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
/**
* @file main.cpp
* @brief
*/
#include <stdexcept>
#include <string>
#include <getopt.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "solver.h"
#include "LocalSearchSolver.h"
#include "TabuSearchSolver.h"
#include "solversexecutor.h"
// error status and messagge buffer
int status;
char errmsg[255];
using namespace std;
/** Struct containing the long options */
static struct option long_options[] = {
{"ls", no_argument, NULL, 'l'}, // Local Search
{"ts", no_argument, NULL, 't'}, // Tabu Search
{"fi", no_argument, NULL, 'f'}, // First Improvement
{"bi", no_argument, NULL, 'b'}, // Best Improvement
{"ac", no_argument, NULL, 'a'}, // Aspiration Criteria
{"maxIter", required_argument, NULL, 'i'}, // Max iteration for TS
{"tenure", required_argument, NULL, 'e'}, // Tenure for TS
{"secs", required_argument, NULL, 's'}, // Seconds for TS
{"bm", required_argument, NULL, 'm'}, // Benchmark
{0, 0, 0, 0}
};
int main (int argc, char *argv[]) {
try {
if (argc < 2) {
throw std::runtime_error("usage: ./main <filename>.dat");
}
SolversExecutor solversExe(argv[1]);
bool benchmark = false;
// Solver option default = LocalSearch
bool localSearch = true;
// Solvers features default = BI
bool bestImprove = true;
bool aspCriteria = false; // only for TabuSearch
// Tabu options
int seconds = 30;
int tenure = 50;
int maxIterations = 1000;
int c;
int option_index;
while((c = getopt_long(argc, argv, "lfbtae:i:s:m", long_options, &option_index)) != EOF) {
switch(c) {
case 'l': {
localSearch = true;
break;
}
case 't': {
localSearch = false;
break;
}
case 'b': {
bestImprove = true;
break;
}
case 'f': {
bestImprove = false;
break;
}
case 'a': {
aspCriteria = true;
cout << endl << "AspCriteria: " << aspCriteria << endl;
break;
}
case 'e': {
tenure = strtol(optarg, NULL, 0);
cout << endl << "Tenure: " << tenure << endl << endl;
break;
}
case 'i': {
maxIterations = (int)strtol(optarg, NULL, 0);
break;
}
case 's': {
seconds = (int)strtol(optarg, NULL, 0);
break;
}
case 'm': {
benchmark = true;
break;
}
}
}
if (benchmark) {
// Test initial solutions
solversExe.addRandomSeedInitSolution(58);
solversExe.addRandomSeedInitSolution(4);
solversExe.addRandomSeedInitSolution(25);
solversExe.addRandomSeedInitSolution(26);
solversExe.addRandomSeedInitSolution(43);
solversExe.addRandomSeedInitSolution(46);
solversExe.addRandomSeedInitSolution(93);
solversExe.addRandomSeedInitSolution(99);
//solversExe.addSolver(new LocalSearchSolver());
//solversExe.addSolver(new LocalSearchSolver(false));
int maxIteration = 300000;
double maxSeconds = 10;
//int tabuLenght = 600;
int tabuLengths[4] = {0, 20, 180, 480};
//solversExe.addSolver(TabuSearchSolver::buildTS_BI(tabuLenght, maxIteration, maxSeconds));
for (int i = 0; i < 4; i++) {
solversExe.addSolver(TabuSearchSolver::buildTS_BI(tabuLengths[i], maxIteration, maxSeconds));
//solversExe.addSolver(TabuSearchSolver::buildTS_BI_AC(tabuLengths[i], maxIteration, maxSeconds));
//solversExe.addSolver(TabuSearchSolver::buildTS_FI(tabuLengths[i], maxIteration, maxSeconds));
//solversExe.addSolver(TabuSearchSolver::buildTS_FI_AC(tabuLengths[i], maxIteration, maxSeconds));
}
} else {
// Command line program
solversExe.addRandomInitSolution();
if (localSearch) {
solversExe.addSolver(new LocalSearchSolver(bestImprove));
} else { // tabu search
solversExe.addSolver(new TabuSearchSolver(tenure, maxIterations, aspCriteria, bestImprove, seconds));
}
}
solversExe.execute();
solversExe.printInitSolutions();
solversExe.printResults();
}
catch (std::exception& e) {
std::cout << ">>>EXCEPTION: " << e.what() << std::endl;
}
return 0;
}