评估纸浆中的表达式
Evaluating expressions in pulp
我已经开发了一个 LP 问题并且它有效。但是,当我打印出 total_transfers 时,它只会给我一个类似“buy10__Aarons,_M + buy10__Adams,_C + buy10__Adrian + buy10__Ait_Nouri,_R + buy10__Ajer,_K +...." 而我认为它应该是一个数字。如何将其打印为数字?我真的很想把它作为 panalty 值放入我的 objective 函数中,但无法做到这一点
# Define constriant for starting team i.e first week (week 4)
for player in players:
fpl_problem += lineup[0][player] == starting_team[player] + tran_in[0][player] - tran_out[0][player]
fpl_problem += tran_in[0][player] + tran_out[0][player] <= 1
fpl_problem += cpt[0][player] <= lineup[0][player]
# Define constriants for teams for rest of the weeks (week 5 and 6. The code is generic and automatically scales for additional weeks)
for week in range(0,len(weeks)-1):
for player in players:
fpl_problem += lineup[week+1][player] == lineup[week][player] + tran_in[week+1][player] - tran_out[week+1][player]
fpl_problem += tran_in[week+1][player] + tran_out[week+1][player] <= 1
fpl_problem += cpt[week+1][player] <= lineup[week+1][player]
for week in range(0,len(weeks)):
fpl_problem += sum(tran_in[week][player] for player in players) <=11 # trade in players should be <= 11 per week
fpl_problem += sum(lineup[week][player] for player in players) == n_players # Lineup size should be equal to maximum players per week
fpl_problem += sum(cpt[week][player] for player in players) == 1 # There should be only 1 captain per week
fpl_problem += sum([player_cost[player] * lineup[week][player] for player in players]) <= float(max_budget) # Total cost constriant per week
total_transfers = total_transfers + sum(tran_in[week][player] for player in players)
fpl_problem += total_transfers <= transfers_left
您所说的所有事情都是正确的。 :). total_transfers
是一个 expression
。你确实意识到你正在用你的循环构建这个表达式,对吧?您也可以在循环之外通过使用对周的理解来这样做……无论哪种方式都有效。
所以你可以:
- 打印表达式(你得到的。)
- 评估表达式(你想做什么)
- 在约束或 OBJ 中任意使用表达式。 (你也想做什么。)
有时在 pulp
中不清楚如何在不进行任何修改的情况下获取方法和属性,但这里缺少要评估的是 value()
方法。请参阅下面的示例,如果您遇到困难,请回复。
# pulp expression example
from pulp import *
prob = LpProblem('example', LpMinimize)
x1 = LpVariable('x1')
x2 = LpVariable('x2')
z = x1 + x2
print(f'z is a : {type(z)}')
print(f'z evaluates to: {z}') # <-- what you are seeing now
print(f'the value of the expression z is: {z.value()}')
# some goofy constraints...
prob += x1 >= 1
prob += x2 >= 3
prob += z >= 6 # the expression z is legal in a constraint
# the objective
prob += z + x1 # the expression z is legal in the OBJ
prob.solve()
print(f'the value of z after solve is now: {z.value()}')
for v in prob.variables():
print(f'the value of {v.name} is: {v.value()}')
产量:
z is a : <class 'pulp.pulp.LpAffineExpression'>
z evaluates to: x1 + x2
the value of the expression z is: None
... solve data ...
the value of z after solve is now: 6.0
the value of x1 is: 1.0
the value of x2 is: 5.0
我已经开发了一个 LP 问题并且它有效。但是,当我打印出 total_transfers 时,它只会给我一个类似“buy10__Aarons,_M + buy10__Adams,_C + buy10__Adrian + buy10__Ait_Nouri,_R + buy10__Ajer,_K +...." 而我认为它应该是一个数字。如何将其打印为数字?我真的很想把它作为 panalty 值放入我的 objective 函数中,但无法做到这一点
# Define constriant for starting team i.e first week (week 4)
for player in players:
fpl_problem += lineup[0][player] == starting_team[player] + tran_in[0][player] - tran_out[0][player]
fpl_problem += tran_in[0][player] + tran_out[0][player] <= 1
fpl_problem += cpt[0][player] <= lineup[0][player]
# Define constriants for teams for rest of the weeks (week 5 and 6. The code is generic and automatically scales for additional weeks)
for week in range(0,len(weeks)-1):
for player in players:
fpl_problem += lineup[week+1][player] == lineup[week][player] + tran_in[week+1][player] - tran_out[week+1][player]
fpl_problem += tran_in[week+1][player] + tran_out[week+1][player] <= 1
fpl_problem += cpt[week+1][player] <= lineup[week+1][player]
for week in range(0,len(weeks)):
fpl_problem += sum(tran_in[week][player] for player in players) <=11 # trade in players should be <= 11 per week
fpl_problem += sum(lineup[week][player] for player in players) == n_players # Lineup size should be equal to maximum players per week
fpl_problem += sum(cpt[week][player] for player in players) == 1 # There should be only 1 captain per week
fpl_problem += sum([player_cost[player] * lineup[week][player] for player in players]) <= float(max_budget) # Total cost constriant per week
total_transfers = total_transfers + sum(tran_in[week][player] for player in players)
fpl_problem += total_transfers <= transfers_left
您所说的所有事情都是正确的。 :). total_transfers
是一个 expression
。你确实意识到你正在用你的循环构建这个表达式,对吧?您也可以在循环之外通过使用对周的理解来这样做……无论哪种方式都有效。
所以你可以:
- 打印表达式(你得到的。)
- 评估表达式(你想做什么)
- 在约束或 OBJ 中任意使用表达式。 (你也想做什么。)
有时在 pulp
中不清楚如何在不进行任何修改的情况下获取方法和属性,但这里缺少要评估的是 value()
方法。请参阅下面的示例,如果您遇到困难,请回复。
# pulp expression example
from pulp import *
prob = LpProblem('example', LpMinimize)
x1 = LpVariable('x1')
x2 = LpVariable('x2')
z = x1 + x2
print(f'z is a : {type(z)}')
print(f'z evaluates to: {z}') # <-- what you are seeing now
print(f'the value of the expression z is: {z.value()}')
# some goofy constraints...
prob += x1 >= 1
prob += x2 >= 3
prob += z >= 6 # the expression z is legal in a constraint
# the objective
prob += z + x1 # the expression z is legal in the OBJ
prob.solve()
print(f'the value of z after solve is now: {z.value()}')
for v in prob.variables():
print(f'the value of {v.name} is: {v.value()}')
产量:
z is a : <class 'pulp.pulp.LpAffineExpression'>
z evaluates to: x1 + x2
the value of the expression z is: None
... solve data ...
the value of z after solve is now: 6.0
the value of x1 is: 1.0
the value of x2 is: 5.0