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

Input linear program using matrix format #7

Closed luli-git closed 3 years ago

luli-git commented 3 years ago

Hi,

I was trying to input a linear program using matrix format, i.e., with constraints in the form of Ax = b. I constructed a COO matrix A but cannot multiply it with the variable x. I get the following error "cannot multiply numopt::matrix::coo::CooMat<f64> by good_lp::Variable". Do you have any more complicated examples where the input is a matrix? Below is my code.

fn main() -> Result<(), Box<dyn Error>> {
        let x = arr1(&[1.0]);
        let c = arr1(&[1.0]); 
        let b = arr1(&[10.0]);
        let u = arr1(&[15.0]);
        let l = arr1(&[-15.0]);
        let p = vec![false ];
        let A: CooMat<f64> = CooMat::<f64>::new(
            (1 , 1 ),
            vec![0],
            vec![0],
            vec![1.0]
        );
        variables! {
            vars:
                   a <= x;
        } // variables can also be added dynamically
        let solution = vars.maximise(  &a  )
            .using(default_solver) // multiple solvers available
            .with(constraint!( A * a == 5))
            .solve()?;
        println!("a={}   b={}", solution.value(a), solution.value(b));
        println!("a + b = {}", solution.eval(a + b));
        Ok(())
    }
}

Thank you for your help!

lovasoa commented 3 years ago

Hello and thanks for getting in touch ! Are you sure what you are looking for is a modeler ?

If you don't have meaningful names for your variables and functions to apply to them, then using good_lp is kind of an useless overhead for you. You can still use it if you want (just create n variables and add the constraint A[i] * var_i == 5 for each one), but you would probably be better off just using the underlying solvers that good_lp uses directly.

The goal of an lp modeler is to create this matrix for you without you having to worry about anything but the variables that make sense in your problem and the relationship between them. If you already have the matrix, you can skip the modeler altogether, and use a solver (such as highs or cbc) directly.

lovasoa commented 3 years ago

If you don't want a modeler, but still want an abstraction over multiple solvers, you can have a look at https://github.com/rust-or/lp-solvers