【问题标题】:Python Pulp - Number of Unique Teams ConstraintPython Pulp - 独特团队数量限制
【发布时间】:2021-02-16 10:35:28
【问题描述】:

我是 Pulp 的新手,因此在尝试进行条件约束时遇到了问题。我制作了一个 Fantasy Football 优化器,可以从 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()

【问题讨论】:

    标签: python pandas optimization linear-programming pulp


    【解决方案1】:

    用线性规划术语

    如果选择了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之间。


    现在,如果任何选定的球员来自t_j 队,你可以让二进制变量y_j = 1,否则为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) &gt; 0,那么y_j 可以是0 或1。

    现在,如果您添加一个约束(y_1 + y_2 + ... + y_T) &gt;= 8,这意味着(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) &gt; 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', '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
    

    【讨论】:

    • 首先我想说,感谢您抽出宝贵时间阅读并回答我的问题。对此,我真的非常感激。不过,现在查看您的代码时,我仍然很困惑(我不是最先进的编码器)。您能否提出我需要实施的代码解决方案?会让我更容易理解它是如何工作的。非常感谢!
    • 我添加了一个 Python 实现,但我无法对其进行测试。希望它可以帮助您解决问题
    • 感谢您花时间实现它,我想看看它是否有效,然后理解它,但我的代码出现了一些错误。在for team in unique_teams 循环中,我得到一个NameError,因为i 没有定义。对于for team in team_vars 循环,我遇到了一个语法问题,我不知道如何解决。对我的任何更正或建议将不胜感激
    • 嗨鲁本,我一直在尝试许多不同的方法来使这段代码工作,我一直在努力实现它。非常感谢您的跟进!
    • 太棒了!我现在可以正常工作了,非常感谢您的帮助!!!真的很高兴你能抽出时间。万事如意
    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多