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
use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
use geom::{Duration, Time};
use map_model::{LaneID, Map, Path, PathConstraints, PathRequest, PathStep, TurnID};
use crate::mechanics::IntersectionSimState;
use crate::{CarID, SimOptions, VehicleType};
type ZoneIdx = usize;
#[derive(Serialize, Deserialize, Clone)]
pub struct CapSimState {
lane_to_zone: BTreeMap<LaneID, ZoneIdx>,
zones: Vec<Zone>,
avoid_congestion: Option<AvoidCongestion>,
}
#[derive(Serialize, Deserialize, Clone)]
struct Zone {
cap: usize,
entered_in_last_hour: BTreeSet<CarID>,
hour_started: Time,
}
impl CapSimState {
pub fn new(map: &Map, opts: &SimOptions) -> CapSimState {
let mut sim = CapSimState {
lane_to_zone: BTreeMap::new(),
zones: Vec::new(),
avoid_congestion: opts
.cancel_drivers_delay_threshold
.map(|delay_threshold| AvoidCongestion { delay_threshold }),
};
for z in map.all_zones() {
if let Some(cap) = z.restrictions.cap_vehicles_per_hour {
let idx = sim.zones.len();
for r in &z.members {
for l in map.get_r(*r).all_lanes() {
if PathConstraints::Car.can_use(map.get_l(l), map) {
sim.lane_to_zone.insert(l, idx);
}
}
}
sim.zones.push(Zone {
cap,
entered_in_last_hour: BTreeSet::new(),
hour_started: Time::START_OF_DAY,
});
}
}
sim
}
fn allow_trip(&mut self, now: Time, car: CarID, path: &Path) -> bool {
if car.1 != VehicleType::Car || self.lane_to_zone.is_empty() {
return true;
}
for step in path.get_steps() {
if let PathStep::Lane(l) = step {
if let Some(idx) = self.lane_to_zone.get(l) {
let zone = &mut self.zones[*idx];
if now - zone.hour_started >= Duration::hours(1) {
zone.hour_started = Time::START_OF_DAY + Duration::hours(now.get_parts().0);
zone.entered_in_last_hour.clear();
}
if zone.entered_in_last_hour.len() >= zone.cap
&& !zone.entered_in_last_hour.contains(&car)
{
return false;
}
zone.entered_in_last_hour.insert(car);
}
}
}
true
}
pub fn validate_path(
&mut self,
req: &PathRequest,
path: Path,
now: Time,
car: CarID,
capped: &mut bool,
intersections: &IntersectionSimState,
map: &Map,
) -> Result<Path, String> {
if let Some(ref avoid) = self.avoid_congestion {
if let Some((turn, delay)) = avoid.path_crosses_delay(now, &path, intersections, map) {
*capped = true;
return Err(format!("path crosses delay of {} at {}", delay, turn));
}
}
if self.allow_trip(now, car, &path) {
return Ok(path);
}
*capped = true;
let mut avoid_lanes: BTreeSet<LaneID> = BTreeSet::new();
for (l, idx) in &self.lane_to_zone {
let zone = &self.zones[*idx];
if zone.entered_in_last_hour.len() >= zone.cap
&& !zone.entered_in_last_hour.contains(&car)
{
avoid_lanes.insert(*l);
}
}
map.pathfind_avoiding_lanes(req.clone(), avoid_lanes)
.ok_or_else(|| format!("no path avoiding caps: {}", req))
}
pub fn get_cap_counter(&self, l: LaneID) -> usize {
if let Some(idx) = self.lane_to_zone.get(&l) {
self.zones[*idx].entered_in_last_hour.len()
} else {
0
}
}
}
#[derive(Serialize, Deserialize, Clone)]
struct AvoidCongestion {
delay_threshold: Duration,
}
impl AvoidCongestion {
fn path_crosses_delay(
&self,
now: Time,
path: &Path,
intersections: &IntersectionSimState,
map: &Map,
) -> Option<(TurnID, Duration)> {
for step in path.get_steps() {
if let PathStep::Lane(l) = step {
let lane = map.get_l(*l);
for (agent, turn, start) in intersections.get_waiting_agents(lane.dst_i) {
if now - start < self.delay_threshold {
continue;
}
if agent.to_vehicle_type() != Some(VehicleType::Car) {
continue;
}
if map.get_l(turn.src).parent != lane.parent {
continue;
}
return Some((turn, now - start));
}
}
}
None
}
}