From e7653d2cc9da778761fdfd6d865d1507ddd38d36 Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Thu, 5 Dec 2024 18:02:10 +0100 Subject: [PATCH] Add minimal model fn, extra test for separator --- src/model.rs | 8 +++++++ src/separator.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/model.rs b/src/model.rs index 0beee29..c3017ca 100644 --- a/src/model.rs +++ b/src/model.rs @@ -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 { + Model::default() + .set_presolving(ParamSetting::Off) + .set_heuristics(ParamSetting::Off) + .set_separating(ParamSetting::Off) +} + impl Model { /// Returns the status of the optimization model. pub fn status(&self) -> Status { diff --git a/src/separator.rs b/src/separator.rs index 21f25d8..f8500ae 100644 --- a/src/separator.rs +++ b/src/separator.rs @@ -61,7 +61,7 @@ impl From 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; @@ -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(); + } } +