Skip to content

Commit

Permalink
feat: add support for setting initial solutions with SCIP (#74)
Browse files Browse the repository at this point in the history
* feat: support setting initial solutions for SCIP

* test: cover initial solutions for SCIP

* fix: add created solution to model

* fix: check result
  • Loading branch information
KnorpelSenf authored Dec 30, 2024
1 parent 1227b3e commit 8b55ac4
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/solvers/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::variable::{UnsolvedProblem, VariableDefinition};
use crate::{
constraint::ConstraintReference,
solvers::{ObjectiveDirection, ResolutionError, Solution, SolverModel},
CardinalityConstraintSolver,
CardinalityConstraintSolver, WithInitialSolution,
};
use crate::{Constraint, Variable};

Expand Down Expand Up @@ -154,6 +154,17 @@ impl SolverModel for SCIPProblem {
}
}

impl WithInitialSolution for SCIPProblem {
fn with_initial_solution(self, solution: impl IntoIterator<Item = (Variable, f64)>) -> Self {
let sol = self.model.create_sol();
for (var, val) in solution {
sol.set_val(Rc::clone(&self.id_for_var[&var]), val);
}
self.model.add_sol(sol).expect("could not set solution");
self
}
}

/// A wrapper to a solved SCIP problem
pub struct SCIPSolved {
solved_problem: Model<Solved>,
Expand All @@ -175,6 +186,7 @@ impl Solution for SCIPSolved {
mod tests {
use crate::{
constraint, variable, variables, CardinalityConstraintSolver, Solution, SolverModel,
WithInitialSolution,
};

use super::scip;
Expand All @@ -193,6 +205,35 @@ mod tests {
assert_eq!((solution.value(x), solution.value(y)), (0.5, 3.))
}

#[test]
fn can_solve_with_initial_solution() {
// Solve problem initially
let mut vars = variables!();
let x = vars.add(variable().clamp(0, 2));
let y = vars.add(variable().clamp(1, 3));
let solution = vars
.maximise(x + y)
.using(scip)
.with((2 * x + y) << 4)
.solve()
.unwrap();
// Recreate same problem with initial values slightly off
let initial_x = solution.value(x) - 0.1;
let initial_y = solution.value(x) - 1.0;
let mut vars = variables!();
let x = vars.add(variable().clamp(0, 2));
let y = vars.add(variable().clamp(1, 3));
let solution = vars
.maximise(x + y)
.using(scip)
.with((2 * x + y) << 4)
.with_initial_solution([(x, initial_x), (y, initial_y)])
.solve()
.unwrap();

assert_eq!((solution.value(x), solution.value(y)), (0.5, 3.))
}

#[test]
fn can_solve_with_equality() {
let mut vars = variables!();
Expand Down

0 comments on commit 8b55ac4

Please sign in to comment.