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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#[cfg(test)]
#[path = "../../../tests/unit/solver/objectives/total_unassigned_jobs_test.rs"]
mod total_unassigned_jobs_test;
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;
pub type UnassignedJobEstimator = Arc<dyn Fn(&InsertionContext, &Job, i32) -> f64 + Send + Sync>;
pub struct TotalUnassignedJobs {
unassigned_job_estimator: UnassignedJobEstimator,
}
impl TotalUnassignedJobs {
pub fn new(unassigned_job_estimator: UnassignedJobEstimator) -> Self {
Self { unassigned_job_estimator }
}
fn is_edge_case(
&self,
a: &<TotalUnassignedJobs as Objective>::Solution,
b: &<TotalUnassignedJobs as Objective>::Solution,
fitness_a: f64,
fitness_b: f64,
) -> bool {
let with_empty_routes = a.solution.routes.is_empty() || b.solution.routes.is_empty();
let with_same_fitness = compare_floats(fitness_a, fitness_b) == Ordering::Equal;
with_same_fitness && with_empty_routes
}
}
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 {
let fitness_a = self.fitness(a);
let fitness_b = self.fitness(b);
let order = compare_floats(fitness_a, fitness_b);
match (self.is_edge_case(a, b, fitness_a, fitness_b), order) {
(true, _) => b.solution.routes.len().cmp(&a.solution.routes.len()),
_ => order,
}
}
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>()
}
}