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

Making CoinCbcProblem cloneable... #26

Closed Thell closed 9 months ago

Thell commented 1 year ago

I was wondering if you'd be willing to have CoinCbcProblem use the Clone attribute.

image

The model is cloneable in coin_cbc and allows for the usage of an unsolved model to be used within a loop...

    let mut ref_model = SubsetModel::new(items, item_reqs, weights, state_1_values, state_2_values);
    ref_model.problem.set_parameter("log", "0");

    for storage in 0..state_1_sum_lb {
        for lodging in 0..state_2_sum_lb {
            let model = ref_model.clone();
            // The state sum LB constraints.
            let state_1_sum_lb_expr: Expression = (storage as f64).into();
            let state_2_sum_lb_expr: Expression = (lodging as f64).into();
            let state_1_sum_lb_constraint =
                constraint!(state_1_sum_lb_expr <= model.state_1_sum_expr);
            let state_2_sum_lb_constraint =
                constraint!(state_2_sum_lb_expr <= model.state_2_sum_expr);

            let subset_solution = model
                .problem
                .with(state_1_sum_lb_constraint)
                .with(state_2_sum_lb_constraint)
                .solve()
                .unwrap();
...

The performance is pretty much identical on my small test sets.

all-in-loop: TotalSeconds      : 70.7896676
  ref_model: TotalSeconds      : 70.5151181

but I imagine that on models with many more variables might benefit performance wise.

Perhaps there is already a better way to handle this kind of situation and I just missed it?