lp_modeler/
util.rs

1pub fn is_zero(n: f32) -> bool {
2    n.abs() < 0.00001
3}
4
5/// This macro allows defining constraints using 'expression1 <= expression2'
6/// instead of `expression1.le(expression2)`. 
7/// 
8/// # Example:
9///
10/// ```
11/// use lp_modeler::dsl::*;
12/// use lp_modeler::constraint;
13///
14/// let ref a = LpInteger::new("a");
15/// let ref b = LpInteger::new("b");
16///
17/// let mut problem = LpProblem::new("One Problem", LpObjective::Maximize);
18/// problem += 5*a + 3*b;
19/// problem += constraint!(a + b*2 <= 10);
20/// problem += constraint!(b >= a);
21/// ```
22#[macro_export]
23macro_rules! constraint {
24    ([$($left:tt)*] <= $($right:tt)*) => {
25        ($($left)*).le($($right)*)
26    };
27    ([$($left:tt)*] >= $($right:tt)*) => {
28        ($($left)*).ge($($right)*)
29    };
30    // Stop condition: all token have been processed
31    ([$($left:tt)*]) => {
32        $($left:tt)*
33    };
34    // The next token is not a special one
35    ([$($left:tt)*] $next:tt $($right:tt)*) => {
36        constraint!([$($left)* $next] $($right)*)
37    };
38    // Initial rule: start the recursive calls
39    ($($all:tt)*) => {
40        constraint!([] $($all)*)
41    };
42}