Reputation: 5
PuLP solver doesnt yield the optimal result, while it was possible. Is it ill-defined problem or is there a problem in the library or constraints?
Normally it was giving optimal for most of the constraints possibility, but its not for this specific case.
from pulp import *
prob = LpProblem("minimization", LpMinimize)
#introducing vars
a=LpVariable("a", 0, None)
b=LpVariable("b", 0, None)
c=LpVariable("c", 0, None)
d=LpVariable("d", 0, None)
e=LpVariable("e", 0, None)
#introducing main obj and constraints
#basically addition of all vars should be minimized to 1.
prob+=a+b+c+d+e>=1,"main objective"
prob+=a<=0.3,"a const"
prob+=b<=0.3,"b const"
prob+=c<=0.3,"c const"
prob+=d<=0.3,"d const" #can change to 0
prob+=e==0.1,"e const"
"""
for this const.
the res is:
a=0.3
b=0.3
c=0.3
d=0.3
e=0.1
a+b+c+d+e=1.3
if you change prob+=d<=0.3 to prob+=d<=0
then:
a=0.3
b=0.3
c=0.3
d=0.0
e=0.1
a+b+c+d+e=1
"""
#solves the problem
status = prob.solve()
#checks if optimal or infeasible
print("status:",LpStatus[prob.status])
#shows all the vars
for variable in prob.variables():
print("{} = {}".format(variable.name, variable.varValue))
for this const.
the res is:
a=0.3
b=0.3
c=0.3
d=0.3
e=0.1
a+b+c+d+e=1.3
It should be 1, as it is LpMinimize.
if you change prob+=d<=0.3 to prob+=d<=0
then:
a=0.3
b=0.3
c=0.3
d=0.0
e=0.1
a+b+c+d+e=1
Upvotes: 0
Views: 839
Reputation: 16724
This is a constraint and not an objective:
prob+=a+b+c+d+e>=1,"main objective"
Essentially you don't have an objective so the solver just looks for a feasible solution.
Try
prob+=a+b+c+d+e,"main objective"
prob+=a+b+c+d+e>=1,"constraint"
Now you are both minimizing a+b+c+d+e
as well as having a constraint on this.
Conclusion: I think PuLP is correct here.
Upvotes: 3