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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::collections::BTreeMap;

use rand::Rng;
use rand_xorshift::XorShiftRng;
use serde::{Deserialize, Serialize};

use geom::{Duration, Time};
use map_model::{BuildingID, BusStopID};

use crate::pandemic::{AnyTime, State};
use crate::{CarID, Event, OffMapLocation, Person, PersonID, Scheduler, TripPhaseType};

// TODO This does not model transmission by surfaces; only person-to-person.
// TODO If two people are in the same shared space indefinitely and neither leaves, we don't model
// transmission. It only occurs when people leave a space.

#[derive(Clone)]
pub struct PandemicModel {
    pop: BTreeMap<PersonID, State>,

    bldgs: SharedSpace<BuildingID>,
    remote_bldgs: SharedSpace<OffMapLocation>,
    bus_stops: SharedSpace<BusStopID>,
    buses: SharedSpace<CarID>,
    person_to_bus: BTreeMap<PersonID, CarID>,

    rng: XorShiftRng,
    initialized: bool,
}

// You can schedule callbacks in the future by doing scheduler.push(future time, one of these)
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub enum Cmd {
    BecomeHospitalized(PersonID),
    BecomeQuarantined(PersonID),
}

// TODO Pretend handle_event and handle_cmd also take in some object that lets you do things like:
//
// - replace_future_trips(PersonID, Vec<IndividTrip>)
//
// I'm not exactly sure how this should work yet. Any place you want to change the rest of the
// simulation, just add a comment describing what you want to do exactly, and we'll figure it out
// from there.

impl PandemicModel {
    pub fn new(rng: XorShiftRng) -> PandemicModel {
        PandemicModel {
            pop: BTreeMap::new(),

            bldgs: SharedSpace::new(),
            remote_bldgs: SharedSpace::new(),
            bus_stops: SharedSpace::new(),
            buses: SharedSpace::new(),
            person_to_bus: BTreeMap::new(),

            rng,
            initialized: false,
        }
    }

    // Sorry, initialization order of simulations is still a bit messy. This'll be called at
    // Time::START_OF_DAY after all of the people have been created from a Scenario.
    pub fn initialize(&mut self, population: &Vec<Person>, _scheduler: &mut Scheduler) {
        assert!(!self.initialized);
        self.initialized = true;

        // Seed initially infected people.
        // TODO the intial time is not well set. it should start "before"
        // the beginning of the day. Also
        for p in population {
            let state = State::new(0.5, 0.5);
            let state = if self.rng.gen_bool(State::ini_exposed_ratio()) {
                let next_state = state
                    .start(
                        AnyTime::from(Time::START_OF_DAY),
                        Duration::seconds(std::f64::MAX),
                        &mut self.rng,
                    )
                    .unwrap();
                let next_state = if self.rng.gen_bool(State::ini_infectious_ratio()) {
                    next_state
                        .next_default(AnyTime::from(Time::START_OF_DAY), &mut self.rng)
                        .unwrap()
                } else {
                    next_state
                };
                next_state
            } else {
                state
            };
            self.pop.insert(p.id, state);
        }
    }

    pub fn count_sane(&self) -> usize {
        self.pop
            .iter()
            .filter(|(_, state)| match state {
                State::Sane(_) => true,
                _ => false,
            })
            .count()
        // self.sane.len()
    }

    pub fn count_exposed(&self) -> usize {
        self.pop
            .iter()
            .filter(|(_, state)| match state {
                State::Exposed(_) => true,
                _ => false,
            })
            .count()
        // self.exposed.len()
    }

    pub fn count_infected(&self) -> usize {
        // self.infected.len()
        self.pop
            .iter()
            .filter(|(_, state)| match state {
                State::Infectious(_) | State::Hospitalized(_) => true,
                _ => false,
            })
            .count()
    }

    pub fn count_recovered(&self) -> usize {
        self.pop
            .iter()
            .filter(|(_, state)| match state {
                State::Recovered(_) => true,
                _ => false,
            })
            .count()
        // self.recovered.len()
    }

    pub fn count_dead(&self) -> usize {
        self.pop
            .iter()
            .filter(|(_, state)| match state {
                State::Dead(_) => true,
                _ => false,
            })
            .count()
        // self.recovered.len()
    }

    pub fn count_total(&self) -> usize {
        self.count_sane()
            + self.count_exposed()
            + self.count_infected()
            + self.count_recovered()
            + self.count_dead()
    }

