Spaces:
Running
Running
Download app.js from thelamapi/Next-Passage: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/spaces/thelamapi/Next-Passage/resolve/main/app.js
- Command line
-
hf download hf://spaces/thelamapi/Next-Passage/app.js
-
curl -L -o app.js https://huggingface.co/spaces/thelamapi/Next-Passage/resolve/main/app.js
5.36 kB
| /* Next Passage Space: generate a scenario, run all planners in the browser, replay any of them. */ | |
| const { $, h, fmt, css } = NLUI; | |
| const NAMES = { timed: "Next Passage (time-aware)", reactive: "Static + reactive replanning", static: "Static shortest path" }; | |
| let ctx, world, results, shown = "timed", frame = 0, timer = null; | |
| function run() { | |
| const seed = Number($("seed").value) | 0, density = $("density").value, p = Number($("stall").value); | |
| $("stall_v").textContent = p.toFixed(2); | |
| world = Core.makeWorld(seed, density, p); | |
| results = {}; | |
| for (const k of ["timed", "reactive", "static"]) { | |
| const t0 = performance.now(); | |
| results[k] = Core.runEpisode(world, k); | |
| results[k].ms = performance.now() - t0; | |
| } | |
| table(); | |
| frame = 0; $("time").max = results[shown].trace.length - 1; $("time").value = 0; | |
| draw(); | |
| return results; | |
| } | |
| function table() { | |
| const tb = $("tbl"); tb.innerHTML = ""; | |
| tb.append(h("tr", {}, ["Planner", "Arrived", "Steps", "Collisions", "Door hits", "Replans", "Nodes", "ms"].map((x, i) => h("th", { class: i ? "num" : "" }, x)))); | |
| for (const k of ["timed", "reactive", "static"]) { | |
| const r = results[k]; | |
| tb.append(h("tr", { style: k === shown ? "font-weight:600" : "" }, | |
| h("td", {}, h("a", { href: "#", onclick: e => { e.preventDefault(); show(k); } }, NAMES[k])), | |
| h("td", { class: "num" }, r.arrived ? "yes" : "no"), h("td", { class: "num" }, r.arrived ? r.arrival_time : `>${r.max_steps}`), | |
| h("td", { class: "num" }, r.collisions), h("td", { class: "num" }, r.door_hits), h("td", { class: "num" }, r.replans), | |
| h("td", { class: "num" }, r.nodes_expanded), h("td", { class: "num" }, fmt(r.ms, 1)))); | |
| } | |
| } | |
| function show(k) { shown = k; $("planner").value = k; frame = 0; $("time").max = results[k].trace.length - 1; table(); draw(); } | |
| function draw() { | |
| const c = $("grid"), Wpx = Math.min(c.parentElement.clientWidth - 4, 700), cell = Math.floor(Wpx / Core.W), dpr = window.devicePixelRatio || 1; | |
| c.width = cell * Core.W * dpr; c.height = cell * Core.H * dpr; c.style.width = cell * Core.W + "px"; c.style.height = cell * Core.H + "px"; | |
| const g = c.getContext("2d"); g.scale(dpr, dpr); | |
| const r = results[shown], st = r.trace[Math.min(frame, r.trace.length - 1)], t = st.t; | |
| g.fillStyle = css("--bg"); g.fillRect(0, 0, cell * Core.W, cell * Core.H); | |
| g.fillStyle = css("--ink"); | |
| for (const [x, y] of world.walls) g.fillRect(x * cell, y * cell, cell, cell); | |
| for (const d of world.doors) { | |
| g.fillStyle = Core.doorClosed(d, t) ? "#c0392b" : "#2e9d6a"; | |
| g.fillRect(d.cell[0] * cell + 2, d.cell[1] * cell + 2, cell - 4, cell - 4); | |
| } | |
| g.strokeStyle = "rgba(176,125,18,.35)"; g.lineWidth = 2; | |
| for (const o of world.obstacles) { g.beginPath(); o.loop.forEach(([x, y], i) => (i ? g.lineTo : g.moveTo).call(g, x * cell + cell / 2, y * cell + cell / 2)); g.stroke(); } | |
| g.strokeStyle = "#2f5fb3"; g.lineWidth = 3; g.beginPath(); | |
| r.trace.slice(0, frame + 1).forEach((s, i) => (i ? g.lineTo : g.moveTo).call(g, s.robot[0] * cell + cell / 2, s.robot[1] * cell + cell / 2)); g.stroke(); | |
| g.fillStyle = "#b07d12"; | |
| for (const [x, y] of st.obs) { g.beginPath(); g.arc(x * cell + cell / 2, y * cell + cell / 2, cell * 0.38, 0, 7); g.fill(); } | |
| g.fillStyle = "#0b6e4f"; g.font = `${cell}px system-ui`; g.fillText("★", world.goal[0] * cell, world.goal[1] * cell + cell * 0.85); | |
| const hit = r.events.some(e => e.t === t); | |
| g.fillStyle = hit ? "#c0392b" : "#2f5fb3"; | |
| g.beginPath(); g.arc(st.robot[0] * cell + cell / 2, st.robot[1] * cell + cell / 2, cell * 0.42, 0, 7); g.fill(); | |
| const evs = r.events.filter(e => e.t <= t).length; | |
| $("info").textContent = `${NAMES[shown]} · t = ${t}${hit ? " · COLLISION" : ""} · events so far: ${evs}`; | |
| $("time").value = frame; | |
| } | |
| function play() { | |
| if (timer) { clearInterval(timer); timer = null; $("play").textContent = "Play"; return; } | |
| $("play").textContent = "Pause"; | |
| if (frame >= results[shown].trace.length - 1) frame = 0; | |
| timer = setInterval(() => { frame++; draw(); if (frame >= results[shown].trace.length - 1) { clearInterval(timer); timer = null; $("play").textContent = "Play"; } }, 160); | |
| } | |
| window.__liveTest = function () { | |
| const before = JSON.stringify(results.timed.trace.map(s => s.robot)); | |
| $("seed").value = "4242"; $("density").value = "high"; $("stall").value = "0.3"; | |
| $("go").click(); | |
| const after = results; | |
| return { ok: JSON.stringify(after.timed.trace.map(s => s.robot)) !== before && $("tbl").rows.length === 4 && world.seed === 4242 && world.obstacles.length > 0, | |
| timed: { arrived: after.timed.arrived, steps: after.timed.arrival_time, collisions: after.timed.collisions, replans: after.timed.replans }, | |
| static: { arrived: after.static.arrived, collisions: after.static.collisions } }; | |
| }; | |
| NLUI.boot(Core, async c => { | |
| ctx = c; | |
| $("go").addEventListener("click", run); | |
| $("density").addEventListener("change", run); | |
| $("stall").addEventListener("input", run); | |
| $("random").addEventListener("click", () => { $("seed").value = 10000 + Math.floor(Math.random() * 90000); run(); }); | |
| $("planner").addEventListener("change", e => show(e.target.value)); | |
| $("time").addEventListener("input", e => { frame = Number(e.target.value); draw(); }); | |
| $("play").addEventListener("click", play); | |
| window.addEventListener("resize", () => results && draw()); | |
| run(); | |
| }); | |