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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#[cfg(test)]
#[path = "../../tests/unit/models/goal_test.rs"]
mod goal_test;
use crate::construction::enablers::*;
use crate::construction::heuristics::*;
use crate::models::common::Cost;
use crate::models::problem::Job;
use hashbrown::{HashMap, HashSet};
use rand::prelude::SliceRandom;
use rosomaxa::algorithms::nsga2::dominance_order;
use rosomaxa::population::Shuffled;
use rosomaxa::prelude::*;
use std::cmp::Ordering;
use std::slice::Iter;
use std::sync::Arc;
#[derive(Clone, Default)]
pub struct GoalContext {
pub(crate) hierarchical_objectives: Vec<Vec<Arc<dyn FeatureObjective<Solution = InsertionContext> + Send + Sync>>>,
pub(crate) flat_objectives: Vec<Arc<dyn FeatureObjective<Solution = InsertionContext> + Send + Sync>>,
pub(crate) local_objectives: Vec<Vec<Arc<dyn FeatureObjective<Solution = InsertionContext> + Send + Sync>>>,
pub(crate) constraints: Vec<Arc<dyn FeatureConstraint + Send + Sync>>,
pub(crate) states: Vec<Arc<dyn FeatureState + Send + Sync>>,
}
impl GoalContext {
pub fn new(
features: &[Feature],
global_objective_map: &[Vec<String>],
local_objective_map: &[Vec<String>],
) -> Result<Self, String> {
let ids_all = features
.iter()
.filter_map(|feature| feature.objective.as_ref().map(|_| feature.name.clone()))
.collect::<Vec<_>>();
let ids_unique = ids_all.iter().collect::<HashSet<_>>();
if ids_unique.len() != ids_all.len() {
return Err(format!(
"some of the features are defined more than once, check ids list: {}",
ids_all.join(",")
));
}
let check_objective_map = |objective_map: &[Vec<String>]| {
let objective_ids_all = objective_map.iter().flat_map(|objective| objective.iter()).collect::<Vec<_>>();
let objective_ids_unique = objective_ids_all.iter().cloned().collect::<HashSet<_>>();
objective_ids_all.len() == objective_ids_unique.len() && objective_ids_unique.is_subset(&ids_unique)
};
if !check_objective_map(global_objective_map) {
return Err(
"global objective map is invalid: it should contain unique ids of the features specified".to_string()
);
}
if !check_objective_map(local_objective_map) {
return Err(
"local objective map is invalid: it should contain unique ids of the features specified".to_string()
);
}
let feature_map = features
.iter()
.filter_map(|feature| feature.objective.as_ref().map(|objective| (feature.name.clone(), objective.clone())))
.collect::<HashMap<_, _>>();
let remap_objectives = |objective_map: &[Vec<String>]| -> Result<Vec<_>, String> {
objective_map.iter().try_fold(Vec::default(), |mut acc_outer, ids| {
acc_outer.push(ids.iter().try_fold(Vec::default(), |mut acc_inner, id| {
if let Some(objective) = feature_map.get(id) {
acc_inner.push(objective.clone());
Ok(acc_inner)
} else {
Err(format!("cannot find objective for feature with id: {id}"))
}
})?);
Ok(acc_outer)
})
};
let hierarchical_objectives = remap_objectives(global_objective_map)?;
let local_objectives = remap_objectives(local_objective_map)?;
let states = features.iter().filter_map(|feature| feature.state.clone()).collect();
let constraints = features.iter().filter_map(|feature| feature.constraint.clone()).collect();
let flat_objectives = hierarchical_objectives.iter().flat_map(|inners| inners.iter()).cloned().collect();
Ok(Self { hierarchical_objectives, flat_objectives, local_objectives, constraints, states })
}
}
#[derive(Clone, Default)]
pub struct Feature {
pub name: String,
pub constraint: Option<Arc<dyn FeatureConstraint + Send + Sync>>,
pub objective: Option<Arc<dyn FeatureObjective<Solution = InsertionContext> + Send + Sync>>,
pub state: Option<Arc<dyn FeatureState + Send + Sync>>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConstraintViolation {
pub code: ViolationCode,
pub stopped: bool,
}
impl ConstraintViolation {
pub fn fail(code: ViolationCode) -> Option<Self> {
Some(ConstraintViolation { code, stopped: true })
}
pub fn skip(code: ViolationCode) -> Option<Self> {
Some(ConstraintViolation { code, stopped: false })
}
pub fn success() -> Option<Self> {
None
}
}
pub type ViolationCode = i32;
pub type StateKey = i32;
#[derive(Default)]
pub struct FeatureBuilder {
feature: Feature,
}
impl FeatureBuilder {
pub fn combine(name: &str, features: &[Feature]) -> Result<Feature, String> {
combine_features(name, features)
}
pub fn from_feature(other: Feature) -> Self {
Self { feature: other }
}
pub fn with_name(mut self, name: &str) -> Self {
self.feature.name = name.to_string();
self
}
pub fn with_constraint<T: FeatureConstraint + Send + Sync + 'static>(mut self, constraint: T) -> Self {
self.feature.constraint = Some(Arc::new(constraint));
self
}
pub fn with_objective<T: FeatureObjective<Solution = InsertionContext> + Send + Sync + 'static>(
mut self,
objective: T,
) -> Self {
self.feature.objective = Some(Arc::new(objective));
self
}
pub fn with_state<T: FeatureState + Send + Sync + 'static>(mut self, state: T) -> Self {
self.feature.state = Some(Arc::new(state));
self
}
pub fn build(self) -> Result<Feature, String> {
let feature = self.feature;
if feature.name == String::default() {
return Err("features with default id are not allowed".to_string());
}
if feature.constraint.is_none() && feature.objective.is_none() {
Err("empty feature is not allowed".to_string())
} else {
Ok(feature)
}
}
}
pub trait FeatureState {
fn accept_insertion(&self, solution_ctx: &mut SolutionContext, route_index: usize, job: &Job);
fn accept_route_state(&self, route_ctx: &mut RouteContext);
fn accept_solution_state(&self, solution_ctx: &mut SolutionContext);
fn state_keys(&self) -> Iter<StateKey>;
}
pub trait FeatureConstraint {
fn evaluate(&self, move_ctx: &MoveContext<'_>) -> Option<ConstraintViolation>;
fn merge(&self, source: Job, candidate: Job) -> Result<Job, ViolationCode>;
}
pub trait FeatureObjective: Objective {
fn estimate(&self, move_ctx: &MoveContext<'_>) -> Cost;
}
impl MultiObjective for GoalContext {
type Solution = InsertionContext;
fn total_order(&self, a: &Self::Solution, b: &Self::Solution) -> Ordering {
unwrap_from_result(self.hierarchical_objectives.iter().try_fold(Ordering::Equal, |_, objectives| {
match dominance_order(a, b, objectives.iter().map(|o| o.as_ref())) {
Ordering::Equal => Ok(Ordering::Equal),
order => Err(order),
}
}))
}
fn fitness<'a>(&'a self, solution: &'a Self::Solution) -> Box<dyn Iterator<Item = f64> + 'a> {
Box::new(self.flat_objectives.iter().map(|o| o.fitness(solution)))
}
fn get_order(&self, a: &Self::Solution, b: &Self::Solution, idx: usize) -> Result<Ordering, String> {
self.flat_objectives
.get(idx)
.map(|o| o.total_order(a, b))
.ok_or_else(|| format!("cannot get total_order with index: {idx}"))
}
fn get_distance(&self, a: &Self::Solution, b: &Self::Solution, idx: usize) -> Result<f64, String> {
self.flat_objectives
.get(idx)
.map(|o| o.distance(a, b))
.ok_or_else(|| format!("cannot get distance with index: {idx}"))
}
fn size(&self) -> usize {
self.flat_objectives.len()
}
}
impl HeuristicObjective for GoalContext {}
impl Shuffled for GoalContext {
fn get_shuffled(&self, random: &(dyn Random + Send + Sync)) -> Self {
let mut hierarchical_objectives = self.hierarchical_objectives.clone();
hierarchical_objectives.shuffle(&mut random.get_rng());
Self { hierarchical_objectives, ..self.clone() }
}
}
impl GoalContext {
pub fn accept_insertion(&self, solution_ctx: &mut SolutionContext, route_index: usize, job: &Job) {
accept_insertion_with_states(&self.states, solution_ctx, route_index, job)
}
pub fn accept_route_state(&self, route_ctx: &mut RouteContext) {
accept_route_state_with_states(&self.states, route_ctx)
}
pub fn accept_solution_state(&self, solution_ctx: &mut SolutionContext) {
accept_solution_state_with_states(&self.states, solution_ctx);
}
pub fn merge(&self, source: Job, candidate: Job) -> Result<Job, ViolationCode> {
merge_with_constraints(&self.constraints, source, candidate)
}
pub fn evaluate(&self, move_ctx: &MoveContext<'_>) -> Option<ConstraintViolation> {
evaluate_with_constraints(&self.constraints, move_ctx)
}
pub fn estimate(&self, move_ctx: &MoveContext<'_>) -> Cost {
self.local_objectives
.iter()
.flat_map(|objectives| objectives.iter().map(|objective| objective.estimate(move_ctx)))
.fold(Cost::default(), |acc, other| acc + other)
}
}