vrp_core/models/solution/
route.rs1use crate::models::common::{Distance, Duration, Location, Schedule, TimeWindow};
2use crate::models::problem::{Actor, Job, Multi, Single};
3use crate::models::solution::Tour;
4use crate::utils::short_type_name;
5use rosomaxa::prelude::Float;
6use std::fmt::{Debug, Formatter};
7use std::sync::Arc;
8
9#[derive(Clone, Debug, Default)]
11pub struct Commute {
12 pub forward: CommuteInfo,
14
15 pub backward: CommuteInfo,
17}
18
19#[derive(Clone, Debug)]
21pub struct CommuteInfo {
22 pub location: Location,
24
25 pub distance: Distance,
27
28 pub duration: Duration,
30}
31
32#[derive(Clone, Debug)]
34pub struct Place {
35 pub idx: usize,
37
38 pub location: Location,
40
41 pub duration: Duration,
43
44 pub time: TimeWindow,
46}
47
48#[derive(Debug)]
50pub struct Activity {
51 pub place: Place,
53
54 pub schedule: Schedule,
56
57 pub job: Option<Arc<Single>>,
60
61 pub commute: Option<Commute>,
63}
64
65pub struct Route {
67 pub actor: Arc<Actor>,
69
70 pub tour: Tour,
72}
73
74impl Route {
75 pub fn deep_copy(&self) -> Self {
77 Self { actor: self.actor.clone(), tour: self.tour.deep_copy() }
78 }
79}
80
81impl Debug for Route {
82 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83 f.debug_struct(short_type_name::<Self>()).field("actor", self.actor.as_ref()).field("tour", &self.tour).finish()
84 }
85}
86
87impl Activity {
88 pub fn new_with_job(job: Arc<Single>) -> Self {
90 Activity {
91 place: Place { idx: 0, location: 0, duration: 0.0, time: TimeWindow { start: 0.0, end: Float::MAX } },
92 schedule: Schedule { arrival: 0.0, departure: 0.0 },
93 job: Some(job),
94 commute: None,
95 }
96 }
97
98 pub fn deep_copy(&self) -> Self {
100 Self {
101 place: Place {
102 idx: self.place.idx,
103 location: self.place.location,
104 duration: self.place.duration,
105 time: self.place.time.clone(),
106 },
107 schedule: self.schedule.clone(),
108 job: self.job.clone(),
109 commute: self.commute.clone(),
110 }
111 }
112
113 pub fn has_same_job(&self, job: &Job) -> bool {
115 self.retrieve_job().as_ref().map_or(false, |other| other == job)
116 }
117
118 pub fn retrieve_job(&self) -> Option<Job> {
120 match self.job.as_ref() {
121 Some(single) => Multi::roots(single).map(Job::Multi).or_else(|| Some(Job::Single(single.clone()))),
122 _ => None,
123 }
124 }
125}
126
127impl Commute {
128 pub fn is_zero_distance(&self) -> bool {
130 self.forward.is_zero_distance() & self.backward.is_zero_distance()
131 }
132
133 pub fn duration(&self) -> Duration {
135 self.forward.duration + self.backward.duration
136 }
137}
138
139impl Default for CommuteInfo {
140 fn default() -> Self {
141 Self { location: 0, distance: 0., duration: 0. }
142 }
143}
144
145impl CommuteInfo {
146 pub fn is_zero_distance(&self) -> bool {
148 let is_zero_distance = self.distance == 0.;
149
150 if is_zero_distance && self.duration != 0. {
151 unreachable!("expected to have duration to be zero, got: {}", self.duration);
152 }
153
154 is_zero_distance
155 }
156}