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
#[cfg(test)]
#[path = "../../../../tests/unit/solver/mutation/ruin/worst_jobs_removal_test.rs"]
mod worst_jobs_removal_test;

use super::*;
use crate::construction::heuristics::{InsertionContext, RouteContext};
use crate::models::common::{Cost, Timestamp};
use crate::models::problem::{Actor, Job, TransportCost};
use crate::models::solution::Activity;
use crate::solver::RefinementContext;
use crate::utils::parallel_collect;
use hashbrown::{HashMap, HashSet};
use rand::prelude::*;
use std::cmp::Ordering::Less;
use std::iter::once;
use std::sync::{Arc, RwLock};

/// A ruin strategy which detects the most cost expensive jobs in each route and delete them
/// with their neighbours.
pub struct WorstJobRemoval {
    /// Specifies limitation for job removal.
    limit: JobRemovalLimit,
    /// Amount of jobs to skip.
    worst_skip: usize,
}

impl WorstJobRemoval {
    /// Creates a new instance of `WorstJobRemoval`.
    pub fn new(worst_skip: usize, limit: JobRemovalLimit) -> Self {
        Self { limit, worst_skip }
    }
}

impl Default for WorstJobRemoval {
    fn default() -> Self {
        Self::new(4, JobRemovalLimit::default())
    }
}

impl Ruin for WorstJobRemoval {
    fn run(&self, _refinement_ctx: &RefinementContext, mut insertion_ctx: InsertionContext) -> InsertionContext {
        let problem = insertion_ctx.problem.clone();
        let random = insertion_ctx.random.clone();

        let can_remove_job = |job: &Job| -> bool {
            let solution = &insertion_ctx.solution;
            !solution.locked.contains(job) && !solution.unassigned.contains_key(job)
        };

        let mut route_jobs = get_route_jobs(&insertion_ctx.solution);
        let mut routes_savings = get_routes_cost_savings(&insertion_ctx);
        let removed_jobs: RwLock<HashSet<Job>> = RwLock::new(HashSet::default());

        routes_savings.shuffle(&mut insertion_ctx.random.get_rng());

        let affected = get_removal_chunk_size(&insertion_ctx, &self.limit);

        routes_savings.iter().take_while(|_| removed_jobs.read().unwrap().len() < affected).for_each(
            |(rc, savings)| {
                let skip = savings.len().min(random.uniform_int(0, self.worst_skip as i32) as usize);
                let worst = savings.iter().filter(|(job, _)| can_remove_job(job)).nth(skip);

                if let Some((job, _)) = worst {
                    // TODO ensure that we do not remove more jobs than specified by affected
                    let remove = random.uniform_int(self.limit.min as i32, self.limit.max as i32) as usize;
                    once(job.clone())
                        .chain(
                            problem
                                .jobs
                                .neighbors(rc.route.actor.vehicle.profile, &job, Timestamp::default())
                                .filter(|(_, cost)| *cost > 0.)
                                .map(|(job, _)| job)
                                .cloned(),
                        )
                        .filter(|job| can_remove_job(job))
                        .take(remove)
                        .for_each(|job| {
                            // NOTE job can be absent if it is unassigned
                            if let Some(rc) = route_jobs.get_mut(&job) {
                                // NOTE actual insertion context modification via route mut
                                if rc.route_mut().tour.remove(&job) {
                                    removed_jobs.write().unwrap().insert(job);
                                }
                            }
                        });
                }
            },
        );

        removed_jobs.write().unwrap().iter().for_each(|job| insertion_ctx.solution.required.push(job.clone()));

        insertion_ctx
    }
}

fn get_routes_cost_savings(insertion_ctx: &InsertionContext) -> Vec<(RouteContext, Vec<(Job, Cost)>)> {
    parallel_collect(&insertion_ctx.solution.routes, |rc| {
        let actor = rc.route.actor.as_ref();
        let mut savings: Vec<(Job, Cost)> = rc
            .route
            .tour
            .all_activities()
            .as_slice()
            .windows(3)
            .fold(HashMap::<Job, Cost>::default(), |mut acc, iter| match iter {
                [start, eval, end] => {
                    let savings = get_cost_savings(actor, start, eval, end, &insertion_ctx.problem.transport);
                    let job = eval.retrieve_job().unwrap_or_else(|| panic!("Unexpected activity without job"));
                    *acc.entry(job).or_insert(0.) += savings;

                    acc
                }
                _ => panic!("Unexpected activity window"),
            })
            .drain()
            .collect();
        savings.sort_by(|(_, a), (_, b)| b.partial_cmp(a).unwrap_or(Less));

        (rc.clone(), savings)
    })
}

#[inline(always)]
fn get_cost_savings(
    actor: &Actor,
    start: &Activity,
    middle: &Activity,
    end: &Activity,
    transport: &Arc<dyn TransportCost + Send + Sync>,
) -> Cost {
    get_cost(actor, start, middle, transport) + get_cost(actor, middle, end, transport)
        - get_cost(actor, start, end, transport)
}

#[inline(always)]
fn get_cost(actor: &Actor, from: &Activity, to: &Activity, transport: &Arc<dyn TransportCost + Send + Sync>) -> Cost {
    transport.cost(actor, from.place.location, to.place.location, from.schedule.departure)
}