-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
356 lines (305 loc) · 8.48 KB
/
Solution.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
#include "Solution.h"
Solution::Solution(pii** _scheduling, ProblemData _problemData) {
scheduling = _scheduling;
problemData = _problemData;
calculateCost();
calculateNbViolations3();
calculateNbViolations4();
calculateNbViolations5();
calculateCorrectedCost();
}
Solution::~Solution() {
for(int i = 0; i < problemData.nRounds; i++) {
delete [] scheduling[i];
}
delete [] scheduling;
}
pii** Solution::getScheduling() {
return scheduling;
}
long Solution::getCorrectedCost() {
return correctedCost;
}
long Solution::getCost() {
return cost;
}
int Solution::getNbViolations3() {
return nbViolations3;
}
int Solution::getNbViolations4() {
return nbViolations4;
}
int Solution::getNbViolations5() {
return nbViolations5;
}
int Solution::getTotalViolations() {
return nbViolations3 + nbViolations4 + nbViolations5;
}
void Solution::calculateCorrectedCost() {
correctedCost = getCost() + problemData.penaltyRate*getTotalViolations();
}
void Solution::calculateCost() {
int** dist = problemData.dist;
int nRounds = problemData.nRounds;
int n = problemData.n;
cost = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < nRounds-1; j++) {
cost = cost + dist[scheduling[j][i].first][scheduling[j+1][i].first];
}
}
}
// Feasibility test for constraint (3):
// Every umpire sees every team at least once at the team home
void Solution::calculateNbViolations3() {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int* homeVisited = new int[nTeams];
int venue;
int contVenues = 0;
nbViolations3 = 0;
for(int u = 0; u < n; u++) {
for(int i = 0; i < nTeams; i++) {
homeVisited[i] = 0;
}
for(int i = 0; i < nRounds; i++) {
venue = scheduling[i][u].first;
if(homeVisited[venue] == 0) {
homeVisited[venue] = 1;
contVenues = contVenues + 1;
}
}
}
nbViolations3 = nTeams*n - contVenues;
delete[] homeVisited;
}
// Feasibility test for constraint (4):
// No umpire is in a home site more than once in any n - d1 consecutive slots
void Solution::calculateNbViolations4() {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int timewindow4 = problemData.timewindow4;
int* lastVisited = new int[nTeams];
int venue;
nbViolations4 = 0;
for(int u = 0; u < n; u++) {
for(int i = 0; i < nTeams; i++) {
lastVisited[i] = -1*timewindow4;
}
for(int i = 0; i < nRounds; i++) {
venue = scheduling[i][u].first;
if(i - lastVisited[venue] < timewindow4) {
nbViolations4 = nbViolations4 + 1;
}
lastVisited[venue] = i;
}
}
delete[] lastVisited;
}
// Feasibility test for constraint (5):
// No umpire sees a team more than once in any n/2 - d2 consecutive slots.
void Solution::calculateNbViolations5() {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int timewindow5 = problemData.timewindow5;
int* lastSeen = new int[nTeams];
int team1, team2;
nbViolations5 = 0;
for(int u = 0; u < n; u++) {
for(int i = 0; i < nTeams; i++) {
lastSeen[i] = -1*timewindow5;
}
for(int i = 0; i < nRounds; i++) {
team1 = scheduling[i][u].first;
team2 = scheduling[i][u].second;
if((i - lastSeen[team1] < timewindow5)) {
nbViolations5 = nbViolations5 + 1;
}
if((i - lastSeen[team2] < timewindow5)) {
nbViolations5 = nbViolations5 + 1;
}
lastSeen[team1] = i;
lastSeen[team2] = i;
}
}
delete[] lastSeen;
}
/*returns cost delta if swap(u,v) on round s is performed*/
long Solution::umpireCostDelta(int u, int v, int s) {
int nRounds = problemData.nRounds;
int** dist = problemData.dist;
long oldContrib = 0;
long newContrib = 0;
int v_left;
int v_rigth;
int v_center = scheduling[s][u].first;
int v_new = scheduling[s][v].first;
long dist_left_old = 0;
long dist_rigth_old = 0;
long dist_left_new = 0;
long dist_rigth_new = 0;
if(s == 0) {
v_rigth = scheduling[s+1][u].first;
dist_rigth_old = dist[v_center][v_rigth];
dist_rigth_new = dist[v_new][v_rigth];
} else if(s == nRounds-1) {
v_left = scheduling[s-1][u].first;
dist_left_old = dist[v_left][v_center];
dist_left_new = dist[v_left][v_new];
} else {
v_left = scheduling[s-1][u].first;
v_rigth = scheduling[s+1][u].first;
dist_left_old = dist[v_left][v_center];
dist_rigth_old = dist[v_center][v_rigth];
dist_left_new = dist[v_left][v_new];
dist_rigth_new = dist[v_new][v_rigth];
}
oldContrib = dist_left_old + dist_rigth_old;
newContrib = dist_left_new + dist_rigth_new;
return newContrib - oldContrib;
}
/*returns the delta of violations on constraint 3 if swap(u,v) on round s is performed*/
int Solution::umpireConst3Delta(int u, int v, int s) {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int* homeVisited = new int[nTeams];
int venue;
int nbViolationsOld = 0;
int nbViolationsNew = 0;
int contVenues = 0;
for(int i = 0; i < nTeams; i++) {
homeVisited[i] = 0;
}
for(int i = 0; i < nRounds; i++) {
venue = scheduling[i][u].first;
if(homeVisited[venue] == 0) {
contVenues = contVenues + 1;
}
homeVisited[venue] = homeVisited[venue] + 1;
}
nbViolationsOld = nTeams - contVenues;
if(homeVisited[scheduling[s][v].first] == 0 && homeVisited[scheduling[s][u].first] > 1) {
contVenues++;
}
if(homeVisited[scheduling[s][v].first] >= 1 && homeVisited[scheduling[s][u].first] == 1) {
contVenues--;
}
nbViolationsNew = nTeams - contVenues;
delete[] homeVisited;
return nbViolationsNew - nbViolationsOld;
}
/*returns the delta of violations on constraint 4 if swap(u,v) on round s is performed*/
int Solution::umpireConst4Delta(int u, int v, int s) {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int timewindow4 = problemData.timewindow4;
int* lastVisited = new int[nTeams];
int venue;
int nbViolationsOld = 0;
int nbViolationsNew = 0;
int limit_left = s - timewindow4 + 1;
int limit_rigth = s + timewindow4;
if(limit_left < 0) {
limit_left = 0;
}
if(limit_rigth > nRounds) {
limit_rigth = nRounds;
}
for(int i = 0; i < nTeams; i++) {
lastVisited[i] = -1*timewindow4;
}
for(int i = limit_left; i < limit_rigth; i++) {
venue = scheduling[i][u].first;
if(i - lastVisited[venue] < timewindow4) {
nbViolationsOld = nbViolationsOld + 1;
}
lastVisited[venue] = i;
}
for(int i = 0; i < nTeams; i++) {
lastVisited[i] = -1*timewindow4;
}
for(int i = limit_left; i < limit_rigth; i++) {
venue = scheduling[i][u].first;
if(i == s) {
venue = scheduling[i][v].first;
}
if(i - lastVisited[venue] < timewindow4) {
nbViolationsNew = nbViolationsNew + 1;
}
lastVisited[venue] = i;
}
delete[] lastVisited;
return nbViolationsNew - nbViolationsOld;
}
/*returns the delta of violations on constraint 5 if swap(u,v) on round s is performed*/
int Solution::umpireConst5Delta(int u, int v, int s) {
int nRounds = problemData.nRounds;
int nTeams = problemData.nTeams;
int n = problemData.n;
int timewindow5 = problemData.timewindow5;
int* lastSeen = new int[nTeams];
int team1, team2;
int nbViolationsOld = 0;
int nbViolationsNew = 0;
int limit_left = s - timewindow5 + 1;
int limit_rigth = s + timewindow5;
if(limit_left < 0) {
limit_left = 0;
}
if(limit_rigth > nRounds) {
limit_rigth = nRounds;
}
for(int i = 0; i < nTeams; i++) {
lastSeen[i] = -1*timewindow5;
}
for(int i = limit_left; i < limit_rigth; i++) {
team1 = scheduling[i][u].first;
team2 = scheduling[i][u].second;
if((i - lastSeen[team1] < timewindow5)) {
nbViolationsOld = nbViolationsOld + 1;
}
if((i - lastSeen[team2] < timewindow5)) {
nbViolationsOld = nbViolationsOld + 1;
}
lastSeen[team1] = i;
lastSeen[team2] = i;
}
for(int i = 0; i < nTeams; i++) {
lastSeen[i] = -1*timewindow5;
}
for(int i = limit_left; i < limit_rigth; i++) {
team1 = scheduling[i][u].first;
team2 = scheduling[i][u].second;
if(i == s) {
team1 = scheduling[i][v].first;
team2 = scheduling[i][v].second;
}
if((i - lastSeen[team1] < timewindow5)) {
nbViolationsNew = nbViolationsNew + 1;
}
if((i - lastSeen[team2] < timewindow5)) {
nbViolationsNew = nbViolationsNew + 1;
}
lastSeen[team1] = i;
lastSeen[team2] = i;
}
delete[] lastSeen;
return nbViolationsNew - nbViolationsOld;
}
void Solution::swap(int u, int v, int s) {
pii temp;
temp.first = scheduling[s][u].first;
temp.second = scheduling[s][u].second;
scheduling[s][u] = scheduling[s][v];
scheduling[s][v] = temp;
calculateCost();
calculateNbViolations3();
calculateNbViolations4();
calculateNbViolations5();
calculateCorrectedCost();
}