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
use std::collections::BTreeSet;
use std::fmt;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use abstio::{CityName, MapName};
use abstutil::prettyprint_usize;
use geom::Time;
use map_model::Map;
use crate::{OrigPersonID, TripEndpoint, TripMode};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Scenario {
pub scenario_name: String,
pub map_name: MapName,
pub people: Vec<PersonSpec>,
pub only_seed_buses: Option<BTreeSet<String>>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct PersonSpec {
pub orig_id: Option<OrigPersonID>,
pub trips: Vec<IndividTrip>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct IndividTrip {
pub depart: Time,
pub origin: TripEndpoint,
pub destination: TripEndpoint,
pub mode: TripMode,
pub purpose: TripPurpose,
pub cancelled: bool,
pub modified: bool,
}
impl IndividTrip {
pub fn new(
depart: Time,
purpose: TripPurpose,
origin: TripEndpoint,
destination: TripEndpoint,
mode: TripMode,
) -> IndividTrip {
IndividTrip {
depart,
origin,
destination,
mode,
purpose,
cancelled: false,
modified: false,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum TripPurpose {
Home,
Work,
School,
Escort,
PersonalBusiness,
Shopping,
Meal,
Social,
Recreation,
Medical,
ParkAndRideTransfer,
}
impl fmt::Display for TripPurpose {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
TripPurpose::Home => "home",
TripPurpose::Work => "work",
TripPurpose::School => "school",
TripPurpose::Escort => "escort",
TripPurpose::PersonalBusiness => "personal business",
TripPurpose::Shopping => "shopping",
TripPurpose::Meal => "eating",
TripPurpose::Social => "social",
TripPurpose::Recreation => "recreation",
TripPurpose::Medical => "medical",
TripPurpose::ParkAndRideTransfer => "park-and-ride transfer",
}
)
}
}
impl Scenario {
pub fn save(&self) {
abstio::write_binary(
abstio::path_scenario(&self.map_name, &self.scenario_name),
self,
);
}
pub fn empty(map: &Map, name: &str) -> Scenario {
Scenario {
scenario_name: name.to_string(),
map_name: map.get_name().clone(),
people: Vec::new(),
only_seed_buses: Some(BTreeSet::new()),
}
}
pub fn remove_weird_schedules(mut self) -> Scenario {
let orig = self.people.len();
self.people.retain(|person| match person.check_schedule() {
Ok(()) => true,
Err(err) => {
println!("{}", err);
false
}
});
warn!(
"{} of {} people have nonsense schedules",
prettyprint_usize(orig - self.people.len()),
prettyprint_usize(orig)
);
self
}
pub fn all_trips(&self) -> impl Iterator<Item = &IndividTrip> {
self.people.iter().flat_map(|p| p.trips.iter())
}
pub fn default_scenario_for_map(name: &MapName) -> String {
if name.city == CityName::seattle()
&& abstio::file_exists(abstio::path_scenario(name, "weekday"))
{
return "weekday".to_string();
}
if name.city.country == "gb" {
for x in ["background", "base_with_bg"] {
if abstio::file_exists(abstio::path_scenario(name, x)) {
return x.to_string();
}
}
}
"home_to_work".to_string()
}
}
impl PersonSpec {
pub fn check_schedule(&self) -> Result<()> {
if self.trips.is_empty() {
bail!("Person ({:?}) has no trips at all", self.orig_id);
}
for pair in self.trips.windows(2) {
if pair[0].depart >= pair[1].depart {
bail!(
"Person ({:?}) starts two trips in the wrong order: {} then {}",
self.orig_id,
pair[0].depart,
pair[1].depart
);
}
if pair[0].destination != pair[1].origin {
if matches!(pair[0].destination, TripEndpoint::Border(_))
&& matches!(pair[1].origin, TripEndpoint::Border(_))
{
continue;
}
bail!(
"Person ({:?}) warps from {:?} to {:?} during adjacent trips",
self.orig_id,
pair[0].destination,
pair[1].origin
);
}
}
for trip in &self.trips {
if trip.origin == trip.destination {
bail!(
"Person ({:?}) has a trip from/to the same place: {:?}",
self.orig_id,
trip.origin
);
}
}
Ok(())
}
}