    pub fn handle_event(&mut self, now: Time, ev: &Event, scheduler: &mut Scheduler) {
        assert!(self.initialized);

        match ev {
            Event::PersonEntersBuilding(person, bldg) => {
                self.bldgs.person_enters_space(now, *person, *bldg);
            }
            Event::PersonLeavesBuilding(person, bldg) => {
                if let Some(others) = self.bldgs.person_leaves_space(now, *person, *bldg) {
                    self.transmission(now, *person, others, scheduler);
                } else {
                    panic!("{} left {}, but they weren't inside", person, bldg);
                }
            }
            Event::PersonEntersRemoteBuilding(person, loc) => {
                self.remote_bldgs
                    .person_enters_space(now, *person, loc.clone());
            }
            Event::PersonLeavesRemoteBuilding(person, loc) => {
                if let Some(others) =
                    self.remote_bldgs
                        .person_leaves_space(now, *person, loc.clone())
                {
                    self.transmission(now, *person, others, scheduler);
                } else {
                    panic!("{} left {:?}, but they weren't inside", person, loc);
                }
            }
            Event::TripPhaseStarting(_, p, _, tpt) => {
                let person = *p;
                match tpt {
                    TripPhaseType::WaitingForBus(_, stop) => {
                        self.bus_stops.person_enters_space(now, person, *stop);
                    }
                    TripPhaseType::RidingBus(_, stop, bus) => {
                        let others = self
                            .bus_stops
                            .person_leaves_space(now, person, *stop)
                            .unwrap();
                        self.transmission(now, person, others, scheduler);

                        self.buses.person_enters_space(now, person, *bus);
                        self.person_to_bus.insert(person, *bus);
                    }
                    TripPhaseType::Walking => {
                        // A person can start walking for many reasons, but the only possible state
                        // transition after riding a bus is walking, so use this to detect the end
                        // of a bus ride.
                        if let Some(car) = self.person_to_bus.remove(&person) {
                            let others = self.buses.person_leaves_space(now, person, car).unwrap();
                            self.transmission(now, person, others, scheduler);
                        }
                    }
                    _ => {
                        self.transition(now, person, scheduler);
                    }
                }
            }
            Event::PersonLeavesMap(_person, _, _, loc) => {
                if let Some(_loc) = loc {
                    // TODO Could make a SharedSpace for loc.parcel_id, representing buildings
                    // off-map.
                }
            }
            Event::PersonEntersMap(_person, _, _, loc) => {
                if let Some(_loc) = loc {
                    // TODO But we don't know how long the person spent at these parcels. They
                    // could've taken tons of trips to other off-map parcels in between
                    // PersonLeavesMap and PersonEntersMap.
                }
            }
            _ => {}
        }
    }

    pub fn handle_cmd(&mut self, _now: Time, cmd: Cmd, _scheduler: &mut Scheduler) {
        assert!(self.initialized);

        // TODO Here we might enforce policies. Like severe -> become hospitalized
        // Symptomatic -> stay quaratined, and/or track contacts to quarantine them too (or test
        // them)
        match cmd {
            Cmd::BecomeHospitalized(_person) => {
                // self.hospitalized.insert(person);
            }
            Cmd::BecomeQuarantined(_person) => {
                // self.quarantined.insert(person);
            }
        }
    }

    pub fn get_time(&self, person: PersonID) -> Option<Time> {
        match self.pop.get(&person) {
            Some(state) => state.get_time(),
            None => unreachable!(),
        }
    }

    pub fn is_sane(&self, person: PersonID) -> bool {
        match self.pop.get(&person) {
            Some(state) => state.is_sane(),
            None => unreachable!(),
        }
    }

    pub fn is_infectious(&self, person: PersonID) -> bool {
        match self.pop.get(&person) {
            Some(state) => state.is_infectious(),
            None => unreachable!(),
        }
    }

    pub fn is_exposed(&self, person: PersonID) -> bool {
        match self.pop.get(&person) {
            Some(state) => state.is_exposed(),
            None => unreachable!(),
        }
    }

    pub fn is_recovered(&self, person: PersonID) -> bool {
        match self.pop.get(&person) {
            Some(state) => state.is_recovered(),
            None => unreachable!(),
        }
    }

    pub fn is_dead(&self, person: PersonID) -> bool {
        match self.pop.get(&person) {
            Some(state) => state.is_dead(),
            None => unreachable!(),
        }
    }

    fn infectious_contact(&self, person: PersonID, other: PersonID) -> Option<PersonID> {
        if self.is_sane(person) && self.is_infectious(other) {
            return Some(person);
        } else if self.is_infectious(person) && self.is_sane(other) {
            return Some(other);
        }
        None
    }

    fn transmission(
        &mut self,
        now: Time,
        person: PersonID,
        other_occupants: Vec<(PersonID, Duration)>,
        scheduler: &mut Scheduler,
    ) {
        // person has spent some duration in the same space as other people. Does transmission
        // occur?
        for (other, overlap) in other_occupants {
            if let Some(pid) = self.infectious_contact(person, other) {
                self.become_exposed(now, overlap, pid, scheduler);
            }
        }
    }

