繁体   English   中英

纸浆中的 LP - 决策变量问题

[英]LP in pulp - problem with decision variable

我希望我的决策变量取决于上一时期的起始变量,该变量由另一个决策变量更改。 我收到错误的行是:

my_problem += lineup_wk1[i] == starting_lineup[i] + trans_in[i] - trans_out[i] for i in players

它说的地方

  File "<ipython-input-21-42d75f1d2bc3>", line 4
    my_problem += lineup_wk1[i] == starting_lineup[i] + trans_in[i] - trans_out[i] for i in players
                                                                                     ^
SyntaxError: invalid syntax

完整的代码是:

players = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
points = [4.2, 5.3, 6.0, 5.5, 4.5, 5.7, 4.8, 6.9]
cost = [4.5, 5.0, 5.1, 4.9, 5.2, 5.7, 5.2, 6.0]
starting_team = [1,0,1,0,1,0,1,0]

player_points = dict(zip(players, points))
player_cost = dict(zip(players, cost))
starting_lineup = dict(zip(players, starting_team))

my_problem = pulp.LpProblem('LP_model', pulp.LpMaximize)
lineup_wk1 = pulp.LpVariable.dict('lineup_wk1_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)
trans_in  = pulp.LpVariable.dict('trans_in_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)
trans_out  = pulp.LpVariable.dict('trans_out_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)

my_problem += sum([player_points[i] * (lineup_wk1[i]) for i in players])

my_problem += sum([lineup_wk1[i] for i in players]) <= 4
my_problem += sum(trans_in[i] for i in players) <=2
my_problem += sum(trans_in[i] for i in players) == sum(trans_out[i] for i in players)
my_problem += lineup_wk1[i] == starting_lineup[i] + trans_in[i] - trans_out[i] for i in players

my_problem.solve()

因此,让您束手无策的基本事情是为您的约束制定正确的pulp表达式。 当您想为每个玩家做出约束时,您会混淆表达式的求和/生成器类型。 因此,您无法在一条线上for i in players做。 相反,只要您的纸浆 model 需要“for each”约束,您就需要制作一个循环(最简单的方法)并使用该循环来制作全套约束。 在下面的示例中,循环将创建 8 个单独的约束,每个玩家一个。

注意:我不是 QA'ing 你的 model 的逻辑,所以如果有数学错误,你需要挖掘。 您在这里有一个很好的框架,但我强烈建议您重新设计 model 以使其成为一个简单的背包问题(您已经拥有零件),以构建规模有限且成本有限的最佳团队。 这些限制非常简单。 如果你能做到这一点,那么你就可以开始冒险尝试这种更具创造性的进出限制。 :)

# pulp player & team

import pulp

players = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
points = [4.2, 5.3, 6.0, 5.5, 4.5, 5.7, 4.8, 6.9]
cost = [4.5, 5.0, 5.1, 4.9, 5.2, 5.7, 5.2, 6.0]
starting_team = [1,0,1,0,1,0,1,0]

player_points = dict(zip(players, points))
player_cost = dict(zip(players, cost))
starting_lineup = dict(zip(players, starting_team))

my_problem = pulp.LpProblem('LP_model', pulp.LpMaximize)
lineup_wk1 = pulp.LpVariable.dict('lineup_wk1_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)
trans_in  = pulp.LpVariable.dict('trans_in_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)
trans_out  = pulp.LpVariable.dict('trans_out_ % s', players, lowBound=0, upBound=1, cat=pulp.LpBinary)

my_problem += sum([player_points[i] * (lineup_wk1[i]) for i in players])

my_problem += sum([lineup_wk1[i] for i in players]) <= 4
my_problem += sum(trans_in[i] for i in players) <=2
my_problem += sum(trans_in[i] for i in players) == sum(trans_out[i] for i in players)
# this will make a "for each player" constraint.  There is no summation... 
# note the change from i -> "player" which makes this more readable.
for player in players:  
   my_problem += lineup_wk1[player] == starting_lineup[player] + trans_in[player] - trans_out[player] 

my_problem.solve()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM