Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the optimality gap of a solution #323

Closed
jflowernet opened this issue May 15, 2024 · 6 comments
Closed

Return the optimality gap of a solution #323

jflowernet opened this issue May 15, 2024 · 6 comments

Comments

@jflowernet
Copy link

I can't remember if we already discussed this, so apologies if we did and the answer was 'it's not possible'!

I often solve multiple problems in a loop, saving each solution. When I use the time_limit parameter in add_gurobi_solver() the solution might not solve to the optimality gap specified it it runs out of time before reaching that gap. The optimality gap outputs to the console, but I wonder if it's possible to return the value as well, to avoid having to go through long console outputs to find optimality gaps for all solutions? It seems like it is possible to return a value from Gurobi using MIPGap, though I don't know if there exists something equivalent for other solvers. I guess the bigger problem is, what do you return the gap to since the output from solve() is just the solution as e.g. a raster or sf object. One option would be to return a list with the solution as the first element and the gap as second, but this makes for a messier returned object.

Just a thought, and totally understand that this might not be practical.

@jeffreyhanson
Copy link
Contributor

jeffreyhanson commented May 15, 2024

Hi,

Yeah, you're right, prioritizr doesn't currently report this information and it looks like it might possible for Gurobi at least (using the mipgap attribute, see https://www.gurobi.com/documentation/current/refman/r_grb.html). I'll take a look and see what I can do.

@jeffreyhanson
Copy link
Contributor

Also, in terms of storing this information, prioritizr uses attributes to append additional information to the output of solve() (e.g., objective value, solver status, run time). E.g., see the first example in the solve() docs (https://prioritizr.net/reference/solve.html#ref-examples). So it would be pretty straightforward (in theory) to use this same mechanism to ouput the optimality gap for a solution.

@jflowernet
Copy link
Author

Ah yes, I'd forgotten the attributes that are added.
Thanks for considering this!

@jeffreyhanson
Copy link
Contributor

Just to follow up, I've merged a PR to the main branch that adds this functionality. After generating a solution (e.g., called s), you can use attr(s, "gap") to extract the gap value. Note that if gap informmation is not available (e.g., due to run time limits, or because the solver doesn't support this functionality, currently only Gurobi and HiGHS solvers support this) than this will return a missing value.

@jflowernet
Copy link
Author

Thanks very much for this Jeff! I just gave it a try and works perfectly.

@jeffreyhanson
Copy link
Contributor

Awesome - thanks for trying it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants