Pablo A - 3 months ago 29

Python Question

im working with an LP model in python using

`PuLP`

`CBC`

`#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`

`CPLEX`

`PuLP`

any guidance ?

Thanks!

`CBC`

`PuLP`

`PuLP`

Answer

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*.

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

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

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

Source (Stackoverflow)

Comments