coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.04k stars 381 forks source link

Model Solution Status for Gurobi Seems Wrong #445

Open ebongo1 opened 3 years ago

ebongo1 commented 3 years ago

Details for the issue

I am running an optimization model and trying to get the status of the solution after it solves. I have just switched to checking the model.sol_status as opposed to model.status since those two things are not the same thing in PuLP. See below dictionaries for reference.

LpStatus: {0: 'Not Solved', 1: 'Optimal', -1: 'Infeasible', -2: 'Unbounded', -3: 'Undefined'}

LpSolution: {0: 'No Solution Found', 1: 'Optimal Solution Found', 2: 'Solution Found', -1: 'No Solution Exists', -2: 'Solution is Unbounded'}

I also noticed in the gurobi_api.py code that these are the Gurobi statuses used to set the various PuLP statuses:

gurobiLpStatus = { GRB.OPTIMAL: constants.LpStatusOptimal =1, GRB.INFEASIBLE: constants.LpStatusInfeasible = -1, GRB.INF_OR_UNBD: constants.LpStatusInfeasible = -1, GRB.UNBOUNDED: constants.LpStatusUnbounded = -2, GRB.ITERATION_LIMIT: constants.LpStatusNotSolved = 0, GRB.NODE_LIMIT: constants.LpStatusNotSolved = 0, GRB.TIME_LIMIT: constants.LpStatusNotSolved = 0, GRB.SOLUTION_LIMIT: constants.LpStatusNotSolved = 0, GRB.INTERRUPTED: constants.LpStatusNotSolved = 0, GRB.NUMERIC: constants.LpStatusNotSolved = 0 }

So the issue I am having is that I set a time limit on the solver to stop after a certain amount of time, and Gurobi has found some solution, however the model.sol_status is reporting -1 which is "No Solution Exists" which I don't think is correct. It should either be "No Solution Found" since it hasn't had enough time to finish solving to find one, or it should be "Solution Found" meaning it found a feasible solution, but just might not be optimal. I attached a screen shot of the command prompt output I am seeing. The Gurobi status for it is equal to 9 which means the time limit was reached (which is more accurate than saying the model is infeasible).

image

Useful extra information

I'm opening this issue because:

I'm using PuLP on:

I'm using python version:

I installed PuLP via:

I have also:

pchtsp commented 3 years ago

From what I see in your logs, you actually found a solution (10, actually). Can you share the .mps file or .json file with the problem so we can see what went wrong? This page shows how: https://coin-or.github.io/pulp/guides/how_to_export_models.html

Also: What interface to gurobi are you using? Is it the GUROBI or the GUROBI_CMD? In case it's the former, we do not yet correctly identify the integer feasible status see https://github.com/coin-or/pulp/blob/799ee632cea4a297aa7ad9f25a27fb2ccbad1415/pulp/apis/gurobi_api.py#L114

try using GUROBI_CMD?

ebongo1 commented 3 years ago

It's been a few weeks since I moved on from this issue so I don't have the exact .mps file to share with you... and I am using GUROBI. I just tried to switch it to GUROBI_CMD and it gave me an error (attached picture of the error). And it would be really helpful to have that status available in the future. image

CMueller12 commented 1 year ago

Hi, I have the same problem and I have not find a solution to this problem until now. I use gurobi in pulp and optimize a problemn with a rolling horizon method. The problem is a huge one, so I set a gap and a timelimit. Every time the timelimit is hit (gurobi status=9), pulp can not retrieve a solution. Hence, the values of the variables are "None".

CMueller12 commented 1 year ago

Hi, I solve my problem by modifying gurobi_api.py. Instead of only "GRB.TIME_LIMIT: constants.LpStatusNotSolved" I changed it to two rows with " GRB.TIME_LIMIT and model.SolCount>0: constants.LpStatusOptimal, GRB.TIME_LIMIT and model.SolCount==0: constants.LpStatusNotSolved,"