-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_f1.py
More file actions
174 lines (125 loc) · 4.09 KB
/
optimize_f1.py
File metadata and controls
174 lines (125 loc) · 4.09 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
from ortools.linear_solver import pywraplp
import data_parser
def formulate_as_mip(solver, racers):
opt_vars = []
cost = 0
points = 0
for i in range(len(racers)):
var_name = racers[i].name
z = solver.IntVar(0,1,var_name)
opt_vars.append(z)
cost += z * racers[i].cost
points += z * racers[i].points
return opt_vars, cost, points
def optimal_fantasy_team(drivers, teams):
solver = pywraplp.Solver.CreateSolver('SCIP')
z_drivers, cost_drivers, points_drivers = formulate_as_mip(solver, drivers)
z_teams, cost_teams, points_teams = formulate_as_mip(solver, teams)
# Pick 5 drivers
z_drivers_sum = 0
for zd in z_drivers:
z_drivers_sum += zd
solver.Add(z_drivers_sum == 5)
# Pick one team
z_teams_sum = 0
for zt in z_teams:
z_teams_sum += zt
solver.Add(z_teams_sum == 1)
# Stay within the budget
solver.Add(cost_drivers + cost_teams <= 100.0)
# Maximize points
solver.Minimize(-points_drivers - points_teams)
# Solve
status = solver.Solve()
# Print solution.
opt_team = ''
opt_drivers = []
if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:
for i in range(len(z_teams)):
zt = z_teams[i]
if zt.solution_value() == 1:
if opt_team != '':
raise Exception("More than one team have been chosen!")
opt_team = teams[i].name
for i in range(len(z_drivers)):
zd = z_drivers[i]
if zd.solution_value() == 1:
opt_drivers.append(drivers[i].name)
else:
print('failed')
return opt_drivers, opt_team
# Optimal lineup for each individual race
def main():
races = data_parser.parse_races()
data_parser.save_races_as_json(races,'clean_data.json')
for race in races:
print(f'--- {race.name} ---')
opt_drivers, opt_team = optimal_fantasy_team(race.drivers, race.teams)
print(f'Team: {opt_team}')
print('Drivers: ')
for d in opt_drivers:
print(d)
print('')
# Optimial lineup for all races combined
def main2():
races = data_parser.parse_races()
drivers = races[0].drivers
teams = races[0].teams
nr_races = 1
for r in races[1:]:
nr_races += 1
for i in range(len(drivers)):
name1 = drivers[i].name
name2 = r.drivers[i].name
assert(name1 == name2)
drivers[i].points += r.drivers[i].points
drivers[i].cost += r.drivers[i].cost
for i in range(len(teams)):
teams[i].points += r.teams[i].points
teams[i].cost += r.teams[i].cost
for i,d in enumerate(drivers):
d.cost /= nr_races
for i,t in enumerate(teams):
t.cost /= nr_races
opt_drivers, opt_team = optimal_fantasy_team(drivers, teams)
print(f'Team: {opt_team}')
print('Drivers: ')
for d in opt_drivers:
print(d)
print('')
# Check the optimal choice for the last 5 races
def main3():
races = data_parser.parse_races()
drivers = races[0].drivers
teams = races[0].teams
nr_races = 1
nr_last_races = 5
for r in races[len(races)-nr_last_races:]:
print(r.name)
nr_races += 1
for i in range(len(drivers)):
name1 = drivers[i].name
name2 = r.drivers[i].name
assert(name1 == name2)
drivers[i].points += r.drivers[i].points
drivers[i].cost += r.drivers[i].cost
for i in range(len(teams)):
teams[i].points += r.teams[i].points
teams[i].cost += r.teams[i].cost
for i,d in enumerate(drivers):
d.cost /= nr_races
for i,t in enumerate(teams):
t.cost /= nr_races
opt_drivers, opt_team = optimal_fantasy_team(drivers, teams)
print("")
print(f'Team: {opt_team}')
print('Drivers: ')
for d in opt_drivers:
print(d)
print('')
if __name__ == '__main__':
main()
print("---")
main2()
print("---")
main3()