Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub mod builder;

/// Contains the `Conshdlr` trait used to define custom constraint handlers.
pub mod conshdlr;
mod probing;

pub use conshdlr::*;

pub use row::*;
Expand Down
18 changes: 18 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::constraint::Constraint;
use crate::eventhdlr::Eventhdlr;
use crate::node::Node;
use crate::param::ScipParameter;
use crate::probing::Prober;
use crate::retcode::Retcode;
use crate::scip::ScipPtr;
use crate::solution::{SolError, Solution};
Expand Down Expand Up @@ -609,6 +610,23 @@ impl Model<Solving> {
pub fn current_val(&self, var: &Variable) -> f64 {
unsafe { ffi::SCIPgetSolVal(self.scip_ptr(), std::ptr::null_mut(), var.inner()) }
}

/// Starts probing at the current node.
///
/// # Returns
/// A `Prober` instance that can be used to access methods allowed only in probing mode.
pub fn start_probing(&mut self) -> Prober {
let scip = self.scip.clone();

unsafe { ffi::SCIPstartProbing(scip.raw) };

Prober { scip }
}

/// Returns the objective value of the current LP relaxation.
pub fn lp_obj_val(&self) -> f64 {
unsafe { ffi::SCIPgetLPObjval(self.scip.raw) }
}
}

