make traffic signals go a little faster by filling out more turns
@@ -23,7 +23,7 @@ impl ControlTrafficSignal {
assert!(map.get_i(intersection).has_traffic_signal);
ControlTrafficSignal {
intersection,
- cycles: ControlTrafficSignal::greedy_assignment(map, intersection),
+ cycles: greedy_assignment(map, intersection),
}
}
@@ -67,6 +67,43 @@ impl ControlTrafficSignal {
let remaining_cycle_time = next_cycle_time - time;
(cycle, remaining_cycle_time)
}
+}
+
+#[derive(Clone, Debug, Serialize)]
+pub struct Cycle {
+ pub turns: Vec<TurnID>,
+ // in the future, what pedestrian crossings this cycle includes, etc
+ changed: bool,
+ duration: si::Second<f64>,
+}
+
+impl Cycle {
+ pub fn conflicts_with(&self, t1: TurnID, map: &Map) -> bool {
+ for t2 in &self.turns {
+ if map.get_t(t1).conflicts_with(map.get_t(*t2)) {
+ return true;
+ }
+ }
+ false
+ }
+
+ pub fn contains(&self, t: TurnID) -> bool {
+ self.turns.contains(&t)
+ }
+
+ pub fn add(&mut self, t: TurnID) {
+ // should assert parent matches, compatible with cycle so far, not in the current set
+ self.turns.push(t);
+ self.changed = true;
+ }
+
+ pub fn remove(&mut self, t: TurnID) {
+ // should assert in current set
+ let idx = self.turns.iter().position(|&id| id == t).unwrap();
+ self.turns.remove(idx);
+ self.changed = true;
+ }
+}
fn greedy_assignment(map: &Map, intersection: IntersectionID) -> Vec<Cycle> {
/*
@@ -113,44 +150,22 @@ impl ControlTrafficSignal {
cycles.push(current_cycle.clone());
}
- // TODO second pass to add all legal turns to the cycles we came up with initially
+ expand_all_cycles(&mut cycles, map, intersection);
cycles
}
-}
-
-#[derive(Clone, Debug, Serialize)]
-pub struct Cycle {
- pub turns: Vec<TurnID>,
- // in the future, what pedestrian crossings this cycle includes, etc
- changed: bool,
- duration: si::Second<f64>,
-}
-
-impl Cycle {
- pub fn conflicts_with(&self, t1: TurnID, map: &Map) -> bool {
- for t2 in &self.turns {
- if map.get_t(t1).conflicts_with(map.get_t(*t2)) {
- return true;
- }
- }
- false
- }
- pub fn contains(&self, t: TurnID) -> bool {
- self.turns.contains(&t)
+// Add all legal turns to existing cycles.
+fn expand_all_cycles(cycles: &mut Vec<Cycle>, map: &Map, intersection: IntersectionID) {
+ let all_turns: Vec<TurnID> = map.get_turns_in_intersection(intersection)
+ .iter()
+ .map(|t| t.id)
+ .collect();
+ for cycle in cycles.iter_mut() {
+ for t in &all_turns {
+ if !cycle.contains(*t) && !cycle.conflicts_with(*t, map) {
+ cycle.add(*t);
}
-
- pub fn add(&mut self, t: TurnID) {
- // should assert parent matches, compatible with cycle so far, not in the current set
- self.turns.push(t);
- self.changed = true;
}
-
- pub fn remove(&mut self, t: TurnID) {
- // should assert in current set
- let idx = self.turns.iter().position(|&id| id == t).unwrap();
- self.turns.remove(idx);
- self.changed = true;
}
}
