Pablo A Pablo A - 1 year ago 256
Python Question

Infeasible solution with redundant constraints - PuLP and COIN-OR

im working with an LP model in python using

. The model has a lot of constraints, and of course many of them are redundant. i will show an example of that.

#import libraries
from pulp import LpVariable, LpProblem, LpMaximize, lpSum, LpConstraint, LpStatus, value

prob = LpProblem("test_model", LpMaximize)
set_pt=[i for i in range(100)] #set of var
var = LpVariable.dicts("var",set_pt,lowBound=0,cat='Continuous')

# The objective function is added to 'prob' first
prob += lpSum([var[i] for i in set_pt]), "f(v)"

for i in set_pt:
prob += LpConstraint(var[i] <= 300000), "max margin "+str(i)
prob += LpConstraint(var[i] <= 30000000000), "ma2 margin "+str(i)

print 'solver begin'

# The status of the solution is printed to the screen
print "Status:", LpStatus[prob.status]

the result of this is:

solver begin
Status: Infeasible

of course in this example both constraints are obviously redundant and in the problem that im solving is a little bit more difficult to see witch of the constraints are redundant.

i don't know if the problem is with the solver (
), so i can use maybe
instead and solve the problem of the redundant constraints, or the problem is
and i need to use another library. Or maybe i need to model the problem to make it redundancy proof.

any guidance ?

Edit: I tried with open solver (in excel) using
and it worked, so i think that must be a problem with the implementation in
, or maybe im doing something wrong or maybe there is not way to add redundant constraint in

Answer Source

I didn't use pulp much, so i can't explain the internals here (which make your case fail), but you are using pulp's constraint-mechanism in a wrong way.

Your approach (fails):

for i in set_pt:
    prob += LpConstraint(var[i] <= 300000), "max margin "+str(i) 
    prob += LpConstraint(var[i] <= 30000000000), "ma2 margin "+str(i) 

Working alternative A (using overloaded operators)

for i in set_pt:
    prob += var[i] <= 300000, "max margin "+str(i)
    prob += var[i] <= 30000000000, "ma2 margin "+str(i)

Working alternative B (explicit use of LpConstraint; needs importing)

for i in set_pt:
    prob += LpConstraint(var[i], LpConstraintLE, 300000), "max margin "+str(i)
    prob += LpConstraint(var[i], LpConstraintLE, 30000000000), "ma2 margin "+str(i)

The latter is more like your initial approach. But your usage doesn't look like something the function expects (see docs)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download