2

I have an optimization problem and I write a python program to solve it. I used Pulp with the CPLEX solver:

import pulp

prob = LpProblem("myProblem", LpMinimize)
x = pulp.LpVariable.dicts("p", range( K ), 0, 1, pulp.LpContinuous)
prob += pulp.lpSum( x[k] for k in range( K ) )
...
# Rest of the constraints

status = prob.solve( pulp.CPLEX( msg = 0 ) )

I get the error:

  File "C:\Anaconda\lib\site-packages\pulp\solvers.py", line 468, in readsol
    raise PulpSolverError, "Unknown status returned by CPLEX: "+statusString
pulp.solvers.PulpSolverError: Unknown status returned by CPLEX: infeasible

My question is : How can I test if the problem is infeasible or not? I want to prevent this event like if problem is infeasible then return 0.

I tried :

if prob.status == 'infeasible':
    ...

and I tried

if pulp.LpStatusInfeasible == 'infeasible':
    ...
1-approximation
  • 321
  • 1
  • 4
  • 13

2 Answers2

8

Is your 'problem' finding whether a given problem instance is feasible or not, or are you actually interested in the solution if it is feasible. Rather than just trap the error when the model is infeasible, I would examine your problem and try to add some slack variables and penalty costs to give you some more information when the problem would otherwise be infeasible.

So rather than add a hard constraint like

sum(x) <= K

you could try something like

sum(x) <= K + penaltyVar 

and add a term into your objective like 1000000 * penaltyVar so that the solver really doesn't want to use that penalty variable as non-zero.

Adding these slack/penalty variables in various places in your model can help identify where the constraints are too tight and making your model infeasible.

Don't just ignore the answer above though, as it is still valuable to trap the error.

TimChippingtonDerrick
  • 2,042
  • 1
  • 12
  • 13
3

I think you can solve this by caging the statement inside a try-exception clause.

for example:

# ...
try:
    status = prob.solve(pulp.CPLEX(msg = 0))
except PulpSolverError:
    # infeasible
    return 0

return status
kmonsoor
  • 7,600
  • 7
  • 41
  • 55