-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode
93 lines (63 loc) · 1.89 KB
/
Code
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
import cvxpy
import pandas as pd
import numpy as np
from wordcloud import WordCloud
import matplotlib.pyplot as plt
###load data
PL = pd.read_csv(dath_path)
###visualization
point_dict = dict(zip(PL.Name,PL.Points))
text = " ".join ([(k + " ")* v for k,v in point_dict.items()])
wordcloud = WordCloud(max_font_size = 50, ,max_words = 100 , background_color = 'white',collocations = False).generate(text)
plt.imshow(wordcloud)
plt.axis("off")
###auxiliary variables
end_GK = PL.index[PL['Role']=='GK']
end_GK = end_GK[len(end_GK)-1]
end_D = PL.index[PL['Role']=='D']
end_D = end_D[len(end_D)-1]
end_M = PL.index[PL['Role']=='M']
end_M = end_M[len(end_M)-1]
###fixed input
Budget = 100
prices = np.array(PL['Price'])
points = np.array(PL['Points'])
GoalKeepers = 2
Defenders = 5
Midfielders = 5
Forwards = 3
###Desicion variabels - choose/not choose player
selection = cvxpy.Bool(len(prices))
###constraints
#Budget
C1 = selection.T * prices <= Budget
#Choose 2 Goal keepers
C2 = selection[:(end_GK+1)].T * np.ones(end_GK+1) == GoalKeepers
#Choose 5 Defenders
C3 = selection[(end_GK+1):(end_D+1)].T * np.ones(end_D-end_GK) == Defenders
#Choose 5 Midfielders
C4 = selection[(end_D+1):(end_M+1)].T * np.ones(end_M-end_D) == Midfielders
#Choose 3 Forwards
C5 = selection[(end_M+1):].T * np.ones(len(prices)-end_M-1) == Forwards
#select at most 3 players from each team
teams = PL.Team.unique()
lsC = []
for t in teams:
lsC.append(selection[PL['Team']==t].T * np.ones(sum(PL['Team']==t))<=3)
lsC.append(C1)
lsC.append(C2)
lsC.append(C3)
lsC.append(C4)
lsC.append(C5)
###target function
TF = selection.T*points
###problem define and solve
PL_problem = cvxpy.Problem(cvxpy.Maximize(TF), lsC)
PL_problem.solve(solver=cvxpy.GLPK_MI)
###chosen variables
CV = selection.value
all_players = PL['Name'].tolist()
opt_team = []
for ind,name in enumerate(all_players):
if CV[ind]==1:
opt_team.append(name)