File size: 3,581 Bytes
fb8fccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Public dataset ids and generator dispatch for delivery demos.
//!
//! City-specific modules only contain depots and visit groups. This file turns
//! those static fixtures into normalized `Plan` values with vehicles, delivery
//! ids, and deterministic service windows.

use std::str::FromStr;

use rand::rngs::StdRng;
use rand::{RngExt, SeedableRng};

use super::types::{LocationData, VEHICLE_NAMES};
use super::{firenze, hartford, philadelphia};
use crate::domain::{Delivery, Plan, Vehicle};

#[derive(Debug, Clone, Copy)]
pub enum DemoData {
    Philadelphia,
    Hartford,
    Firenze,
}

impl DemoData {
    pub fn id(self) -> &'static str {
        match self {
            DemoData::Philadelphia => "PHILADELPHIA",
            DemoData::Hartford => "HARTFORD",
            DemoData::Firenze => "FIRENZE",
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            DemoData::Philadelphia => "Philadelphia",
            DemoData::Hartford => "Hartford",
            DemoData::Firenze => "Firenze",
        }
    }
}

impl FromStr for DemoData {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_uppercase().as_str() {
            "PHILADELPHIA" => Ok(DemoData::Philadelphia),
            "HARTFORD" => Ok(DemoData::Hartford),
            "FIRENZE" => Ok(DemoData::Firenze),
            _ => Err(()),
        }
    }
}

pub fn generate(demo: DemoData) -> Plan {
    match demo {
        DemoData::Philadelphia => generate_demo_data(
            demo,
            0,
            philadelphia::DEPOTS,
            philadelphia::VISIT_GROUPS,
            6 * 3600,
            36,
            48,
        ),
        DemoData::Hartford => generate_demo_data(
            demo,
            1,
            hartford::DEPOTS,
            hartford::VISIT_GROUPS,
            6 * 3600,
            24,
            34,
        ),
        DemoData::Firenze => generate_demo_data(
            demo,
            2,
            firenze::DEPOTS,
            firenze::VISIT_GROUPS,
            6 * 3600,
            38,
            52,
        ),
    }
}

/// Builds one city plan from static stops and deterministic vehicle settings.
fn generate_demo_data(
    demo: DemoData,
    seed: u64,
    depots: &[LocationData],
    stop_groups: &[&[LocationData]],
    departure_time: i64,
    min_capacity: i32,
    max_capacity: i32,
) -> Plan {
    let mut rng = StdRng::seed_from_u64(seed);
    let vehicles = depots
        .iter()
        .enumerate()
        .map(|(idx, depot)| {
            Vehicle::new(
                idx,
                VEHICLE_NAMES[idx % VEHICLE_NAMES.len()],
                rng.random_range(min_capacity..=max_capacity),
                depot.lat,
                depot.lng,
                departure_time,
            )
        })
        .collect::<Vec<_>>();

    let deliveries = stop_groups
        .iter()
        .flat_map(|stops| stops.iter())
        .enumerate()
        .map(|(idx, location)| {
            let (kind, min_start_time, max_end_time, demand_range, service_range) =
                location.customer_type.profile();
            Delivery::new(
                idx,
                location.name,
                kind,
                (location.lat, location.lng),
                rng.random_range(demand_range.0..=demand_range.1),
                (min_start_time, max_end_time),
                rng.random_range(service_range.0..=service_range.1),
            )
        })
        .collect::<Vec<_>>();

    Plan::new(demo.label(), deliveries, vehicles)
}