-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolleyball.py
More file actions
executable file
·400 lines (347 loc) · 8.46 KB
/
Copy pathvolleyball.py
File metadata and controls
executable file
·400 lines (347 loc) · 8.46 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/python
import math
import sys
if len(sys.argv) != 5:
print "USAGE: volleyball.py <teams.csv> <gyms.csv> <exclusions.cvs> <linkages.csv>"
exit(1)
# read teams.csv
f = open(sys.argv[1])
teamlines = f.readlines()
f.close()
teamList = []
for l in teamlines:
teamList.append( l.split(',') )
# read gyms.csv
f = open(sys.argv[2])
gymlines = f.readlines()
f.close()
gymMatrix = []
for l in gymlines:
gymMatrix.append( l.split(',') )
# read exclusions.csv
f = open(sys.argv[3])
exlines = f.readlines()
f.close()
exclusionMap = {}
for l in exlines:
a = l.split(',')
exclusionMap[a[0]] = a[1]
# read linkages.csv
f = open(sys.argv[4])
linkages = f.readlines()
f.close()
def getHomePairs():
pairs = []
for l in linkages:
ll = l.strip().split(',')
pairs.append( (int(ll[0]),int(ll[1])) )
return pairs
def getGyms():
x = gymMatrix[5]
y = x[1:-1]
z = [int(i) for i in y]
return z
def getDivisions():
divs = []
numDivs = int(teamList[0][0])
teams = teamList[2:]
for d in range(1,numDivs+1):
div = []
for t in teams:
if t[2] == "division " + str(d):
div.append(int(t[0]))
divs.append(div)
return divs
def getTeams():
tl = teamList[2:]
return [int(t[0]) for t in tl]
# index sets
weeks = [i+1 for i in xrange(10)]
teams = getTeams()
divisions = getDivisions()
gyms = getGyms()
homePairs = getHomePairs()
homeTeamWeek = [] #[(1,4),(2,5)] # (team,week)
specificMatches = [] #[(1,2,3)] # (team,team,week)
# decision variables
def game(i,j,t):
return "game_team" + str(i) + "_team" + str(j) + "_week" + str(t);
def gymSelection(t,i,g):
return "gymSelection_week" + str(t) + "_team" + str(i) + "_gym" + str(g);
def z(i,j):
return "z_" + str(i) + "_" + str(j)
def m_p(i,j):
return "m_p_" + str(i) + "_" + str(j)
def m_pp(i,j):
return "m_pp_" + str(i) + "_" + str(j)
def m(i,j):
return m_p(i,j) + " - " + m_pp(i,j)
def abs_m(i,j):
return m_p(i,j) + " + " + m_pp(i,j)
# parameters
def homeGym(t,i,g):
if g in exclusionMap and exclusionMap[g] == t:
return 0
else:
return int(gymMatrix[i+7][g])
def expectedGames(i,j):
if teamList[i+1][2] == teamList[j+1][2]:
return 1
else:
return 0
# objective
def objectiveFunction():
g = []
for i in teams:
for j in teams:
if ( i != j ):
g.append( abs_m(i,j) )
#g.append( m_pp(i,j) )
#print "max: " + " + ".join(g) + ";"
print "min: + " + " + ".join(g) + ";"
# constraints
def playAllGames():
print "// playAllGames"
# enumerate the combinations
for i in teams:
for j in teams:
# create the inequality sum for each combination
if (i != j):
tgames = []
for t in weeks:
tgames.append( game(i,j,t) )
print " + ".join(tgames) + " = " + str(expectedGames(i,j)) + ";"
def eitherHomeOrAway():
print "// eitherHomeOrAway"
# enumerate the combinations
for j in teams:
for t in weeks:
# create the inequality for each combination
lhs = []
rhs = []
for i in teams:
if ( i != j ):
lhs.append(game(i,j,t))
rhs.append(game(j,i,t))
print " + ".join(lhs) + " <= 1 - " + " + ".join(rhs) + ";"
def useOneHomeGym():
print "// useOneHomeGym"
for g in gyms:
for t in weeks:
sel = []
for i in teams:
sel.append( gymSelection(t,i,g) )
print " + ".join(sel) + " <= 1" + ";"
def gymSelectionMaxRequirement():
print "// gymSelectionMaxRequirement"
for g in gyms:
for t in weeks:
for i in teams:
jsum = []
for j in teams:
if (i != j):
jsum.append(game(i,j,t) + " " + str(homeGym(t,i,g)))
print gymSelection(t,i,g) + " <= " + " + ".join(jsum) + ";"
def allGameMustHaveGym():
print "// allGameMustHaveGym"
for t in weeks:
for i in teams:
games = []
gymSels = []
for j in teams:
if ( i != j ):
games.append(game(i,j,t))
for g in gyms:
gymSels.append(gymSelection(t,i,g));
print " + ".join(games) + " = " + " + ".join(gymSels) + ";"
def noConsecutiveHomeGames():
print "// noConsecutiveHomeGames"
for x in range(len(weeks)):
if ( x < 2 ):
continue
for i in teams:
thisWeek = []
previous = []
for j in teams:
if (i != j):
thisWeek.append(game(i,j,weeks[x]))
previous.append(game(i,j,weeks[x-1]))
previous.append(game(i,j,weeks[x-2]))
print " + ".join(thisWeek) + " <= 2 - " + " - ".join(previous) + ";"
def noConsecutiveAwayGames():
print "// noConsecutiveAwayGames"
for x in range(len(weeks)):
if ( x < 2 ):
continue
for j in teams:
thisWeek = []
previous = []
for i in teams:
if (i != j):
thisWeek.append(game(i,j,weeks[x]))
previous.append(game(i,j,weeks[x-1]))
previous.append(game(i,j,weeks[x-2]))
print " + ".join(thisWeek) + " <= 2 - " + " - ".join(previous) + ";"
def consecutiveHomeWithBye():
print "// consecutiveHomeWithBye"
for x in range((len(weeks)-3)):
# if ( x >= len(weeks) - 3 ):
# continue
for i in teams:
g = []
for j in teams:
if (i != j):
g.append(game(i,j,weeks[x]))
g.append(game(i,j,weeks[x+1]))
g.append(game(i,j,weeks[x+2]))
g.append(game(i,j,weeks[x+3]))
print " + ".join(g) + " >= 1" + ";"
def consecutiveAwayWithBye():
print "// consecutiveAwayWithBye"
for x in range((len(weeks)-3)):
# if ( x >= len(weeks) - 3 ):
# continue
for j in teams:
g = []
for i in teams:
if (i != j):
g.append(game(i,j,weeks[x]))
g.append(game(i,j,weeks[x+1]))
g.append(game(i,j,weeks[x+2]))
g.append(game(i,j,weeks[x+3]))
print " + ".join(g) + " >= 1" + ";"
def oneByeInARow():
print "// oneByeInARow"
for x in range(len(weeks)):
if ( x >= len(weeks) - 1 ):
continue
for i in teams:
g = []
for j in teams:
if (i != j):
g.append(game(i,j,weeks[x]))
g.append(game(i,j,weeks[x+1]))
g.append(game(j,i,weeks[x]))
g.append(game(j,i,weeks[x+1]))
print " + ".join(g) + " >= 1" + ";"
#
# Note that this is my interpretation of bothPlayHome, because I believe
# that the document contains a typo and mistakenly duplicates oneByeInARow.
#
def bothPlayHome():
print "// bothPlayHome"
for t in weeks:
for p in homePairs:
i = p[0]
j = p[1]
ig = []
jg = []
for g in gyms:
ig.append(gymSelection(t,i,g) + " " + str(homeGym(t,i,g)))
jg.append(gymSelection(t,j,g) + " " + str(homeGym(t,j,g)))
print " + ".join(ig) + " = " + " + ".join(jg) + ";"
def mustPlayHome():
print "// mustPlayHome"
g = []
for j in teams:
for h in homeTeamWeek:
i = h[0]
t = h[1]
g.append(game(i,j,t))
if len(g) > 0:
print " + ".join(g) + " = 1" + ";"
def mustSchedules():
print "// mustSchedules"
for s in specificMatches:
i = s[0]
j = s[1]
t = s[2]
if (i != j):
print game(i,j,t) + " = 1" + ";"
def maxGamesForDivisions():
for x in range(len(divisions)):
d = divisions[x]
print "// maxGamesForDivision" + str(x+1)
maxGames = len(d) * (len(d)-1)
g = []
for i in d:
for j in d:
if (i != j):
for t in weeks:
g.append(game(i,j,t))
print " + ".join(g) + " = " + str(maxGames) + ";"
def zij():
print "// zij"
for i in teams:
for j in teams:
if ( i != j ):
g = []
for t in weeks:
g.append( game(i,j,t) + " " + str(t) )
print z(i,j) + " = " + " + ".join(g) + ";"
def mij():
print "// mij"
for i in teams:
for j in teams:
if ( i != j ):
print m(i,j) + " = " + z(i,j) + " - " + z(j,i) + ";"
def bound_gameSpread():
print "// bound gameSpread"
for i in teams:
for j in teams:
if ( i != j ):
print m_p(i,j) + " >= 0;"
print m_pp(i,j) + " >= 0;"
print z(i,j) + " >= 0;"
def declare_games():
print "// declare games"
for i in teams:
for j in teams:
if ( i != j ):
for t in weeks:
print "bin " + game(i,j,t) + ";"
def declare_gymSelection():
print "// declare gymSelection"
for t in weeks:
for i in teams:
for g in gyms:
print "bin " + gymSelection(t,i,g) + ";"
def declare_gameSpread():
print "// declare gameSpread"
for i in teams:
for j in teams:
if ( i != j ):
print "int " + m_p(i,j) + ";"
print "int " + m_pp(i,j) + ";"
print "int " + z(i,j) + ";"
objectiveFunction()
playAllGames()
eitherHomeOrAway()
useOneHomeGym()
gymSelectionMaxRequirement()
allGameMustHaveGym()
noConsecutiveHomeGames()
noConsecutiveAwayGames()
consecutiveHomeWithBye()
consecutiveAwayWithBye()
oneByeInARow()
bothPlayHome()
mustPlayHome()
mustSchedules()
maxGamesForDivisions()
zij()
mij()
bound_gameSpread()
declare_games()
declare_gymSelection()
declare_gameSpread()
#print teams
#print divisions
#print gyms
#print gymMatrix
#def asdf():
# for g in gyms:
# for t in weeks:
# for i in teams:
# print gymSelection(t,i,g) + " " + str(homeGym(t,i,g))
#asdf()