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

add support for SCIP cardinality constraints #39

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ coin_cbc = { version = "0.1", optional = true, default-features = false }
minilp = { version = "0.2", optional = true }
lpsolve = { version = "0.1", optional = true }
highs = { version = "1.5.0", optional = true }
russcip = { version = "0.2.4", optional = true }
russcip = { version = "0.2.6", optional = true }
lp-solvers = { version = "1.0.0", features = ["cplex"], optional = true }
fnv = "1.0.5"

Expand Down
7 changes: 7 additions & 0 deletions src/cardinality_constraint_solver_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::{Variable, constraint::ConstraintReference};

/// A trait for solvers that support cardinality constraints
pub trait CardinalityConstraintSolver {
/// Add cardinality constraint. Constrains the number of non-zero variables from `vars` to at most `rhs`.
fn add_cardinality_constraint(&mut self, vars: &[Variable], rhs: usize) -> ConstraintReference;
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
//!

pub use affine_expression_trait::IntoAffineExpression;
pub use cardinality_constraint_solver_trait::CardinalityConstraintSolver;
pub use constraint::Constraint;
pub use expression::Expression;
#[cfg_attr(docsrs, doc(cfg(feature = "minilp")))]
Expand Down Expand Up @@ -149,6 +150,7 @@ mod expression;
#[macro_use]
pub mod variable;
mod affine_expression_trait;
mod cardinality_constraint_solver_trait;
pub mod constraint;
pub mod solvers;
mod variables_macro;
37 changes: 36 additions & 1 deletion src/solvers/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use russcip::WithSolutions;

use crate::variable::{UnsolvedProblem, VariableDefinition};
use crate::{
CardinalityConstraintSolver,
constraint::ConstraintReference,
solvers::{ObjectiveDirection, ResolutionError, Solution, SolverModel},
};
Expand Down Expand Up @@ -83,6 +84,24 @@ impl SCIPProblem {
}
}

impl CardinalityConstraintSolver for SCIPProblem {
/// Add cardinality constraint. Constrains the number of non-zero variables to at most `rhs`.
fn add_cardinality_constraint(&mut self, vars: &[Variable], rhs: usize) -> ConstraintReference {
let scip_vars = vars.iter()
.map(|v| Rc::clone(&self.id_for_var[v]))
.collect::<Vec<_>>();

let index = self.model.n_conss() + 1;
self.model.add_cons_cardinality(
scip_vars,
rhs,
format!("cardinality{}", index).as_str(),
);

ConstraintReference { index }
}
}

impl SolverModel for SCIPProblem {
type Solution = SCIPSolved;
type Error = ResolutionError;
Expand Down Expand Up @@ -162,7 +181,7 @@ impl Solution for SCIPSolved {

#[cfg(test)]
mod tests {
use crate::{constraint, variable, variables, Solution, SolverModel};
use crate::{constraint, variable, variables, Solution, SolverModel, CardinalityConstraintSolver};

use super::scip;

Expand Down Expand Up @@ -194,4 +213,20 @@ mod tests {
.unwrap();
assert_eq!((solution.value(x), solution.value(y)), (1., 2.));
}

#[test]
fn can_solve_cardinality_constraint() {
let mut vars = variables!();
let x = vars.add(variable().clamp(0, 2).integer());
let y = vars.add(variable().clamp(0, 3).integer());
let mut model = vars
.maximise(5.0 * x + 3.0 * y)
.using(scip);
model.add_cardinality_constraint(&[x, y], 1);
let solution = model
.solve()
.unwrap();
assert_eq!((solution.value(x), solution.value(y)), (2., 0.));
}

}
Loading