How dolphindasher/kanto-rl works
Purpose and Entry Points
PokemonRL trains and evaluates PPO agents for Pokémon Red using a Game Boy emulator, verified RAM state, Gymnasium, and Stable Baselines3. The user-facing package commands are PokemonRL/cli/train.py and PokemonRL/cli/watch.py; the main operational scripts are tools/train_ppo_multimodal.py, tools/watch_latest_policy.py, and tools/map_server.py.
Emulator-Backed RL Environment
PokemonRL/env/environment.py is the core Gymnasium environment boundary. It owns emulator interaction, reset/step lifecycle, action execution, observation collection, and episode state. It converts raw emulator/RAM state into agent-facing observations and Gym-compatible rewards, termination signals, and info. PokemonRL/env/wrappers.py adapts or augments that environment for training use.
The environment depends on a locally supplied Pokémon Red ROM and PyBoy save state; neither is versioned in the repository.
RAM State and Game Semantics
PokemonRL/core/game_data.py and PokemonRL/core/memory.py define the RAM-driven view of gameplay: map/location, player state, inventory, HP, badges, and event flags. PokemonRL/core/constants.py centralizes game-specific identifiers and addresses. This is the key correctness boundary: agents and reward logic should derive progress from observed game memory rather than visual inference alone.
tools/verify_ram.py and tools/verify_rewards.py support validation of this emulator/RAM contract.
Observation and Runtime Layer
PokemonRL/core/models.py and PokemonRL/core/runtime.py support the structured runtime representation used across environment, training, and monitoring. Training is multimodal: screen pixels are combined with structured RAM-derived features. PokemonRL/core/paths.py resolves project assets and output locations, including environment-variable overrides for ROMs, saves, models, logs, and run artifacts.
Configurable Reward Pipeline
Rewards are isolated behind PokemonRL/rewards/base.py, with construction in PokemonRL/rewards/factory.py and progress-oriented implementation in PokemonRL/rewards/progress.py. The environment supplies current and prior game state; the reward system evaluates configured milestones, events, exploration/progress signals, and profile parameters.
Profiles and objective data are externalized in config/reward_presets.json, config/brock_badge1_profile.json, config/full_game_reward_profile.json, and config/event_flags.json. This lets researchers alter task objectives without changing emulator or training code.
Training, Policies, and Persistence
tools/train_ppo_multimodal.py is the principal PPO orchestration path, creating one or more environment instances, selecting a reward profile, training through Stable Baselines3, and writing policies/checkpoints and telemetry. PokemonRL/cli/train.py provides package-level training access. PokemonRL/core/flags.py and PokemonRL/core/paths.py govern runtime options and storage boundaries.
Material runtime dependencies are Python 3.10+, PyBoy/Game Boy emulation, Gymnasium, Stable Baselines3 PPO, a valid ROM, and a compatible emulator save state.
Live Monitoring and Visualization
PokemonRL/visualization/live_map_logger.py emits runtime map/progress artifacts, using coordinate conversion from PokemonRL/visualization/coord_transform.py. tools/map_server.py serves those artifacts as a local real-time dashboard, while tools/monitor.py and watching commands consume saved policy/run outputs. Layout is externally configured by config/live_map_layout.json.
The visualization boundary is intentionally downstream of training: it observes exported run data rather than participating in action selection or reward computation.
Main entry points: environment.py Pokemon Gym environment · train_ppo_multimodal.py PPO multimodal trainer · memory.py RAM access · factory.py Reward pipeline
Components 18
Emulator Environment
- Training wrappers
PokemonRL/env/wrappers.py - Pokemon Gym environment · Owns emulator lifecycle, actions, observations, and episode results.
PokemonRL/env/environment.py - PyBoy, ROM & save state · Local ROM and compatible save state are required and unversioned.
Game State & Rewards
- RAM game state · Derives player, map, inventory, HP, badges, and event flags from RAM.
PokemonRL/core/game_data.py - RAM access
PokemonRL/core/memory.py - Game constants
PokemonRL/core/constants.py - Structured runtime model · Shared RAM-derived representation for training and monitoring.
PokemonRL/core/runtime.py - Reward pipeline · Builds configured progress-oriented rewards.
PokemonRL/rewards/factory.py - Progress rewards · Evaluates milestones, events, exploration, and profile parameters.
PokemonRL/rewards/progress.py - Reward profiles & flags · External JSON presets, profiles, and event-flag definitions.
config/reward_presets.json
Training & Persistence
- Training CLI
PokemonRL/cli/train.py - PPO multimodal trainer · Creates environments, selects profiles, and trains PPO.
tools/train_ppo_multimodal.py - Paths & runtime flags · Resolves asset, model, log, and artifact locations.
PokemonRL/core/paths.py - Policies & telemetry · Saved policies, checkpoints, logs, and exported run data.
Monitoring
- Watch policy
tools/watch_latest_policy.py - Live map logger · Emits map and progress artifacts from runtime data.
PokemonRL/visualization/live_map_logger.py - Local map dashboard
tools/map_server.py - Map layout
config/live_map_layout.json
Connections 19
- Training CLI → PPO multimodal trainer: launches
- PPO multimodal trainer → Training wrappers: creates
- Training wrappers → Pokemon Gym environment: adapts
- Pokemon Gym environment → PyBoy, ROM & save state: drives
- Pokemon Gym environment → RAM access: reads RAM through
- RAM access → RAM game state: supplies
- Game constants → RAM access: defines addresses
- RAM game state → Structured runtime model: produces features
- Structured runtime model → Pokemon Gym environment: forms structured observations
- Pokemon Gym environment → Reward pipeline: provides current and prior state
- Reward pipeline → Progress rewards: constructs
- Reward profiles & flags → Reward pipeline: configures
- PPO multimodal trainer → Paths & runtime flags: uses
- Paths & runtime flags → PyBoy, ROM & save state: locates
- PPO multimodal trainer → Policies & telemetry: writes
- Watch policy → Policies & telemetry: loads
- Policies & telemetry → Live map logger: exports run data to
- Live map logger → Local map dashboard: emits artifacts for
- Map layout → Local map dashboard: configures