Publish Next-Passage model
Browse files- README.md +64 -0
- evaluation.json +391 -0
- inference.py +48 -0
- model.json +1 -0
- nlrt.py +226 -0
- passage_core.py +286 -0
- preview.png +0 -0
- replay.mp4 +0 -0
- selftest.json +1 -0
README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- planning
|
| 5 |
+
- robotics
|
| 6 |
+
- simulation
|
| 7 |
+
- next-lab
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Next Passage
|
| 11 |
+
|
| 12 |
+
Next Passage routes a robot across a grid floor where **doors open and close on a schedule** and **obstacles patrol
|
| 13 |
+
corridors and randomly stall**. The planner searches over space *and time* (with a wait action), so it can wait for a
|
| 14 |
+
door or let an obstacle pass, and it replans whenever an obstacle deviates from its prediction. **Simulation only.**
|
| 15 |
+
|
| 16 |
+
**Space:** https://huggingface.co/spaces/thelamapi/Next-Passage — pick a scenario seed, obstacle density and stall probability; all three planners run in the browser.
|
| 17 |
+
|
| 18 |
+
## Results (600 test episodes per planner: 100 seeds × 3 densities × 2 stall rates)
|
| 19 |
+
|
| 20 |
+
| Planner | Arrived | Episodes with a collision | Door closed on robot / episode | Steps ÷ static shortest length | Replans / episode | Search nodes / episode | Python ms / episode |
|
| 21 |
+
|---|---:|---:|---:|---:|---:|---:|---:|
|
| 22 |
+
| Next Passage (time-aware) | 100.0% | 0.0% | 0.000 | 1.13 | 19.5 | 1438 | 10.9 |
|
| 23 |
+
| Static + reactive replanning | 100.0% | 50.0% | 0.173 | 1.26 | 1.7 | 434 | 1.3 |
|
| 24 |
+
| Static shortest path | 100.0% | 61.3% | 0.192 | 1.10 | 0.0 | 194 | 0.9 |
|
| 25 |
+
|
| 26 |
+
On the 600 episodes where every planner arrived, mean arrival steps were
|
| 27 |
+
30.8 (time-aware), 34.3 (reactive) and 30.1 (static).
|
| 28 |
+
The time-aware planner had 0 unsafe or unfinished episodes out of 600; they are listed in `evaluation.json`.
|
| 29 |
+
|
| 30 |
+
Share of episodes with any unsafe event (collision or door closing on the robot):
|
| 31 |
+
|
| 32 |
+
| Condition | Time-aware | Reactive | Static |
|
| 33 |
+
|---|---:|---:|---:|
|
| 34 |
+
| low/stall=0.1 | 0% | 36% | 53% |
|
| 35 |
+
| low/stall=0.25 | 0% | 41% | 48% |
|
| 36 |
+
| medium/stall=0.1 | 0% | 64% | 74% |
|
| 37 |
+
| medium/stall=0.25 | 0% | 61% | 72% |
|
| 38 |
+
| high/stall=0.1 | 0% | 76% | 88% |
|
| 39 |
+
| high/stall=0.25 | 0% | 77% | 85% |
|
| 40 |
+
|
| 41 |
+

