Search code examples
pythonconstraintslinear-programmingpulpcoin-or-cbc

Infeasible solution with redundant constraints - PuLP and COIN-OR


im working with an LP model in python using PuLP with CBC. 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)"

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

#solve
prob.writeLP("price_mod2.lp")
print 'solver begin'
prob.solve()

# 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 (CBC), so i can use maybe CPLEX instead and solve the problem of the redundant constraints, or the problem is PuLP and i need to use another library. Or maybe i need to model the problem to make it redundancy proof.

any guidance ? Thanks!

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


Solution

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