use std::collections::HashMap;
use std::fmt;
use std::fmt::Display;
#[derive(Eq, PartialEq, Debug)]
pub struct Solution<F> {
pub objective_value: F,
pub solution_values: Vec<(String, F)>,
}
impl<F: PartialEq + Clone> Solution<F> {
pub fn new(objective: F, solution_values: Vec<(String, F)>) -> Self {
Self {
objective_value: objective,
solution_values,
}
}
pub fn is_probably_equal_to(&self, other: &Self, min_equal: f64) -> bool {
if !(self.objective_value == other.objective_value) {
return false;
}
if !(self.solution_values.len() == other.solution_values.len()) {
return false;
}
let this_map = self.solution_values.iter().cloned().collect::<HashMap<_, _>>();
let other_map = other.solution_values.iter().cloned().collect::<HashMap<_, _>>();
if !{
this_map.len() == other_map.len() && this_map.keys().all(|k| other_map.contains_key(k))
} {
return false;
}
let nr_total = self.solution_values.len();
if nr_total < 10 {
return true;
}
let mut nr_equal = 0;
for name in this_map.keys() {
if this_map.get(name) == other_map.get(name) {
nr_equal += 1;
}
}
nr_equal as f64 / nr_total as f64 > min_equal }
}
impl<F: Display> Display for Solution<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Objective value: {}", self.objective_value)
}
}