唯一团队数约束

2024-05-16 23:03:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新手,因此在尝试进行条件约束时遇到了一个问题。我已经做了一个幻想足球优化器,可以选择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

CSV File with player data

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()

Tags: inidsfordatarawifpositionvars
1条回答
网友
1楼 · 发布于 2024-05-16 23:03:53

在线性规划术语中

如果选择了i^th播放器,则设x_i = 1,否则设为0,i = 1....I
t_i成为i^th玩家的团队,这是一个常数。
t_j成为j^th唯一的团队,也是一个常量,j = 1....T
如果t_i == t_j,则设t_{ij} = 1,否则设为0。这也是一个常数

然后您可以说从团队t_j中选择的玩家总数是(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I),从逻辑上讲,它的值介于0和I之间


现在,您可以让二进制变量y_j = 1如果任何选定的玩家来自团队t_j,则为0,否则为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


在纸浆方面(类似这样的东西,无法测试)

如果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', 'C', '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

相关问题 更多 >