问题描述
我是Pulp的新手,因此在尝试进行条件约束时遇到了问题。我制作了一个幻想足球优化器,可以选择9名球员的最佳选择,我的求解器目前可以在位置限制,薪水限制等条件下完全正常工作。
我需要添加的最后一件事是一个约束,这使得它从它挑选的9名球员中脱颖而出,需要有8个唯一的球员球队名称。例如:在我的代码###Stack QB with 2 teammates
中,鉴于此约束,四分卫和WR / TE将在同一个团队中。因此,其他所有人都应该位于不同的团队中,以拥有8个唯一的团队名称。
下面是我试图用来制作此约束的代码,excel文件的头部已优化,并且到目前为止我的代码在没有约束的情况下仍能正常工作,我想在选择的9个球员中添加8个唯一的团队名称
我目前已经尝试过此方法,但是它不起作用!非常感谢您的帮助!
list_of_teams = raw_data['Team'].unique()
team_vars = pulp.LpVariable.dicts('team',list_of_teams,cat = 'Binary')
for team in list_of_teams:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Team'][i] == team] + [-9*team_vars[team]]) <= 0
prob += pulp.lpSum([team_vars[t] for t in list_of_teams]) >= 8
file_name = 'C:/Users/Michael Arena/Desktop/Football/Simulation.csv'
raw_data = pd.read_csv(file_name,engine="python",index_col=False,header=0,delimiter=",",quoting = 3)
player_ids = raw_data.index
player_vars = pulp.LpVariable.dicts('player',player_ids,cat='Binary')
prob = pulp.LpProblem("DFS Optimizer",pulp.LpMaximize)
prob += pulp.lpSum([raw_data['Projection'][i]*player_vars[i] for i in player_ids])
##Total Salary upper:
prob += pulp.lpSum([raw_data['Salary'][i]*player_vars[i] for i in player_ids]) <= 50000
##Total Salary lower:
prob += pulp.lpSum([raw_data['Salary'][i]*player_vars[i] for i in player_ids]) >= 49900
##Exactly 9 players:
prob += pulp.lpSum([player_vars[i] for i in player_ids]) == 9
##2-3 RBs:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'RB']) >= 2
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'RB']) <= 3
##1 QB:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'QB']) == 1
##3-4 WRs:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'WR']) >= 3
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'WR']) <= 4
##1-2 TE's:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'TE']) >= 1
# prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'TE']) <= 2
##1 DST:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'DST']) == 1
###Stack QB with 2 teammates
for qbid in player_ids:
if raw_data['Position'][qbid] == 'QB':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
(raw_data['Team'][i] == raw_data['Team'][qbid] and
raw_data['Position'][i] in ('WR','TE'))] +
[-1*player_vars[qbid]]) >= 0
###Don't stack with opposing DST:
for dstid in player_ids:
if raw_data['Position'][dstid] == 'DST':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
raw_data['Team'][i] == raw_data['Opponent'][dstid]] +
[8*player_vars[dstid]]) <= 8
###Stack QB with 1 opposing player:
for qbid in player_ids:
if raw_data['Position'][qbid] == 'QB':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
(raw_data['Team'][i] == raw_data['Opponent'][qbid] and
raw_data['Position'][i] in ('WR','TE'))]+
[-1*player_vars[qbid]]) >= 0
prob.solve()
解决方法
以线性编程术语
如果选择了x_i = 1
播放器,让i^th
,否则选择0,i = 1....I
。
假设t_i
是i^th
玩家的团队,这是一个常量。
假设t_j
是j^th
唯一的团队,也是一个不变的j = 1....T
。
如果t_{ij} = 1
,则让t_i == t_j
,否则为0。这也是一个常数。
那么您可以说,从t_j
团队中选出的球员总数为(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I)
,从逻辑上来说,其取值范围为0到I。
现在,如果任何选定的球员来自团队y_j = 1
,则可以让二进制变量t_j
进入,否则为0,就像这样:
(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) >= y_j
这给您以下情况:
- 如果
(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) = 0
,则y_j
为0; - 如果
(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) > 0
,则y_j
可以为0或1。
现在,如果您添加约束(y_1 + y_2 + ... + y_T) >= 8
,则意味着(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) > 0
至少有8个不同的团队t_j
。
以PULP术语(类似这样,无法对其进行测试)
如果player_vars
是等效于x_i
的二进制变量
teams = raw_data['Team'] # t_i
unique_teams = teams.unique() # t_j
player_in_team = teams.str.get_dummies() # t_{ij}
# Example output for `teams = pd.Series(['A','B','C','D','E','F','A','E'])`:
# A B C D E F
# 0 1 0 0 0 0 0
# 1 0 1 0 0 0 0
# 2 0 0 1 0 0 0
# 3 0 0 0 1 0 0
# 4 0 0 0 0 1 0
# 5 0 0 0 0 0 1
# 6 1 0 0 0 0 0
# 7 0 0 1 0 0 0
# 8 0 0 0 0 1 0
team_vars = pulp.LpVariable.dicts('team',unique_teams,cat='Binary') # y_j
for team in unique_teams:
prob += pulp.lpSum(
[player_in_team[team][i] * player_vars[i] for i in player_ids]
) >= team_vars[team]
prob += pulp.lpSum([team_vars[t] for t in unique_teams]) >= 8