Skip to content

Commit

Permalink
Merge pull request #149 from Andful/add-lifetime-solution
Browse files Browse the repository at this point in the history
Add lifetime to solution
  • Loading branch information
mmghannam authored Sep 13, 2024
2 parents 530390a + 0d7090f commit d3507d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,13 @@ impl_ModelWithProblem!(for Model<ProblemCreated>, Model<Solved>, Model<Solving>)
/// A trait for optimization models with a problem created or solved.
pub trait ProblemOrSolving {
/// Creates a new solution initialized to zero.
fn create_sol(&mut self) -> Solution;
fn create_sol(&self) -> Solution;

/// Adds a solution to the model
///
/// # Returns
/// A `Result` indicating whether the solution was added successfully.
fn add_sol(&self, sol: Solution) -> Result<(), SolError>;
fn add_sol(&self, sol: Solution<'_>) -> Result<(), SolError>;

/// Adds a binary variable to the given set partitioning constraint.
///
Expand Down Expand Up @@ -724,7 +724,7 @@ macro_rules! impl_ProblemOrSolving {
$(impl ProblemOrSolving for $t {

/// Creates a new solution initialized to zero.
fn create_sol(&mut self) -> Solution {
fn create_sol(&self) -> Solution {
self.scip
.create_sol()
.expect("Failed to create solution in state ProblemCreated")
Expand Down
4 changes: 3 additions & 1 deletion src/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ impl ScipPtr {
unsafe { ffi::SCIPgetNSols(self.raw) as usize }
}

pub(crate) fn best_sol(&self) -> Solution {
pub(crate) fn best_sol(&self) -> Solution<'_> {
let sol = unsafe { ffi::SCIPgetBestSol(self.raw) };

Solution {
scip_ptr: self.raw,
raw: sol,
pd: Default::default(),
}
}

Expand Down Expand Up @@ -497,6 +498,7 @@ impl ScipPtr {
Ok(Solution {
scip_ptr: self.raw,
raw: sol,
pd: Default::default(),
})
}

Expand Down
7 changes: 4 additions & 3 deletions src/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use crate::{ffi, scip_call_panic};

/// A wrapper for a SCIP solution.
#[derive(PartialEq, Eq)]
pub struct Solution {
pub struct Solution<'a> {
pub(crate) scip_ptr: *mut ffi::SCIP,
pub(crate) raw: *mut ffi::SCIP_SOL,
pub(crate) pd: std::marker::PhantomData<fn() -> &'a ()>, //covariant
}

impl Solution {
impl Solution<'_> {
/// Returns the objective value of the solution.
pub fn obj_val(&self) -> f64 {
unsafe { ffi::SCIPgetSolOrigObj(self.scip_ptr, self.raw) }
Expand All @@ -28,7 +29,7 @@ impl Solution {
}
}

impl fmt::Debug for Solution {
impl fmt::Debug for Solution<'_> {
/// Formats the solution for debugging purposes.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let obj_val = self.obj_val();
Expand Down

0 comments on commit d3507d1

Please sign in to comment.