-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.cpp
329 lines (279 loc) · 10.9 KB
/
tournament.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
# include "headers.h"
# include "iostream"
# include "iomanip"
# include "cmath"
# include <fstream>
Tournament::Tournament(vector<vector<int>> schedule) {
this->schedule = schedule;
}
vector<vector<int>> Tournament::getSchedule() {
return schedule;
}
int Tournament::getImprovements() {
return improvements;
}
void Tournament::setImprovements(int improvements) {
this->improvements = improvements;
}
// Function to calculate the total distance traveled by each team
// Returns a vector<int> that contains total distance traveled by each team
vector<int> Tournament::calculateTotalDistanceByTeam(vector<vector<int>> distances) {
vector<int> total_distance_by_team;
for (int i = 0; i < int(distances.size()); i++) {
total_distance_by_team.push_back(0);
}
for (int e = 0; e < int(schedule.size()); e++) {
int current_loc = e + 1;
for (int r = 0; r < int(schedule[e].size()); r++) {
// If the value is negative, it means that the team must travel to opponent home
if (schedule[e][r] < 0) {
total_distance_by_team[e] += distances[current_loc - 1][schedule[e][r] * -1 - 1];
current_loc = schedule[e][r] * -1;
}
// If the value is positive, it means that the team must travel to home
else if (schedule[e][r] > 0) {
total_distance_by_team[e] += distances[current_loc - 1][e];
current_loc = e + 1;
}
}
total_distance_by_team[e] += distances[current_loc - 1][e];
}
return total_distance_by_team;
}
// Function to calculate the total distance traveled by all teams
// Returns the total distance traveled by all teams
int Tournament::calculateTotalDistance(vector<vector<int>> distances) {
vector<int> total_distance_by_team = calculateTotalDistanceByTeam(distances);
int total_distance = 0;
for (int i = 0; i < int(total_distance_by_team.size()); i++) {
total_distance += total_distance_by_team[i];
}
return total_distance;
}
// Function to verify that the number of consecutive local games for any team is between l and u
// Returns 1 if the condition is met, 0 otherwise
int Tournament::verifyConsecutiveLocalGamesBounds(int l, int u) {
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_local_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] > 0) {
consecutive_local_games++;
if (consecutive_local_games < l || consecutive_local_games > u) return 0;
}
else if (schedule[e][r] < 0) consecutive_local_games = 0;
}
}
return 1;
}
// Function to verify that the number of consecutive away games for any team is between l and u
// Returns 1 if the condition is met, 0 otherwise
int Tournament::verifyConsecutiveAwayGamesBounds(int l, int u) {
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_away_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] < 0) {
consecutive_away_games++;
if (consecutive_away_games < l || consecutive_away_games > u) return 0;
}
else if (schedule[e][r] > 0) consecutive_away_games = 0;
}
}
return 1;
}
// Function to verify that maximum number of consecutive games for any team is b
// Returns 1 if the condition is met, 0 otherwise
int Tournament::verifyConsecutiveMaxGames(int b) {
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] != 0) {
consecutive_games++;
if (consecutive_games > b) return 0;
}
else consecutive_games = 0;
}
}
return 1;
}
// Function to verify that maximum number of consecutive byes for any team is o
// Returns 1 if the condition is met, 0 otherwise
int Tournament::verifyConsecutiveMaxByes(int o) {
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_byes = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] == 0) {
consecutive_byes++;
if (consecutive_byes > o) return 0;
}
else consecutive_byes = 0;
}
}
return 1;
}
// Function to count the number of consecutive local games violations
// Returns the number of violations
int Tournament::countViolationsConsecutiveLocalGamesBounds(int l, int u) {
int count = 0;
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_local_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] > 0) {
consecutive_local_games++;
if (consecutive_local_games < l || consecutive_local_games > u) count++;
}
else if (schedule[e][r] < 0) consecutive_local_games = 0;
}
}
return count;
}
// Function to count the number of consecutive away games violations
// Returns the number of violations
int Tournament::countViolationsConsecutiveAwayGamesBounds(int l, int u) {
int count = 0;
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_away_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] < 0) {
consecutive_away_games++;
if (consecutive_away_games < l || consecutive_away_games > u) count++;
}
else if (schedule[e][r] > 0) consecutive_away_games = 0;
}
}
return count;
}
// Function to count the number of consecutive games violations
// Returns the number of violations
int Tournament::countViolationsConsecutiveMaxGames(int b) {
int count = 0;
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_games = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] != 0) {
consecutive_games++;
if (consecutive_games > b) count++;
}
else consecutive_games = 0;
}
}
return count;
}
// Function to count the number of consecutive byes violations
// Returns the number of violations
int Tournament::countViolationsConsecutiveMaxByes(int o) {
int count = 0;
for (int e = 0; e < int(schedule.size()); e++) {
int consecutive_byes = 0;
for (int r = 0; r < int(schedule[e].size()); r++) {
if (schedule[e][r] == 0) {
consecutive_byes++;
if (consecutive_byes > o) count++;
}
else consecutive_byes = 0;
}
}
return count;
}
// Function to verify that all constraints are met
// Returns 1 if all constraints are met, 0 otherwise
int Tournament::verifyAllConstraints(Instance instance) {
if (verifyConsecutiveLocalGamesBounds(instance.getL(), instance.getU()) == 0) return 0;
if (verifyConsecutiveAwayGamesBounds(instance.getL(), instance.getU()) == 0) return 0;
if (verifyConsecutiveMaxGames(instance.getB()) == 0) return 0;
if (verifyConsecutiveMaxByes(instance.getO()) == 0) return 0;
return 1;
}
// Function to calculate the number of constraints violated
// Returns the number of constraints violated
int Tournament::calculateConstraintsViolated(Instance instance) {
int constraints_violated = 0;
if (verifyConsecutiveLocalGamesBounds(instance.getL(), instance.getU()) == 0) constraints_violated++;
if (verifyConsecutiveAwayGamesBounds(instance.getL(), instance.getU()) == 0) constraints_violated++;
if (verifyConsecutiveMaxGames(instance.getB()) == 0) constraints_violated++;
if (verifyConsecutiveMaxByes(instance.getO()) == 0) constraints_violated++;
return constraints_violated;
}
// Function to count the number of total constraints violations
// Returns the number of total constraints violations
int Tournament::countAllViolations(Instance instance) {
int l = instance.getL();
int u = instance.getU();
int b = instance.getB();
int o = instance.getO();
return
countViolationsConsecutiveAwayGamesBounds(l, u) +
countViolationsConsecutiveLocalGamesBounds(l, u) +
countViolationsConsecutiveMaxByes(o) +
countViolationsConsecutiveMaxGames(b);
}
// Function to calculate the average distance between teams
// Returns the average distance
int Tournament::calculateAverageDistance(vector<vector<int>> distances) {
int total_distance = 0;
int average_distance = 0;
for (int i = 0; i < int(distances.size()); i++) {
for (int j = 0; j < int(distances[i].size()); j++) {
total_distance += distances[i][j];
}
}
average_distance = total_distance / (int(distances.size()) * int(distances[0].size()));
return average_distance;
}
// Function to calculate the fitness
// Returns the fitness
int Tournament::calculateFitness(Instance instance) {
int total_distance = calculateTotalDistance(instance.getDistances());
int n = instance.getN();
// Penalizations
int constraints_violated = countAllViolations(instance);
int average_distance = calculateAverageDistance(instance.getDistances());
return total_distance + average_distance * constraints_violated * (n - 1) * 0.5;
}
void Tournament::print() {
int i = 1;
for (vector<int> row : schedule) {
cout << setw(2) << i++ << ": ";
for (int element : row) {
cout << setw(3) << element << " ";
}
cout << endl;
}
cout << endl;
}
// Function to write a .txt solution file
void Tournament::writeToTxt(string filename, Instance instance, int execution_time) {
ofstream file(filename);
file << "Solution:" << endl;
// Write schedule
int team = 1;
for (vector<int> row : schedule) {
file << team++ << ": ";
for (int element : row) {
file << element << " ";
}
file << endl;
}
// Write distances traveled by each teams
vector<int> total_distance_by_team = calculateTotalDistanceByTeam(instance.getDistances());
file << endl;
file << "Total distance by team:" << endl;
for (int i = 0; i < int(total_distance_by_team.size()); i++) {
file << i + 1 << ": " << total_distance_by_team[i] << endl;
}
// Write total distance
file << endl;
file << "Total distance: " << calculateTotalDistance(instance.getDistances()) << endl;
// Write fitness
file << endl;
file << "Fitness: " << calculateFitness(instance) << endl;
// Write execution time
file << endl;
file << "Execution time (s): " << execution_time * 1e-3 << endl;
// Write number of violated constraints
file << endl;
file << "Number of violated constraints: " << calculateConstraintsViolated(instance) << endl;
// Write number of iterations
file << endl;
file << "Number of iterations achieved: " << getImprovements() << endl;
file.close();
}