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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#[cfg(test)]
#[path = "../../../tests/unit/construction/heuristics/selectors_test.rs"]
mod selectors_test;
use crate::construction::heuristics::*;
use crate::models::problem::Job;
use crate::utils::{map_reduce, Noise};
pub trait RouteSelector {
fn select<'a>(&'a self, ctx: &'a InsertionContext, job: &'a Job) -> Box<dyn Iterator<Item = RouteContext> + 'a>;
}
pub struct AllRouteSelector {}
impl Default for AllRouteSelector {
fn default() -> Self {
Self {}
}
}
impl RouteSelector for AllRouteSelector {
fn select<'a>(&'a self, ctx: &'a InsertionContext, _job: &'a Job) -> Box<dyn Iterator<Item = RouteContext> + 'a> {
Box::new(ctx.solution.routes.iter().cloned().chain(ctx.solution.registry.next()))
}
}
pub trait JobSelector {
fn select<'a>(&'a self, ctx: &'a mut InsertionContext) -> Box<dyn Iterator<Item = Job> + 'a>;
}
pub struct AllJobSelector {}
impl Default for AllJobSelector {
fn default() -> Self {
Self {}
}
}
impl JobSelector for AllJobSelector {
fn select<'a>(&'a self, ctx: &'a mut InsertionContext) -> Box<dyn Iterator<Item = Job> + 'a> {
Box::new(ctx.solution.required.iter().cloned())
}
}
pub trait JobMapReducer {
fn reduce<'a>(
&'a self,
ctx: &'a InsertionContext,
jobs: Vec<Job>,
insertion_position: InsertionPosition,
) -> InsertionResult;
}
pub struct PairJobMapReducer {
route_selector: Box<dyn RouteSelector + Send + Sync>,
result_selector: Box<dyn ResultSelector + Send + Sync>,
}
impl PairJobMapReducer {
pub fn new(
route_selector: Box<dyn RouteSelector + Send + Sync>,
result_selector: Box<dyn ResultSelector + Send + Sync>,
) -> Self {
Self { route_selector, result_selector }
}
}
impl JobMapReducer for PairJobMapReducer {
fn reduce<'a>(
&'a self,
ctx: &'a InsertionContext,
jobs: Vec<Job>,
insertion_position: InsertionPosition,
) -> InsertionResult {
map_reduce(
&jobs,
|job| {
evaluate_job_insertion(
&job,
&ctx,
self.route_selector.as_ref(),
self.result_selector.as_ref(),
insertion_position,
)
},
InsertionResult::make_failure,
|a, b| self.result_selector.select(&ctx, a, b),
)
}
}
pub trait ResultSelector {
fn select(&self, ctx: &InsertionContext, left: InsertionResult, right: InsertionResult) -> InsertionResult;
}
pub struct BestResultSelector {}
impl Default for BestResultSelector {
fn default() -> Self {
Self {}
}
}
impl ResultSelector for BestResultSelector {
fn select(&self, _: &InsertionContext, left: InsertionResult, right: InsertionResult) -> InsertionResult {
InsertionResult::choose_best_result(left, right)
}
}
pub struct NoiseResultSelector {
noise: Noise,
}
impl NoiseResultSelector {
pub fn new(noise: Noise) -> Self {
Self { noise }
}
}
impl ResultSelector for NoiseResultSelector {
fn select(&self, _: &InsertionContext, left: InsertionResult, right: InsertionResult) -> InsertionResult {
match (&left, &right) {
(InsertionResult::Success(_), InsertionResult::Failure(_)) => left,
(InsertionResult::Failure(_), InsertionResult::Success(_)) => right,
(InsertionResult::Success(left_success), InsertionResult::Success(right_success)) => {
let left_cost = self.noise.add(left_success.cost);
let right_cost = self.noise.add(right_success.cost);
if left_cost < right_cost {
left
} else {
right
}
}
_ => right,
}
}
}