Skip to content

Commit

Permalink
Merge branch 'main' of github.com:scipopt/russcip
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Jan 25, 2025
2 parents 3ea634e + c4956e4 commit d4e0ce9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ mod scip;
pub mod col;
pub use col::*;

mod param;
/// Contains the `Row` struct, which represents a row in an LP relaxation.
pub mod row;

pub use row::*;

/// A macro for calling a `SCIP` function and returning an error if the return code is not `SCIP_OKAY`.
Expand Down
36 changes: 36 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::constraint::Constraint;
use crate::eventhdlr::Eventhdlr;
use crate::node::Node;
use crate::param::ScipParameter;
use crate::retcode::Retcode;
use crate::scip::ScipPtr;
use crate::solution::{SolError, Solution};
Expand Down Expand Up @@ -1319,6 +1320,25 @@ impl<T> Model<T> {
.to_string()
}

/// Returns the value of a SCIP paramter.
pub fn param<P: ScipParameter>(&self, param: &str) -> P {
P::get(self, param)
}

/// Tries to set the value of a SCIP parameter and returns the same `Model` instance if successful.
pub fn try_set_param<P: ScipParameter>(
self,
param: &str,
value: P,
) -> Result<Model<T>, Retcode> {
P::set(self, param, value)
}

/// Sets the value of a SCIP parameter.
pub fn set_param<P: ScipParameter>(self, param: &str, value: P) -> Model<T> {
P::set(self, param, value).expect("Failed to set parameter")
}

/// Returns the value of a SCIP boolean parameter.
pub fn bool_param(&self, param: &str) -> bool {
self.scip
Expand Down Expand Up @@ -1908,6 +1928,22 @@ mod tests {
.unwrap();
}

#[test]
fn generic_params() {
let model = Model::new()
.hide_output()
.include_default_plugins()
.create_prob("test")
.set_obj_sense(ObjSense::Maximize)
.set_param("display/verblevel", 0)
.set_param("limits/time", 0.0)
.set_param("limits/memory", 0.0);

assert_eq!(model.param::<i32>("display/verblevel"), 0);
assert_eq!(model.param::<f64>("limits/time"), 0.0);
assert_eq!(model.param::<f64>("limits/memory"), 0.0);
}

#[test]
fn free_transform() {
let model = create_model();
Expand Down
61 changes: 61 additions & 0 deletions src/param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::{Model, Retcode};

pub trait ScipParameter: Sized {
fn set<T>(model: Model<T>, name: &str, value: Self) -> Result<Model<T>, Retcode>;
fn get<T>(model: &Model<T>, name: &str) -> Self;
}

impl ScipParameter for f64 {
fn set<T>(model: Model<T>, name: &str, value: f64) -> Result<Model<T>, Retcode> {
let model = model.set_real_param(name, value)?;
Ok(model)
}

fn get<T>(model: &Model<T>, name: &str) -> f64 {
model.real_param(name)
}
}

impl ScipParameter for i32 {
fn set<T>(model: Model<T>, name: &str, value: i32) -> Result<Model<T>, Retcode> {
let model = model.set_int_param(name, value)?;
Ok(model)
}

fn get<T>(model: &Model<T>, name: &str) -> i32 {
model.int_param(name)
}
}

impl ScipParameter for bool {
fn set<T>(model: Model<T>, name: &str, value: bool) -> Result<Model<T>, Retcode> {
let model = model.set_bool_param(name, value)?;
Ok(model)
}

fn get<T>(model: &Model<T>, name: &str) -> bool {
model.bool_param(name)
}
}

impl ScipParameter for i64 {
fn set<T>(model: Model<T>, name: &str, value: i64) -> Result<Model<T>, Retcode> {
let model = model.set_longint_param(name, value)?;
Ok(model)
}

fn get<T>(model: &Model<T>, name: &str) -> i64 {
model.longint_param(name)
}
}

impl ScipParameter for String {
fn set<T>(model: Model<T>, name: &str, value: String) -> Result<Model<T>, Retcode> {
let model = model.set_str_param(name, &value)?;
Ok(model)
}

fn get<T>(model: &Model<T>, name: &str) -> String {
model.str_param(name)
}
}

0 comments on commit d4e0ce9

Please sign in to comment.