-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoose_team.py
executable file
·191 lines (158 loc) · 5.67 KB
/
choose_team.py
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
import argparse
import pandas as pd
import pyomo.environ as pyomo_env
from pyomo.opt import SolverFactory
from prettytable import PrettyTable
from common import Position
def create_model(dataframe):
# Configuration
expected_position_counts = {
Position.GOALKEEPER: 3,
Position.DEFENDER: 8,
Position.MIDFIELDER: 9,
Position.FORWARD: 6,
}
total_players = 26
players_per_nation = (0, 30) # minimum 0, maximum 30 -> the limit got removed
maximum_ingame_value = 70.0 * 10**6 # million
# Define the actual model.
model = pyomo_env.ConcreteModel()
model.name_ = pyomo_env.Set(initialize=dataframe.name_.to_list())
model.cost_ingame = pyomo_env.Param(
model.name_,
initialize=dict(zip(dataframe.name_, dataframe.cost_ingame)),
within=pyomo_env.NonNegativeReals,
)
model.market_value = pyomo_env.Param(
model.name_, initialize=dict(zip(dataframe.name_, dataframe.market_value))
)
model.nationality = pyomo_env.Param(
model.name_,
initialize=dict(zip(dataframe.name_, dataframe.nationality)),
within=pyomo_env.Any,
)
model.position = pyomo_env.Param(
model.name_, initialize=dict(zip(dataframe.name_, dataframe.position))
)
# Is the player chosen for the final team?
model.chosen = pyomo_env.Var(model.name_, within=pyomo_env.Boolean, initialize=1)
# Objective: Maximize the market value of the players.
def market_value_rule(model):
market_value_sum = sum(
model.market_value[i] * model.chosen[i] for i in model.name_
)
return market_value_sum
model.average_market_value = pyomo_env.Objective(
rule=market_value_rule, sense=pyomo_env.maximize
)
# Constraint: Ingame cost of the players.
def cost_rule(model):
value = sum(model.cost_ingame[i] * model.chosen[i] for i in model.name_)
return value <= maximum_ingame_value
model.total_cost = pyomo_env.Constraint(rule=cost_rule)
# Constraint: Amount of players for each position.
def position_rule(model, position, expected_count):
actual_count = sum(
model.chosen[i] * int(model.position[i] == position.name)
for i in model.name_
)
return actual_count == expected_count
model.positions = pyomo_env.Constraint(
expected_position_counts.items(), rule=position_rule
)
# Constraint: Maximum team size.
def total_players_rule(model):
value = sum(model.chosen[i] for i in model.name_)
return value == total_players
model.total_players = pyomo_env.Constraint(rule=total_players_rule)
# Constraint: Amount of players of each national team.
def nationality_rule(model, nation, count_min, count_max):
actual_count = sum(
model.chosen[i] * int(model.nationality[i] == nation) for i in model.name_
)
return pyomo_env.inequality(count_min, actual_count, count_max)
expected_nation_counts = {
nation: players_per_nation for nation in set(dataframe.nationality.to_list())
}
model.nationalities = pyomo_env.Constraint(
expected_nation_counts.items(), rule=nationality_rule
)
return model
def print_results(model):
total_ingame_sum = 0
total_market_value = 0
team = []
for i in model.name_:
if bool(pyomo_env.value(model.chosen[i])):
team.append(
(
Position[model.position[i]],
i,
model.nationality[i],
model.cost_ingame[i] / 1000000.0,
model.market_value[i] / 1000000.0,
f"{model.market_value[i] / model.cost_ingame[i]:.2f}",
)
)
total_ingame_sum += model.cost_ingame[i]
total_market_value += model.market_value[i]
table = PrettyTable()
table.field_names = [
"Position",
"Name",
"Club",
"Ingame value [Mio. €]",
"Market value [Mio. €]",
"Ratio (market / ingame)",
]
for player in sorted(team):
table.add_row(player)
table.add_row(
(
"-",
"Total",
"",
total_ingame_sum / 1000000.0,
total_market_value / 1000000.0,
f"{total_market_value / total_ingame_sum:.2f}",
)
)
print(table)
def print_top_ratios(data):
print("Top Ratios:")
df_top_ratios = (
data[["name_", "nationality", "position", "cost_ingame", "market_value", "ratio"]]
.sort_values("ratio", ascending=False)
.head(20)
)
print(df_top_ratios)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--force", action="store_true", help="Force refreshing of the cache."
)
parser.add_argument(
"--exclude-list",
default=None,
help="List of players to exclude. Separated by new line.",
)
parser.add_argument(
"--show-top-ratios",
action="store_true",
help="Show players with the baes ratio (market value / ingame value).",
)
args = parser.parse_args()
dataframe = pd.read_csv("work/test.csv")
dataframe["ratio"] = dataframe["market_value"] / dataframe["cost_ingame"]
if args.exclude_list is not None:
with open(args.exclude_list) as infile:
exclude_list = infile.readlines()
dataframe = dataframe[~dataframe["name_"].isin(exclude_list)]
model = create_model(dataframe)
opt = SolverFactory("glpk")
opt.solve(model)
print_results(model)
if args.show_top_ratios:
print_top_ratios(dataframe) # Only for information.
if __name__ == "__main__":
main()