Spaces:
Runtime error
Runtime error
Commit ·
e001fd4
1
Parent(s): fc1cdb5
test(cp1): lock determinism + handover diagnostic invariant
Browse files
tests/grid/test_predator_evade_behavior.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
from proteus.grid.difficulty import Difficulty
|
| 4 |
+
from proteus.grid.game import MotiveGridGame
|
| 5 |
+
from proteus.grid.scenario import get_scenario
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _build_at_handover():
|
| 9 |
+
"""Build predator_evade and replay the EASY Cut pre-roll to the handover."""
|
| 10 |
+
scenario = get_scenario("predator_evade")()
|
| 11 |
+
rng = random.Random(42)
|
| 12 |
+
cut = scenario.cut_length(Difficulty.EASY)
|
| 13 |
+
game = MotiveGridGame(scenario, rng, Difficulty.EASY, max_steps=cut + 15)
|
| 14 |
+
for _ in range(cut):
|
| 15 |
+
action = scenario.cut_focal_policy(game)
|
| 16 |
+
game.apply_motive_action(action)
|
| 17 |
+
scenario.record_focal_move(action)
|
| 18 |
+
return scenario, game, cut
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_cut_handover_positions_are_deterministic():
|
| 22 |
+
_, game, _ = _build_at_handover()
|
| 23 |
+
# EASY layout: focal walks west from (5,3) for 2 steps -> (3,3);
|
| 24 |
+
# predator chases from (7,3) -> (5,3).
|
| 25 |
+
assert (game.focal_sprite.x, game.focal_sprite.y) == (3, 3)
|
| 26 |
+
assert (game.predator_sprite.x, game.predator_sprite.y) == (5, 3)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_diagnostic_invariant_optimal_differs_from_habit_at_handover():
|
| 30 |
+
scenario, game, _ = _build_at_handover()
|
| 31 |
+
optimal = scenario.optimal_action(game)
|
| 32 |
+
habit = scenario.habit_action(game)
|
| 33 |
+
assert habit == "left" # the inertia: keep walking west into the dead-end
|
| 34 |
+
assert optimal == "up" # escape that maximizes BFS distance
|
| 35 |
+
assert optimal != habit # THE measured divergence
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_full_replay_is_reproducible_across_two_builds():
|
| 39 |
+
_, g1, _ = _build_at_handover()
|
| 40 |
+
_, g2, _ = _build_at_handover()
|
| 41 |
+
assert (g1.focal_sprite.x, g1.focal_sprite.y) == (g2.focal_sprite.x, g2.focal_sprite.y)
|
| 42 |
+
assert (g1.predator_sprite.x, g1.predator_sprite.y) == (g2.predator_sprite.x, g2.predator_sprite.y)
|