/* Next Drive Space: generate an intersection scene, let a driver drive it in the browser, replay with events. */ const { $, h, fmt } = NLUI; const DRIVERS = { shield: "Learned + safety check", learned: "Learned driver", rule: "Rule-based driver", expert: "Expert planner (teacher)" }; const KIND_NAMES = { mixed: "Everything at once", occlusion: "Parked cars hide a pedestrian", crosswalk: "Pedestrians on the crosswalk", lead_brake: "Car ahead brakes suddenly", light: "Traffic light only" }; let ctx, bench, sc, run, frame = 0, playing = false, last = 0, showHidden = true; function newRun() { sc = Core.makeScenario(Number($("seed").value) | 0, $("kind").value, Number($("density").value)); run = Core.runEpisode(sc, $("driver").value, ctx.layers, true); frame = 0; $("scrub").max = run.trace.length - 1; summary(); events(); draw(); return run; } function summary() { const ms = run.decision_ms.slice().sort((a, b) => a - b); const outcome = run.collision ? `collision with ${run.collision_with}` : run.reached ? "reached the goal" : "timed out (45 s)"; $("outcome").innerHTML = ""; $("outcome").append(h("div", { class: "kpi" }, h("div", {}, h("b", { style: `color:${run.collision ? "var(--red)" : run.reached ? "var(--accent)" : "var(--gold)"}` }, outcome), h("span", {}, `route: ${sc.route} · ${fmt(run.time, 1)} s`)), h("div", {}, h("b", {}, run.red_violations), h("span", {}, "red-light violations")), h("div", {}, h("b", {}, run.hard_brakes), h("span", {}, "hard brakes")), h("div", {}, h("b", {}, run.overrides), h("span", {}, "safety overrides")), h("div", {}, h("b", {}, fmt(ms.reduce((a, b) => a + b, 0) / ms.length, 2) + " ms"), h("span", {}, `decision latency (p95 ${fmt(ms[Math.floor(0.95 * (ms.length - 1))], 2)})`)))); } function events() { const box = $("events"); box.innerHTML = ""; if (!run.events.length) box.append(h("span", { class: "small muted" }, "No critical events.")); for (const e of run.events) { const k = Math.max(0, run.trace.findIndex(r => r.t >= e.t - 1e-9)); box.append(h("button", { class: "chip", style: e.type === "collision" ? "border-color:var(--red);color:var(--red)" : "", onclick: () => { frame = Math.max(0, k - 15); draw(); } }, `${fmt(e.t, 1)} s · ${e.type.replace("_", " ")}${e.with ? " (" + e.with + ")" : ""}`)); } } function compareAll() { const tb = $("cmp"); tb.innerHTML = ""; tb.append(h("tr", {}, ["Driver", "Outcome", "Time", "Red", "Hard brakes", "ms/decision"].map((x, i) => h("th", { class: i > 1 ? "num" : "" }, x)))); for (const d of ["shield", "learned", "rule", "expert"]) { const r = Core.runEpisode(sc, d, ctx.layers, false); const ms = r.decision_ms.reduce((a, b) => a + b, 0) / r.decision_ms.length; tb.append(h("tr", {}, h("td", {}, DRIVERS[d]), h("td", { style: r.collision ? "color:var(--red)" : "" }, r.collision ? "collision" : r.reached ? "goal" : "timeout"), h("td", { class: "num" }, fmt(r.time, 1)), h("td", { class: "num" }, r.red_violations), h("td", { class: "num" }, r.hard_brakes), h("td", { class: "num" }, fmt(ms, 2)))); } } function draw() { const c = $("view"), W = c.clientWidth || 640, H = Math.round(Math.min(W * 0.72, 520)), dpr = window.devicePixelRatio || 1; c.width = W * dpr; c.height = H * dpr; c.style.height = H + "px"; const g = c.getContext("2d"); g.setTransform(dpr, 0, 0, dpr, 0, 0); const r = run.trace[Math.min(frame, run.trace.length - 1)]; const scale = W / 62, cx = Math.max(-40, Math.min(40, r.x + 10 * Math.cos(r.h))), cy = Math.max(-55, Math.min(45, r.y + 10 * Math.sin(r.h))); const X = x => W / 2 + (x - cx) * scale, Y = y => H / 2 - (y - cy) * scale; g.fillStyle = "#5b8a4a"; g.fillRect(0, 0, W, H); // grass g.fillStyle = "#c9c4b8"; g.fillRect(X(-6), 0, 12 * scale, H); g.fillRect(0, Y(6), W, 12 * scale); // sidewalks g.fillStyle = "#3a3d42"; g.fillRect(X(-3.5), 0, 7 * scale, H); g.fillRect(0, Y(3.5), W, 7 * scale); // asphalt g.strokeStyle = "#f2c94c"; g.lineWidth = 2; g.setLineDash([3 * scale, 3 * scale]); g.beginPath(); g.moveTo(X(0), 0); g.lineTo(X(0), Y(3.5)); g.moveTo(X(0), Y(-3.5)); g.lineTo(X(0), H); g.moveTo(0, Y(0)); g.lineTo(X(-3.5), Y(0)); g.moveTo(X(3.5), Y(0)); g.lineTo(W, Y(0)); g.stroke(); g.setLineDash([]); g.fillStyle = "#eee"; // crosswalk + stop line for (let x = -3.2; x < 3.4; x += 0.9) g.fillRect(X(x), Y(-6.0), 0.5 * scale, 3.0 * scale); g.fillRect(X(0), Y(-9.9), 3.5 * scale, 0.35 * scale); for (const [px, py, pw, ph] of sc.parked) car(g, X, Y, scale, px, py, Math.PI / 2, "#8d939c", pw, ph); const goal = Core.pose(sc.route, Core.pathLen(sc.route) - 5); // goal flag g.fillStyle = "#0b6e4f"; g.beginPath(); g.arc(X(goal[0]), Y(goal[1]), 0.8 * scale, 0, 7); g.fill(); g.strokeStyle = "rgba(76,195,154,.85)"; g.lineWidth = 3; g.beginPath(); // driven path run.trace.slice(0, frame + 1).forEach((q, k) => (k ? g.lineTo : g.moveTo).call(g, X(q.x), Y(q.y))); g.stroke(); for (const cr of r.cars) car(g, X, Y, scale, cr[0], cr[1], cr[2], cr[3] === "lead" ? "#7d5ba6" : "#3d6fb6"); r.peds.forEach(([px, py], k) => { const vis = r.visible ? r.visible[k] : true; if (!vis && !showHidden) return; g.beginPath(); g.arc(X(px), Y(py), 0.45 * scale, 0, 7); if (vis) { g.fillStyle = "#f28e2b"; g.fill(); } else { g.setLineDash([3, 3]); g.strokeStyle = "#f28e2b"; g.lineWidth = 2; g.stroke(); g.setLineDash([]); } }); const hit = run.collision && frame >= run.trace.length - 1; car(g, X, Y, scale, r.x, r.y, r.h, hit ? "#c0392b" : "#1f9d6b"); lights(g, X, Y, scale, r.light, r.ew); const act = Core.ACTION_NAMES[r.i]; g.fillStyle = "rgba(20,22,26,.78)"; g.fillRect(8, 8, 238, 62); g.fillStyle = "#fff"; g.font = "600 14px system-ui"; g.fillText(`${DRIVERS[$("driver").value]}`, 16, 27); g.font = "13px system-ui"; g.fillText(`t ${fmt(r.t, 1)} s · ${fmt(r.v * 3.6, 0)} km/h`, 16, 45); g.fillStyle = r.override ? "#f0a070" : (r.i <= 1 ? "#ff8a7a" : "#9be3c4"); g.fillText(`decision: ${act}${r.override ? " (safety override)" : ""}`, 16, 62); if (hit) { g.fillStyle = "rgba(192,57,43,.9)"; g.font = "700 20px system-ui"; g.fillText(`COLLISION (${run.collision_with})`, W / 2 - 110, 36); } $("scrub").value = frame; } function car(g, X, Y, s, x, y, hd, col, w, l) { w = w || 1.8; l = l || 4.4; g.save(); g.translate(X(x), Y(y)); g.rotate(-hd); g.fillStyle = col; g.fillRect(-l / 2 * s, -w / 2 * s, l * s, w * s); g.fillStyle = "rgba(255,255,255,.55)"; g.fillRect(l * 0.12 * s, -w * 0.38 * s, l * 0.18 * s, w * 0.76 * s); g.restore(); } function lights(g, X, Y, s, ns, ew) { const col = k => ["#2ecc71", "#f1c40f", "#e74c3c"][k]; const box = (x, y, k, label) => { g.fillStyle = "#222"; g.fillRect(X(x) - 9, Y(y) - 9, 18, 18); g.fillStyle = col(k); g.beginPath(); g.arc(X(x), Y(y), 6, 0, 7); g.fill(); g.fillStyle = "#fff"; g.font = "10px system-ui"; g.fillText(label, X(x) + 11, Y(y) + 4); }; box(4.8, -11.5, ns, "you"); box(-11.5, -4.8, ew, "cross"); } function tick(ts) { if (playing) { if (ts - last > 1000 * 0.1 / Number($("speed").value)) { last = ts; frame++; if (frame >= run.trace.length - 1) { frame = run.trace.length - 1; playing = false; $("play").textContent = "Replay"; } draw(); } } requestAnimationFrame(tick); } function play() { if (!playing && frame >= run.trace.length - 1) frame = 0; playing = !playing; $("play").textContent = playing ? "Pause" : "Play"; } function benchTable() { const tb = $("bench"); tb.innerHTML = ""; tb.append(h("tr", {}, ["Driver", "Collisions", "Red-light", "Reached", "Hard brakes / drive", "ms / decision"].map((x, i) => h("th", { class: i ? "num" : "" }, x)))); for (const d of ["shield", "learned", "rule", "expert"]) { const r = bench.overall[d]; tb.append(h("tr", {}, h("td", {}, DRIVERS[d]), h("td", { class: "num" }, fmt(100 * r.collision_rate, 1) + "%"), h("td", { class: "num" }, fmt(100 * r.red_rate, 1) + "%"), h("td", { class: "num" }, fmt(100 * r.reach_rate, 1) + "%"), h("td", { class: "num" }, fmt(r.hard_brakes, 2)), h("td", { class: "num" }, fmt(r.ms_mean, 2)))); } $("benchnote").textContent = `${bench.episodes_per_driver} unseen test drives per driver (seeds ≥ 10000, densities 1–3; density 3 never seen in training). Timing from Python on the training machine.`; const box = $("fails"); box.innerHTML = ""; for (const f of bench.failures.slice(0, 16)) box.append(h("button", { class: "chip", onclick: () => load(f) }, `${DRIVERS[f.driver]} · ${f.kind} d${f.density} #${f.seed} · ${f.what}`)); if (!bench.failures.length) box.append(h("span", { class: "small muted" }, "No failures recorded for the learned drivers.")); } function load(f) { $("seed").value = f.seed; $("kind").value = f.kind; $("density").value = f.density; $("driver").value = f.driver; newRun(); compareAll(); playing = true; $("play").textContent = "Pause"; window.scrollTo({ top: 0, behavior: "smooth" }); } window.__liveTest = function () { $("seed").value = "424242"; $("kind").value = "mixed"; $("density").value = "3"; $("driver").value = "shield"; $("go").click(); const r = run; frame = Math.floor(r.trace.length / 2); draw(); return { ok: r.steps > 0 && r.trace.length === r.steps && $("cmp").rows.length === 5 && sc.seed === 424242 && sc.density === 3, outcome: r.collision ? "collision" : r.reached ? "goal" : "timeout", steps: r.steps, overrides: r.overrides, route: sc.route }; }; NLUI.boot(Core, async c => { ctx = c; bench = await (await fetch("benchmark.json")).json(); for (const [k, v] of Object.entries(KIND_NAMES)) $("kind").append(h("option", { value: k }, v)); for (const [k, v] of Object.entries(DRIVERS)) $("driver").append(h("option", { value: k }, v)); $("go").addEventListener("click", () => { newRun(); compareAll(); }); $("rand").addEventListener("click", () => { $("seed").value = 10000 + Math.floor(Math.random() * 89999); newRun(); compareAll(); }); for (const id of ["kind", "density", "driver"]) $(id).addEventListener("change", () => { newRun(); compareAll(); }); $("play").addEventListener("click", play); $("scrub").addEventListener("input", e => { frame = Number(e.target.value); playing = false; $("play").textContent = "Play"; draw(); }); $("hidden").addEventListener("change", e => { showHidden = e.target.checked; draw(); }); window.addEventListener("resize", draw); benchTable(); newRun(); compareAll(); requestAnimationFrame(tick); });