纸浆中的 LP - 决策变量问题
LP in pulp - problem with decision varaible
我的学习 python 之旅才刚刚开始。我的问题是我希望我的决策变量依赖于由另一个决策变量改变的前一个时期的起始变量。我收到错误的行是:
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
。相反,任何时候你的纸浆模型需要一个“for each”约束,你将需要制作一个循环(最简单的方法)并使用循环来制作完整的约束集。在我下面的示例中,循环将创建 8 个独立的约束,每个玩家一个。
注意:我不会对您模型的逻辑进行质量检查,因此如果存在数学错误,您需要深入研究。你这里有一个很好的框架,但我 强烈地 建议你重新设计模型,使它成为一个简单的背包问题(你已经有了零件),以构建尽可能有限的最佳团队和成本有限。这些约束非常简单。如果你能做到这一点,你就可以开始尝试这个更有创意的进出限制。 :)
# 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()
我的学习 python 之旅才刚刚开始。我的问题是我希望我的决策变量依赖于由另一个决策变量改变的前一个时期的起始变量。我收到错误的行是:
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
。相反,任何时候你的纸浆模型需要一个“for each”约束,你将需要制作一个循环(最简单的方法)并使用循环来制作完整的约束集。在我下面的示例中,循环将创建 8 个独立的约束,每个玩家一个。
注意:我不会对您模型的逻辑进行质量检查,因此如果存在数学错误,您需要深入研究。你这里有一个很好的框架,但我 强烈地 建议你重新设计模型,使它成为一个简单的背包问题(你已经有了零件),以构建尽可能有限的最佳团队和成本有限。这些约束非常简单。如果你能做到这一点,你就可以开始尝试这个更有创意的进出限制。 :)
# 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()