diff --git a/README.md b/README.md index 76ab55a6..1cf02931 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,34 @@ uv run scripts/observe_cli.py --session Auto-detects `.tapes/tapes.sqlite` from cwd. Override with `--db`. +## AlphaEvolve Strategy Evolution + +Inspired by [AlphaEvolve](https://arxiv.org/abs/2506.13131) (DeepMind), the agent can automatically improve its navigation parameters through headless evaluation runs. Instead of manually tuning thresholds, the evolution harness runs 10 agent variants in parallel, scores them against a composite fitness function, and keeps the winner. + +**How it works.** The agent's navigator has tunable knobs: stuck threshold, door cooldown, waypoint skip distance, axis preference. The harness treats these as a genome. Each generation, it either asks an LLM to propose a mutation (informed by observer diagnostics) or randomly perturbs values. The variant runs headless, and its fitness is compared to the current best. + +```bash +# Run the evolution harness (LLM-free random perturbation by default) +uv run scripts/evolve.py rom/pokemon_red.gb --generations 5 --max-turns 1000 + +# Run 10 parameter variants in parallel and rank them +uv run scripts/run_10_agents.py rom/pokemon_red.gb +``` + +The observer feeds failure context (stuck events, tool errors) into the LLM mutation prompt so variants target actual problems rather than making blind changes. + +**First finding:** `door_cooldown=2` beats the default of 8. Shorter cooldown means fewer wasted turns walking away from doors before retrying. Confirmed across two milestones (Pokemon selection and rival battle) with 10 independent runs each. + +### Long-session mode + +You can still run the agent the traditional way for a single long session, the way [ClaudePlaysPokemon](https://www.twitch.tv/claudeplayspokemon) works on Twitch: + +```bash +uv run scripts/agent.py rom/pokemon_red.gb --strategy heuristic --max-turns 50000 +``` + +The two approaches complement each other. Long sessions are better for discovering new capabilities and debugging game-specific logic. The evolution loop is better for optimizing parameters once the code structure exists. + ## Project Structure ``` @@ -127,7 +155,9 @@ pokemon-agent/ │ ├── memory_reader.py # memory address definitions │ ├── tape_reader.py # Tapes SQLite reader (stdlib only) │ ├── observer.py # heuristic observation extractor -│ └── observe_cli.py # CLI for running the observer +│ ├── observe_cli.py # CLI for running the observer +│ ├── evolve.py # AlphaEvolve strategy evolution harness +│ └── run_10_agents.py # parallel multi-agent evaluation runner ├── references/ │ ├── routes.json # overworld waypoints │ └── type_chart.json # type effectiveness data @@ -157,6 +187,8 @@ Target turn counts for community benchmarking. Fork it, improve the strategy, po ## Inspiration & References +- [AlphaEvolve](https://arxiv.org/abs/2506.13131) — DeepMind's LLM-driven code evolution framework +- [Discovering Multiagent Learning Algorithms with LLMs](https://arxiv.org/abs/2602.16928) — AlphaEvolve applied to game-playing agents - [ClaudePlaysPokemon](https://www.twitch.tv/claudeplayspokemon) — Anthropic's Claude-plays-Pokemon Twitch stream - [Insights into Claude Opus 4.5 from Pokemon](https://www.lesswrong.com/posts/u6Lacc7wx4yYkBQ3r/insights-into-claude-opus-4-5-from-pokemon) — Navigation, memory notes, and spatial reasoning analysis - [ClaudePlaysPokemon Harness Changes](https://docs.google.com/document/u/1/d/e/2PACX-1vRIsu2pLI21W4KjfYbN13or8E-8cvJYw570wGMEp4UQU63ZhEh9FPGgj2ark8Yk7Vyrtt9MWq3jnn4h/pub) — Minimap, navigator, and memory file evolution diff --git a/pokedex/evolve_results.json b/pokedex/evolve_results.json index da248c5a..5019da03 100644 --- a/pokedex/evolve_results.json +++ b/pokedex/evolve_results.json @@ -1,242 +1,242 @@ [ { - "agent_id": 3, - "label": "short_door", + "agent_id": 2, + "label": "dc2", "params": { "stuck_threshold": 8, - "door_cooldown": 4, + "door_cooldown": 2, "waypoint_skip_distance": 3, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, - "final_x": 7, - "final_y": 5, + "final_map_id": 0, + "final_x": 11, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 9 + "stuck_count": 15 }, - "score": 40375.0, - "elapsed": 5.4, + "score": -65.0, + "elapsed": 15.8, "returncode": 0 }, { - "agent_id": 0, - "label": "baseline", + "agent_id": 4, + "label": "high_stuck_dc4", "params": { - "stuck_threshold": 8, - "door_cooldown": 8, + "stuck_threshold": 12, + "door_cooldown": 4, "waypoint_skip_distance": 3, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.5, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { - "agent_id": 2, - "label": "high_stuck", + "agent_id": 3, + "label": "low_stuck_dc4", "params": { - "stuck_threshold": 12, - "door_cooldown": 8, + "stuck_threshold": 4, + "door_cooldown": 4, "waypoint_skip_distance": 3, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.5, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { - "agent_id": 1, - "label": "low_stuck", + "agent_id": 0, + "label": "baseline_4dc", "params": { - "stuck_threshold": 4, - "door_cooldown": 8, + "stuck_threshold": 8, + "door_cooldown": 4, "waypoint_skip_distance": 3, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.6, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { "agent_id": 5, - "label": "wide_skip", + "label": "wide_skip_dc4", "params": { "stuck_threshold": 8, - "door_cooldown": 8, + "door_cooldown": 4, "waypoint_skip_distance": 6, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.4, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { - "agent_id": 6, - "label": "narrow_skip", + "agent_id": 8, + "label": "aggressive", "params": { - "stuck_threshold": 8, - "door_cooldown": 8, - "waypoint_skip_distance": 1, + "stuck_threshold": 3, + "door_cooldown": 2, + "waypoint_skip_distance": 5, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, - "final_x": 7, - "final_y": 5, + "final_map_id": 0, + "final_x": 11, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.3, + "score": -65.0, + "elapsed": 15.8, "returncode": 0 }, { "agent_id": 7, - "label": "x_axis", + "label": "x_axis_dc4", "params": { "stuck_threshold": 8, - "door_cooldown": 8, + "door_cooldown": 4, "waypoint_skip_distance": 3, "axis_preference_map_0": "x" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.4, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { - "agent_id": 8, - "label": "aggressive", + "agent_id": 6, + "label": "narrow_dc4", "params": { - "stuck_threshold": 4, - "door_cooldown": 6, - "waypoint_skip_distance": 5, + "stuck_threshold": 8, + "door_cooldown": 4, + "waypoint_skip_distance": 1, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 11 + "stuck_count": 15 }, - "score": 40365.0, - "elapsed": 5.3, + "score": -65.0, + "elapsed": 17.2, "returncode": 0 }, { - "agent_id": 4, - "label": "long_door", + "agent_id": 1, + "label": "original", "params": { "stuck_threshold": 8, - "door_cooldown": 12, + "door_cooldown": 8, "waypoint_skip_distance": 3, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 16 + "stuck_count": 18 }, - "score": 40340.0, - "elapsed": 5.5, + "score": -80.0, + "elapsed": 17.8, "returncode": 0 }, { "agent_id": 9, - "label": "conservative", + "label": "moderate", "params": { - "stuck_threshold": 14, - "door_cooldown": 12, - "waypoint_skip_distance": 2, + "stuck_threshold": 6, + "door_cooldown": 6, + "waypoint_skip_distance": 4, "axis_preference_map_0": "y" }, "fitness": { - "turns": 800, - "battles_won": 0, + "turns": 5000, + "battles_won": 1, "maps_visited": 4, - "final_map_id": 40, + "final_map_id": 0, "final_x": 7, - "final_y": 5, + "final_y": 12, "badges": 0, "party_size": 1, - "stuck_count": 16 + "stuck_count": 18 }, - "score": 40340.0, - "elapsed": 5.4, + "score": -80.0, + "elapsed": 17.9, "returncode": 0 } ] diff --git a/scripts/agent.py b/scripts/agent.py index 449eda81..7bee8530 100644 --- a/scripts/agent.py +++ b/scripts/agent.py @@ -538,12 +538,31 @@ def choose_overworld_action(self, state: OverworldState) -> str: return "up" return "a" - # In Oak's Lab with a Pokemon: alternate A/down to advance scripted - # sequence while moving south (away from bookshelf/table). + # In Oak's Lab with a Pokemon: navigate to exit and trigger rival. + # After picking a starter, the rival picks his, then challenges when + # the player walks toward the exit. NPCs can block the path south + # from the table area, so go left first, then south. + # The exit door is at roughly (4, 11). if state.map_id == 40 and state.party_count > 0: - if self.turn_count % 3 == 0: - return "down" - return "a" + if not hasattr(self, '_lab_exit_turns'): + self._lab_exit_turns = 0 + self._lab_exit_turns += 1 + + # First 30 turns: heavy A-mash to clear rival pick dialogue + if self._lab_exit_turns <= 30: + if self._lab_exit_turns % 5 == 0: + return "down" + return "a" + + # Navigate: go left to center column, then south to exit door (~y=11) + if state.x > 5: + if self._lab_exit_turns % 3 == 0: + return "a" # talk to NPCs / clear dialogue + return "left" + # Keep walking south — door is at y=11, interleave A for dialogue + if self._lab_exit_turns % 4 == 0: + return "a" # interact with rival when intercepted / clear text + return "down" direction = self.navigator.next_direction( state, diff --git a/scripts/run_10_agents.py b/scripts/run_10_agents.py index 329de82e..6ae80b1f 100644 --- a/scripts/run_10_agents.py +++ b/scripts/run_10_agents.py @@ -20,41 +20,42 @@ SCRIPT_DIR = Path(__file__).parent AGENT_SCRIPT = SCRIPT_DIR / "agent.py" -# 10 parameter variants to try +# 10 parameter variants to try — tuned for reaching rival battle +# Previous winner: door_cooldown=4 beat baseline for Pokemon selection PARAM_VARIANTS = [ - # Baseline - {"stuck_threshold": 8, "door_cooldown": 8, "waypoint_skip_distance": 3, - "axis_preference_map_0": "y", "label": "baseline"}, - # Lower stuck threshold — skip waypoints faster - {"stuck_threshold": 4, "door_cooldown": 8, "waypoint_skip_distance": 3, - "axis_preference_map_0": "y", "label": "low_stuck"}, - # Higher stuck threshold — more patient - {"stuck_threshold": 12, "door_cooldown": 8, "waypoint_skip_distance": 3, - "axis_preference_map_0": "y", "label": "high_stuck"}, - # Short door cooldown + # Baseline (previous winner door_cooldown=4) {"stuck_threshold": 8, "door_cooldown": 4, "waypoint_skip_distance": 3, - "axis_preference_map_0": "y", "label": "short_door"}, - # Long door cooldown - {"stuck_threshold": 8, "door_cooldown": 12, "waypoint_skip_distance": 3, - "axis_preference_map_0": "y", "label": "long_door"}, - # Wide skip distance - {"stuck_threshold": 8, "door_cooldown": 8, "waypoint_skip_distance": 6, - "axis_preference_map_0": "y", "label": "wide_skip"}, - # Narrow skip distance - {"stuck_threshold": 8, "door_cooldown": 8, "waypoint_skip_distance": 1, - "axis_preference_map_0": "y", "label": "narrow_skip"}, - # X-axis preference + "axis_preference_map_0": "y", "label": "baseline_4dc"}, + # Original defaults {"stuck_threshold": 8, "door_cooldown": 8, "waypoint_skip_distance": 3, - "axis_preference_map_0": "x", "label": "x_axis"}, - # Aggressive: low stuck + wide skip - {"stuck_threshold": 4, "door_cooldown": 6, "waypoint_skip_distance": 5, + "axis_preference_map_0": "y", "label": "original"}, + # Very short door cooldown + {"stuck_threshold": 8, "door_cooldown": 2, "waypoint_skip_distance": 3, + "axis_preference_map_0": "y", "label": "dc2"}, + # Low stuck + short door + {"stuck_threshold": 4, "door_cooldown": 4, "waypoint_skip_distance": 3, + "axis_preference_map_0": "y", "label": "low_stuck_dc4"}, + # High stuck + short door + {"stuck_threshold": 12, "door_cooldown": 4, "waypoint_skip_distance": 3, + "axis_preference_map_0": "y", "label": "high_stuck_dc4"}, + # Wide skip + short door + {"stuck_threshold": 8, "door_cooldown": 4, "waypoint_skip_distance": 6, + "axis_preference_map_0": "y", "label": "wide_skip_dc4"}, + # Narrow skip + short door + {"stuck_threshold": 8, "door_cooldown": 4, "waypoint_skip_distance": 1, + "axis_preference_map_0": "y", "label": "narrow_dc4"}, + # X-axis + short door + {"stuck_threshold": 8, "door_cooldown": 4, "waypoint_skip_distance": 3, + "axis_preference_map_0": "x", "label": "x_axis_dc4"}, + # Aggressive: low stuck + very short door + wide skip + {"stuck_threshold": 3, "door_cooldown": 2, "waypoint_skip_distance": 5, "axis_preference_map_0": "y", "label": "aggressive"}, - # Conservative: high stuck + narrow skip + long door - {"stuck_threshold": 14, "door_cooldown": 12, "waypoint_skip_distance": 2, - "axis_preference_map_0": "y", "label": "conservative"}, + # Moderate: medium stuck + short door + {"stuck_threshold": 6, "door_cooldown": 6, "waypoint_skip_distance": 4, + "axis_preference_map_0": "y", "label": "moderate"}, ] -MAX_TURNS = 800 # Enough to get through intro + navigate to Pokemon selection +MAX_TURNS = 5000 # Intro + Pokemon selection + rival scripted sequence + battle + exit def score(fitness: dict) -> float: diff --git a/tests/test_agent.py b/tests/test_agent.py index 939513b0..bdefd0b4 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -751,12 +751,12 @@ def test_oaks_lab_no_party_returns_a(self, tmp_path): # Phase 0 of the lab strategy: dismiss text (b) or move south (down) assert result in ("a", "b", "down", "right", "up") - def test_oaks_lab_with_party_uses_navigator(self, tmp_path): + def test_oaks_lab_with_party_uses_lab_exit(self, tmp_path): ag = _make_agent(tmp_path) state = OverworldState(map_id=40, party_count=1, x=5, y=5) result = ag.choose_overworld_action(state) - # map 40 not in EARLY_GAME_TARGETS and not in routes -> cycles directions - assert result in ("down", "right", "left", "up") + # Lab exit navigation: at x=5 (center), should go down or A + assert result in ("down", "a") def test_navigator_returns_none_falls_back_to_a(self, tmp_path): ag = _make_agent(tmp_path) @@ -1694,18 +1694,50 @@ def test_lab_phase2_odd_turn_returns_a(self, tmp_path): result = ag.choose_overworld_action(state) assert result == "a" - def test_lab_with_pokemon_turn_div3_returns_down(self, tmp_path): - """Line 519-520: map 40, party>0, turn_count % 3 == 0 -> 'down'.""" + def test_lab_exit_early_mash_a(self, tmp_path): + """First 30 turns in lab with party: mostly A-mash.""" ag = _make_agent(tmp_path) - ag.turn_count = 9 # 9 % 3 == 0 + state = OverworldState(map_id=40, party_count=1, x=7, y=5) + # First call -> _lab_exit_turns = 1, not % 5 == 0 -> "a" + result = ag.choose_overworld_action(state) + assert result == "a" + + def test_lab_exit_early_occasional_down(self, tmp_path): + """Early lab exit: every 5th turn returns 'down'.""" + ag = _make_agent(tmp_path) + ag._lab_exit_turns = 4 # will be 5, 5 % 5 == 0 + state = OverworldState(map_id=40, party_count=1, x=7, y=5) + result = ag.choose_overworld_action(state) + assert result == "down" + + def test_lab_exit_go_left_when_east(self, tmp_path): + """After 30 turns, x>5 -> go left.""" + ag = _make_agent(tmp_path) + ag._lab_exit_turns = 30 # will be 31, not % 3 == 0 + state = OverworldState(map_id=40, party_count=1, x=7, y=5) + result = ag.choose_overworld_action(state) + assert result == "left" + + def test_lab_exit_go_left_interleave_a(self, tmp_path): + """After 30 turns, x>5, every 3rd turn -> A.""" + ag = _make_agent(tmp_path) + ag._lab_exit_turns = 32 # will be 33, 33 % 3 == 0 + state = OverworldState(map_id=40, party_count=1, x=7, y=5) + result = ag.choose_overworld_action(state) + assert result == "a" + + def test_lab_exit_go_south_at_center(self, tmp_path): + """At x<=5, go south toward exit.""" + ag = _make_agent(tmp_path) + ag._lab_exit_turns = 30 # will be 31, not % 4 == 0 state = OverworldState(map_id=40, party_count=1, x=5, y=5) result = ag.choose_overworld_action(state) assert result == "down" - def test_lab_with_pokemon_turn_not_div3_returns_a(self, tmp_path): - """Line 521: map 40, party>0, turn_count % 3 != 0 -> 'a'.""" + def test_lab_exit_south_interleave_a(self, tmp_path): + """At x<=5, every 4th turn -> A.""" ag = _make_agent(tmp_path) - ag.turn_count = 10 # 10 % 3 == 1 + ag._lab_exit_turns = 31 # will be 32, 32 % 4 == 0 state = OverworldState(map_id=40, party_count=1, x=5, y=5) result = ag.choose_overworld_action(state) assert result == "a"