"""Build a SUMO signal program that follows the County's real dual-ring phase sequence (NEMA), scaled to a plan. A plan fixes, per light: cycle C, the Big Bend block length (g_main + its clearance), the cross-street block (the rest), and the offset (start of the Big Bend block). Inside each block every phase keeps its real yellow + all-red, and the remaining green time is shared in the County's proportions (timing sheet, design pattern). Phase meanings come from the sheet's movement text: "Big Bend Blvd. SB LT" -> SB left, protected "Big Bend Blvd. NB" -> NB through + right; NB left permissive (sheets mark LT phases Protected/Permissive) "Forsyth Blvd. WB & LT" -> WB through + right + left, protected (split phase) "Big Bend Blvd. SB LT ... Clayton Rd. WB RT" -> SB left protected + WB right overlap Ring 1 = phases 1-4, ring 2 = phases 5-8; a ring with no phases in a block follows the other ring there. """ import re ORDER = {None: 0, "r": 0, "y": 1, "g": 2, "G": 3} def _moves(desc, has_lt): prot, perm = {}, set() for m in re.finditer(r"(NB|SB|EB|WB)\s*(& LT|LT|RT)?", desc): a, k = m.group(1), (m.group(2) or "").strip() if k == "LT": prot.setdefault(a, set()).add("L") elif k == "RT": prot.setdefault(a, set()).add("R") elif k == "& LT": prot.setdefault(a, set()).update({"L", "T", "R"}) else: prot.setdefault(a, set()).update({"T", "R"}) perm.add(a) # left turns on gaps during the through phase (protected/permissive or no LT phase) return prot, perm def _max(a, b): return a if ORDER[a] >= ORDER[b] else b def left_only(desc): """Approach served by a protected-left-only phase (e.g. 'Big Bend Blvd. SB LT'), else None.""" m = re.search(r"(NB|SB|EB|WB)\s*LT", desc) return m.group(1) if m and "& LT" not in desc else None def build(phases, splits, main_phases, C_plan, main_block_plan, left_demand=None, sat_left=1800.0, x_target=0.9): """phases: {n: {movement, yellow, all_red, min_green}}, splits: {n: seconds} (design pattern), main_phases: Big Bend phases. left_demand: {approach: left-turn veh/h}. If given, each protected-left-only phase gets the green its demand needs (q*C/(s*x_target)), clamped to [min green, its County split green]; the time saved goes to the other phases of that ring block (what the actuated controller does when a left arrow gaps out). Returns [(duration_s, {(approach, 'L'|'T'|'R'): 'G'|'g'|'y'})] covering one cycle, starting at the Big Bend block.""" has_lt = {m.group(1) for p in phases.values() for m in re.finditer(r"(NB|SB|EB|WB)\s*LT", p["movement"])} clr = lambda n: phases[n]["yellow"] + (phases[n]["all_red"] or 0.0) rings = {} for r, ids in ((1, [1, 2, 3, 4]), (2, [5, 6, 7, 8])): seq = [n for n in ids if n in splits] t = 0.0; tl = [] for group, block in (([n for n in seq if n in main_phases], main_block_plan), ([n for n in seq if n not in main_phases], C_plan-main_block_plan)): if not group: tl.append((t, t+block, None)); t += block; continue avail = block - sum(clr(n) for n in group) if avail <= 0: raise ValueError(f"block {block:.1f}s shorter than its clearances") share = {n: max(splits[n]-clr(n), 0.1) for n in group} green = {n: avail*share[n]/sum(share.values()) for n in group} if left_demand: fixed = {} for n in group: a = left_only(phases[n]["movement"]) if a is not None and len(group) > 1: need = left_demand.get(a, 0.0)*C_plan/(sat_left*x_target) lo = phases[n].get("min_green") or 4.0 fixed[n] = min(max(need, lo), green[n]) rest = [n for n in group if n not in fixed] if fixed and rest: left = avail - sum(fixed.values()); tot = sum(share[n] for n in rest) green = {**fixed, **{n: left*share[n]/tot for n in rest}} for n in group: d = clr(n) + green[n] tl.append((t, t+d, n)); t += d rings[r] = tl def state_at(r, t): for s, e, n in rings[r]: if s <= t < e: if n is None: return None y, ar = phases[n]["yellow"], phases[n]["all_red"] or 0.0 return n, ("G" if t < e-y-ar else ("y" if t < e-ar else "r")) return None pts = {0.0, C_plan} for tl in rings.values(): for s, e, n in tl: pts |= {s, e} if n is not None: pts |= {e-phases[n]["yellow"]-(phases[n]["all_red"] or 0.0), e-(phases[n]["all_red"] or 0.0)} pts = sorted(p for p in pts if 0 <= p <= C_plan) out = [] for a, b in zip(pts, pts[1:]): if b-a < 1e-3: continue mid = (a+b)/2; col = {} for r in (1, 2): st = state_at(r, mid) or state_at(3-r, mid) if not st or st[1] == "r": continue n, c = st prot, perm = _moves(phases[n]["movement"], has_lt) for appr, mv in prot.items(): for m in mv: col[(appr, m)] = _max(col.get((appr, m)), c) for appr in perm: col[(appr, "L")] = _max(col.get((appr, "L")), "g" if c == "G" else "y") out.append((b-a, col)) return out