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 serde::{Deserialize, Serialize};
use abstio::MapName;
use abstutil::{prettyprint_usize, Counter, Timer};
use geom::Distance;
use map_model::{
IntersectionID, Map, PathRequest, PathStepV2, PathfinderCaching, RoadID, RoutingParams,
};
#[derive(Clone, Serialize, Deserialize)]
pub struct TrafficCounts {
pub map: MapName,
pub description: String,
pub per_road: Counter<RoadID>,
pub per_intersection: Counter<IntersectionID>,
}
impl Default for TrafficCounts {
fn default() -> Self {
Self {
map: MapName::new("zz", "place", "holder"),
description: String::new(),
per_road: Counter::new(),
per_intersection: Counter::new(),
}
}
}
impl TrafficCounts {
pub fn from_path_requests(
map: &Map,
description: String,
requests: &[(PathRequest, usize)],
params: RoutingParams,
cache_custom: PathfinderCaching,
timer: &mut Timer,
) -> Self {
let mut counts = Self {
map: map.get_name().clone(),
description,
per_road: Counter::new(),
per_intersection: Counter::new(),
};
for r in map.all_roads() {
counts.per_road.add(r.id, 0);
}
for i in map.all_intersections() {
counts.per_intersection.add(i.id, 0);
}
timer.start_iter("calculate routes", requests.len());
for (req, count) in requests {
timer.next();
if let Ok(path) = map.pathfind_v2_with_params(req.clone(), ¶ms, cache_custom) {
let count = *count;
for step in path.get_steps() {
match step {
PathStepV2::Along(dr) | PathStepV2::Contraflow(dr) => {
counts.per_road.add(dr.road, count);
}
PathStepV2::Movement(m) | PathStepV2::ContraflowMovement(m) => {
counts.per_intersection.add(m.parent, count);
}
}
}
if req.start.dist_along() == Distance::ZERO {
let i = map.get_l(req.start.lane()).src_i;
if map.get_i(i).is_border() {
counts.per_intersection.add(i, count);
}
} else {
let i = map.get_l(req.end.lane()).dst_i;
if map.get_i(i).is_border() {
counts.per_intersection.add(i, count);
}
}
}
}
counts
}
pub fn quickly_compare(&self, other: &TrafficCounts) {
println!("{} vs {}", self.description, other.description);
let mut sum = 0.0;
let mut n = 0;
for (r, cnt1) in self.per_road.borrow() {
let cnt1 = *cnt1;
let cnt2 = other.per_road.get(*r);
println!(
"{}: {} vs {}",
r,
prettyprint_usize(cnt1),
prettyprint_usize(cnt2)
);
sum += (cnt1 as f64 - cnt2 as f64).powi(2);
n += 1;
}
for (i, cnt1) in self.per_intersection.borrow() {
let cnt1 = *cnt1;
let cnt2 = other.per_intersection.get(*i);
println!(
"{}: {} vs {}",
i,
prettyprint_usize(cnt1),
prettyprint_usize(cnt2)
);
sum += (cnt1 as f64 - cnt2 as f64).powi(2);
n += 1;
}
println!("RMSE = {:.2}", (sum / n as f64).sqrt());
}
}