use std::{cmp::Ordering, fmt::Debug};
use super::{Objective, Problem};
pub struct Evaluation<P: Problem + ?Sized> {
value: P::Value,
solution: P::Solution,
}
impl<P: Problem + ?Sized> Debug for Evaluation<P>
where
P::Solution: Debug,
P::Value: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Evaluation")
.field("value", &self.value)
.field("solution", &self.solution)
.finish()
}
}
impl<P: Problem + ?Sized> PartialEq for Evaluation<P>
where
P::Solution: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.value == other.value && self.solution == other.solution
}
}
impl<P: Problem> Evaluation<P> {
pub fn new(solution: P::Solution, value: P::Value) -> Self {
Self { solution, value }
}
pub fn solution(&self) -> &P::Solution {
&self.solution
}
pub fn into_solution(self) -> P::Solution {
self.solution
}
pub fn value(&self) -> <P as Problem>::Value {
self.value
}
pub fn compare(&self, other: &Self) -> Comparison {
self.compare_value(other.value)
}
pub fn compare_value(&self, other: P::Value) -> Comparison {
match P::OBJECTIVE {
Objective::Min => match self.value.cmp(&other) {
Ordering::Less => Comparison::Better,
Ordering::Equal => Comparison::Equal,
Ordering::Greater => Comparison::Worse,
},
Objective::Max => match self.value.cmp(&other) {
Ordering::Less => Comparison::Worse,
Ordering::Equal => Comparison::Equal,
Ordering::Greater => Comparison::Better,
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Comparison {
Better,
Equal,
Worse,
}