File size: 3,919 Bytes
90141fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Next Reward</title>
<style>

*{box-sizing:border-box}body{margin:0;background:#f3f6f1;color:#142c27;font:16px system-ui}

main{max-width:960px;margin:auto;padding:36px 20px}h1{font-size:clamp(42px,8vw,78px);letter-spacing:-.06em;margin:0}

p{line-height:1.55;color:#415a52}canvas,video{width:100%;display:block;border-radius:16px;background:#e4eee5}

button,select,input,textarea{font:inherit}button,select,input,textarea{padding:9px;border:1px solid #aabdaf;border-radius:8px;background:white}

button{background:#08775e;color:white;cursor:pointer;margin:8px 0}button:disabled{opacity:.5}

textarea{width:100%;font:16px monospace}a{color:#08644f}label{display:inline-block;margin:6px}

</style>
<main><h1>Next Reward</h1><p>Run a learned routing policy on an editable grid.</p>
<label>Grid <textarea id="board" rows="6" spellcheck="false">............
.####.......
.....#...X..
.....#......
.....#####..
...........G</textarea></label>
<p>The agent starts in the top-left cell. Use . for floor, # for walls, X for pits and G for the goal.</p>
<button id="run" disabled>Run learned policy</button>
<p id="status" role="status">Loading checkpoint...</p>
<canvas id="map" width="720" height="380" aria-label="Grid and route"></canvas>
<script type="module">

const el=id=>document.getElementById(id), ctx=el("map").getContext("2d");

let py;

function draw(board,route){

 ctx.fillStyle="#e4eee5";ctx.fillRect(0,0,720,380);

 const k=Math.min(660/board[0].length,330/board.length);

 const x=(720-k*board[0].length)/2,y=(380-k*board.length)/2;

 const colors={".":"#f9fbf6","#":"#a8bbae","X":"#e2a08d","G":"#397bb5"};

 board.forEach((row,r)=>[...row].forEach((tile,c)=>{

  ctx.fillStyle=colors[tile];ctx.fillRect(x+c*k,y+r*k,k-1,k-1);

 }));

 ctx.beginPath();route.forEach(([r,c],i)=>i?ctx.lineTo(x+(c+.5)*k,y+(r+.5)*k):ctx.moveTo(x+(c+.5)*k,y+(r+.5)*k));

 ctx.strokeStyle="#08775e";ctx.lineWidth=4;ctx.lineJoin="round";ctx.stroke();

}

function run(){

 const board=el("board").value.trim().split(/\r?\n/);

 try{

  if(board.length>60||board[0].length>60||board[0][0]!==".")throw Error("Use a floor start and a grid up to 60 × 60.");

  py.globals.set("board_json",JSON.stringify(board));

  const [route,ok]=JSON.parse(py.runPython("rollout(board_json)"));

  draw(board,route);el("status").textContent=ok?"Goal reached in "+(route.length-1)+" steps.":"The policy did not reach the goal.";

 }catch(error){el("status").textContent=error.message;}

}

el("run").onclick=run;

try{

 const indexURL="https://cdn.jsdelivr.net/pyodide/v0.27.7/full/";

 py=await (await import(indexURL+"pyodide.mjs")).loadPyodide({indexURL});

 await py.loadPackage("numpy");

 for(const name of ["next_reward.py","model.json"]){

  const response=await fetch(name);if(!response.ok)throw Error(name+" unavailable");

  py.FS.writeFile(name,(await response.text()).replace(/^\uFEFF/,""));

 }

 py.runPython(`from next_reward import NextReward, MOVES

import json

agent = NextReward.load("model.json")

def rollout(raw):

    board = json.loads(raw)

    policy = agent.policy(board)

    path = [(0, 0)]

    seen = set(path)

    while True:

        r, c = path[-1]

        dy, dx = MOVES[int(policy[r, c])]

        nxt = r+dy, c+dx

        nr, nc = nxt

        if not (0 <= nr < len(board) and 0 <= nc < len(board[0])) or nxt in seen or board[nr][nc] in "#X":

            return json.dumps([path, False])

        path.append(nxt)

        seen.add(nxt)

        if board[nr][nc] == "G":

            return json.dumps([path, True])

`);

 el("run").disabled=false;run();

}catch(error){el("status").textContent="Could not load checkpoint. "+error.message}

</script>

<p><a href="https://huggingface.co/thelamapi/Next-Reward">Model card and source</a></p>
</main></html>