Skip to content

Commit

Permalink
Support SCIPfreeTransform (#129)
Browse files Browse the repository at this point in the history
* Support SCIPfreeTransform

* Cargo fmt
  • Loading branch information
mmghannam authored Mar 13, 2024
1 parent cea5d96 commit b766b29
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Unreleased
### Added
- Support `SCIPfreeTransform` to allow for iterated solving.
### Fixed
### Changed
### Remove
Expand Down
34 changes: 34 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ impl Model<Solved> {
pub fn n_lp_iterations(&self) -> usize {
self.scip.n_lp_iterations()
}

/// Frees the transformed problem and returns the model the ProblemCreated state where you
/// can add variables and constraints, useful for iterated solving
pub fn free_transform(self) -> Model<ProblemCreated> {
self.scip
.free_transform()
.unwrap_or_else(|retcode| panic!("SCIP returned unexpected retcode {:?}", retcode));
Model {
scip: self.scip,
state: ProblemCreated {
vars: self.state.vars,
conss: self.state.conss,
},
}
}
}

/// A trait for optimization models with a problem created.
Expand Down Expand Up @@ -1753,4 +1768,23 @@ mod tests {
.set_int_param("display/verblevel", 0)
.unwrap();
}

#[test]
fn free_transform() {
let model = create_model();
let solved_model = model.solve();
let obj_val = solved_model.obj_val();

let mut second_model = solved_model.free_transform();

let x3 = second_model.add_var(0.0, f64::INFINITY, 1.0, "x3", VarType::Integer);

let bound = 2.0;
second_model.add_cons(vec![x3], &[1.0], 0.0, bound, "x3-cons");

let second_solved = second_model.solve();
let expected_obj = obj_val + bound;
assert_eq!(second_solved.status(), Status::Optimal);
assert!((second_solved.obj_val() - expected_obj).abs() <= 1e-6);
}
}
5 changes: 5 additions & 0 deletions src/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@ impl ScipPtr {
let stored = unsafe { stored.assume_init() };
Ok(stored != 0)
}

pub(crate) fn free_transform(&self) -> Result<(), Retcode> {
scip_call!(ffi::SCIPfreeTransform(self.raw));
Ok(())
}
}

impl Drop for ScipPtr {
Expand Down

0 comments on commit b766b29

Please sign in to comment.