impl Model<Solved> {
Expand Down
247 changes: 247 additions & 0 deletions src/probing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
use crate::scip::ScipPtr;
use crate::{ffi, Retcode, Variable};
use std::rc::Rc;

/// Struct giving access to methods allowed in probing mode
pub struct Prober {
pub(crate) scip: Rc<ScipPtr>,
}

impl Prober {
/// Creates a new probing (sub-)node, whose changes can be undone by backtracking to a higher node
/// in the probing path with a call to the `backtrack()` method.
pub fn new_node(&mut self) {
unsafe { ffi::SCIPnewProbingNode(self.scip.raw) };
}

/// Returns the current probing depth
pub fn depth(&self) -> usize {
unsafe { ffi::SCIPgetProbingDepth(self.scip.raw) }
.try_into()
.expect("Invalid depth value")
}

/// Undoes all changes to the problem applied in probing up to the given probing depth;
/// the changes of the probing node of the given probing depth are the last ones that remain active;
/// changes that were applied before calling `new_node()` cannot be undone
pub fn backtrack(&mut self, depth: usize) {
assert!(
depth < self.depth(),
"Probing depth must be less than the current probing depth."
);
unsafe { ffi::SCIPbacktrackProbing(self.scip.raw, depth.try_into().unwrap()) };
}

/// Changes the lower bound of a variable in the current probing node
pub fn chg_var_lb(&mut self, var: &Variable, new_bound: f64) {
unsafe { ffi::SCIPchgVarLbProbing(self.scip.raw, var.inner(), new_bound) };
}

/// Changes the upper bound of a variable in the current probing node
pub fn chg_var_ub(&mut self, var: &Variable, new_bound: f64) {
unsafe { ffi::SCIPchgVarUbProbing(self.scip.raw, var.inner(), new_bound) };
}

/// Retrieves the objective value of a variable in the current probing node
pub fn var_obj(&self, var: &Variable) -> f64 {
unsafe { ffi::SCIPgetVarObjProbing(self.scip.raw, var.inner()) }
}

/// Fixes a variable to a value in the current probing node
pub fn fix_var(&mut self, var: &Variable, value: f64) {
unsafe { ffi::SCIPfixVarProbing(self.scip.raw, var.inner(), value) };
}

/// Changes the objective value of a variable in the current probing node
pub fn chg_var_obj(&mut self, var: &Variable, new_obj: f64) {
unsafe { ffi::SCIPchgVarObjProbing(self.scip.raw, var.inner(), new_obj) };
}

/// Returns whether the probing subproblem objective function has been changed
pub fn is_obj_changed(&self) -> bool {
unsafe { ffi::SCIPisObjChangedProbing(self.scip.raw) != 0 }
}

/// Applies domain propagation on the probing subproblem; the propagated domains of the variables
/// can be accessed with the usual bound accessing calls to `var.lb_local()` and `var.ub_local()`
///
/// # Arguments
/// - `max_rounds`: the maximum number of rounds to be performed, or `None` for no limit
///
/// # Returns
/// A tuple (`cutoff`, `nreductions_found`)
/// - `cutoff`: whether a cutoff was detected
/// - `nreductions_found`: the number of reductions found
pub fn propagate(&mut self, max_rounds: Option<usize>) -> (bool, usize) {
let mut cutoff = 0;
let mut nreductions_found = 0;
let mut r = -1;
if let Some(rounds) = max_rounds {
r = rounds.try_into().unwrap();
}
unsafe {
ffi::SCIPpropagateProbing(self.scip.raw, r, &mut cutoff, &mut nreductions_found);
}

(cutoff != 0, nreductions_found.try_into().unwrap())
}

/// Applies domain propagation on the probing subproblem; only propagations of the binary variables
/// fixed at the current probing node that are triggered by the implication graph and the clique
/// table are applied; the propagated domains of the variables can be accessed with the usual
/// bound accessing calls to `var.lb_local()` and `var.ub_local()`
///
/// # Returns
/// - `cutoff`: whether a cutoff was detected
pub fn propagate_implications(&mut self) -> bool {
let mut cutoff = 0;
unsafe {
ffi::SCIPpropagateProbingImplications(self.scip.raw, &mut cutoff);
}

cutoff != 0
}

/// Solves the probing subproblem; the solution can be accessed with the `model.current_val()` method
///
/// # Arguments
/// - `iteration_limit`: the maximum number of iterations to be performed, or `None` for no limit
///
/// # Returns
/// - `cutoff`: whether a cutoff was detected
pub fn solve_lp(&mut self, iteration_limit: Option<usize>) -> Result<bool, Retcode> {
if !self.scip.is_lp_constructed() {
self.scip.construct_lp()?;
}

let mut limit = -1;
if let Some(iterations) = iteration_limit {
limit = iterations.try_into().unwrap();
}
let mut cutoff = 0;
let mut lperror = 0;
unsafe { ffi::SCIPsolveProbingLP(self.scip.raw, limit, &mut cutoff, &mut lperror) };

if lperror != 0 {
return Err(Retcode::LpError);
}

Ok(cutoff != 0)
}

/// Solves the probing subproblem with pricing; the solution can be accessed
/// with the `model.current_val()` method.
///
/// # Arguments
/// - `max_pricing_rounds`: the maximum number of pricing rounds to be performed, or `None` for no limit
///
/// # Returns
/// - `cutoff`: whether a cutoff was detected
pub fn solve_lp_with_pricing(
&mut self,
max_pricing_rounds: Option<usize>,
) -> Result<bool, Retcode> {
if !self.scip.is_lp_constructed() {
self.scip.construct_lp()?;
}

let mut rounds = -1;
if let Some(r) = max_pricing_rounds {
rounds = r.try_into().unwrap();
}
let mut cutoff = 0;
let mut lperror = 0;

// set a default for now to communicate the current state, any further needed communication
// can be done by sharing data between plugins
const PRETENDATROOT: u32 = 0;

// enable always for now, to avoid unnecessary complexity
const DISPLAYINFO: u32 = 1;

unsafe {
ffi::SCIPsolveProbingLPWithPricing(
self.scip.raw,
PRETENDATROOT,
DISPLAYINFO,
rounds,
&mut cutoff,
&mut lperror,
)
};

if lperror != 0 {
return Err(Retcode::LpError);
}

Ok(cutoff != 0)
}
}

impl Drop for Prober {
fn drop(&mut self) {
assert_eq!(
unsafe { ffi::SCIPinProbing(self.scip.raw) },
1,
"SCIP is expected to be in probing mode before Prober is dropped."
);
unsafe { ffi::SCIPendProbing(self.scip.raw) };
}
}

#[cfg(test)]
mod tests {
use crate::model::Model;
use crate::prelude::eventhdlr;
use crate::{ffi, Eventhdlr, ModelWithProblem, ParamSetting};
use crate::{Event, EventMask, SCIPEventhdlr, Solving};

#[test]
fn test_prober() {
struct ProbingTester;

impl Eventhdlr for ProbingTester {
fn get_type(&self) -> EventMask {
EventMask::NODE_SOLVED
}

fn execute(
&mut self,
mut model: Model<Solving>,
_eventhdlr: SCIPEventhdlr,
_event: Event,
) {
let mut prober = model.start_probing();
assert!(!prober.is_obj_changed());

let vars = model.vars();
for var in vars {
prober.chg_var_obj(&var, 0.0);
}
assert!(prober.is_obj_changed());

prober.solve_lp(None).unwrap();

assert!(model.lp_obj_val().abs() < 1e-6);

drop(prober);

// have to use unsafe here as the method is not available in the public API
assert_eq!(unsafe { ffi::SCIPinProbing(model.scip_ptr()) }, 0);
}
}

let mut model = Model::new()
.include_default_plugins()
.read_prob("data/test/simple.mps")
.unwrap()
.hide_output()
.set_presolving(ParamSetting::Off)
.set_separating(ParamSetting::Off)
.set_heuristics(ParamSetting::Off)
.set_param("branching/pscost/priority", 100000);

model.add(eventhdlr(ProbingTester));
model.solve();
}
}
10 changes: 10 additions & 0 deletions src/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ impl ScipPtr {
Ok(trans_var_ptr)
}

pub(crate) fn is_lp_constructed(&self) -> bool {
unsafe { ffi::SCIPisLPConstructed(self.raw) != 0 }
}

pub(crate) fn construct_lp(&self) -> Result<Option<bool>, Retcode> {
let mut cutoff = 0;
scip_call! { ffi::SCIPconstructLP(self.raw, &mut cutoff) }
Ok(Some(cutoff != 0))
}

pub(crate) fn create_priced_var(
&self,
lb: f64,
Expand Down
Loading