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
use crate::{IndividTrip, PersonID, Scenario, SpawnTrip, TripMode};
use abstutil::Timer;
use geom::{Duration, Time};
use map_model::Map;
use rand::Rng;
use rand_xorshift::XorShiftRng;
use std::collections::BTreeSet;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum ScenarioModifier {
RepeatDays(usize),
CancelPeople(usize),
ChangeMode {
to_mode: TripMode,
pct_ppl: usize,
departure_filter: (Time, Time),
from_modes: BTreeSet<TripMode>,
},
AddExtraTrips(String),
}
impl ScenarioModifier {
pub fn apply(&self, map: &Map, mut s: Scenario, rng: &mut XorShiftRng) -> Scenario {
match self {
ScenarioModifier::RepeatDays(n) => repeat_days(s, *n),
ScenarioModifier::CancelPeople(pct) => cancel_people(s, *pct, rng),
ScenarioModifier::ChangeMode {
to_mode,
pct_ppl,
departure_filter,
from_modes,
} => {
let pct_ppl = (*pct_ppl as f64) / 100.0;
for person in &mut s.people {
if !rng.gen_bool(pct_ppl) {
continue;
}
for trip in &mut person.trips {
if trip.depart < departure_filter.0 || trip.depart > departure_filter.1 {
continue;
}
if !from_modes.contains(&trip.trip.mode()) {
continue;
}
if let Some(new) =
SpawnTrip::new(trip.trip.start(map), trip.trip.end(map), *to_mode, map)
{
trip.modified = true;
trip.trip = new;
}
}
}
s
}
ScenarioModifier::AddExtraTrips(name) => {
let other: Scenario = abstutil::read_binary(
abstutil::path_scenario(map.get_name(), name),
&mut Timer::throwaway(),
);
for mut p in other.people {
p.id = PersonID(s.people.len());
for trip in &mut p.trips {
trip.modified = true;
}
s.people.push(p);
}
s
}
}
}
pub fn describe(&self) -> String {
match self {
ScenarioModifier::RepeatDays(n) => format!("repeat the entire day {} times", n),
ScenarioModifier::CancelPeople(pct) => {
format!("cancel all trips for {}% of people", pct)
}
ScenarioModifier::ChangeMode {
pct_ppl,
to_mode,
departure_filter,
from_modes,
} => format!(
"change all trips for {}% of people of types {:?} leaving between {} and {} to {}",
pct_ppl,
from_modes,
departure_filter.0.ampm_tostring(),
departure_filter.1.ampm_tostring(),
to_mode.verb()
),
ScenarioModifier::AddExtraTrips(name) => format!("Add extra trips from {}", name),
}
}
}
fn repeat_days(mut s: Scenario, days: usize) -> Scenario {
s.scenario_name = format!("{} (repeated {} days)", s.scenario_name, days);
for person in &mut s.people {
let mut trips = Vec::new();
let mut offset = Duration::ZERO;
for _ in 0..days {
for trip in &person.trips {
let mut new = IndividTrip::new(trip.depart + offset, trip.trip.clone());
new.modified = true;
trips.push(new);
}
offset += Duration::hours(24);
}
person.trips = trips;
}
s
}
fn cancel_people(mut s: Scenario, pct: usize, rng: &mut XorShiftRng) -> Scenario {
let pct = (pct as f64) / 100.0;
for person in &mut s.people {
if rng.gen_bool(pct) {
for trip in &mut person.trips {
trip.modified = true;
trip.cancelled = true;
}
}
}
s
}