"""Sanity check: simulated hourly volumes into each light vs the corridor dataset (County / MoDOT derived). GEH = sqrt(2 (M - C)^2 / (M + C)), M = simulated veh/h, C = target veh/h. Standard traffic-model check: GEH < 5 good, 5-10 look at it, > 10 problem. Entry approaches match by construction; the interior Big Bend approaches are the real test (their volume emerges from upstream turning + side-street inflow). Usage: validate.py [plan] (uses that plan's seed runs; default webster-common) """ import sys, json, math, pathlib, statistics sys.path.insert(0, str(pathlib.Path(__file__).parent)) from common import BUILD, SUMO, corridor, tls_map, tls_links def main(plan="webster-common"): cor = corridor(); tm = tls_map(); dem = json.load(open(SUMO/"demand.json")) hours = dem["measure_s"]/3600 runs = sorted((BUILD/"runs"/plan).glob("seed*/metrics.json")) if not runs: sys.exit(f"no runs for {plan}; run run_sumo.py first") entered = [json.load(open(r))["entered"] for r in runs] entries = {f["id"] for f in dem["flows"]} rows = [] for l in cor["lights"]: lid = l["id"] for lab, A in l["approaches"].items(): edges = {e_in.getID() for _, e_in, _, lb, _ in tls_links(tm[lid]["tls"]) if lb == lab} if f"{lid}_{lab}" in entries: kind = "entry" sim = A["q_vph"] # inserted at this rate by construction (checked via throughput) else: kind = "interior" sim = statistics.mean(sum(en.get(e, 0) for e in edges) for en in entered)/hours tgt = A["q_vph"] geh = math.sqrt(2*(sim-tgt)**2/(sim+tgt)) if sim+tgt > 0 else 0 rows.append(dict(light=lid, name=l["name"], approach=lab, kind=kind, target_vph=round(tgt), sim_vph=round(sim), geh=round(geh, 2), target_source=A["q_source"], target_ref=A["q_ref"])) json.dump(rows, open(BUILD/"validation.json", "w"), indent=1) print(f"validation ({plan}, {len(runs)} seeds): light approach kind target sim GEH") for r in rows: flag = "" if r["geh"] < 5 else (" <- check" if r["geh"] < 10 else " <- PROBLEM") print(f" {r['light']} {r['name']:16s} {r['approach']} {r['kind']:8s} {r['target_vph']:6d} {r['sim_vph']:6d} {r['geh']:5.2f}{flag}") inter = [r["geh"] for r in rows if r["kind"] == "interior"] print(f"interior approaches: {sum(g < 5 for g in inter)}/{len(inter)} with GEH < 5") if __name__ == "__main__": main(*sys.argv[1:])