-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from scipopt/cons-builder
Ergonomic constraint builder
- Loading branch information
Showing
6 changed files
with
177 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::builder::CanBeAddedToModel; | ||
use crate::{Constraint, Model, ModelWithProblem, ProblemCreated, ProblemOrSolving, Variable}; | ||
|
||
/// A builder for creating constraints. | ||
#[derive(Debug)] | ||
pub struct ConsBuilder<'a> { | ||
lhs: f64, | ||
rhs: f64, | ||
name: Option<&'a str>, | ||
coefs: Vec<(&'a Variable, f64)>, | ||
} | ||
|
||
/// Creates a new default `ConsBuilder`. | ||
pub fn cons() -> ConsBuilder<'static> { | ||
ConsBuilder::default() | ||
} | ||
|
||
impl Default for ConsBuilder<'_> { | ||
fn default() -> Self { | ||
ConsBuilder { | ||
lhs: f64::NEG_INFINITY, | ||
rhs: f64::INFINITY, | ||
name: None, | ||
coefs: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> ConsBuilder<'a> { | ||
/// Creates a constraint of the form `expr <= val`. | ||
pub fn le(mut self, val: f64) -> Self { | ||
self.rhs = val; | ||
self.lhs = f64::NEG_INFINITY; | ||
self | ||
} | ||
|
||
/// Creates a constraint of the form `val <= expr`. | ||
pub fn ge(mut self, val: f64) -> Self { | ||
self.lhs = val; | ||
self.rhs = f64::INFINITY; | ||
self | ||
} | ||
|
||
/// Creates a constraint of the form `expr = val`. | ||
pub fn eq(mut self, val: f64) -> Self { | ||
self.lhs = val; | ||
self.rhs = val; | ||
self | ||
} | ||
|
||
/// Sets the name of the constraint. | ||
pub fn name(mut self, name: &'a str) -> Self { | ||
self.name = Some(name); | ||
self | ||
} | ||
|
||
/// Adds a coefficient to the constraint. | ||
pub fn coef(mut self, var: &'a Variable, coef: f64) -> Self { | ||
self.coefs.push((var, coef)); | ||
self | ||
} | ||
} | ||
|
||
impl CanBeAddedToModel for ConsBuilder<'_> { | ||
type Return = Constraint; | ||
fn add(self, model: &mut Model<ProblemCreated>) -> Self::Return { | ||
let mut vars = Vec::new(); | ||
let mut coefs = Vec::new(); | ||
for (var, coef) in self.coefs { | ||
vars.push(var); | ||
coefs.push(coef); | ||
} | ||
|
||
let name = self.name.map(|s| s.to_string()).unwrap_or_else(|| { | ||
let n_cons = model.n_conss(); | ||
format!("cons{}", n_cons) | ||
}); | ||
model.add_cons(vars, &coefs, self.lhs, self.rhs, &name) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::builder::var::var; | ||
use crate::minimal_model; | ||
|
||
#[test] | ||
fn test_cons_builder() { | ||
let mut model = minimal_model().hide_output(); | ||
let var = model.add(var().binary().obj(1.)); | ||
let cons = cons().name("c").eq(1.0).coef(&var, 1.0); | ||
|
||
assert_eq!(cons.name, Some("c")); | ||
assert_eq!(cons.lhs, 1.0); | ||
assert_eq!(cons.rhs, 1.0); | ||
assert_eq!(cons.coefs.len(), 1); | ||
assert_eq!(cons.coefs[0].1, 1.0); | ||
|
||
model.add(cons); | ||
|
||
assert_eq!(model.n_conss(), 1); | ||
let cons = &model.conss()[0]; | ||
assert_eq!(cons.name(), "c"); | ||
|
||
let solved = model.solve(); | ||
|
||
assert_eq!(solved.status(), crate::Status::Optimal); | ||
assert_eq!(solved.obj_val(), 1.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// This module contains `ConsBuilder` for easily creating constraints. | ||
pub mod cons; | ||
/// This module contains `VarBuilder` for easily creating variables. | ||
pub mod var; | ||
|
||
use crate::{Model, ProblemCreated}; | ||
|
||
/// A trait for adding two values together. | ||
pub trait CanBeAddedToModel { | ||
/// The return type after adding to the model (e.g. `Variable` / `Constraint` ). | ||
type Return; | ||
/// How to add the value to the model. | ||
fn add(self, model: &mut Model<ProblemCreated>) -> Self::Return; | ||
} | ||
|
||
impl<T, I> CanBeAddedToModel for I | ||
where | ||
T: CanBeAddedToModel, | ||
I: IntoIterator<Item = T>, | ||
{ | ||
type Return = Vec<T::Return>; | ||
fn add(self, model: &mut Model<ProblemCreated>) -> Self::Return { | ||
self.into_iter().map(|x| x.add(model)).collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters