1

I have the following example of a small example of a linear program in Rust:

use good_lp::{variables, variable, default_solver, SolverModel, Solution, constraint};

fn main() {
    // Declaring binary variables
    let mut vars = variables!();
    let x = vars.add(variable().binary());
    let y = vars.add(variable().binary());

    // Creating the modell and solving it
    let mut model = vars
        .maximise(2.5 * x - y)
        .using(default_solver);

    model = model.with(constraint!(2 * x + y <= 1)); // Adding Constrain
    model = model.with(constraint!(x + y <= 1)); // Adding Constraint

    let solution = model.solve().unwrap();

    // Printing the result's
    println!("x = {}", solution.value(x));
    println!("y = {}", solution.value(y));
}

I want to allow in the code that the found solution doesn't have to be 100% perfect. For example 99% would be also fine. If you use PuLP_CBC with Python you only need to write a command like "PULP_CBC_CMD(gapRel=0.01)".

I searched in crates.io in the docs of coin_cbc for some similar syntax/notation but I couldn't find anything so far. Did I miss something or much better do you know how to write it?

2 Answers 2

1

Ok I finally could find it in good_lp which is a bit hidden: crates.io

I also added it into my code from above. Here is the reuslt. I added a commend "NEW !!!" on some spots which are new in the code:

use good_lp::{variables, variable, default_solver, SolverModel, Solution, constraint};
use good_lp::solvers::WithMipGap; // NEW !!!

fn main() {
    // Declaring binary variables
    let mut vars = variables!();
    let x = vars.add(variable().binary());
    let y = vars.add(variable().binary());

    // Creating the modell and solving it
    let mut model = vars
        .maximise(2.5 * x + y)
        .using(default_solver)
        .with_mip_gap(0.05) // NEW !!!
        .unwrap();  // NEW !!!

    model = model.with(constraint!(2 * x + y <= 1)); // Adding Constrain
    model = model.with(constraint!(x + y <= 1)); // Adding Constrain

    let solution = model.solve().unwrap();

    // Printing the result's
    println!("x = {}", solution.value(x));
    println!("y = {}", solution.value(y));
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is a with_mip_gap method provided by the lp_solvers feature/crate. Perhaps you can just replace default_solver with default_solver.with_mip_gap(0.05).unwrap()?

2 Comments

That doesn't work. I use good_lp and not lp_solvers. These are two different packages.
Ok, I just translated my program into the syntax of lp_solvers, based on this example docs.rs/crate/lp-solvers/1.0.1 But I noticed that "StrExpression("x - y".to_string())" is different to StrExpression("x-y".to_string()) (gives wrong solution!!) which is in my opinion a very bad and unflexible design especially if you have quite more complex expressions like terms with more than 50 summands. It shouldn't be impoartant if you use Whitespaces or not!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.