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
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::{Path, PathConstraints, PathRequest, Pathfinder, RoadID};
use synthpop::{Scenario, TrafficCounts, TripEndpoint, TripMode};
use widgetry::EventCtx;

pub use self::ui::ShowResults;
use crate::filters::ChangeKey;
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: ChangeKey,
}

#[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: ChangeKey::default(),
        }
    }

    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.get_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.get_pathfinder(),
            timer,
        );

        let counts_b = self.counts_b(app, timer);

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

    fn map_edits_changed(&mut self, ctx: &mut EventCtx, app: &App, timer: &mut Timer) {
        self.change_key = app.session.modal_filters.get_change_key();
        let counts_b = self.counts_b(app, timer);
        self.compare_counts.recalculate_b(ctx, app, counts_b);
    }

    fn counts_b(&self, app: &App, timer: &mut Timer) -> TrafficCounts {
        let constraints: BTreeSet<PathConstraints> = self
            .filters
            .modes
            .iter()
            .map(|m| m.to_constraints())
            .collect();

        let map = &app.map;
        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.
        // This depends on the current map edits, so no need to cache
        let pathfinder_after =
            Pathfinder::new_ch(map, params, constraints.into_iter().collect(), timer);

        // We can't simply use TrafficCounts::from_path_requests. Due to spurious diffs with paths,
        // we need to skip cases where the path before and after have the same cost. It's easiest
        // (code-wise) to just repeat some calculation here.
        let mut counts = TrafficCounts::from_path_requests(
            map,
            // Don't bother describing all the trip filtering
            "after filters".to_string(),
            &vec![],
            &pathfinder_after,
            timer,
        );

        timer.start_iter("calculate routes", self.filtered_trips.len());
        for (req, count) in &self.filtered_trips {
            timer.next();
            if let (Some(path1), Some(path2)) = (
                map.get_pathfinder().pathfind_v2(req.clone(), map),
                pathfinder_after.pathfind_v2(req.clone(), map),
            ) {
                if path1.get_cost() == path2.get_cost() {
                    // When the path maybe changed but the cost is the same, just count it the same
                    // as the original path
                    counts.update_with_path(path1, *count, map);
                } else {
                    counts.update_with_path(path2, *count, map);
                }
            }
        }

        counts
    }

    /// Returns routes that start or stop crossing the given road. Returns paths (before filters,
    /// after)
    pub fn find_changed_routes(
        &self,
        app: &App,
        r: RoadID,
        timer: &mut Timer,
    ) -> Vec<(Path, Path)> {
        let map = &app.map;
        // TODO Cache the pathfinder. It depends both on the change_key and modes belonging to
        // filtered_trips.
        let pathfinder_after = {
            let constraints: BTreeSet<PathConstraints> = self
                .filters
                .modes
                .iter()
                .map(|m| m.to_constraints())
                .collect();
            let mut params = map.routing_params().clone();
            app.session.modal_filters.update_routing_params(&mut params);
            Pathfinder::new_ch(map, params, constraints.into_iter().collect(), timer)
        };

        let mut changed = Vec::new();
        timer.start_iter("find changed routes", self.filtered_trips.len());
        for (req, _) in &self.filtered_trips {
            timer.next();
            if let (Some(path1), Some(path2)) = (
                map.get_pathfinder().pathfind_v2(req.clone(), map),
                pathfinder_after.pathfind_v2(req.clone(), map),
            ) {
                // Skip spurious changes where the cost matches.
                if path1.get_cost() == path2.get_cost() {
                    continue;
                }

                if path1.crosses_road(r) != path2.crosses_road(r) {
                    if let (Ok(path1), Ok(path2)) = (path1.into_v1(map), path2.into_v1(map)) {
                        changed.push((path1, path2));
                    }
                }
            }
        }
        changed
    }
}

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