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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#[cfg(test)]
#[path = "../../../../tests/unit/construction/clustering/vicinity/vicinity_test.rs"]
mod vicinity_test;
use crate::construction::heuristics::*;
use crate::models::common::*;
use crate::models::common::{Dimensions, ValueDimension};
use crate::models::problem::{Actor, Job};
use crate::models::Problem;
use hashbrown::HashSet;
use rosomaxa::prelude::*;
use std::cmp::Ordering;
use std::ops::Deref;
use std::sync::Arc;
mod estimations;
use self::estimations::*;
use crate::models::solution::Commute;
const CLUSTER_DIMENSION_KEY: &str = "cls";
pub trait ClusterDimension {
fn set_cluster(&mut self, jobs: Vec<ClusterInfo>) -> &mut Self;
fn get_cluster(&self) -> Option<&Vec<ClusterInfo>>;
}
impl ClusterDimension for Dimensions {
fn set_cluster(&mut self, jobs: Vec<ClusterInfo>) -> &mut Self {
self.set_value(CLUSTER_DIMENSION_KEY, jobs);
self
}
fn get_cluster(&self) -> Option<&Vec<ClusterInfo>> {
self.get_value(CLUSTER_DIMENSION_KEY)
}
}
pub type ClusterCandidate<'a> = (&'a Job, &'a HashSet<Job>);
type CheckInsertionFn = (dyn Fn(&Job) -> Result<(), i32> + Send + Sync);
#[derive(Clone)]
pub struct ClusterConfig {
pub profile: Profile,
pub threshold: ThresholdPolicy,
pub visiting: VisitPolicy,
pub serving: ServingPolicy,
pub filtering: FilterPolicy,
pub building: BuilderPolicy,
}
#[derive(Clone)]
pub struct ThresholdPolicy {
pub moving_duration: Duration,
pub moving_distance: Distance,
pub min_shared_time: Option<Duration>,
pub smallest_time_window: Option<f64>,
pub max_jobs_per_cluster: Option<usize>,
}
#[derive(Clone)]
pub enum VisitPolicy {
Return,
ClosedContinuation,
OpenContinuation,
}
#[derive(Clone)]
pub struct FilterPolicy {
pub job_filter: Arc<dyn Fn(&Job) -> bool + Send + Sync>,
pub actor_filter: Arc<dyn Fn(&Actor) -> bool + Send + Sync>,
}
#[derive(Clone)]
pub enum ServingPolicy {
Original {
parking: f64,
},
Multiplier {
multiplier: f64,
parking: f64,
},
Fixed {
value: f64,
parking: f64,
},
}
#[derive(Clone)]
pub struct BuilderPolicy {
pub ordering_global: Arc<dyn Fn(ClusterCandidate, ClusterCandidate) -> Ordering + Send + Sync>,
pub ordering_local: Arc<dyn Fn(&ClusterInfo, &ClusterInfo) -> Ordering + Send + Sync>,
}
#[derive(Clone)]
pub struct ClusterInfo {
pub job: Job,
pub service_time: Duration,
pub place_idx: usize,
pub commute: Commute,
}
pub fn create_job_clusters(
problem: Arc<Problem>,
environment: Arc<Environment>,
config: &ClusterConfig,
) -> Vec<(Job, Vec<Job>)> {
let insertion_ctx = InsertionContext::new_empty(problem.clone(), environment);
let constraint = insertion_ctx.problem.constraint.clone();
let check_insertion = get_check_insertion_fn(insertion_ctx, config.filtering.actor_filter.as_ref());
let transport = problem.transport.as_ref();
let jobs = problem
.jobs
.all()
.filter(&*config.filtering.job_filter)
.filter(|job| job.as_single().is_some())
.collect::<Vec<_>>();
let estimates = get_jobs_dissimilarities(jobs.as_slice(), transport, config);
get_clusters(&constraint, estimates, config, &check_insertion)
}
fn get_check_insertion_fn(
insertion_ctx: InsertionContext,
actor_filter: &(dyn Fn(&Actor) -> bool + Send + Sync),
) -> impl Fn(&Job) -> Result<(), i32> {
let leg_selector = VariableLegSelector::new(insertion_ctx.environment.random.clone());
let result_selector = BestResultSelector::default();
let routes = insertion_ctx
.solution
.registry
.next()
.filter(|route_ctx| actor_filter.deref()(&route_ctx.route.actor))
.collect::<Vec<_>>();
move |job: &Job| -> Result<(), i32> {
let eval_ctx = EvaluationContext {
constraint: &insertion_ctx.problem.constraint,
job,
leg_selector: &leg_selector,
result_selector: &result_selector,
};
unwrap_from_result(routes.iter().try_fold(Err(-1), |_, route_ctx| {
let result = evaluate_job_insertion_in_route(
&insertion_ctx,
&eval_ctx,
route_ctx,
InsertionPosition::Any,
InsertionResult::make_failure(),
);
match result {
InsertionResult::Success(_) => Err(Ok(())),
InsertionResult::Failure(failure) => Ok(Err(failure.constraint)),
}
}))
}
}
impl ServingPolicy {
pub fn get_parking(&self) -> f64 {
match &self {
Self::Original { parking } => *parking,
Self::Multiplier { parking, .. } => *parking,
Self::Fixed { parking, .. } => *parking,
}
}
}