python PLUP出乎意料的结果

时间:2017-05-30 22:48:09

标签: python python-3.x optimization

我关注了tutorial的python PLUP,但得到了不同的结果。 而不是

Optimal weekly number of soldiers to produce: 20
Optimal weekly number of trains to produce: 60

我得到了:

Optimal weekly number of soldiers to produce: 0
Optimal weekly number of trains to produce: 0

但是休息是一样的......如果你想知道,那就是代码(几乎是复制和粘贴):

prob = pulp.LpProblem('Giapetto', pulp.LpMinimize)
soldiers = pulp.LpVariable('soldiers', lowBound=0, cat='Integer')
trains = pulp.LpVariable('trains', lowBound=0, cat='Integer')

raw_material_costs = 10 * soldiers + 9 * trains
variable_costs = 14 * soldiers + 10 * trains
revenues = 27 * soldiers + 21 * trains
profit = revenues - (raw_material_costs + variable_costs)
prob += profit

carpentry_hours = soldiers + trains
prob += (carpentry_hours <= 80)

finishing_hours = 2*soldiers + trains
prob += (finishing_hours <= 100)

prob += (soldiers <= 40)
print(prob)
optimization_result = prob.solve()
assert optimization_result == pulp.LpStatusOptimal

for var in (soldiers, trains):
    print('Optimal weekly number of {} to produce: {:1.0f}'.format(var.name, var.value()))

有什么不对吗?

1 个答案:

答案 0 :(得分:1)

它正在做它应该做的事情。

你修改了最重要的东西,目标:

prob = pulp.LpProblem('Giapetto', pulp.LpMinimize)

所以你想最小化目标,即:

profit = revenues - (raw_material_costs + variable_costs)

由于两者都只依赖于两个非负的变量,因此最小值为0.

当两个变量都是非负时,你可以改变你的目标,看它不能低于0:

obj = 27 * soldiers + 21 trains - 24 soldiers - 19 trains
    = 3 * soldiers + 2 trains