Skip to content

Commit 26b580c

Browse files
Implemented WithInitialSolution trait for lp-solvers solvers that implement WithMipStart (#118)
1 parent effe018 commit 26b580c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/solvers/lp_solvers.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! A solver binary will need to be present on the user's computer at runtime.
44
55
use std::cmp::Ordering;
6+
use std::collections::HashMap;
67

78
use lp_solvers::lp_format::LpObjective;
89
use lp_solvers::problem::StrExpression;
@@ -157,6 +158,42 @@ fn linear_coefficients_str(
157158
)
158159
}
159160

161+
impl<T> crate::solvers::WithInitialSolution for Model<T>
162+
where
163+
T: WithMipStart<T>,
164+
{
165+
fn with_initial_solution(
166+
mut self,
167+
solution: impl IntoIterator<Item = (Variable, f64)>,
168+
) -> Self {
169+
let mut start: HashMap<String, f32> = HashMap::new();
170+
171+
for (v, val) in solution {
172+
if !val.is_finite() {
173+
continue;
174+
}
175+
176+
let idx = v.index();
177+
let Some(lp_var) = self.problem.variables.get(idx) else {
178+
continue;
179+
};
180+
181+
let val_f32 = val as f32;
182+
if !val_f32.is_finite() {
183+
continue;
184+
}
185+
186+
start.insert(lp_var.name.clone(), val_f32);
187+
}
188+
189+
if let Ok(solver) = self.solver.with_mip_start(&start) {
190+
self.solver = solver;
191+
}
192+
193+
self
194+
}
195+
}
196+
160197
/// A solution
161198
pub struct LpSolution {
162199
solution: Vec<f64>,

0 commit comments

Comments
 (0)