Skip to content

Commit

Permalink
Separate solution methods to its own trait (#98)
Browse files Browse the repository at this point in the history
* Solving state, represents methods accessible from plugin implementations

* Separate solution methods to its own trait
  • Loading branch information
mmghannam authored Jul 27, 2023
1 parent a331abd commit 248aebf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use russcip::variable::VarType;
use russcip::retcode::Retcode;
use crate::russcip::model::ModelWithProblem;
use crate::russcip::model::ProblemOrSolving;
use crate::russcip::WithSolutions;

fn main() {
// Create model
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! use russcip::variable::VarType;
//! use crate::russcip::model::ProblemOrSolving;
//! use crate::russcip::model::ModelWithProblem;
//!
//! use crate::russcip::WithSolutions;
//!
//! // Create model
//! let mut model = Model::new()
Expand Down
47 changes: 33 additions & 14 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,20 +451,6 @@ impl Model<Solving> {
}

impl Model<Solved> {
/// Returns the best solution for the optimization model, if one exists.
pub fn best_sol(&self) -> Option<Solution> {
if self.n_sols() > 0 {
Some(self.scip.best_sol())
} else {
None
}
}

/// Returns the number of solutions found by the optimization model.
pub fn n_sols(&self) -> usize {
self.scip.n_sols()
}

/// Returns the objective value of the best solution found by the optimization model.
pub fn obj_val(&self) -> f64 {
self.scip.obj_val()
Expand Down Expand Up @@ -915,6 +901,39 @@ macro_rules! impl_ProblemOrSolving {

impl_ProblemOrSolving!(for Model<ProblemCreated>, Model<Solving>);

/// A trait for optimization models with any state that might have solutions.
pub trait WithSolutions {
/// Returns the best solution for the optimization model, if one exists.
fn best_sol(&self) -> Option<Solution>;

/// Returns the number of solutions found by the optimization model.
fn n_sols(&self) -> usize;
}

macro_rules! impl_WithSolutions {
(for $($t:ty),+) => {
$(impl WithSolutions for $t {

/// Returns the best solution for the optimization model, if one exists.
fn best_sol(&self) -> Option<Solution> {
if self.n_sols() > 0 {
Some(self.scip.best_sol())
} else {
None
}
}

/// Returns the number of solutions found by the optimization model.
fn n_sols(&self) -> usize {
self.scip.n_sols()
}

})*
}
}

impl_WithSolutions!(for Model<Solved>, Model<Solving>, Model<ProblemCreated>);

impl<T> Model<T> {
/// Returns a pointer to the underlying SCIP instance.
///
Expand Down

0 comments on commit 248aebf

Please sign in to comment.