Skip to content

Commit c4956e4

Browse files
authored
Merge pull request #196 from scipopt/generic-params
Generic parametr setter and getter
2 parents 7ffa83c + ada47f1 commit c4956e4

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ mod scip;
6666
pub mod col;
6767
pub use col::*;
6868

69+
mod param;
6970
/// Contains the `Row` struct, which represents a row in an LP relaxation.
7071
pub mod row;
72+
7173
pub use row::*;
7274

7375
/// A macro for calling a `SCIP` function and returning an error if the return code is not `SCIP_OKAY`.

src/model.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::constraint::Constraint;
22
use crate::eventhdlr::Eventhdlr;
33
use crate::node::Node;
4+
use crate::param::ScipParameter;
45
use crate::retcode::Retcode;
56
use crate::scip::ScipPtr;
67
use crate::solution::{SolError, Solution};
@@ -1319,6 +1320,25 @@ impl<T> Model<T> {
13191320
.to_string()
13201321
}
13211322

1323+
/// Returns the value of a SCIP paramter.
1324+
pub fn param<P: ScipParameter>(&self, param: &str) -> P {
1325+
P::get(self, param)
1326+
}
1327+
1328+
/// Tries to set the value of a SCIP parameter and returns the same `Model` instance if successful.
1329+
pub fn try_set_param<P: ScipParameter>(
1330+
self,
1331+
param: &str,
1332+
value: P,
1333+
) -> Result<Model<T>, Retcode> {
1334+
P::set(self, param, value)
1335+
}
1336+
1337+
/// Sets the value of a SCIP parameter.
1338+
pub fn set_param<P: ScipParameter>(self, param: &str, value: P) -> Model<T> {
1339+
P::set(self, param, value).expect("Failed to set parameter")
1340+
}
1341+
13221342
/// Returns the value of a SCIP boolean parameter.
13231343
pub fn bool_param(&self, param: &str) -> bool {
13241344
self.scip
@@ -1908,6 +1928,22 @@ mod tests {
19081928
.unwrap();
19091929
}
19101930

1931+
#[test]
1932+
fn generic_params() {
1933+
let model = Model::new()
1934+
.hide_output()
1935+
.include_default_plugins()
1936+
.create_prob("test")
1937+
.set_obj_sense(ObjSense::Maximize)
1938+
.set_param("display/verblevel", 0)
1939+
.set_param("limits/time", 0.0)
1940+
.set_param("limits/memory", 0.0);
1941+
1942+
assert_eq!(model.param::<i32>("display/verblevel"), 0);
1943+
assert_eq!(model.param::<f64>("limits/time"), 0.0);
1944+
assert_eq!(model.param::<f64>("limits/memory"), 0.0);
1945+
}
1946+
19111947
#[test]
19121948
fn free_transform() {
19131949
let model = create_model();

src/param.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::{Model, Retcode};
2+
3+
pub trait ScipParameter: Sized {
4+
fn set<T>(model: Model<T>, name: &str, value: Self) -> Result<Model<T>, Retcode>;
5+
fn get<T>(model: &Model<T>, name: &str) -> Self;
6+
}
7+
8+
impl ScipParameter for f64 {
9+
fn set<T>(model: Model<T>, name: &str, value: f64) -> Result<Model<T>, Retcode> {
10+
let model = model.set_real_param(name, value)?;
11+
Ok(model)
12+
}
13+
14+
fn get<T>(model: &Model<T>, name: &str) -> f64 {
15+
model.real_param(name)
16+
}
17+
}
18+
19+
impl ScipParameter for i32 {
20+
fn set<T>(model: Model<T>, name: &str, value: i32) -> Result<Model<T>, Retcode> {
21+
let model = model.set_int_param(name, value)?;
22+
Ok(model)
23+
}
24+
25+
fn get<T>(model: &Model<T>, name: &str) -> i32 {
26+
model.int_param(name)
27+
}
28+
}
29+
30+
impl ScipParameter for bool {
31+
fn set<T>(model: Model<T>, name: &str, value: bool) -> Result<Model<T>, Retcode> {
32+
let model = model.set_bool_param(name, value)?;
33+
Ok(model)
34+
}
35+
36+
fn get<T>(model: &Model<T>, name: &str) -> bool {
37+
model.bool_param(name)
38+
}
39+
}
40+
41+
impl ScipParameter for i64 {
42+
fn set<T>(model: Model<T>, name: &str, value: i64) -> Result<Model<T>, Retcode> {
43+
let model = model.set_longint_param(name, value)?;
44+
Ok(model)
45+
}
46+
47+
fn get<T>(model: &Model<T>, name: &str) -> i64 {
48+
model.longint_param(name)
49+
}
50+
}
51+
52+
impl ScipParameter for String {
53+
fn set<T>(model: Model<T>, name: &str, value: String) -> Result<Model<T>, Retcode> {
54+
let model = model.set_str_param(name, &value)?;
55+
Ok(model)
56+
}
57+
58+
fn get<T>(model: &Model<T>, name: &str) -> String {
59+
model.str_param(name)
60+
}
61+
}

0 commit comments

Comments
 (0)