-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlp_solvers.rs
201 lines (179 loc) · 6.03 KB
/
lp_solvers.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! This module allows solving problems is external solver binaries.
//! Contrarily to other solver modules, this one doesn't require linking your program to any solver.
//! A solver binary will need to be present on the user's computer at runtime.
use std::cmp::Ordering;
use lp_solvers::lp_format::LpObjective;
use lp_solvers::problem::StrExpression;
pub use lp_solvers::solvers::*;
use lp_solvers::util::UniqueNameGenerator;
use crate::constraint::ConstraintReference;
use crate::solvers::{MipGapError, ObjectiveDirection};
use crate::variable::UnsolvedProblem;
use crate::{
Constraint, Expression, IntoAffineExpression, ResolutionError, Solution as GoodLpSolution,
Solver, SolverModel, Variable,
};
/// An external solver
pub struct LpSolver<T: lp_solvers::solvers::SolverTrait>(pub T);
impl<T: lp_solvers::solvers::SolverTrait + Clone> Solver for LpSolver<T> {
type Model = Model<T>;
fn create_model(&mut self, problem: UnsolvedProblem) -> Self::Model {
let name = "good_lp_problem".to_string();
let sense = match problem.direction {
ObjectiveDirection::Maximisation => LpObjective::Maximize,
ObjectiveDirection::Minimisation => LpObjective::Minimize,
};
let mut gen = UniqueNameGenerator::default();
let variables: Vec<lp_solvers::problem::Variable> = problem
.variables
.into_iter()
.map(|def| lp_solvers::problem::Variable {
name: gen.add_variable(&def.name).to_string(),
is_integer: def.is_integer,
lower_bound: def.min,
upper_bound: def.max,
})
.collect();
let objective = linear_coefficients_str(&problem.objective, &variables);
Model {
problem: lp_solvers::problem::Problem {
name,
sense,
objective,
variables,
constraints: vec![],
},
solver: self.0.clone(),
}
}
fn name() -> &'static str {
<Model<T> as SolverModel>::name()
}
}
impl<T> crate::solvers::WithMipGap for Model<T>
where
T: lp_solvers::solvers::WithMipGap<T>,
{
fn mip_gap(&self) -> Option<f32> {
self.solver.mip_gap()
}
fn with_mip_gap(mut self, mip_gap: f32) -> Result<Self, MipGapError> {
match self.solver.with_mip_gap(mip_gap) {
Ok(solver) => {
self.solver = solver;
Ok(self)
}
Err(err) => {
if mip_gap.is_sign_negative() {
Err(MipGapError::Negative)
} else if mip_gap.is_infinite() {
Err(MipGapError::Infinite)
} else {
Err(MipGapError::Other(err))
}
}
}
}
}
/// A problem to be used by lp-solvers
pub struct Model<T> {
problem: lp_solvers::problem::Problem,
solver: T,
}
impl<T: SolverTrait> SolverModel for Model<T> {
type Solution = LpSolution;
type Error = ResolutionError;
fn solve(self) -> Result<Self::Solution, Self::Error> {
let map = self.solver.run(&self.problem)?;
match map.status {
Status::Infeasible => return Err(ResolutionError::Infeasible),
Status::Unbounded => return Err(ResolutionError::Unbounded),
Status::NotSolved => return Err(ResolutionError::Other("unknown error: not solved")),
_ => {}
}
let solution = self
.problem
.variables
.iter()
.map(|v| f64::from(*map.results.get(&v.name).unwrap_or(&0.)))
.collect();
Ok(LpSolution { solution })
}
fn add_constraint(&mut self, c: Constraint) -> ConstraintReference {
let reference = ConstraintReference {
index: self.problem.constraints.len(),
};
self.problem
.constraints
.push(lp_solvers::lp_format::Constraint {
lhs: linear_coefficients_str(&c.expression, &self.problem.variables),
operator: if c.is_equality {
Ordering::Equal
} else {
Ordering::Less
},
rhs: -c.expression.constant,
});
reference
}
fn name() -> &'static str {
"External Solver (through lp_solvers)"
}
}
fn linear_coefficients_str(
expr: &Expression,
variables: &[lp_solvers::problem::Variable],
) -> StrExpression {
StrExpression(
expr.linear_coefficients()
.map(|(var, coeff)| format!("{:+} {}", coeff, variables[var.index()].name))
.collect::<Vec<String>>()
.join(" "),
)
}
/// A solution
pub struct LpSolution {
solution: Vec<f64>,
}
impl GoodLpSolution for LpSolution {
fn value(&self, variable: Variable) -> f64 {
self.solution[variable.index()]
}
}
#[cfg(test)]
mod tests {
use crate::solvers::lp_solvers::{GlpkSolver, LpSolver};
use crate::variables;
#[test]
fn coefficient_formatting_pos_pos() {
variables! {vars: a; b; }
let problem = vars
.minimise(1 * a + 2 * b)
.using(LpSolver(GlpkSolver::new()));
assert_eq!(problem.problem.objective.0, "+2 b +1 a");
}
#[test]
fn coefficient_formatting_pos_neg() {
variables! {vars: a; b; }
let problem = vars
.minimise(1 * a - 2 * b)
.using(LpSolver(GlpkSolver::new()));
assert_eq!(problem.problem.objective.0, "-2 b +1 a");
}
#[test]
fn coefficient_formatting_neg_pos() {
variables! {vars: a; b; }
let problem = vars
.minimise(-1 * a + 2 * b)
.using(LpSolver(GlpkSolver::new()));
assert_eq!(problem.problem.objective.0, "+2 b -1 a");
}
#[test]
fn coefficient_formatting_neg_neg() {
variables! {vars: a; b; }
let problem = vars
.minimise(-1 * a - 2 * b)
.using(LpSolver(GlpkSolver::new()));
assert_eq!(problem.problem.objective.0, "-2 b -1 a");
}
}