    // transition from a state to another without interaction with others
    fn transition(&mut self, now: Time, person: PersonID, _scheduler: &mut Scheduler) {
        let state = self.pop.remove(&person).unwrap();
        let state = state.next(AnyTime::from(now), &mut self.rng).unwrap();
        self.pop.insert(person, state);

        // if self.rng.gen_bool(0.1) {
        //     scheduler.push(
        //         now + self.rand_duration(Duration::hours(1), Duration::hours(3)),
        //         Command::Pandemic(Cmd::BecomeHospitalized(person)),
        //     );
        // }
    }

    fn become_exposed(
        &mut self,
        now: Time,
        overlap: Duration,
        person: PersonID,
        _scheduler: &mut Scheduler,
    ) {
        // When poeple become expose
        let state = self.pop.remove(&person).unwrap();
        assert_eq!(
            state.get_event_time().unwrap().inner_seconds(),
            std::f64::INFINITY
        );
        let state = state
            .start(AnyTime::from(now), overlap, &mut self.rng)
            .unwrap();
        self.pop.insert(person, state);

        // if self.rng.gen_bool(0.1) {
        //     scheduler.push(
        //         now + self.rand_duration(Duration::hours(1), Duration::hours(3)),
        //         Command::Pandemic(Cmd::BecomeHospitalized(person)),
        //     );
        // }
    }
}

#[derive(Clone)]
struct SharedSpace<T: Ord> {
    // Since when has a person been in some shared space?
    // TODO This is an awkward data structure; abstutil::MultiMap is also bad, because key removal
    // would require knowing the time. Want something closer to
    // https://guava.dev/releases/19.0/api/docs/com/google/common/collect/Table.html.
    occupants: BTreeMap<T, Vec<(PersonID, Time)>>,
}

impl<T: Ord> SharedSpace<T> {
    fn new() -> SharedSpace<T> {
        SharedSpace {
            occupants: BTreeMap::new(),
        }
    }

    fn person_enters_space(&mut self, now: Time, person: PersonID, space: T) {
        self.occupants
            .entry(space)
            .or_insert_with(Vec::new)
            .push((person, now));
    }

    // Returns a list of all other people that the person was in the shared space with, and how
    // long their time overlapped. If it returns None, then a bug must have occurred, because
    // somebody has left a space they never entered.
    fn person_leaves_space(
        &mut self,
        now: Time,
        person: PersonID,
        space: T,
    ) -> Option<Vec<(PersonID, Duration)>> {
        // TODO Messy to mutate state inside a retain closure
        let mut inside_since: Option<Time> = None;
        let occupants = self.occupants.entry(space).or_insert_with(Vec::new);
        occupants.retain(|(p, t)| {
            if *p == person {
                inside_since = Some(*t);
                false
            } else {
                true
            }
        });
        // TODO Bug!
        let inside_since = inside_since?;

        Some(
            occupants
                .iter()
                .map(|(p, t)| (*p, now - (*t).max(inside_since)))
                .collect(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn time(x: usize) -> Time {
        Time::START_OF_DAY + Duration::hours(x)
    }

    #[test]
    fn test_overlap() {
        let mut space = SharedSpace::new();
        let mut now = time(0);

        let bldg1 = BuildingID(1);
        let bldg2 = BuildingID(2);

        let person1 = PersonID(1);
        let person2 = PersonID(2);
        let person3 = PersonID(3);

        // Only one person
        space.person_enters_space(now, person1, bldg1);
        now = time(1);
        assert_eq!(
            space.person_leaves_space(now, person1, bldg1),
            Some(Vec::new())
        );

        // Two people at the same time
        now = time(2);
        space.person_enters_space(now, person1, bldg2);
        space.person_enters_space(now, person2, bldg2);
        now = time(3);
        assert_eq!(
            space.person_leaves_space(now, person1, bldg2),
            Some(vec![(person2, Duration::hours(1))])
        );

        // Bug
        assert_eq!(space.person_leaves_space(now, person3, bldg2), None);

        // Different times
        now = time(5);
        space.person_enters_space(now, person1, bldg1);
        now = time(6);
        space.person_enters_space(now, person2, bldg1);
        now = time(7);
        space.person_enters_space(now, person3, bldg1);
        now = time(10);
        assert_eq!(
            space.person_leaves_space(now, person1, bldg1),
            Some(vec![
                (person2, Duration::hours(4)),
                (person3, Duration::hours(3))
            ])
        );
        now = time(12);
        assert_eq!(
            space.person_leaves_space(now, person2, bldg1),
            Some(vec![(person3, Duration::hours(5))])
        );
    }
}