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
//! Provides a way to represent VRP solution in adjacency matrix form (experimental).
//!

#[cfg(test)]
#[path = "../../../tests/unit/models/matrix/decipher_test.rs"]
mod decipher_test;

use super::inserter::ActivityInfoInserter;
use super::AdjacencyMatrix;
use crate::construction::heuristics::{InsertionContext, RouteContext, SolutionContext};
use crate::models::problem::{Actor, ActorDetail, Job, Place, Single};
use crate::models::solution::TourActivity;
use crate::models::Problem;
use crate::utils::DefaultRandom;
use hashbrown::{HashMap, HashSet};
use std::hash::Hash;
use std::sync::Arc;

/// Provides way to encode/decode solution to adjacency matrix representation.
pub struct AdjacencyMatrixDecipher {
    problem: Arc<Problem>,
    activity_direct_index: HashMap<ActivityInfo, usize>,
    activity_reverse_index: HashMap<usize, ActivityInfo>,
    actor_direct_index: HashMap<Arc<Actor>, usize>,
    actor_reverse_index: HashMap<usize, Arc<Actor>>,
}

/// Provides way to store job or actor information to restore tour activity properly.
#[derive(Hash, Eq, PartialEq, Clone)]
pub enum ActivityInfo {
    Job(ActivityWithJob),
    Terminal(ActivityWithActor),
}

/// Represents specific job activity: (job, single index, place index, time window index) schema.
pub type ActivityWithJob = (Job, usize, usize, usize);
/// Represent specific terminal activity: (actor detail, location).
pub type ActivityWithActor = (ActorDetail, usize);

impl AdjacencyMatrixDecipher {
    /// Creates `AdjacencyMatrixDecipher` for the given problem.
    pub fn new(problem: Arc<Problem>) -> Self {
        let mut decipher = Self {
            problem: problem.clone(),
            activity_direct_index: Default::default(),
            activity_reverse_index: Default::default(),
            actor_direct_index: problem.fleet.actors.iter().cloned().zip(1..).collect(),
            actor_reverse_index: (1..).zip(problem.fleet.actors.iter().cloned()).collect(),
        };

        get_unique_actor_details(&problem.fleet.actors).into_iter().for_each(|adk| match (adk.start, adk.end) {
            (Some(_), Some(_)) => {
                decipher.add(ActivityInfo::Terminal((adk.clone(), 0)));
                decipher.add(ActivityInfo::Terminal((adk, 1)));
            }
            (None, Some(end)) => decipher.add(ActivityInfo::Terminal((adk, end))),
            (Some(start), None) => decipher.add(ActivityInfo::Terminal((adk, start))),
            _ => {}
        });

        problem.jobs.all().for_each(|job| {
            match &job {
                Job::Single(single) => vec![(0, single.places.iter().collect::<Vec<_>>())],
                Job::Multi(multi) => (0..)
                    .zip(multi.jobs.iter())
                    .map(|(idx, j)| (idx, j.places.iter().collect::<Vec<_>>()))
                    .collect::<Vec<_>>(),
            }
            .iter()
            .for_each(|(single_idx, places)| {
                (0..).zip(places.iter()).for_each(|(place_idx, place)| {
                    (0..).zip(place.times.iter()).for_each(|(tw_idx, _)| {
                        decipher.add(ActivityInfo::Job((job.clone(), *single_idx, place_idx, tw_idx)));
                    })
                })
            });
        });

        decipher
    }

    /// Encodes solution to adjacency matrix.
    pub fn encode<T: AdjacencyMatrix>(&self, solution_ctx: &SolutionContext) -> T {
        let mut matrix = T::new(self.dimensions());

        solution_ctx.routes.iter().for_each(|rc| {
            let actor = &rc.route.actor;
            let actor_idx = *self.actor_direct_index.get(actor).unwrap() as f64;

            rc.route.tour.legs().for_each(|(items, leg_idx)| {
                match items {
                    [prev, next] => {
                        let from =
                            *self.activity_direct_index.get(&create_activity_info(actor, prev, leg_idx)).unwrap();
                        let to = *self.activity_direct_index.get(&create_activity_info(actor, next, leg_idx)).unwrap();

                        matrix.set_cell(from, to, actor_idx);
                    }
                    [_] => {}
                    _ => panic!("Unexpected route leg configuration."),
                };
            });
        });

        matrix
    }

