/
hcwr.cpp
334 lines (284 loc) · 8.94 KB
/
hcwr.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
#include "hcwr.h"
#include <iostream>
#define ENDC "\033[m"
#define RED "\033[93m"
#define GREEN "\033[92m"
#define DEBUG(x) std::cout << "[DEBUG] " << x << endl
#define OUTPUT(x) std::cout << "---> " << x << endl
#define RAW_OUTPUT(x) std::cout << "---> " << x
#define RAW_OUTPUT_NO_ARROW(x) std::cout << x
#define RAW_OUTPUT_NL(x) std::cout << x << endl
#define OUTPUT_BEGIN(x) std::cout << "---> " << x << " ... "
#define OUTPUT_END std::cout << "done!" << endl
#define OUTPUT_RED(x) std::cout << "---> " << RED << x << ENDC << endl;
#define OUTPUT_GREEN(x) std::cout << "---> " << GREEN << x << ENDC << endl;
// Returns a string with leading/trailing characters of a set stripped
std::string trimmed( std::string const& str, char const* sepSet)
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos ) ? std::string() : str.substr(first, str.find_last_not_of(sepSet)-first+1);
}
// -----------------------------------------------------------------------------------
HCWR::HCWR(string input, int maxIterations, int maxRestarts)
{
this->maxIterations = maxIterations;
this->maxRestarts = maxRestarts;
this->input = input;
// Open the input file
//ifstream inputFile (input.c_str());
//istream inputFile = cin;
//if ( ! inputFile.is_open())
//{
// exit(1);
//}
// Parse it
string line;
string number;
vector< vector<int> > distances; // Stores distances matrix
//while (getline(inputFile, line))
while (getline(cin, line))
{
if (trimmed(line, " \t") == "")
{
continue;
}
stringstream linestream(line);
vector<int> row;
while (linestream >> number)
{
row.push_back(atoi(number.c_str()));
}
distances.push_back(row);
row.clear();
}
// We can now initialize our RTTP instance
this->rttp = new RTTP(distances.size(), (2 * (distances.size() - 1) + OFF_DAYS), MAX_CONSECUTIVE_OFF_DAYS, MAX_CONSECUTIVE_GAMES, MAX_CONSECUTIVE_AWAY_GAMES);
// Set costs
for (size_t i = 0; i < distances.size(); i++)
{
for (size_t j = 0; j < distances.size(); j++)
{
int test = distances[i][j];
this->rttp->setIndividualCost(i, j, test);
}
distances[i].clear();
}
// Done
distances.clear();
this->continue_iterating = true;
}
// -----------------------------------------------------------------------------------
HCWR::~HCWR()
{
delete this->rttp;
}
// -----------------------------------------------------------------------------------
void HCWR::storeCurrentSolutionAsBest()
{
this->G = this->rttp->G;
this->O = this->rttp->O;
this->V = this->rttp->V;
this->cost = this->rttp->objectiveFunction();
this->valid = this->rttp->validSolution();
this->restrictionsNotMet = this->rttp->numberOfRestrictionsNotMet();
}
// -----------------------------------------------------------------------------------
void HCWR::start()
{
clock_t start = clock(); // Store the time we begin the algorithm.
OUTPUT("Starting");
OUTPUT_BEGIN("Generating initial solution");
//this->rttp->generateRandomSolution();
this->rttp->generateInitialDoubleRoundRobinSolution();
OUTPUT_END;
OUTPUT_BEGIN("Storing initial solution as best");
this->storeCurrentSolutionAsBest();
OUTPUT_END;
for (int i = 0; i < this->maxRestarts && this->continue_iterating; i++)
{
//RAW_OUTPUT("Starting iteration #");
//RAW_OUTPUT_NL(i);
std::cout << '\r';
RAW_OUTPUT("Iterating ... [");
int percent = (int)(((float)i / (float)this->maxRestarts) * 100) + 1;
int lines = percent / 5;
for (int l = 0; l < lines; l++) RAW_OUTPUT_NO_ARROW("|");
for (int l = 0; l < (20 - lines); l++) RAW_OUTPUT_NO_ARROW("-");
RAW_OUTPUT_NO_ARROW("] ");
RAW_OUTPUT_NO_ARROW(percent);
RAW_OUTPUT_NO_ARROW("%");
RAW_OUTPUT_NO_ARROW(" | Best so far [");
RAW_OUTPUT_NO_ARROW(this->cost);
RAW_OUTPUT_NO_ARROW("/");
RAW_OUTPUT_NO_ARROW(this->restrictionsNotMet);
RAW_OUTPUT_NO_ARROW("] | Running time [");
RAW_OUTPUT_NO_ARROW((int)(((double)clock() - start) / CLOCKS_PER_SEC));
RAW_OUTPUT_NO_ARROW(" secs]");
std::cout.flush();
// Say progress if on mac
#ifdef __APPLE_CC__
static bool said = false;
static int last_cost = -1;
if (percent % 5 == 0 && ! said)
{
char command[1024];
if (last_cost > this->cost)
{
sprintf(command, "say \"Best so far: %d and %d restrictions not met. Running time is %d seconds.\" &", this->cost, this->restrictionsNotMet, (int)(((double)clock() - start) / CLOCKS_PER_SEC));
}
else
{
sprintf(command, "say \"No improvements. Running time is %d seconds.\" &", (int)(((double)clock() - start) / CLOCKS_PER_SEC));
}
system(command);
said = true;
last_cost = this->cost;
}
else if (percent % 5 != 0)
{
said = false;
}
#endif
// New random solution
//this->rttp->generateRandomSolution();
this->rttp->generateInitialDoubleRoundRobinSolution();
//if (this->rttp->objectiveFunction() < this->cost) {this->storeCurrentSolutionAsBest();}
for (int j = 0; j < this->maxIterations; j++)
{
// Look for better solution
//OUTPUT_BEGIN("Generating neighbour");
this->rttp->generateBestNeighbour();
//this->rttp->generateBestNeighbour();
//OUTPUT_END;
//system("echo \"\" > tempfile &");
//this->rttp->saveCurrentSolutionToFile("tempfile");
// Is it better?
if (this->rttp->objectiveFunction() < (int)(this->cost * 1.0)/* && this->rttp->nonRelaxedRestrictions()*/) // The latter to avoid accepting randomized invalid solutions as better
{
// It is, store it.
this->storeCurrentSolutionAsBest();
/*RAW_OUTPUT_NO_ARROW(" (found better: ");
RAW_OUTPUT_NO_ARROW(this->cost);
RAW_OUTPUT_NO_ARROW(" ["); RAW_OUTPUT_NO_ARROW(this->rttp->objectiveFunctionNotPenalized()); RAW_OUTPUT_NO_ARROW("]");
RAW_OUTPUT_NO_ARROW(" doesn't meet ");
RAW_OUTPUT_NO_ARROW(this->rttp->numberOfRestrictionsNotMet());
RAW_OUTPUT_NL(" restrictions)");*/
}
else if(this->rttp->objectiveFunction() >= this->cost && BREAK_ON_NO_BETTER_SOLUTION)
{
// We already have the best point. stop iterating.
break;
}
}
//RAW_OUTPUT("Iteration ended #");
//RAW_OUTPUT_NL(i);
}
std::cout << endl;
RAW_OUTPUT("Best solution cost: ");
RAW_OUTPUT_NL(this->cost);
RAW_OUTPUT("Restrictions not met: ");
RAW_OUTPUT_NL(this->restrictionsNotMet);
// This enables us to check restrictions not met and save the current solution to a file
this->rttp->G = this->G;
this->rttp->O = this->O;
this->rttp->V = this->V;
OUTPUT_BEGIN("Writing solution to " + this->input + ".sol");
this->rttp->saveCurrentSolutionToFile(this->input + ".sol");
OUTPUT_END;
if (this->valid)
{
OUTPUT("Solution is valid!");
}
else
{
OUTPUT("Solution is NOT valid.");
if (this->rttp->noConsecutiveHomeGames())
{
OUTPUT_GREEN("noConsecutiveHomeGames is met");
}
else
{
OUTPUT_RED("noConsecutiveHomeGames is NOT met");
}
if (this->rttp->lengthOfGames())
{
OUTPUT_GREEN("lengthOfGames is met");
}
else
{
OUTPUT_RED("lengthOfGames is NOT met");
}
if (this->rttp->lengthOfOffDays())
{
OUTPUT_GREEN("lengthOfOffDays is met");
}
else
{
OUTPUT_RED("lengthOfOffDays is NOT met");
}
if (this->rttp->lengthOfAwayGames())
{
OUTPUT_GREEN("lengthOfAwayGames is met");
}
else
{
OUTPUT_RED("lengthOfAwayGames is NOT met");
}
if (this->rttp->doubleRoundRobinTournament())
{
OUTPUT_GREEN("doubleRoundRobinTournament is met");
}
else
{
OUTPUT_RED("doubleRoundRobinTournament is NOT met");
}
if (this->rttp->stayAtHomeOnHomeGameDay())
{
OUTPUT_GREEN("stayAtHomeOnHomeGameDay is met");
}
else
{
OUTPUT_RED("stayAtHomeOnHomeGameDay is NOT met");
}
if (this->rttp->stayAtOpponentOnRoadGameDay())
{
OUTPUT_GREEN("stayAtOpponentOnRoadGameDay is met");
}
else
{
OUTPUT_RED("stayAtOpponentOnRoadGameDay is NOT met");
}
if (this->rttp->stayAtPreviousVenueOnOffDay())
{
OUTPUT_GREEN("stayAtPreviousVenueOnOffDay is met");
}
else
{
OUTPUT_RED("stayAtPreviousVenueOnOffDay is NOT met");
}
if (this->rttp->roundConsistency())
{
OUTPUT_GREEN("roundConsistency is met");
}
else
{
OUTPUT_RED("roundConsistency is NOT met");
}
if (this->rttp->freeGamesConsistency())
{
OUTPUT_GREEN("freeGamesConsistency is met");
}
else
{
OUTPUT_RED("freeGamesConsistency is NOT met");
}
if (this->rttp->teamShouldNotPlayItself())
{
OUTPUT_GREEN("teamShouldNotPlayItself is met");
}
else
{
OUTPUT_RED("teamShouldNotPlayItself is NOT met");
}
}
}