简体   繁体   中英

Solve Python Pulp without variables

I have a python pulp linear program which is minimizing costs. In the degenerate case where there are no ways to reduce costs I would like it to return the fixed cost. However pulp seems to add a __dummy variable in the case of no variables, which has a value of None. I have added a minimal working example below.

from pulp import *
model = LpProblem("Degenerate_Model",LpMinimize)

fixed_cost = 10
model += fixed_cost
print(model) #Prints MINIMIZE 10 
model.solve()
print(model.objective) #prints 0*__dummy + 10
print(value(model.objective)) #returns None. Desired output is 10

My desired output in the above example would be to return 10. Any help is greatly appreciated

You're absolutely right. This happens because __dummy has a varValue of None , while you didn't expect it to be included at all. I would file a bug report to ask them to exclude dummy variables from LpAffineExpression.value() - since pulp.value(model.objective) is a shortcut for model.objective.value() .

For now, I'd use the following workaround : model.objective.valueOrDefault() .

You could declare the fixed cost as a variable with a fixed value.

>>> model = LpProblem("Degenerate_Model",LpMinimize)
>>> fixed_cost = LpVariable('fixed cost', lowBound=10, upBound=10)
>>> model += fixed_cost
>>> model.solve()
>>> print(value(model.objective))
10.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM