```  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
```
```extern crate uuid;

use std::collections::HashMap;

use self::uuid::Uuid;
use dsl::*;

/// Enum helping to specify the objective function of the linear problem.
///
/// # Examples:
///
/// ```
/// use lp_modeler::dsl::{LpObjective, LpProblem};
///
/// let mut problem = LpProblem::new("One Problem", LpObjective::Maximize);
/// ```
#[derive(Debug, PartialEq)]
pub enum LpObjective {
Minimize,
Maximize,
}

pub trait Problem {
fn add_objective_expression(&mut self, expr_arena: &mut LpExpression);
}

/// Structure used for creating the model and solving a linear problem.
///
/// # Examples:
///
/// ```
/// use lp_modeler::dsl::*;
/// use lp_modeler::solvers::{SolverTrait, CbcSolver, Solution};
///
/// let ref a = LpInteger::new("a");
/// let ref b = LpInteger::new("b");
/// let ref c = LpInteger::new("c");
///
/// let mut problem = LpProblem::new("One Problem", LpObjective::Maximize);
/// problem += 10.0 * a + 20.0 * b;
///
/// problem += (500*a + 1200*b + 1500*c).le(10000);
/// problem += (a + b*2 + c).le(10);
/// problem += (a).le(b);
///
/// let solver = CbcSolver::new();
///
/// match solver.run(&problem) {
/// Ok( solution ) => {
///     println!("Status {:?}", solution.status);
///         for (name, value) in solution.results.iter() {
///             println!("value of {} = {}", name, value);
///         }
///     },
///     Err(msg) => println!("{}", msg),
/// }
/// ```
#[derive(Debug)]
pub struct LpProblem {
pub name: &'static str,
pub unique_name: String,
pub objective_type: LpObjective,
pub obj_expr_arena: Option<LpExpression>,
pub constraints: Vec<LpConstraint>,
}

impl LpProblem {
/// Create a new problem
pub fn new(name: &'static str, objective: LpObjective) -> LpProblem {
let unique_name = format!("{}_{}", name, Uuid::new_v4());
LpProblem {
name,
unique_name,
objective_type: objective,
obj_expr_arena: None,
constraints: Vec::new(),
}
}

// TODO: Call once and pass into parameter
// TODO: Check variables on the objective function
pub fn variables(&self) -> HashMap<String, (usize, usize)> {
let mut lst: HashMap<String, (usize, usize)> = HashMap::new();
for constraint_index in 0..self.constraints.len() {
let constraint = self.constraints.get(constraint_index).unwrap();
constraint.var(constraint.0.get_root_index(), constraint_index, &mut lst);
}
lst
}
}

impl Problem for LpProblem {
fn add_objective_expression(&mut self, expr_arena: &mut LpExpression) {
if let Some(e) = &self.obj_expr_arena {
let mut simple_expr = expr_arena
let _ = simple_expr.simplify().split_off_constant();
self.obj_expr_arena = Some(simple_expr);
} else {
let mut simple_expr = expr_arena.clone();
let _ = simple_expr.simplify().split_off_constant();
self.obj_expr_arena = Some(simple_expr);
}
}

fn add_constraints(&mut self, constraint_expr: &LpConstraint) {
self.constraints.push(constraint_expr.clone());
}
}

(\$problem: ty) => {
fn add_assign(&mut self, _rhs: LpConstraint) {
}
}
/// Add an expression as an objective function