vrp_core/models/solution/
route.rs

1use 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/// Specifies an extra commute information to reach the actual place.
10#[derive(Clone, Debug, Default)]
11pub struct Commute {
12    /// An commute information to reach place from other location.
13    pub forward: CommuteInfo,
14
15    /// An commute information to get out from the place to the next location.
16    pub backward: CommuteInfo,
17}
18
19/// Commute information.
20#[derive(Clone, Debug)]
21pub struct CommuteInfo {
22    /// A previous or next location.
23    pub location: Location,
24
25    /// Travelled distance.
26    pub distance: Distance,
27
28    /// Travelled duration.
29    pub duration: Duration,
30}
31
32/// Specifies activity place.
33#[derive(Clone, Debug)]
34pub struct Place {
35    /// An index of the place in original job definition.
36    pub idx: usize,
37
38    /// Location where activity is performed.
39    pub location: Location,
40
41    /// Specifies activity's duration.
42    pub duration: Duration,
43
44    /// Specifies activity's time window: an interval when job is allowed to be started.
45    pub time: TimeWindow,
46}
47
48/// Represents activity which is needed to be performed.
49#[derive(Debug)]
50pub struct Activity {
51    /// Specifies activity details.
52    pub place: Place,
53
54    /// Specifies activity's schedule including commute time.
55    pub schedule: Schedule,
56
57    /// Specifies associated job. Empty if it has no association with a single job (e.g. tour start or end).
58    /// If single job is part of multi job, then original job can be received via `retrieve_job` method.
59    pub job: Option<Arc<Single>>,
60
61    /// An extra commute time to the place.
62    pub commute: Option<Commute>,
63}
64
65/// Represents a tour performing jobs.
66pub struct Route {
67    /// An actor associated within route.
68    pub actor: Arc<Actor>,
69
70    /// Specifies job tour assigned to this route.
71    pub tour: Tour,
72}
73
74impl Route {
75    /// Returns a deep copy of `Route`.
76    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    /// Creates an activity with a job.
89    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    /// Creates a deep copy of `Activity`.
99    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    /// Checks whether activity has given job.
114    pub fn has_same_job(&self, job: &Job) -> bool {
115        self.retrieve_job().as_ref().map_or(false, |other| other == job)
116    }
117
118    /// Returns job if activity has it.
119    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    /// Checks whether there is no distance costs for commute.
129    pub fn is_zero_distance(&self) -> bool {
130        self.forward.is_zero_distance() & self.backward.is_zero_distance()
131    }
132
133    /// Gets total commute duration.
134    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    /// Checks whether there is no distance costs for part of commute.
147    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}