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

feat: add support for setting initial solutions with cbc #71

Merged
merged 14 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub use solvers::scip::scip as default_solver;

pub use solvers::{
solver_name, DualValues, ModelWithSOS1, ResolutionError, Solution, SolutionWithDual, Solver,
SolverModel, StaticSolver, WithMipGap,
SolverModel, StaticSolver, WithInitialSolution, WithMipGap,
};
pub use variable::{variable, ProblemVariables, Variable, VariableDefinition};

Expand Down
47 changes: 46 additions & 1 deletion src/solvers/coin_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::convert::TryInto;

use coin_cbc::{raw::Status, Col, Model, Sense, Solution as CbcSolution};

use crate::solvers::{MipGapError, ModelWithSOS1, WithMipGap};
use crate::solvers::{MipGapError, ModelWithSOS1, WithInitialSolution, WithMipGap};
use crate::variable::{UnsolvedProblem, VariableDefinition};
use crate::{
constraint::ConstraintReference,
Expand Down Expand Up @@ -166,6 +166,16 @@ impl SolverModel for CoinCbcProblem {
}
}

impl WithInitialSolution for CoinCbcProblem {
fn with_initial_solution(mut self, solution: &Vec<(Variable, f64)>) -> Self {
for (var, val) in solution {
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
self.model
.set_col_initial_solution(self.columns[var.index()], *val);
}
self
}
}

/// Unfortunately, the current version of cbc silently ignores
/// sos constraints on continuous variables.
/// See <https://github.com/coin-or/Cbc/issues/376>
Expand Down Expand Up @@ -218,3 +228,38 @@ impl WithMipGap for CoinCbcProblem {
}
}
}

#[cfg(test)]
mod tests {
use std::iter::FromIterator;

use crate::{
solvers::coin_cbc::CoinCbcSolution, variables, Solution, SolverModel, WithInitialSolution,
};
use float_eq::assert_float_eq;

#[test]
fn solve_problem_with_initial_solution() {
let limit = 3.0;
// Solve problem once
variables! {
vars:
0.0 <= v <= limit;
};
let pb = vars.maximise(v).using(super::coin_cbc);
let sol = pb.solve().unwrap();
assert_float_eq!(sol.value(v), limit, abs <= 1e-8);
// Recreate problem and solve with initial solution
let initial_solution = vec![(v, sol.value(v))];
variables! {
vars:
0.0 <= v <= limit;
};
let pb = vars
.maximise(v)
.using(super::coin_cbc)
.with_initial_solution(&initial_solution);
let sol = pb.solve().unwrap();
assert_float_eq!(sol.value(v), limit, abs <= 1e-8);
}
}
6 changes: 6 additions & 0 deletions src/solvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ pub trait SolverModel {
fn name() -> &'static str;
}

/// A solver that can take an initial solution to a problem before solving it
pub trait WithInitialSolution {
/// Sets the initial solution to the problem
fn with_initial_solution(self, solution: &Vec<(Variable, f64)>) -> Self;
}
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved

/// A problem solution
pub trait Solution {
/// Get the optimal value of a variable of the problem
Expand Down
Loading