Skip to content

feat: Use NonNull for SCIP_SOL pointer in Solution struct #258

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 11 additions & 7 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{BranchRule, HeurTiming, Heuristic, Pricer};
use crate::{Conshdlr, Diver};
use crate::{Row, Separator, ffi, scip_call_panic};
use scip_sys::SCIP;
use std::ptr::NonNull;
use std::rc::Rc;

/// Represents an optimization model.
Expand Down Expand Up @@ -423,8 +424,9 @@ impl Model<Solving> {
pub fn create_sol(&self) -> Solution {
let sol_ptr = self
.scip
.create_sol(false)
.create_sol(true)
.expect("Failed to create solution in state ProblemCreated");
let sol_ptr = NonNull::new(sol_ptr).expect("SCIP returned null solution pointer");
Solution {
scip_ptr: self.scip.clone(),
raw: sol_ptr,
Expand Down Expand Up @@ -1043,6 +1045,7 @@ impl<S: ModelStageProblemOrSolving> ProblemOrSolving for Model<S> {
.scip
.create_sol(true)
.expect("Failed to create solution in state ProblemCreated");
let sol_ptr = NonNull::new(sol_ptr).expect("SCIP returned null solution pointer");
Solution {
scip_ptr: self.scip.clone(),
raw: sol_ptr,
Expand Down Expand Up @@ -1383,7 +1386,8 @@ impl<S: ModelStageWithSolutions> WithSolutions for Model<S> {
if self.n_sols() > 0 {
let sol = Solution {
scip_ptr: self.scip.clone(),
raw: self.scip.best_sol().unwrap(),
raw: std::ptr::NonNull::new(self.scip.best_sol().unwrap())
.expect("SCIP returned null pointer for best solution"),
};
Some(sol)
} else {
Expand All @@ -1399,17 +1403,17 @@ impl<S: ModelStageWithSolutions> WithSolutions for Model<S> {
/// Returns a vector containing all solutions stored in the solution storage.
fn get_sols(&self) -> Option<Vec<Solution>> {
if self.n_sols() > 0 {
let scip_sols = self
.scip
self.scip
.get_sols()
.unwrap()
.into_iter()
.map(|x| Solution {
raw: x,
raw: std::ptr::NonNull::new(x)
.expect("SCIP returned null pointer for solution"),
scip_ptr: self.scip.clone(),
})
.collect();
Some(scip_sols)
.collect::<Vec<_>>()
.into()
} else {
None
}
Expand Down
17 changes: 10 additions & 7 deletions src/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ impl ScipPtr {
assert!(!sol.is_null());

let solution = Solution {
raw: sol,
raw: std::ptr::NonNull::new(sol).expect("sol is null"),
scip_ptr: scip_ptr.clone(),
};

Expand Down Expand Up @@ -1472,26 +1472,29 @@ impl ScipPtr {
Ok(node_ptr)
}

pub(crate) fn add_sol(&self, mut sol: Solution) -> Result<bool, Retcode> {
pub(crate) fn add_sol(&self, sol: Solution) -> Result<bool, Retcode> {
let mut feasible = 0;
assert!(!sol.raw.is_null());
let is_orig = unsafe { ffi::SCIPsolIsOriginal(sol.raw) } == 1;
let is_orig = unsafe { ffi::SCIPsolIsOriginal(sol.raw.as_ptr()) } == 1;
if is_orig {
scip_call!(ffi::SCIPcheckSolOrig(
self.raw,
sol.raw,
sol.raw.as_ptr(),
&mut feasible,
false.into(),
true.into(),
));
if feasible == 1 {
scip_call!(ffi::SCIPaddSolFree(self.raw, &mut sol.raw, &mut feasible));
scip_call!(ffi::SCIPaddSolFree(
self.raw,
&mut sol.raw.as_ptr(),
&mut feasible
));
}
return Ok(feasible != 0);
} else {
scip_call!(ffi::SCIPtrySol(
self.raw,
sol.raw,
sol.raw.as_ptr(),
false.into(),
true.into(),
true.into(),
Expand Down
17 changes: 9 additions & 8 deletions src/solution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use std::ptr::NonNull;
use std::rc::Rc;

use crate::scip::ScipPtr;
Expand All @@ -8,31 +9,31 @@ use crate::{ffi, scip_call_panic};
/// A wrapper for a SCIP solution.
#[derive(Clone)]
pub struct Solution {
pub(crate) raw: *mut ffi::SCIP_SOL,
pub(crate) scip_ptr: Rc<ScipPtr>,
pub(crate) raw: NonNull<ffi::SCIP_SOL>,
}

impl Solution {
/// Returns a raw pointer to the underlying `ffi::SCIP_SOL` struct.
pub fn inner(&self) -> *mut ffi::SCIP_SOL {
self.raw
self.raw.as_ptr()
}

/// Returns the objective value of the solution.
pub fn obj_val(&self) -> f64 {
unsafe { ffi::SCIPgetSolOrigObj(self.scip_ptr.raw, self.raw) }
unsafe { ffi::SCIPgetSolOrigObj(self.scip_ptr.raw, self.raw.as_ptr()) }
}

/// Returns the value of a variable in the solution.
pub fn val(&self, var: &Variable) -> f64 {
unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw, var.raw) }
unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw.as_ptr(), var.raw) }
}

/// Sets the value of a variable in the solution.
pub fn set_val(&self, var: &Variable, val: f64) {
scip_call_panic!(ffi::SCIPsetSolVal(
self.scip_ptr.raw,
self.raw,
self.raw.as_ptr(),
var.raw,
val
));
Expand All @@ -45,7 +46,7 @@ impl Solution {
let mut map = std::collections::HashMap::new();
for i in 0..n_vars {
let var = unsafe { *vars.offset(i as isize) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw, var) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw.as_ptr(), var) };
let eps = unsafe { ffi::SCIPepsilon(self.scip_ptr.raw) };
if val > eps || val < -eps {
let name_ptr = unsafe { ffi::SCIPvarGetName(var) };
Expand All @@ -64,7 +65,7 @@ impl Solution {
let mut map = std::collections::HashMap::new();
for i in 0..n_vars {
let var = unsafe { *vars.offset(i as isize) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw, var) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw.as_ptr(), var) };
let eps = unsafe { ffi::SCIPepsilon(self.scip_ptr.raw) };
if val > eps || val < -eps {
let id = unsafe { ffi::SCIPvarGetProbindex(var) };
Expand All @@ -84,7 +85,7 @@ impl fmt::Debug for Solution {
let n_vars = unsafe { ffi::SCIPgetNOrigVars(self.scip_ptr.raw) };
for i in 0..n_vars {
let var = unsafe { *vars.offset(i as isize) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw, var) };
let val = unsafe { ffi::SCIPgetSolVal(self.scip_ptr.raw, self.raw.as_ptr(), var) };
let eps = unsafe { ffi::SCIPepsilon(self.scip_ptr.raw) };
if val > eps || val < -eps {
let name_ptr = unsafe { ffi::SCIPvarGetName(var) };
Expand Down
Loading