Search code examples
pythoncplexpulp

How to prevent infeasible error with pulp and python?


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':
    ...

Solution

  • 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