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

Input linear program using matrix format #7

Closed
luli-git opened this issue May 21, 2021 · 2 comments
Closed

Input linear program using matrix format #7

luli-git opened this issue May 21, 2021 · 2 comments

Comments

@luli-git
Copy link

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
Copy link
Collaborator

lovasoa commented May 21, 2021

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 lovasoa closed this as completed May 21, 2021
@lovasoa
Copy link
Collaborator

lovasoa commented May 21, 2021

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

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

No branches or pull requests

2 participants