|
| 42 |
+
|
| 43 |
+
[Replay video](replay.mp4): one unselected test seed with all planners, then a failure case (static planner collision).
|
| 44 |
+
|
| 45 |
+
## Method
|
| 46 |
+
* Grid 21×15, three walls with one open gap and one scheduled door each, pillars, 3/6/10 patrolling obstacles.
|
| 47 |
+
* Obstacles advance one cell per step along a back-and-forth loop and stall with probability p. They ignore the robot.
|
| 48 |
+
* Time-aware planner: A* over (cell, t) with moves {wait, N, S, E, W}, heuristic = static BFS distance (admissible),
|
| 49 |
+
horizon 160 steps; cells predicted to hold an obstacle at t or t−1 are avoided (covers swaps and single stalls); doors
|
| 50 |
+
are entered only when open on arrival. It replans when an observed obstacle phase differs from its prediction.
|
| 51 |
+
* Collisions: robot and obstacle in the same cell after a step, or swapping cells.
|
| 52 |
+
* The JavaScript port in the Space reproduces Python exactly (Node parity: 6 cases, 0 mismatches).
|
| 53 |
+
|
| 54 |
+
## Usage
|
| 55 |
+
```bash
|
| 56 |
+
python inference.py --seed 20001 --density high --planner timed
|
| 57 |
+
python inference.py --selftest
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## Limitations
|
| 61 |
+
* Discrete grid, perfect localisation and perfect knowledge of the door schedule; obstacles are observed exactly.
|
| 62 |
+
* Obstacles never react to the robot; real crowds and vehicles do.
|
| 63 |
+
* Search effort grows with horizon and obstacle count; a real system would need a time budget per replan.
|
| 64 |
+
* Recorded on NVIDIA L4 (Python timing); no physical robot was used.
|
evaluation.json
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 12 |
+
"gpu": "NVIDIA L4",
|
| 13 |
+
"colab": true
|
| 14 |
+
},
|
| 15 |
+
"data": {
|
| 16 |
+
"type": "simulation",
|
| 17 |
+
"note": "Worlds generated by passage_core.make_world(seed); no external data.",
|
| 18 |
+
"test_seeds": [
|
| 19 |
+
20000,
|
| 20 |
+
20099
|
| 21 |
+
],
|
| 22 |
+
"densities": [
|
| 23 |
+
"low",
|
| 24 |
+
"medium",
|
| 25 |
+
"high"
|
| 26 |
+
],
|
| 27 |
+
"stall_probabilities": [
|
| 28 |
+
0.1,
|
| 29 |
+
0.25
|
| 30 |
+
],
|
| 31 |
+
"development_seeds": "1-999 (used while writing the code, not in the benchmark)"
|
| 32 |
+
},
|
| 33 |
+
"results": {
|
| 34 |
+
"overall": {
|
| 35 |
+
"timed": {
|
| 36 |
+
"episodes": 600,
|
| 37 |
+
"arrival_rate": 1.0,
|
| 38 |
+
"collision_episode_rate": 0.0,
|
| 39 |
+
"collisions_per_episode": 0.0,
|
| 40 |
+
"door_hits_per_episode": 0.0,
|
| 41 |
+
"unsafe_episode_rate": 0.0,
|
| 42 |
+
"arrival_time_mean": 30.821666666666665,
|
| 43 |
+
"arrival_over_static_length": 1.1283655425180663,
|
| 44 |
+
"replans_per_episode": 19.506666666666668,
|
| 45 |
+
"nodes_per_episode": 1437.6466666666668,
|
| 46 |
+
"ms_per_episode_python": 10.851596298343793
|
| 47 |
+
},
|
| 48 |
+
"reactive": {
|
| 49 |
+
"episodes": 600,
|
| 50 |
+
"arrival_rate": 1.0,
|
| 51 |
+
"collision_episode_rate": 0.5,
|
| 52 |
+
"collisions_per_episode": 0.9183333333333333,
|
| 53 |
+
"door_hits_per_episode": 0.17333333333333334,
|
| 54 |
+
"unsafe_episode_rate": 0.5916666666666667,
|
| 55 |
+
"arrival_time_mean": 34.32,
|
| 56 |
+
"arrival_over_static_length": 1.2595982537349353,
|
| 57 |
+
"replans_per_episode": 1.68,
|
| 58 |
+
"nodes_per_episode": 433.9866666666667,
|
| 59 |
+
"ms_per_episode_python": 1.3411656633365965
|
| 60 |
+
},
|
| 61 |
+
"static": {
|
| 62 |
+
"episodes": 600,
|
| 63 |
+
"arrival_rate": 1.0,
|
| 64 |
+
"collision_episode_rate": 0.6133333333333333,
|
| 65 |
+
"collisions_per_episode": 1.2216666666666667,
|
| 66 |
+
"door_hits_per_episode": 0.19166666666666668,
|
| 67 |
+
"unsafe_episode_rate": 0.7,
|
| 68 |
+
"arrival_time_mean": 30.113333333333333,
|
| 69 |
+
"arrival_over_static_length": 1.1001174710211388,
|
| 70 |
+
"replans_per_episode": 0.0,
|
| 71 |
+
"nodes_per_episode": 194.46,
|
| 72 |
+
"ms_per_episode_python": 0.9334195850048369
|
| 73 |
+
}
|
| 74 |
+
},
|
| 75 |
+
"low/stall=0.1": {
|
| 76 |
+
"timed": {
|
| 77 |
+
"episodes": 100,
|
| 78 |
+
"arrival_rate": 1.0,
|
| 79 |
+
"collision_episode_rate": 0.0,
|
| 80 |
+
"collisions_per_episode": 0.0,
|
| 81 |
+
"door_hits_per_episode": 0.0,
|
| 82 |
+
"unsafe_episode_rate": 0.0,
|
| 83 |
+
"arrival_time_mean": 29.82,
|
| 84 |
+
"arrival_over_static_length": 1.0895462809004304,
|
| 85 |
+
"replans_per_episode": 8.38,
|
| 86 |
+
"nodes_per_episode": 610.55,
|
| 87 |
+
"ms_per_episode_python": 4.061648179981603
|
| 88 |
+
},
|
| 89 |
+
"reactive": {
|
| 90 |
+
"episodes": 100,
|
| 91 |
+
"arrival_rate": 1.0,
|
| 92 |
+
"collision_episode_rate": 0.24,
|
| 93 |
+
"collisions_per_episode": 0.43,
|
| 94 |
+
"door_hits_per_episode": 0.17,
|
| 95 |
+
"unsafe_episode_rate": 0.36,
|
| 96 |
+
"arrival_time_mean": 33.61,
|
| 97 |
+
"arrival_over_static_length": 1.233392826034949,
|
| 98 |
+
"replans_per_episode": 1.3,
|
| 99 |
+
"nodes_per_episode": 386.17,
|
| 100 |
+
"ms_per_episode_python": 1.0644452600081422
|
| 101 |
+
},
|
| 102 |
+
"static": {
|
| 103 |
+
"episodes": 100,
|
| 104 |
+
"arrival_rate": 1.0,
|
| 105 |
+
"collision_episode_rate": 0.39,
|
| 106 |
+
"collisions_per_episode": 0.61,
|
| 107 |
+
"door_hits_per_episode": 0.18,
|
| 108 |
+
"unsafe_episode_rate": 0.53,
|
| 109 |
+
"arrival_time_mean": 29.56,
|
| 110 |
+
"arrival_over_static_length": 1.0800256884070292,
|
| 111 |
+
"replans_per_episode": 0.0,
|
| 112 |
+
"nodes_per_episode": 194.46,
|
| 113 |
+
"ms_per_episode_python": 0.754936149983223
|
| 114 |
+
}
|
| 115 |
+
},
|
| 116 |
+
"low/stall=0.25": {
|
| 117 |
+
"timed": {
|
| 118 |
+
"episodes": 100,
|
| 119 |
+
"arrival_rate": 1.0,
|
| 120 |
+
"collision_episode_rate": 0.0,
|
| 121 |
+
"collisions_per_episode": 0.0,
|
| 122 |
+
"door_hits_per_episode": 0.0,
|
| 123 |
+
"unsafe_episode_rate": 0.0,
|
| 124 |
+
"arrival_time_mean": 29.87,
|
| 125 |
+
"arrival_over_static_length": 1.0914604301164639,
|
| 126 |
+
"replans_per_episode": 17.37,
|
| 127 |
+
"nodes_per_episode": 934.54,
|
| 128 |
+
"ms_per_episode_python": 6.63758367999435
|
| 129 |
+
},
|
| 130 |
+
"reactive": {
|
| 131 |
+
"episodes": 100,
|
| 132 |
+
"arrival_rate": 1.0,
|
| 133 |
+
"collision_episode_rate": 0.27,
|
| 134 |
+
"collisions_per_episode": 0.4,
|
| 135 |
+
"door_hits_per_episode": 0.16,
|
| 136 |
+
"unsafe_episode_rate": 0.41,
|
| 137 |
+
"arrival_time_mean": 33.27,
|
| 138 |
+
"arrival_over_static_length": 1.2239181965181254,
|
| 139 |
+
"replans_per_episode": 1.02,
|
| 140 |
+
"nodes_per_episode": 356.71,
|
| 141 |
+
"ms_per_episode_python": 1.0321695299853673
|
| 142 |
+
},
|
| 143 |
+
"static": {
|
| 144 |
+
"episodes": 100,
|
| 145 |
+
"arrival_rate": 1.0,
|
| 146 |
+
"collision_episode_rate": 0.36,
|
| 147 |
+
"collisions_per_episode": 0.52,
|
| 148 |
+
"door_hits_per_episode": 0.17,
|
| 149 |
+
"unsafe_episode_rate": 0.48,
|
| 150 |
+
"arrival_time_mean": 29.56,
|
| 151 |
+
"arrival_over_static_length": 1.079774069752129,
|
| 152 |
+
"replans_per_episode": 0.0,
|
| 153 |
+
"nodes_per_episode": 194.46,
|
| 154 |
+
"ms_per_episode_python": 0.7569391700008055
|
| 155 |
+
}
|
| 156 |
+
},
|
| 157 |
+
"medium/stall=0.1": {
|
| 158 |
+
"timed": {
|
| 159 |
+
"episodes": 100,
|
| 160 |
+
"arrival_rate": 1.0,
|
| 161 |
+
"collision_episode_rate": 0.0,
|
| 162 |
+
"collisions_per_episode": 0.0,
|
| 163 |
+
"door_hits_per_episode": 0.0,
|
| 164 |
+
"unsafe_episode_rate": 0.0,
|
| 165 |
+
"arrival_time_mean": 30.47,
|
| 166 |
+
"arrival_over_static_length": 1.114301031781453,
|
| 167 |
+
"replans_per_episode": 14.49,
|
| 168 |
+
"nodes_per_episode": 1082.31,
|
| 169 |
+
"ms_per_episode_python": 7.966914250005175
|
| 170 |
+
},
|
| 171 |
+
"reactive": {
|
| 172 |
+
"episodes": 100,
|
| 173 |
+
"arrival_rate": 1.0,
|
| 174 |
+
"collision_episode_rate": 0.53,
|
| 175 |
+
"collisions_per_episode": 1.01,
|
| 176 |
+
"door_hits_per_episode": 0.2,
|
| 177 |
+
"unsafe_episode_rate": 0.64,
|
| 178 |
+
"arrival_time_mean": 34.09,
|
| 179 |
+
"arrival_over_static_length": 1.2461882677597556,
|
| 180 |
+
"replans_per_episode": 1.5,
|
| 181 |
+
"nodes_per_episode": 430.99,
|
| 182 |
+
"ms_per_episode_python": 1.324342320026517
|
| 183 |
+
},
|
| 184 |
+
"static": {
|
| 185 |
+
"episodes": 100,
|
| 186 |
+
"arrival_rate": 1.0,
|
| 187 |
+
"collision_episode_rate": 0.66,
|
| 188 |
+
"collisions_per_episode": 1.42,
|
| 189 |
+
"door_hits_per_episode": 0.24,
|
| 190 |
+
"unsafe_episode_rate": 0.74,
|
| 191 |
+
"arrival_time_mean": 29.91,
|
| 192 |
+
"arrival_over_static_length": 1.0910707461530154,
|
| 193 |
+
"replans_per_episode": 0.0,
|
| 194 |
+
"nodes_per_episode": 194.46,
|
| 195 |
+
"ms_per_episode_python": 0.9206604100108962
|
| 196 |
+
}
|
| 197 |
+
},
|
| 198 |
+
"medium/stall=0.25": {
|
| 199 |
+
"timed": {
|
| 200 |
+
"episodes": 100,
|
| 201 |
+
"arrival_rate": 1.0,
|
| 202 |
+
"collision_episode_rate": 0.0,
|
| 203 |
+
"collisions_per_episode": 0.0,
|
| 204 |
+
"door_hits_per_episode": 0.0,
|
| 205 |
+
"unsafe_episode_rate": 0.0,
|
| 206 |
+
"arrival_time_mean": 30.87,
|
| 207 |
+
"arrival_over_static_length": 1.1302208142868928,
|
| 208 |
+
"replans_per_episode": 25.3,
|
| 209 |
+
"nodes_per_episode": 1593.24,
|
| 210 |
+
"ms_per_episode_python": 12.074100600034399
|
| 211 |
+
},
|
| 212 |
+
"reactive": {
|
| 213 |
+
"episodes": 100,
|
| 214 |
+
"arrival_rate": 1.0,
|
| 215 |
+
"collision_episode_rate": 0.53,
|
| 216 |
+
"collisions_per_episode": 0.94,
|
| 217 |
+
"door_hits_per_episode": 0.16,
|
| 218 |
+
"unsafe_episode_rate": 0.61,
|
| 219 |
+
"arrival_time_mean": 34.73,
|
| 220 |
+
"arrival_over_static_length": 1.2771276768999125,
|
| 221 |
+
"replans_per_episode": 1.75,
|
| 222 |
+
"nodes_per_episode": 437.83,
|
| 223 |
+
"ms_per_episode_python": 1.3322608600083186
|
| 224 |
+
},
|
| 225 |
+
"static": {
|
| 226 |
+
"episodes": 100,
|
| 227 |
+
"arrival_rate": 1.0,
|
| 228 |
+
"collision_episode_rate": 0.65,
|
| 229 |
+
"collisions_per_episode": 1.28,
|
| 230 |
+
"door_hits_per_episode": 0.18,
|
| 231 |
+
"unsafe_episode_rate": 0.72,
|
| 232 |
+
"arrival_time_mean": 30.26,
|
| 233 |
+
"arrival_over_static_length": 1.105225036931732,
|
| 234 |
+
"replans_per_episode": 0.0,
|
| 235 |
+
"nodes_per_episode": 194.46,
|
| 236 |
+
"ms_per_episode_python": 0.9200024500296422
|
| 237 |
+
}
|
| 238 |
+
},
|
| 239 |
+
"high/stall=0.1": {
|
| 240 |
+
"timed": {
|
| 241 |
+
"episodes": 100,
|
| 242 |
+
"arrival_rate": 1.0,
|
| 243 |
+
"collision_episode_rate": 0.0,
|
| 244 |
+
"collisions_per_episode": 0.0,
|
| 245 |
+
"door_hits_per_episode": 0.0,
|
| 246 |
+
"unsafe_episode_rate": 0.0,
|
| 247 |
+
"arrival_time_mean": 31.69,
|
| 248 |
+
"arrival_over_static_length": 1.1632242208869854,
|
| 249 |
+
"replans_per_episode": 20.94,
|
| 250 |
+
"nodes_per_episode": 1917.9,
|
| 251 |
+
"ms_per_episode_python": 14.807957160005571
|
| 252 |
+
},
|
| 253 |
+
"reactive": {
|
| 254 |
+
"episodes": 100,
|
| 255 |
+
"arrival_rate": 1.0,
|
| 256 |
+
"collision_episode_rate": 0.7,
|
| 257 |
+
"collisions_per_episode": 1.38,
|
| 258 |
+
"door_hits_per_episode": 0.16,
|
| 259 |
+
"unsafe_episode_rate": 0.76,
|
| 260 |
+
"arrival_time_mean": 33.84,
|
| 261 |
+
"arrival_over_static_length": 1.240357567893443,
|
| 262 |
+
"replans_per_episode": 1.9,
|
| 263 |
+
"nodes_per_episode": 448.13,
|
| 264 |
+
"ms_per_episode_python": 1.5566964800200367
|
| 265 |
+
},
|
| 266 |
+
"static": {
|
| 267 |
+
"episodes": 100,
|
| 268 |
+
"arrival_rate": 1.0,
|
| 269 |
+
"collision_episode_rate": 0.84,
|
| 270 |
+
"collisions_per_episode": 1.88,
|
| 271 |
+
"door_hits_per_episode": 0.2,
|
| 272 |
+
"unsafe_episode_rate": 0.88,
|
| 273 |
+
"arrival_time_mean": 30.33,
|
| 274 |
+
"arrival_over_static_length": 1.1080862929280573,
|
| 275 |
+
"replans_per_episode": 0.0,
|
| 276 |
+
"nodes_per_episode": 194.46,
|
| 277 |
+
"ms_per_episode_python": 1.1195999200072038
|
| 278 |
+
}
|
| 279 |
+
},
|
| 280 |
+
"high/stall=0.25": {
|
| 281 |
+
"timed": {
|
| 282 |
+
"episodes": 100,
|
| 283 |
+
"arrival_rate": 1.0,
|
| 284 |
+
"collision_episode_rate": 0.0,
|
| 285 |
+
"collisions_per_episode": 0.0,
|
| 286 |
+
"door_hits_per_episode": 0.0,
|
| 287 |
+
"unsafe_episode_rate": 0.0,
|
| 288 |
+
"arrival_time_mean": 32.21,
|
| 289 |
+
"arrival_over_static_length": 1.1814404771361715,
|
| 290 |
+
"replans_per_episode": 30.56,
|
| 291 |
+
"nodes_per_episode": 2487.34,
|
| 292 |
+
"ms_per_episode_python": 19.56137392004166
|
| 293 |
+
},
|
| 294 |
+
"reactive": {
|
| 295 |
+
"episodes": 100,
|
| 296 |
+
"arrival_rate": 1.0,
|
| 297 |
+
"collision_episode_rate": 0.73,
|
| 298 |
+
"collisions_per_episode": 1.35,
|
| 299 |
+
"door_hits_per_episode": 0.19,
|
| 300 |
+
"unsafe_episode_rate": 0.77,
|
| 301 |
+
"arrival_time_mean": 36.38,
|
| 302 |
+
"arrival_over_static_length": 1.3366049873034263,
|
| 303 |
+
"replans_per_episode": 2.61,
|
| 304 |
+
"nodes_per_episode": 544.09,
|
| 305 |
+
"ms_per_episode_python": 1.7370795299711972
|
| 306 |
+
},
|
| 307 |
+
"static": {
|
| 308 |
+
"episodes": 100,
|
| 309 |
+
"arrival_rate": 1.0,
|
| 310 |
+
"collision_episode_rate": 0.78,
|
| 311 |
+
"collisions_per_episode": 1.62,
|
| 312 |
+
"door_hits_per_episode": 0.18,
|
| 313 |
+
"unsafe_episode_rate": 0.85,
|
| 314 |
+
"arrival_time_mean": 31.06,
|
| 315 |
+
"arrival_over_static_length": 1.136522991954869,
|
| 316 |
+
"replans_per_episode": 0.0,
|
| 317 |
+
"nodes_per_episode": 194.46,
|
| 318 |
+
"ms_per_episode_python": 1.128379409997251
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
},
|
| 322 |
+
"paired_arrival_time": {
|
| 323 |
+
"episodes_all_arrived": 600,
|
| 324 |
+
"arrival_time_timed": 30.821666666666665,
|
| 325 |
+
"arrival_time_reactive": 34.32,
|
| 326 |
+
"arrival_time_static": 30.113333333333333
|
| 327 |
+
},
|
| 328 |
+
"failures_time_aware": [],
|
| 329 |
+
"n_failures_time_aware": 0,
|
| 330 |
+
"media": {
|
| 331 |
+
"file": "replay.mp4",
|
| 332 |
+
"frames": 92,
|
| 333 |
+
"fps": 6,
|
| 334 |
+
"bytes": 99705,
|
| 335 |
+
"segments": [
|
| 336 |
+
"seed 20000",
|
| 337 |
+
"static planner collision: seed 20001 low stall 0.1"
|
| 338 |
+
]
|
| 339 |
+
},
|
| 340 |
+
"tests": {
|
| 341 |
+
"reload": {
|
| 342 |
+
"returncode": 0,
|
| 343 |
+
"seconds": 0.28,
|
| 344 |
+
"process": "fresh python -E subprocess, cwd = temporary copy of the model folder",
|
| 345 |
+
"passed": true,
|
| 346 |
+
"cases": 6,
|
| 347 |
+
"mismatches": []
|
| 348 |
+
},
|
| 349 |
+
"node_parity": {
|
| 350 |
+
"cases": 6,
|
| 351 |
+
"failed": 0,
|
| 352 |
+
"maxDiff": 0,
|
| 353 |
+
"results": [
|
| 354 |
+
{
|
| 355 |
+
"name": "20001-high-timed",
|
| 356 |
+
"maxDiff": 0
|
| 357 |
+
},
|
| 358 |
+
{
|
| 359 |
+
"name": "20001-high-static",
|
| 360 |
+
"maxDiff": 0
|
| 361 |
+
},
|
| 362 |
+
{
|
| 363 |
+
"name": "20002-medium-reactive",
|
| 364 |
+
"maxDiff": 0
|
| 365 |
+
},
|
| 366 |
+
{
|
| 367 |
+
"name": "20003-low-timed",
|
| 368 |
+
"maxDiff": 0
|
| 369 |
+
},
|
| 370 |
+
{
|
| 371 |
+
"name": "20004-high-timed",
|
| 372 |
+
"maxDiff": 0
|
| 373 |
+
},
|
| 374 |
+
{
|
| 375 |
+
"name": "31337-medium-timed",
|
| 376 |
+
"maxDiff": 0
|
| 377 |
+
}
|
| 378 |
+
],
|
| 379 |
+
"ms": 55,
|
| 380 |
+
"runtime": "node v22.23.4",
|
| 381 |
+
"status": "passed"
|
| 382 |
+
},
|
| 383 |
+
"browser": {
|
| 384 |
+
"status": "skipped",
|
| 385 |
+
"reason": "playwright not installed"
|
| 386 |
+
}
|
| 387 |
+
},
|
| 388 |
+
"notes": [
|
| 389 |
+
"ms_per_episode is Python wall time on the recorded device; nodes_expanded is implementation-independent."
|
| 390 |
+
]
|
| 391 |
+
}
|
inference.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Next Passage: run the time-aware planner (and baselines) on a generated scenario.
|
| 2 |
+
|
| 3 |
+
Usage:
|
| 4 |
+
python inference.py --seed 20001 --density high --planner timed
|
| 5 |
+
python inference.py --selftest
|
| 6 |
+
"""
|
| 7 |
+
import argparse
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
+
sys.path.insert(0, HERE)
|
| 14 |
+
|
| 15 |
+
import passage_core as pc # noqa: E402
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def plan_and_run(seed, density="medium", p_stall=0.15, planner="timed"):
|
| 19 |
+
world = pc.make_world(seed, density, p_stall)
|
| 20 |
+
return world, pc.run_episode(world, planner)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _selftest():
|
| 24 |
+
with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f:
|
| 25 |
+
cases = json.load(f)["cases"]
|
| 26 |
+
bad = []
|
| 27 |
+
for c in cases:
|
| 28 |
+
_, r = plan_and_run(**c["input"])
|
| 29 |
+
got = {k: r[k] for k in c["expected"]}
|
| 30 |
+
if got != c["expected"]:
|
| 31 |
+
bad.append({"input": c["input"], "got": got, "expected": c["expected"]})
|
| 32 |
+
print(json.dumps({"passed": not bad, "cases": len(cases), "mismatches": bad[:3]}))
|
| 33 |
+
return not bad
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
ap = argparse.ArgumentParser()
|
| 38 |
+
ap.add_argument("--selftest", action="store_true")
|
| 39 |
+
ap.add_argument("--seed", type=int, default=20001)
|
| 40 |
+
ap.add_argument("--density", default="medium", choices=["low", "medium", "high"])
|
| 41 |
+
ap.add_argument("--p_stall", type=float, default=0.15)
|
| 42 |
+
ap.add_argument("--planner", default="timed", choices=["timed", "reactive", "static"])
|
| 43 |
+
a = ap.parse_args()
|
| 44 |
+
if a.selftest:
|
| 45 |
+
sys.exit(0 if _selftest() else 1)
|
| 46 |
+
_, r = plan_and_run(a.seed, a.density, a.p_stall, a.planner)
|
| 47 |
+
r.pop("trace")
|
| 48 |
+
print(json.dumps(r, indent=1))
|
model.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"format":"nlrt-1","meta":{"name":"Next Passage","algorithm":"space-time A* with wait action","grid":[21,15],"horizon":160,"max_expand":60000,"obstacle_margin":"cells predicted at t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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""nlrt: tiny portable runtime shared by Next Lab models.
|
| 2 |
+
|
| 3 |
+
The same layer list runs in NumPy (this file) and in the browser (nlrt.js).
|
| 4 |
+
Weights are stored as base64 little-endian float32 inside model.json.
|
| 5 |
+
Only NumPy is required to load and run a model.
|
| 6 |
+
"""
|
| 7 |
+
import base64
|
| 8 |
+
import json
|
| 9 |
+
import math
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
__all__ = ["b64e", "b64d", "forward", "load_model", "Mulberry32", "export_sequential"]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def b64e(a):
|
| 17 |
+
return base64.b64encode(np.ascontiguousarray(a, dtype="<f4").tobytes()).decode("ascii")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def b64d(s, shape=None):
|
| 21 |
+
a = np.frombuffer(base64.b64decode(s), dtype="<f4").astype(np.float64)
|
| 22 |
+
return a.reshape(shape) if shape is not None else a
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def load_model(path):
|
| 26 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 27 |
+
spec = json.load(f)
|
| 28 |
+
for net in spec.get("nets", {}).values():
|
| 29 |
+
_decode(net)
|
| 30 |
+
for k, v in list(spec.get("tensors", {}).items()):
|
| 31 |
+
spec["tensors"][k] = b64d(v["data"], v["shape"])
|
| 32 |
+
return spec
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _decode(layers):
|
| 36 |
+
for L in layers:
|
| 37 |
+
for key in ("w", "b"):
|
| 38 |
+
if key in L and isinstance(L[key], str):
|
| 39 |
+
L[key] = b64d(L[key], L.get(key + "_shape"))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class Mulberry32:
|
| 43 |
+
"""Deterministic PRNG with an identical JavaScript twin in nlrt.js."""
|
| 44 |
+
|
| 45 |
+
def __init__(self, seed):
|
| 46 |
+
self.a = seed & 0xFFFFFFFF
|
| 47 |
+
|
| 48 |
+
def random(self):
|
| 49 |
+
self.a = (self.a + 0x6D2B79F5) & 0xFFFFFFFF
|
| 50 |
+
t = self.a
|
| 51 |
+
t = ((t ^ (t >> 15)) * (t | 1)) & 0xFFFFFFFF
|
| 52 |
+
t ^= (t + (((t ^ (t >> 7)) * (t | 61)) & 0xFFFFFFFF)) & 0xFFFFFFFF
|
| 53 |
+
return ((t ^ (t >> 14)) & 0xFFFFFFFF) / 4294967296.0
|
| 54 |
+
|
| 55 |
+
def uniform(self, lo, hi):
|
| 56 |
+
return lo + (hi - lo) * self.random()
|
| 57 |
+
|
| 58 |
+
def randint(self, n):
|
| 59 |
+
return int(math.floor(self.random() * n))
|
| 60 |
+
|
| 61 |
+
def normal(self):
|
| 62 |
+
u = max(self.random(), 1e-12)
|
| 63 |
+
v = self.random()
|
| 64 |
+
return math.sqrt(-2.0 * math.log(u)) * math.cos(2.0 * math.pi * v)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _pair(v):
|
| 68 |
+
return list(v) if isinstance(v, (list, tuple)) else [v, v]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _conv2d(x, w, b, stride, pad):
|
| 72 |
+
n, c, h, wd = x.shape
|
| 73 |
+
o, _, kh, kw = w.shape
|
| 74 |
+
sh, sw = stride
|
| 75 |
+
ph, pw = pad
|
| 76 |
+
xp = np.pad(x, ((0, 0), (0, 0), (ph, ph), (pw, pw)))
|
| 77 |
+
oh = (h + 2 * ph - kh) // sh + 1
|
| 78 |
+
ow = (wd + 2 * pw - kw) // sw + 1
|
| 79 |
+
cols = np.empty((n, c, kh, kw, oh, ow), dtype=x.dtype)
|
| 80 |
+
for i in range(kh):
|
| 81 |
+
for j in range(kw):
|
| 82 |
+
cols[:, :, i, j] = xp[:, :, i:i + sh * oh:sh, j:j + sw * ow:sw]
|
| 83 |
+
out = np.tensordot(cols, w, axes=([1, 2, 3], [1, 2, 3])) # n, oh, ow, o
|
| 84 |
+
out = out.transpose(0, 3, 1, 2)
|
| 85 |
+
if b is not None:
|
| 86 |
+
out = out + b[None, :, None, None]
|
| 87 |
+
return out
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def _maxpool(x, k):
|
| 91 |
+
kh, kw = k
|
| 92 |
+
n, c, h, w = x.shape
|
| 93 |
+
oh, ow = h // kh, w // kw
|
| 94 |
+
x = x[:, :, :oh * kh, :ow * kw].reshape(n, c, oh, kh, ow, kw)
|
| 95 |
+
return x.max(axis=(3, 5))
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def forward(layers, x):
|
| 99 |
+
"""Run a layer list on a batch. x: (N, D) or (N, C, H, W) or (N, C, L)."""
|
| 100 |
+
x = np.asarray(x, dtype=np.float64)
|
| 101 |
+
for L in layers:
|
| 102 |
+
t = L["type"]
|
| 103 |
+
if t == "linear":
|
| 104 |
+
x = x @ L["w"].T + L["b"]
|
| 105 |
+
elif t == "conv2d":
|
| 106 |
+
squeeze = x.ndim == 3
|
| 107 |
+
if squeeze:
|
| 108 |
+
x = x[:, :, None, :]
|
| 109 |
+
x = _conv2d(x, L["w"], L["b"], _pair(L["stride"]), _pair(L["pad"]))
|
| 110 |
+
if squeeze:
|
| 111 |
+
x = x[:, :, 0, :]
|
| 112 |
+
elif t == "relu":
|
| 113 |
+
x = np.maximum(x, 0)
|
| 114 |
+
elif t == "tanh":
|
| 115 |
+
x = np.tanh(x)
|
| 116 |
+
elif t == "sigmoid":
|
| 117 |
+
x = 1 / (1 + np.exp(-x))
|
| 118 |
+
elif t == "maxpool":
|
| 119 |
+
squeeze = x.ndim == 3
|
| 120 |
+
if squeeze:
|
| 121 |
+
x = x[:, :, None, :]
|
| 122 |
+
x = _maxpool(x, _pair(L["k"]))
|
| 123 |
+
if squeeze:
|
| 124 |
+
x = x[:, :, 0, :]
|
| 125 |
+
elif t == "gap":
|
| 126 |
+
x = x.reshape(x.shape[0], x.shape[1], -1).mean(axis=2)
|
| 127 |
+
elif t == "flatten":
|
| 128 |
+
x = x.reshape(x.shape[0], -1)
|
| 129 |
+
elif t == "upsample":
|
| 130 |
+
x = x.repeat(L["s"], axis=-1)
|
| 131 |
+
if x.ndim == 4:
|
| 132 |
+
x = x.repeat(L["s"], axis=-2)
|
| 133 |
+
elif t == "l2norm":
|
| 134 |
+
x = x / np.maximum(np.linalg.norm(x, axis=1, keepdims=True), 1e-12)
|
| 135 |
+
elif t == "reshape":
|
| 136 |
+
x = x.reshape([x.shape[0]] + list(L["shape"]))
|
| 137 |
+
else:
|
| 138 |
+
raise ValueError("unknown layer " + t)
|
| 139 |
+
return x
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def export_sequential(seq):
|
| 143 |
+
"""Convert a torch nn.Sequential into a portable layer list (BatchNorm folded)."""
|
| 144 |
+
import torch.nn as nn
|
| 145 |
+
|
| 146 |
+
mods = list(seq.modules())[1:] if not isinstance(seq, (list, tuple)) else list(seq)
|
| 147 |
+
mods = [m for m in mods if not isinstance(m, nn.Sequential)]
|
| 148 |
+
layers = []
|
| 149 |
+
|
| 150 |
+
def tensor(t):
|
| 151 |
+
return t.detach().cpu().double().numpy()
|
| 152 |
+
|
| 153 |
+
for m in mods:
|
| 154 |
+
if isinstance(m, nn.Linear):
|
| 155 |
+
b = tensor(m.bias) if m.bias is not None else np.zeros(m.out_features)
|
| 156 |
+
layers.append({"type": "linear", "w": tensor(m.weight), "b": b})
|
| 157 |
+
elif isinstance(m, (nn.Conv2d, nn.Conv1d)):
|
| 158 |
+
w = tensor(m.weight)
|
| 159 |
+
b = tensor(m.bias) if m.bias is not None else np.zeros(w.shape[0])
|
| 160 |
+
if isinstance(m, nn.Conv1d):
|
| 161 |
+
w = w[:, :, None, :]
|
| 162 |
+
stride, pad = [1, m.stride[0]], [0, m.padding[0]]
|
| 163 |
+
else:
|
| 164 |
+
stride, pad = list(m.stride), list(m.padding)
|
| 165 |
+
layers.append({"type": "conv2d", "w": w, "b": b, "stride": stride, "pad": pad})
|
| 166 |
+
elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
|
| 167 |
+
prev = layers[-1]
|
| 168 |
+
assert prev["type"] in ("conv2d", "linear"), "BatchNorm must follow conv/linear"
|
| 169 |
+
scale = tensor(m.weight) / np.sqrt(tensor(m.running_var) + m.eps)
|
| 170 |
+
shift = tensor(m.bias) - tensor(m.running_mean) * scale
|
| 171 |
+
if prev["type"] == "conv2d":
|
| 172 |
+
prev["w"] = prev["w"] * scale[:, None, None, None]
|
| 173 |
+
else:
|
| 174 |
+
prev["w"] = prev["w"] * scale[:, None]
|
| 175 |
+
prev["b"] = prev["b"] * scale + shift
|
| 176 |
+
elif isinstance(m, nn.ReLU):
|
| 177 |
+
layers.append({"type": "relu"})
|
| 178 |
+
elif isinstance(m, nn.Tanh):
|
| 179 |
+
layers.append({"type": "tanh"})
|
| 180 |
+
elif isinstance(m, nn.Sigmoid):
|
| 181 |
+
layers.append({"type": "sigmoid"})
|
| 182 |
+
elif isinstance(m, nn.MaxPool2d):
|
| 183 |
+
layers.append({"type": "maxpool", "k": _pair(m.kernel_size)})
|
| 184 |
+
elif isinstance(m, nn.MaxPool1d):
|
| 185 |
+
k = m.kernel_size if isinstance(m, int) else m.kernel_size
|
| 186 |
+
layers.append({"type": "maxpool", "k": [1, k if isinstance(k, int) else k[0]]})
|
| 187 |
+
elif isinstance(m, (nn.AdaptiveAvgPool2d, nn.AdaptiveAvgPool1d)):
|
| 188 |
+
layers.append({"type": "gap"})
|
| 189 |
+
elif isinstance(m, nn.Flatten):
|
| 190 |
+
layers.append({"type": "flatten"})
|
| 191 |
+
elif isinstance(m, nn.Upsample):
|
| 192 |
+
layers.append({"type": "upsample", "s": int(m.scale_factor)})
|
| 193 |
+
elif isinstance(m, nn.Dropout):
|
| 194 |
+
continue
|
| 195 |
+
elif m.__class__.__name__ == "L2Norm":
|
| 196 |
+
layers.append({"type": "l2norm"})
|
| 197 |
+
elif m.__class__.__name__ == "Reshape":
|
| 198 |
+
layers.append({"type": "reshape", "shape": list(m.shape)})
|
| 199 |
+
else:
|
| 200 |
+
raise ValueError("cannot export " + m.__class__.__name__)
|
| 201 |
+
return layers
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def encode_layers(layers):
|
| 205 |
+
out = []
|
| 206 |
+
for L in layers:
|
| 207 |
+
L2 = dict(L)
|
| 208 |
+
for key in ("w", "b"):
|
| 209 |
+
if key in L2:
|
| 210 |
+
arr = np.asarray(L2[key])
|
| 211 |
+
L2[key + "_shape"] = list(arr.shape)
|
| 212 |
+
L2[key] = b64e(arr)
|
| 213 |
+
out.append(L2)
|
| 214 |
+
return out
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def save_model(path, nets=None, tensors=None, meta=None):
|
| 218 |
+
spec = {"format": "nlrt-1", "meta": meta or {}, "nets": {}, "tensors": {}}
|
| 219 |
+
for name, layers in (nets or {}).items():
|
| 220 |
+
spec["nets"][name] = encode_layers(layers)
|
| 221 |
+
for name, arr in (tensors or {}).items():
|
| 222 |
+
arr = np.asarray(arr)
|
| 223 |
+
spec["tensors"][name] = {"shape": list(arr.shape), "data": b64e(arr)}
|
| 224 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 225 |
+
json.dump(spec, f, separators=(",", ":"))
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Next Passage: time-constrained grid navigation with periodic doors and moving obstacles.
|
| 2 |
+
|
| 3 |
+
Simulation only. One cell per step (4-neighbour moves or wait).
|
| 4 |
+
* Doors open and close on a known periodic schedule.
|
| 5 |
+
* Obstacles patrol back and forth along corridors and stall at random (probability p_stall per step),
|
| 6 |
+
so their future positions are only approximately predictable. They do not avoid the robot.
|
| 7 |
+
|
| 8 |
+
Planners
|
| 9 |
+
* static : BFS shortest path on the static map (doors treated as open); waits if the next cell is blocked now.
|
| 10 |
+
* reactive : like static, but re-runs BFS around currently blocked cells whenever it is blocked.
|
| 11 |
+
* timed : Next Passage. Space-time A* over (cell, time) with wait actions, using the door schedule and
|
| 12 |
+
predicted obstacle motion (cells occupied at t and t-1 are avoided). Replans when an observed
|
| 13 |
+
obstacle deviates from prediction or the next step became invalid.
|
| 14 |
+
A line-by-line JavaScript port lives in web/core.js; parity is tested on fixed seeds.
|
| 15 |
+
"""
|
| 16 |
+
from nlrt import Mulberry32
|
| 17 |
+
|
| 18 |
+
W, H = 21, 15
|
| 19 |
+
MOVES = [(0, 0), (1, 0), (-1, 0), (0, 1), (0, -1)]
|
| 20 |
+
DENSITY = {"low": 3, "medium": 6, "high": 10}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Heap:
|
| 24 |
+
"""Binary min-heap on tuples; identical algorithm in JS so pop order matches exactly."""
|
| 25 |
+
|
| 26 |
+
def __init__(self):
|
| 27 |
+
self.a = []
|
| 28 |
+
|
| 29 |
+
def push(self, item):
|
| 30 |
+
a = self.a
|
| 31 |
+
a.append(item)
|
| 32 |
+
i = len(a) - 1
|
| 33 |
+
while i > 0:
|
| 34 |
+
p = (i - 1) // 2
|
| 35 |
+
if a[p] <= a[i]:
|
| 36 |
+
break
|
| 37 |
+
a[p], a[i] = a[i], a[p]
|
| 38 |
+
i = p
|
| 39 |
+
|
| 40 |
+
def pop(self):
|
| 41 |
+
a = self.a
|
| 42 |
+
top = a[0]
|
| 43 |
+
last = a.pop()
|
| 44 |
+
if a:
|
| 45 |
+
a[0] = last
|
| 46 |
+
i, n = 0, len(a)
|
| 47 |
+
while True:
|
| 48 |
+
l, r, m = 2 * i + 1, 2 * i + 2, i
|
| 49 |
+
if l < n and a[l] < a[m]:
|
| 50 |
+
m = l
|
| 51 |
+
if r < n and a[r] < a[m]:
|
| 52 |
+
m = r
|
| 53 |
+
if m == i:
|
| 54 |
+
break
|
| 55 |
+
a[m], a[i] = a[i], a[m]
|
| 56 |
+
i = m
|
| 57 |
+
return top
|
| 58 |
+
|
| 59 |
+
def __len__(self):
|
| 60 |
+
return len(self.a)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def make_world(seed, density="medium", p_stall=0.15):
|
| 64 |
+
rng = Mulberry32(seed * 2654435761 % 4294967296)
|
| 65 |
+
walls = set()
|
| 66 |
+
for x in range(W):
|
| 67 |
+
walls.add((x, 0)); walls.add((x, H - 1))
|
| 68 |
+
for y in range(H):
|
| 69 |
+
walls.add((0, y)); walls.add((W - 1, y))
|
| 70 |
+
doors = []
|
| 71 |
+
for wx in (5, 10, 15):
|
| 72 |
+
gap_open = 1 + rng.randint(H - 2)
|
| 73 |
+
gap_door = 1 + rng.randint(H - 2)
|
| 74 |
+
while abs(gap_door - gap_open) < 3:
|
| 75 |
+
gap_door = 1 + rng.randint(H - 2)
|
| 76 |
+
for y in range(1, H - 1):
|
| 77 |
+
if y not in (gap_open, gap_door):
|
| 78 |
+
walls.add((wx, y))
|
| 79 |
+
period = 8 + rng.randint(8)
|
| 80 |
+
doors.append({"cell": [wx, gap_door], "period": period, "phase": rng.randint(period),
|
| 81 |
+
"closed": max(2, period // 2 - 1 + rng.randint(3))})
|
| 82 |
+
for _ in range(6): # pillars
|
| 83 |
+
x, y = 2 + rng.randint(W - 4), 2 + rng.randint(H - 4)
|
| 84 |
+
if x not in (4, 5, 6, 9, 10, 11, 14, 15, 16):
|
| 85 |
+
walls.add((x, y))
|
| 86 |
+
start, goal = [1, 1 + rng.randint(H - 2)], [W - 2, 1 + rng.randint(H - 2)]
|
| 87 |
+
walls.discard(tuple(start)); walls.discard(tuple(goal))
|
| 88 |
+
door_cells = {tuple(d["cell"]) for d in doors}
|
| 89 |
+
obstacles, used = [], set()
|
| 90 |
+
tries = 0
|
| 91 |
+
while len(obstacles) < DENSITY[density] and tries < 400:
|
| 92 |
+
tries += 1
|
| 93 |
+
horiz = rng.random() < 0.5
|
| 94 |
+
x, y = 1 + rng.randint(W - 2), 1 + rng.randint(H - 2)
|
| 95 |
+
length = 4 + rng.randint(7)
|
| 96 |
+
cells = []
|
| 97 |
+
for k in range(length):
|
| 98 |
+
c = (x + k, y) if horiz else (x, y + k)
|
| 99 |
+
if c in walls or c in door_cells or c in used or c[0] >= W - 1 or c[1] >= H - 1 \
|
| 100 |
+
or list(c) == start or list(c) == goal:
|
| 101 |
+
break
|
| 102 |
+
cells.append(c)
|
| 103 |
+
if len(cells) < 3:
|
| 104 |
+
continue
|
| 105 |
+
loop = cells + cells[-2:0:-1]
|
| 106 |
+
used.update(cells)
|
| 107 |
+
obstacles.append({"loop": [list(c) for c in loop], "offset": rng.randint(len(loop))})
|
| 108 |
+
return {"seed": seed, "density": density, "p_stall": p_stall, "walls": sorted(list(c) for c in walls),
|
| 109 |
+
"doors": doors, "obstacles": obstacles, "start": start, "goal": goal}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def door_closed(d, t):
|
| 113 |
+
return (t + d["phase"]) % d["period"] < d["closed"]
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def free(world_walls, c):
|
| 117 |
+
return 0 <= c[0] < W and 0 <= c[1] < H and c not in world_walls
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def bfs(walls, start, goal, blocked=frozenset()):
|
| 121 |
+
"""Return (path, expanded). Neighbour order follows MOVES[1:]."""
|
| 122 |
+
start, goal = tuple(start), tuple(goal)
|
| 123 |
+
prev = {start: None}
|
| 124 |
+
q, qi, expanded = [start], 0, 0
|
| 125 |
+
while qi < len(q):
|
| 126 |
+
c = q[qi]; qi += 1; expanded += 1
|
| 127 |
+
if c == goal:
|
| 128 |
+
path = []
|
| 129 |
+
while c is not None:
|
| 130 |
+
path.append(c); c = prev[c]
|
| 131 |
+
return path[::-1], expanded
|
| 132 |
+
for dx, dy in MOVES[1:]:
|
| 133 |
+
n = (c[0] + dx, c[1] + dy)
|
| 134 |
+
if n not in prev and free(walls, n) and n not in blocked:
|
| 135 |
+
prev[n] = c
|
| 136 |
+
q.append(n)
|
| 137 |
+
return None, expanded
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def distance_map(walls, goal):
|
| 141 |
+
goal = tuple(goal)
|
| 142 |
+
dist = {goal: 0}
|
| 143 |
+
q, qi = [goal], 0
|
| 144 |
+
while qi < len(q):
|
| 145 |
+
c = q[qi]; qi += 1
|
| 146 |
+
for dx, dy in MOVES[1:]:
|
| 147 |
+
n = (c[0] + dx, c[1] + dy)
|
| 148 |
+
if n not in dist and free(walls, n):
|
| 149 |
+
dist[n] = dist[c] + 1
|
| 150 |
+
q.append(n)
|
| 151 |
+
return dist
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def timed_astar(walls, doors, dist, start, t0, goal, obs_idx, loops, horizon=160, max_expand=60000):
|
| 155 |
+
"""Space-time A*. obs_idx: current loop index of each obstacle (prediction assumes no stalls)."""
|
| 156 |
+
goal = tuple(goal)
|
| 157 |
+
door_at = {tuple(d["cell"]): d for d in doors}
|
| 158 |
+
|
| 159 |
+
def occupied(t):
|
| 160 |
+
cells = set()
|
| 161 |
+
for i, L in enumerate(loops):
|
| 162 |
+
for tt in (t, t - 1):
|
| 163 |
+
if tt >= t0:
|
| 164 |
+
cells.add(tuple(L[(obs_idx[i] + tt - t0) % len(L)]))
|
| 165 |
+
return cells
|
| 166 |
+
|
| 167 |
+
occ_cache = {}
|
| 168 |
+
start = tuple(start)
|
| 169 |
+
if start not in dist:
|
| 170 |
+
return None, 0
|
| 171 |
+
heap = Heap()
|
| 172 |
+
seq = 0
|
| 173 |
+
heap.push((dist[start], 0, seq, start[0], start[1], t0))
|
| 174 |
+
parent = {(start, t0): None}
|
| 175 |
+
expanded = 0
|
| 176 |
+
while len(heap) and expanded < max_expand:
|
| 177 |
+
f, negg, _, x, y, t = heap.pop()
|
| 178 |
+
c = (x, y)
|
| 179 |
+
expanded += 1
|
| 180 |
+
if c == goal:
|
| 181 |
+
path, node = [], (c, t)
|
| 182 |
+
while node is not None:
|
| 183 |
+
path.append(list(node[0])); node = parent[node]
|
| 184 |
+
return path[::-1], expanded
|
| 185 |
+
if t - t0 >= horizon:
|
| 186 |
+
continue
|
| 187 |
+
nt = t + 1
|
| 188 |
+
if nt not in occ_cache:
|
| 189 |
+
occ_cache[nt] = occupied(nt)
|
| 190 |
+
occ = occ_cache[nt]
|
| 191 |
+
for dx, dy in MOVES:
|
| 192 |
+
n = (x + dx, y + dy)
|
| 193 |
+
if not free(walls, n) or n in occ or n not in dist:
|
| 194 |
+
continue
|
| 195 |
+
d = door_at.get(n)
|
| 196 |
+
if d is not None and door_closed(d, nt):
|
| 197 |
+
continue
|
| 198 |
+
key = (n, nt)
|
| 199 |
+
if key in parent:
|
| 200 |
+
continue
|
| 201 |
+
parent[key] = (c, t)
|
| 202 |
+
seq += 1
|
| 203 |
+
g = nt - t0
|
| 204 |
+
heap.push((g + dist[n], -g, seq, n[0], n[1], nt))
|
| 205 |
+
return None, expanded
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def run_episode(world, planner, max_steps=None):
|
| 209 |
+
walls = {tuple(c) for c in world["walls"]}
|
| 210 |
+
doors = world["doors"]
|
| 211 |
+
door_at = {tuple(d["cell"]): d for d in doors}
|
| 212 |
+
loops = [o["loop"] for o in world["obstacles"]]
|
| 213 |
+
idx = [o["offset"] % len(o["loop"]) for o in world["obstacles"]]
|
| 214 |
+
dist = distance_map(walls, world["goal"])
|
| 215 |
+
static_path, nodes = bfs(walls, world["start"], world["goal"])
|
| 216 |
+
max_steps = max_steps or 3 * len(static_path) + 60
|
| 217 |
+
rng = Mulberry32(world["seed"] * 7 + 3)
|
| 218 |
+
pos, t = tuple(world["start"]), 0
|
| 219 |
+
goal = tuple(world["goal"])
|
| 220 |
+
plan, plan_i, replans, collisions, door_hits, hard_waits = list(static_path), 0, 0, 0, 0, 0
|
| 221 |
+
pred = list(idx)
|
| 222 |
+
trace = [{"t": 0, "robot": list(pos), "obs": [loops[i][idx[i]] for i in range(len(loops))]}]
|
| 223 |
+
events = []
|
| 224 |
+
if planner == "timed":
|
| 225 |
+
plan, n = timed_astar(walls, doors, dist, pos, 0, goal, idx, loops)
|
| 226 |
+
nodes += n; replans += 1; plan_i = 0
|
| 227 |
+
arrived = None
|
| 228 |
+
while t < max_steps:
|
| 229 |
+
if pos == goal:
|
| 230 |
+
arrived = t
|
| 231 |
+
break
|
| 232 |
+
obs_now = {tuple(loops[i][idx[i]]) for i in range(len(loops))}
|
| 233 |
+
nxt = pos
|
| 234 |
+
if planner in ("static", "reactive"):
|
| 235 |
+
if plan is not None and plan_i + 1 < len(plan):
|
| 236 |
+
cand = tuple(plan[plan_i + 1])
|
| 237 |
+
d = door_at.get(cand)
|
| 238 |
+
blocked = cand in obs_now or (d is not None and door_closed(d, t))
|
| 239 |
+
if not blocked:
|
| 240 |
+
nxt = cand
|
| 241 |
+
plan_i += 1
|
| 242 |
+
elif planner == "reactive":
|
| 243 |
+
block = set(obs_now) | {tuple(dd["cell"]) for dd in doors if door_closed(dd, t)}
|
| 244 |
+
newp, n = bfs(walls, pos, goal, frozenset(block - {pos}))
|
| 245 |
+
nodes += n; replans += 1
|
| 246 |
+
if newp is not None and len(newp) > 1:
|
| 247 |
+
plan, plan_i = newp, 1
|
| 248 |
+
nxt = tuple(newp[1])
|
| 249 |
+
else:
|
| 250 |
+
hard_waits += 1
|
| 251 |
+
else:
|
| 252 |
+
hard_waits += 1
|
| 253 |
+
else:
|
| 254 |
+
deviated = any(idx[i] != pred[i] for i in range(len(loops)))
|
| 255 |
+
invalid = plan is None or plan_i + 1 >= len(plan)
|
| 256 |
+
if deviated or invalid:
|
| 257 |
+
plan, n = timed_astar(walls, doors, dist, pos, t, goal, idx, loops)
|
| 258 |
+
nodes += n; replans += 1; plan_i = 0
|
| 259 |
+
pred = list(idx)
|
| 260 |
+
if plan is not None and plan_i + 1 < len(plan):
|
| 261 |
+
nxt = tuple(plan[plan_i + 1]); plan_i += 1
|
| 262 |
+
else:
|
| 263 |
+
hard_waits += 1
|
| 264 |
+
# obstacles move (stall with probability p_stall), then robot moves
|
| 265 |
+
old = [tuple(loops[i][idx[i]]) for i in range(len(loops))]
|
| 266 |
+
for i in range(len(loops)):
|
| 267 |
+
if rng.random() >= world["p_stall"]:
|
| 268 |
+
idx[i] = (idx[i] + 1) % len(loops[i])
|
| 269 |
+
pred[i] = (pred[i] + 1) % len(loops[i])
|
| 270 |
+
t += 1
|
| 271 |
+
new = [tuple(loops[i][idx[i]]) for i in range(len(loops))]
|
| 272 |
+
hit = any(new[i] == nxt or (old[i] == nxt and new[i] == pos) for i in range(len(loops)))
|
| 273 |
+
prev_pos, pos = pos, nxt
|
| 274 |
+
if hit:
|
| 275 |
+
collisions += 1
|
| 276 |
+
events.append({"t": t, "type": "collision", "cell": list(pos)})
|
| 277 |
+
d = door_at.get(pos)
|
| 278 |
+
if d is not None and door_closed(d, t):
|
| 279 |
+
door_hits += 1
|
| 280 |
+
events.append({"t": t, "type": "door_closed_on_robot", "cell": list(pos)})
|
| 281 |
+
trace.append({"t": t, "robot": list(pos), "obs": [list(c) for c in new]})
|
| 282 |
+
if arrived is None and pos == goal:
|
| 283 |
+
arrived = t
|
| 284 |
+
return {"arrived": arrived is not None, "arrival_time": arrived, "collisions": collisions, "door_hits": door_hits,
|
| 285 |
+
"replans": replans, "nodes_expanded": nodes, "waits_blocked": hard_waits, "static_length": len(static_path) - 1,
|
| 286 |
+
"max_steps": max_steps, "events": events, "trace": trace}
|
preview.png
ADDED
|
replay.mp4
ADDED
|
Binary file (99.7 kB). View file
|
|
|
selftest.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"cases": [{"input": {"seed": 20001, "density": "high", "p_stall": 0.15, "planner": "timed"}, "expected": {"arrived": true, "arrival_time": 40, "collisions": 0, "door_hits": 0, "replans": 35, "nodes_expanded": 10846}}, {"input": {"seed": 20001, "density": "high", "p_stall": 0.15, "planner": "static"}, "expected": {"arrived": true, "arrival_time": 29, "collisions": 3, "door_hits": 0, "replans": 0, "nodes_expanded": 175}}, {"input": {"seed": 20002, "density": "medium", "p_stall": 0.25, "planner": "reactive"}, "expected": {"arrived": true, "arrival_time": 24, "collisions": 1, "door_hits": 0, "replans": 0, "nodes_expanded": 185}}, {"input": {"seed": 20003, "density": "low", "p_stall": 0.1, "planner": "timed"}, "expected": {"arrived": true, "arrival_time": 30, "collisions": 0, "door_hits": 0, "replans": 11, "nodes_expanded": 618}}, {"input": {"seed": 20004, "density": "high", "p_stall": 0.3, "planner": "timed"}, "expected": {"arrived": true, "arrival_time": 33, "collisions": 0, "door_hits": 0, "replans": 32, "nodes_expanded": 852}}, {"input": {"seed": 31337, "density": "medium", "p_stall": 0.0, "planner": "timed"}, "expected": {"arrived": true, "arrival_time": 32, "collisions": 0, "door_hits": 0, "replans": 1, "nodes_expanded": 254}}]}
|