    /// Decodes a feasible solution from adjacency matrix specified by `matrix` which, potentially
    /// might define an unfeasible solution.
    pub fn decode<T: AdjacencyMatrix>(&self, matrix: &T) -> SolutionContext {
        // NOTE A new context already contains routes with locked jobs which is important as
        // passed AM solution might ignore these rules.
        let mut ctx = InsertionContext::new(self.problem.clone(), Arc::new(DefaultRandom::default()));
        ctx.problem.constraint.accept_solution_state(&mut ctx.solution);

        let mut unprocessed =
            ctx.solution.ignored.iter().chain(ctx.solution.required.iter()).cloned().collect::<HashSet<_>>();
        let mut unassigned: HashSet<Job> = Default::default();
        let mut routes = self.get_routes(&mut ctx.solution, matrix);

        routes.iter_mut().for_each(|mut rc| {
            let actor = &rc.route.actor;
            let actor_idx = *self.actor_direct_index.get(actor).unwrap();

            let start_info = create_activity_info(actor, rc.route.tour.start().unwrap(), 0);
            let start_row_idx = *self.activity_direct_index.get(&start_info).unwrap();
            let activity_infos = self.get_activity_infos(matrix, actor_idx, start_row_idx);

            ActivityInfoInserter::new(&mut ctx, &mut rc, &mut unprocessed, &mut unassigned, activity_infos).insert();
        });

        ctx.solution.required = unprocessed
            .into_iter()
            .chain(unassigned.into_iter())
            .chain(ctx.solution.required.into_iter())
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        ctx.solution.routes = routes;

        ctx.restore();

        ctx.solution
    }

    fn add(&mut self, activity_info: ActivityInfo) {
        assert_eq!(self.activity_direct_index.len(), self.activity_reverse_index.len());

        self.activity_direct_index.insert(activity_info.clone(), self.activity_direct_index.len());
        self.activity_reverse_index.insert(self.activity_reverse_index.len(), activity_info);
    }

    fn dimensions(&self) -> usize {
        self.activity_direct_index.len()
    }

    fn get_routes<T: AdjacencyMatrix>(&self, solution: &mut SolutionContext, matrix: &T) -> Vec<RouteContext> {
        let used_actors = solution.routes.iter().map(|r| r.route.actor.clone()).collect::<HashSet<_>>();
        let mut routes = solution.routes.clone();

        routes.extend(
            matrix
                .values()
                .map(|i| self.actor_reverse_index.get(&(i as usize)).cloned().unwrap())
                .filter(|a| used_actors.get(a).is_none())
                .map(|a| {
                    solution.registry.use_actor(&a);
                    RouteContext::new(a)
                }),
        );

        routes
    }

    fn get_activity_infos<T: AdjacencyMatrix>(
        &self,
        matrix: &T,
        actor_idx: usize,
        start_row_idx: usize,
    ) -> Vec<&ActivityInfo> {
        let mut next_row_idx = start_row_idx;
        let mut activity_infos = vec![];
        let mut processed: HashSet<usize> = Default::default();

        loop {
            if let Some(activity_info_idx) = matrix.scan_row(next_row_idx, |v| v.round() as usize == actor_idx) {
                if processed.contains(&activity_info_idx) {
                    break;
                }
                processed.insert(activity_info_idx);

                activity_infos.push(self.activity_reverse_index.get(&activity_info_idx).unwrap());
                next_row_idx = activity_info_idx;

                continue;
            }
            break;
        }

        activity_infos
    }
}

fn get_unique_actor_details(actors: &[Arc<Actor>]) -> Vec<ActorDetail> {
    let mut unique: HashSet<ActorDetail> = Default::default();
    let mut details = actors.iter().map(|a| a.detail.clone()).collect::<Vec<_>>();

    details.retain(|d| unique.insert(d.clone()));

    details
}

fn create_activity_info(actor: &Arc<Actor>, a: &TourActivity, leg_idx: usize) -> ActivityInfo {
    match a.retrieve_job() {
        Some(job) => {
            let (single_idx, single) = match &job {
                Job::Multi(multi) => {
                    let job = a.job.as_ref().unwrap();
                    let position = multi
                        .jobs
                        .iter()
                        .position(|j| &*j.as_ref() as *const Single == &*job.as_ref() as *const Single)
                        .unwrap();

                    (position, multi.jobs.get(position).unwrap().clone())
                }
                Job::Single(single) => (0, single.clone()),
            };

            let (place_idx, tw_idx) = try_match_activity_place(a, &single.places).unwrap();

            ActivityInfo::Job((job, single_idx, place_idx, tw_idx))
        }
        None => ActivityInfo::Terminal((actor.detail.clone(), if leg_idx > 0 { 1 } else { 0 })),
    }
}

fn try_match_activity_place(activity: &TourActivity, places: &[Place]) -> Option<(usize, usize)> {
    places.iter().enumerate().fold(None, |acc, (place_idx, place)| {
        if acc.is_none()
            && place.location.map_or(true, |location| location == activity.place.location)
            && (activity.place.duration - place.duration).abs() < std::f64::EPSILON
        {
            for (tw_idx, tw) in place.times.iter().enumerate() {
                // NOTE tw offset is considered as match
                let is_correct = tw.as_time_window().map_or(true, |tw| activity.place.time == tw);
                if is_correct {
                    return Some((place_idx, tw_idx));
                }
            }
        }

        acc
    })
}