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
#[cfg(test)]
#[path = "../../../tests/unit/construction/constraints/locking_test.rs"]
mod locking_test;

use crate::construction::constraints::*;
use crate::construction::heuristics::{ActivityContext, RouteContext, SolutionContext};
use crate::models::problem::{Actor, Fleet, Job};
use crate::models::{Lock, LockOrder, LockPosition};
use hashbrown::{HashMap, HashSet};
use std::slice::Iter;
use std::sync::Arc;

/// A module which allows to lock specific actors within specific jobs using different rules.
pub struct StrictLockingModule {
    state_keys: Vec<i32>,
    constraints: Vec<ConstraintVariant>,
}

impl ConstraintModule for StrictLockingModule {
    fn accept_insertion(&self, _solution_ctx: &mut SolutionContext, _route_index: usize, _job: &Job) {}

    fn accept_route_state(&self, _ctx: &mut RouteContext) {}

    fn accept_solution_state(&self, _ctx: &mut SolutionContext) {}

    fn state_keys(&self) -> Iter<i32> {
        self.state_keys.iter()
    }

    fn get_constraints(&self) -> Iter<ConstraintVariant> {
        self.constraints.iter()
    }
}

impl StrictLockingModule {
    /// Creates an instance of `StrictLockingModule`.
    pub fn new(fleet: &Fleet, locks: &[Arc<Lock>], code: i32) -> Self {
        let mut rules = vec![];
        let mut conditions = HashMap::new();
        locks.iter().for_each(|lock| {
            let condition = lock.condition.clone();
            lock.details.iter().for_each(|detail| {
                // NOTE create rule only for strict order
                if let LockOrder::Strict = detail.order {
                    assert!(!detail.jobs.is_empty());
                    rules.push(Arc::new(Rule {
                        condition: condition.clone(),
                        position: detail.position.clone(),
                        index: JobIndex {
                            first: detail.jobs.first().unwrap().clone(),
                            last: detail.jobs.last().unwrap().clone(),
                            jobs: detail.jobs.iter().cloned().collect(),
                        },
                    }));
                }

                detail.jobs.iter().cloned().collect::<HashSet<Job>>().into_iter().for_each(|job| {
                    conditions.insert(job, condition.clone());
                });
            });
        });

        let mut actor_rules = HashMap::new();
        fleet.actors.iter().for_each(|actor| {
            actor_rules.insert(actor.clone(), rules.iter().filter(|rule| (rule.condition)(actor)).cloned().collect());
        });

        Self {
            state_keys: vec![],
            constraints: vec![
                ConstraintVariant::HardRoute(Arc::new(StrictLockingHardRouteConstraint { code, conditions })),
                ConstraintVariant::HardActivity(Arc::new(StrictLockingHardActivityConstraint {
                    code,
                    rules: actor_rules,
                })),
            ],
        }
    }
}

struct StrictLockingHardRouteConstraint {
    code: i32,
    conditions: HashMap<Job, Arc<dyn Fn(&Actor) -> bool + Sync + Send>>,
}

impl HardRouteConstraint for StrictLockingHardRouteConstraint {
    fn evaluate_job(&self, _: &SolutionContext, ctx: &RouteContext, job: &Job) -> Option<RouteConstraintViolation> {
        if let Some(condition) = self.conditions.get(job) {
            if !(condition)(&ctx.route.actor) {
                return Some(RouteConstraintViolation { code: self.code });
            }
        }

        None
    }
}

struct StrictLockingHardActivityConstraint {
    code: i32,
    rules: HashMap<Arc<Actor>, Vec<Arc<Rule>>>,
}

impl HardActivityConstraint for StrictLockingHardActivityConstraint {
    fn evaluate_activity(
        &self,
        route_ctx: &RouteContext,
        activity_ctx: &ActivityContext,
    ) -> Option<ActivityConstraintViolation> {
        if let Some(rules) = self.rules.get(&route_ctx.route.actor) {
            let can_insert = rules.iter().all(|rule| {
                rule.can_insert(
                    &activity_ctx.target.retrieve_job(),
                    &activity_ctx.prev.retrieve_job(),
                    &activity_ctx.next.and_then(|n| n.retrieve_job()),
                )
            });

            if !can_insert {
                return Some(ActivityConstraintViolation { code: self.code, stopped: false });
            }
        }

        None
    }
}

struct JobIndex {
    first: Job,
    last: Job,
    jobs: HashSet<Job>,
}

/// Represents a rule created from lock model.
struct Rule {
    condition: Arc<dyn Fn(&Actor) -> bool + Sync + Send>,
    position: LockPosition,
    index: JobIndex,
}

impl Rule {
    /// Checks whether a new job can be inserted between given prev/next according to rules.
    pub fn can_insert(&self, job: &Option<Job>, prev: &Option<Job>, next: &Option<Job>) -> bool {
        self.is_in_rule(job)
            || match self.position {
                LockPosition::Any => self.can_insert_after(prev, next) || self.can_insert_before(prev, next),
                LockPosition::Departure => self.can_insert_after(prev, next),
                LockPosition::Arrival => self.can_insert_before(prev, &next),
                LockPosition::Fixed => false,
            }
    }

    fn contains(&self, job: &Job) -> bool {
        self.index.jobs.contains(job)
    }

    /// Checks whether given job is in rule. Such jobs are inserted manually and should not by
    /// prevented from insertion.
    fn is_in_rule(&self, job: &Option<Job>) -> bool {
        job.as_ref().map_or(false, |job| self.contains(job))
    }

    /// Checks whether a new job can be inserted between given prev/next according to after rule.
    fn can_insert_after(&self, prev: &Option<Job>, next: &Option<Job>) -> bool {
        prev.as_ref().map_or(false, |p| !self.contains(p) || *p == self.index.last)
            && next.as_ref().map_or(true, |n| !self.contains(n))
    }

    /// Checks whether a new job can be inserted between given prev/next according to before rule.
    fn can_insert_before(&self, prev: &Option<Job>, next: &Option<Job>) -> bool {
        next.as_ref().map_or(false, |n| !self.contains(n) || *n == self.index.first)
            && prev.as_ref().map_or(true, |p| !self.contains(p))
    }
}