rust-or / good_lp

Linear Programming for Rust, with a user-friendly API. This crate allows modeling LP problems, and lets you solve them with various solvers.
https://crates.io/crates/good_lp
MIT License
216 stars 35 forks source link

Add WithMipGap trait #38

Closed mfuhr closed 10 months ago

mfuhr commented 10 months ago

Add a WithMipGap trait and implement it for coin_cbc, highs, and lp_solvers. This trait mimics the trait of the same name that we recently added to the lp-solvers crate.

Implementing WithMipGap for lpsolve and scip appears to be non-trivial so I haven't done those, and minilp doesn't support integer variables.

I created a macro for generating tests of the WithMipGap trait. This is my first experience writing a Rust macro so hopefully it's okay. It does work as intended.

lovasoa commented 10 months ago

@mmghannam any idea how hard it would be to implement mip gap in russcip ?

mfuhr commented 10 months ago

I think implementing MIP gap in russcip should be as simple as calling model.set_real_param("limits/gap", gap) but set_real_param is implemented only when the model is in the Unsolved state and the state changes almost immediately after the scip() function creates it:

let mut model = Model::new()  // state is Unsolved
        .hide_output()  // state is Unsolved
        .include_default_plugins()  // state is PluginsIncluded
        .create_prob("problem") // state is ProblemCreated
        .set_obj_sense(match to_solve.direction {
            ObjectiveDirection::Maximisation => ObjSense::Maximize,
            ObjectiveDirection::Minimisation => ObjSense::Minimize,
        });  // state is ProblemCreated

A with_mip_gap method might be able to call model.try_new() to get the model back into the Unsolved state so we can call set_real_param but then we'd have to get the model back into the ProblemCreated state before returning. I haven't investigated any farther yet.

lovasoa commented 10 months ago

@mmghannam what do you think? Maybe russcip could allow setting parameters in the ProblemCreated state? Or would that introduce the possibility for an inconsistent state ?

@mfuhr : anyway, I don't see an issue with with merging this PR without russcip support now, and adding it later.

mmghannam commented 10 months ago

@mmghannam what do you think? Maybe russcip could allow setting parameters in the ProblemCreated state? Or would that introduce the possibility for an inconsistent state ?

Setting parameters in ProblemCreated state should be fine as far as I know. I added an issue https://github.com/scipopt/russcip/issues/105. I'm just not sure when I'll have time to work on it.

mfuhr commented 10 months ago

How's the latest?

mfuhr commented 10 months ago

@lovasoa I'd be happy to make any more changes you suggest. Thanks for the help -- I'm pretty new to Rust and to GitHub collaboration so your feedback has been very useful!