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

Creating a variable bounded by real numbers #14

Closed keegit closed 2 years ago

keegit commented 2 years ago

Quick side note: this crate is awesome!

One problem I am running into is that I want to create an f64 variable that lies within the domain of all real numbers. How can I achieve this?

This is what I am currently trying to do, but it does not compile due to From<bool> is not implemented for f64.

use std::error::Error;

use good_lp::{constraint, coin_cbc, SolverModel, variables};

fn main() -> Result<(), Box<dyn Error>> {
    let INF: f32 = f32::INFINITY;
    let NEG_INF: f32 = f32::NEG_INFINITY; 

    variables! {
        vars:
            NEG_INF <= x1 <= INF;
            NEG_INF <= x2 <= INF;
    }
    let _solution = vars.minimise(2 * x1 + 3 * x2)
        .using(coin_cbc)
        .with(constraint!((3 * x1) + (4 * x2) >= 1))
        .solve()?;

    Ok(())
}

Edit: I can specify either a lower or upper bound but not both; I am wondering if this is suitable for this situation.

keegit commented 2 years ago

Use clamp() :)

lovasoa commented 2 years ago

No, don't use clamp. Variables are unbounded by default.

    variables! { problem: x1; x2; }