Skip to content

Commit

Permalink
Add minimal model fn, extra test for separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Dec 5, 2024
1 parent d9cbda0 commit e7653d2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,14 @@ impl HasScipPtr for ModelSolving {
}
}

/// Creates a minimal `Model` instance and sets off a lot of SCIP plugins, useful for writing tests.
pub fn minimal_model() -> Model<ProblemCreated> {
Model::default()
.set_presolving(ParamSetting::Off)
.set_heuristics(ParamSetting::Off)
.set_separating(ParamSetting::Off)
}

impl<T> Model<T> {
/// Returns the status of the optimization model.
pub fn status(&self) -> Status {
Expand Down
54 changes: 53 additions & 1 deletion src/separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl From<SeparationResult> for SCIP_Result {
#[cfg(test)]
mod tests {
use super::*;
use crate::Model;
use crate::{minimal_model, Model, ModelSolving, ModelWithProblem, ObjSense, ParamSetting, ProblemOrSolving, Unsolved, VarType};

struct NotRunningSeparator;

Expand Down Expand Up @@ -96,4 +96,56 @@ mod tests {
)
.solve();
}


struct ConsAddingSeparator {
model: ModelSolving,
}


impl Separator for ConsAddingSeparator {
fn execute_lp(&mut self) -> SeparationResult {
// adds a row representing the sum of all variables >= 1
let vars = self.model.vars();
let varlen = vars.len();

self.model.add_cons(
vars,
&vec![1.0; varlen],
5.0,
5.0,
"cons_added",
);
SeparationResult::ConsAdded
}
}


#[test]
fn cons_adding_separator() {
let mut model = minimal_model()
.hide_output()
.set_obj_sense(ObjSense::Maximize);

let x= model.add_var(0.0, 1.0, 1.0, "x", VarType::Binary);
let y= model.add_var(0.0, 1.0, 1.0, "y", VarType::Binary);

model.add_cons(vec![x, y], &[1.0, 1.0], 1.0, 1.0, "cons1");

let sep = ConsAddingSeparator { model: model.clone_for_plugins() };

model
.include_separator(
"ConsAddingSeparator",
"",
1000000,
1,
1.0,
false,
false,
Box::new(sep),
)
.solve();
}
}

0 comments on commit e7653d2

Please sign in to comment.