【问题标题】:Unexpected result with python PLUPpython PLUP的意外结果
【发布时间】:2017-05-31 06:58:12
【问题描述】:

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

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()))

有什么问题吗?

【问题讨论】:

  • 在我看来你告诉它要最小化利润,它已经成功地最小化了利润。

标签: python python-3.x optimization


【解决方案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

【讨论】:

  • 天哪...我启用了自动完成功能,没有注意到...谢谢!
猜你喜欢
  • 2018-12-28
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多