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
#[cfg(test)]
#[path = "../../tests/unit/validation/relations_test.rs"]
mod relations_test;
use super::*;
use hashbrown::HashSet;
fn check_e1200_job_existence(ctx: &ValidationContext, relations: &[Relation]) -> Result<(), FormatError> {
let job_ids = relations
.iter()
.flat_map(|relation| {
relation
.jobs
.iter()
.filter(|&job_id| !is_reserved_job_id(job_id))
.filter(|&job_id| !ctx.job_index.contains_key(job_id))
.cloned()
})
.collect::<Vec<_>>();
if job_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1200".to_string(),
"relation has job id which does not present in the plan".to_string(),
format!("remove from relations or add jobs to the plan, ids: '{}'", job_ids.join(", ")),
))
}
}
fn check_e1201_vehicle_existence(
relations: &[Relation],
vehicle_map: &HashMap<String, &VehicleType>,
) -> Result<(), FormatError> {
let vehicle_ids = relations
.iter()
.map(|relation| relation.vehicle_id.clone())
.filter(|vehicle_id| !vehicle_map.contains_key(vehicle_id))
.collect::<Vec<_>>();
if vehicle_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1201".to_string(),
"relation has vehicle id which does not present in the fleet".to_string(),
format!("remove from relations or add vehicle types to the fleet, ids: '{}'", vehicle_ids.join(", ")),
))
}
}
fn check_e1202_empty_job_list(relations: &[Relation]) -> Result<(), FormatError> {
let has_empty_relations =
relations.iter().any(|relation| relation.jobs.iter().find(|id| !is_reserved_job_id(id)).is_none());
if has_empty_relations {
Err(FormatError::new(
"E1202".to_string(),
"relation has empty job id list".to_string(),
"remove relation with empty jobs list or add job ids to them".to_string(),
))
} else {
Ok(())
}
}
fn check_e1203_no_multiple_places_times(ctx: &ValidationContext, relations: &[Relation]) -> Result<(), FormatError> {
let mut job_ids = relations
.iter()
.filter(|relation| !matches!(relation.type_field, RelationType::Any))
.flat_map(|relation| {
relation
.jobs
.iter()
.filter(|&job_id| !is_reserved_job_id(job_id))
.filter_map(|job_id| ctx.job_index.get(job_id))
.filter(|&job| {
ctx.tasks(job).into_iter().any(|task| {
task.places.len() > 1
|| task.places.iter().any(|place| place.times.as_ref().map_or(false, |tw| tw.len() > 1))
})
})
.map(|job| job.id.clone())
})
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
job_ids.sort();
if job_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1203".to_string(),
"strict or sequence relation has job with multiple places or time windows".to_string(),
format!(
"remove job from relation or specify only one place and time window, job ids: '{}'",
job_ids.join(", ")
),
))
}
}
fn check_e1204_job_assigned_to_multiple_vehicles(relations: &[Relation]) -> Result<(), FormatError> {
let mut job_vehicle_map = HashMap::<String, String>::new();
let job_ids: Vec<String> = relations
.iter()
.flat_map(|relation| {
relation
.jobs
.clone()
.into_iter()
.filter(|job_id| !is_reserved_job_id(job_id))
.filter(|job_id| {
*job_vehicle_map.entry(job_id.clone()).or_insert_with(|| relation.vehicle_id.clone())
!= relation.vehicle_id
})
.collect::<Vec<String>>()
.into_iter()
})
.collect::<Vec<_>>();
if job_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1204".to_string(),
"job is assigned to different vehicles in relations".to_string(),
format!("assign jobs only to one vehicle, ids: '{}'", job_ids.join(", ")),
))
}
}
fn check_e1205_relation_has_correct_shift_index(
relations: &[Relation],
vehicle_map: &HashMap<String, &VehicleType>,
) -> Result<(), FormatError> {
let vehicle_ids: Vec<String> = relations
.iter()
.filter_map(|relation| vehicle_map.get(&relation.vehicle_id).map(|vehicle| (vehicle, relation)))
.filter(|(vehicle, relation)| vehicle.shifts.get(relation.shift_index.unwrap_or(0)).is_none())
.map(|(_, relation)| relation.vehicle_id.clone())
.collect::<Vec<_>>();
if vehicle_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1205".to_string(),
"relation has invalid shift index".to_string(),
format!(
"check that vehicle has enough shifts defined or correct relation, vehicle ids: '{}'",
vehicle_ids.join(", ")
),
))
}
}
fn check_e1206_relation_has_no_missing_shift_properties(
relations: &[Relation],
vehicle_map: &HashMap<String, &VehicleType>,
) -> Result<(), FormatError> {
let vehicle_ids: Vec<String> = relations
.iter()
.filter_map(|relation| {
vehicle_map
.get(&relation.vehicle_id)
.and_then(|vehicle| vehicle.shifts.get(relation.shift_index.unwrap_or(0)))
.map(|vehicle_shift| (vehicle_shift, relation))
})
.filter(|(vehicle_shift, relation)| {
relation.jobs.iter().filter(|job_id| is_reserved_job_id(job_id)).any(|job_id| match job_id.as_str() {
"break" => vehicle_shift.breaks.is_none(),
"dispatch" => vehicle_shift.dispatch.is_none(),
"reload" => vehicle_shift.reloads.is_none(),
"arrival" => vehicle_shift.end.is_none(),
_ => false,
})
})
.map(|(_, relation)| relation.vehicle_id.clone())
.collect::<Vec<_>>();
if vehicle_ids.is_empty() {
Ok(())
} else {
Err(FormatError::new(
"E1206".to_string(),
"relation has special job id which is not defined on vehicle shift".to_string(),
format!(
"remove special job id or add vehicle shift property \
(e.g. break, dispatch, reload), vehicle ids: '{}'",
vehicle_ids.join(", ")
),
))
}
}
pub fn validate_relations(ctx: &ValidationContext) -> Result<(), Vec<FormatError>> {
let vehicle_map = ctx
.vehicles()
.flat_map(|v_type| v_type.vehicle_ids.iter().map(move |id| (id.clone(), v_type)))
.collect::<HashMap<_, _>>();
if let Some(relations) = ctx.problem.plan.relations.as_ref() {
combine_error_results(&[
check_e1200_job_existence(ctx, relations),
check_e1201_vehicle_existence(relations, &vehicle_map),
check_e1202_empty_job_list(relations),
check_e1203_no_multiple_places_times(ctx, relations),
check_e1204_job_assigned_to_multiple_vehicles(relations),
check_e1205_relation_has_correct_shift_index(relations, &vehicle_map),
check_e1206_relation_has_no_missing_shift_properties(relations, &vehicle_map),
])
} else {
Ok(())
}
}