Skip to content

Commit

Permalink
test: cover setting initial solutions for cbc
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf committed Dec 13, 2024
1 parent 630baf1 commit 5b7963c
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion tests/solver_scaling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use float_eq::assert_float_eq;
use good_lp::{constraint, default_solver, variable, variables, Expression, Solution, SolverModel};
use good_lp::{
constraint, default_solver, variable, variables, Expression, Solution, SolverModel,
WithInitialSolution,
};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
const BIG_NUM: usize = 1000; // <- Set this higher to test how good_lp and the solvers scale
Expand All @@ -21,6 +24,41 @@ fn solve_large_problem() {
}
}

#[test]
fn solve_problem_with_initial_solution() {
// Solve problem once
let mut vars = variables!();
let min = -((BIG_NUM / 2) as f64);
let max = (BIG_NUM / 2 - 1) as f64;
let v = vars.add_vector(variable().min(min).max(max), BIG_NUM);
let objective: Expression = v.iter().sum();
let mut pb = vars.maximise(objective).using(default_solver);
for vs in v.windows(2) {
pb = pb.with(constraint!(vs[0] + 1 <= vs[1]));
}
let sol = pb.solve().unwrap();
for (i, var) in v.iter().enumerate() {
assert_float_eq!(sol.value(*var), min + i as f64, abs <= 1e-8);
}
// Recreate problem and solve with initial solution
let mut vars = variables!();
let min = -((BIG_NUM / 2) as f64);
let max = (BIG_NUM / 2 - 1) as f64;
let v = vars.add_vector(variable().min(min).max(max), BIG_NUM);
let objective: Expression = v.iter().sum();
let mut pb = vars
.maximise(objective)
.using(default_solver)
.set_initial_solution(&sol);
for vs in v.windows(2) {
pb = pb.with(constraint!(vs[0] + 1 <= vs[1]));
}
let sol = pb.solve().expect("problem has no solution");
for (i, var) in v.iter().enumerate() {
assert_float_eq!(sol.value(*var), min + i as f64, abs <= 1e-8);
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn add_10_000_constraints() {
Expand Down

0 comments on commit 5b7963c

Please sign in to comment.