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
use super::*;
use crate::algorithms::nsga2::Objective;
use crate::models::problem::Job;
use crate::utils::compare_floats;
use std::ops::Deref;
use std::sync::Arc;

/// A type which allows to control how job is estimated in objective fitness
pub type UnassignedJobEstimator = Arc<dyn Fn(&InsertionContext, &Job, i32) -> f64 + Send + Sync>;

/// An objective function which minimizes amount of unassigned jobs as a target.
pub struct TotalUnassignedJobs {
    unassigned_job_estimator: UnassignedJobEstimator,
}

impl TotalUnassignedJobs {
    /// Creates a new instance of `TotalUnassignedJobs`.
    pub fn new(unassigned_job_estimator: UnassignedJobEstimator) -> Self {
        Self { unassigned_job_estimator }
    }
}

impl Default for TotalUnassignedJobs {
    fn default() -> Self {
        Self::new(Arc::new(|_, _, _| 1.))
    }
}

impl Objective for TotalUnassignedJobs {
    type Solution = InsertionContext;

    fn total_order(&self, a: &Self::Solution, b: &Self::Solution) -> Ordering {
        compare_floats(self.fitness(a), self.fitness(b))
    }

    fn distance(&self, a: &Self::Solution, b: &Self::Solution) -> f64 {
        self.fitness(a) - self.fitness(b)
    }

    fn fitness(&self, solution: &Self::Solution) -> f64 {
        solution
            .solution
            .unassigned
            .iter()
            .map(|(job, code)| self.unassigned_job_estimator.deref()(solution, job, *code))
            .sum::<f64>()
    }
}