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
mod ui;

use std::collections::BTreeSet;

use abstio::MapName;
use abstutil::Timer;
use geom::{Duration, Time};
use map_gui::tools::compare_counts::CompareCounts;
use map_model::{PathConstraints, PathRequest, PathfinderCaching};
use synthpop::{Scenario, TrafficCounts, TripEndpoint, TripMode};
use widgetry::EventCtx;

pub use self::ui::ShowResults;
use crate::App;

// TODO Configurable main road penalty, like in the pathfinding tool
// TODO Share structure or pieces with Ungap's predict mode
// ... can't we just produce data of a certain shape, and have a UI pretty tuned for that?

// This gets incrementally recalculated when stuff changes.
//
// - all_trips and everything else depends just on the map (we only have one scenario per map now)
// - filtered_trips depend on filters
// - the 'b' and 'relative' parts of compare_counts depend on change_key (for when the map is edited)
pub struct Impact {
    pub map: MapName,
    pub filters: Filters,

    all_trips: Vec<PathRequest>,
    // A subset of all_trips, and the number of times somebody takes the same trip
    filtered_trips: Vec<(PathRequest, usize)>,

    pub compare_counts: CompareCounts,
    pub change_key: usize,
}

#[derive(PartialEq)]
pub struct Filters {
    pub modes: BTreeSet<TripMode>,
    // TODO Has no effect yet. Do we need to store the TripEndpoints / can we detect from the
    // PathRequest reasonably?
    pub include_borders: bool,
    pub departure_time: (Time, Time),
}

impl Impact {
    pub fn empty(ctx: &EventCtx) -> Self {
        Self {
            map: MapName::new("zz", "place", "holder"),
            filters: Filters {
                modes: vec![TripMode::Drive].into_iter().collect(),
                include_borders: true,
                departure_time: (Time::START_OF_DAY, end_of_day()),
            },

            all_trips: Vec::new(),
            filtered_trips: Vec::new(),

            compare_counts: CompareCounts::empty(ctx),
            change_key: 0,
        }
    }

    fn from_scenario(
        ctx: &mut EventCtx,
        app: &App,
        scenario: Scenario,
        timer: &mut Timer,
    ) -> Impact {
        let mut impact = Impact::empty(ctx);
        let map = &app.map;

        impact.map = app.map.get_name().clone();
        impact.change_key = app.session.modal_filters.change_key;
        impact.all_trips = timer
            .parallelize("analyze trips", scenario.all_trips().collect(), |trip| {
                TripEndpoint::path_req(trip.origin, trip.destination, trip.mode, map)
            })
            .into_iter()
            .flatten()
            .collect();
        impact.trips_changed(ctx, app, timer);
        impact.compare_counts.autoselect_layer();
        impact
    }

    fn trips_changed(&mut self, ctx: &mut EventCtx, app: &App, timer: &mut Timer) {
        let map = &app.map;
        let constraints: BTreeSet<PathConstraints> = self
            .filters
            .modes
            .iter()
            .map(|m| m.to_constraints())
            .collect();
        self.filtered_trips = PathRequest::deduplicate(
            map,
            self.all_trips
                .iter()
                .filter(|req| constraints.contains(&req.constraints))
                .cloned()
                .collect(),
        );

        let counts_a = TrafficCounts::from_path_requests(
            map,
            // Don't bother describing all the trip filtering
            "before filters".to_string(),
            &self.filtered_trips,
            map.routing_params().clone(),
            PathfinderCaching::NoCache,
            timer,
        );

        let counts_b = {
            let mut params = map.routing_params().clone();
            app.session.modal_filters.update_routing_params(&mut params);
            // Since we're making so many requests, it's worth it to rebuild a contraction
            // hierarchy. And since we're single-threaded, no complications there.
            TrafficCounts::from_path_requests(
                map,
                // Don't bother describing all the trip filtering
                "after filters".to_string(),
                &self.filtered_trips,
                params,
                PathfinderCaching::CacheCH,
                timer,
            )
        };

        self.compare_counts =
            CompareCounts::new(ctx, app, counts_a, counts_b, self.compare_counts.layer);
    }

    fn map_edits_changed(&mut self, ctx: &mut EventCtx, app: &App, timer: &mut Timer) {
        self.change_key = app.session.modal_filters.change_key;
        let map = &app.map;

        let counts_b = {
            let mut params = map.routing_params().clone();
            app.session.modal_filters.update_routing_params(&mut params);
            // Since we're making so many requests, it's worth it to rebuild a contraction
            // hierarchy. And since we're single-threaded, no complications there.
            TrafficCounts::from_path_requests(
                map,
                // Don't bother describing all the trip filtering
                "after filters".to_string(),
                &self.filtered_trips,
                params,
                PathfinderCaching::CacheCH,
                timer,
            )
        };
        self.compare_counts.recalculate_b(ctx, app, counts_b);
    }
}

// TODO Fixed, and sadly not const
fn end_of_day() -> Time {
    Time::START_OF_DAY + Duration::hours(24)
}