Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/solvers/lp_solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! A solver binary will need to be present on the user's computer at runtime.

use std::cmp::Ordering;
use std::collections::HashMap;

use lp_solvers::lp_format::LpObjective;
use lp_solvers::problem::StrExpression;
Expand Down Expand Up @@ -157,6 +158,42 @@ fn linear_coefficients_str(
)
}

impl<T> crate::solvers::WithInitialSolution for Model<T>
where
T: WithMipStart<T>,
{
fn with_initial_solution(
mut self,
solution: impl IntoIterator<Item = (Variable, f64)>,
) -> Self {
let mut start: HashMap<String, f32> = HashMap::new();

for (v, val) in solution {
if !val.is_finite() {
continue;
}

let idx = v.index();
let Some(lp_var) = self.problem.variables.get(idx) else {
continue;
};

let val_f32 = val as f32;
if !val_f32.is_finite() {
continue;
}

start.insert(lp_var.name.clone(), val_f32);
}

if let Ok(solver) = self.solver.with_mip_start(&start) {
self.solver = solver;
}

self
}
}

/// A solution
pub struct LpSolution {
solution: Vec<f64>,
Expand Down