2017-05-30 66 views
-2

我跟着python PLUP的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

對我來說,就像你告訴它最大限度地減少利潤,它已經成功地將利潤最小化。 – user2357112

回答

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 
+0

Gosh ...我啓用了自動完成功能,沒有注意到...謝謝! –