# dolphindasher/kanto-rl architecture

> Architecture diagram of the GitHub repository dolphindasher/kanto-rl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dolphindasher/kanto-rl
- Repository: https://github.com/dolphindasher/kanto-rl

## Overview

#### 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: [`PokemonRL/env/environment.py`](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/env/environment.py) (Pokemon Gym environment), [`tools/train_ppo_multimodal.py`](https://github.com/dolphindasher/kanto-rl/blob/main/tools/train_ppo_multimodal.py) (PPO multimodal trainer), [`PokemonRL/core/memory.py`](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/memory.py) (RAM access), [`PokemonRL/rewards/factory.py`](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/rewards/factory.py) (Reward pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_env["Emulator Environment"]
  node_env_wrappers["Training wrappers<br/>Gym wrappers<br/>[wrappers.py]"]
  node_gym_env["Pokemon Gym environment<br/>Gymnasium environment<br/>[environment.py]"]
  node_emulator_assets{{"PyBoy, ROM &amp; save state<br/>external runtime"}}
end

subgraph group_semantics["Game State &amp; Rewards"]
  node_game_memory["RAM game state<br/>state model<br/>[game_data.py]"]
  node_memory_access["RAM access<br/>memory adapter<br/>[memory.py]"]
  node_game_constants["Game constants<br/>addresses and IDs<br/>[constants.py]"]
  node_runtime_model["Structured runtime model<br/>multimodal features<br/>[runtime.py]"]
  node_reward_factory["Reward pipeline<br/>reward factory<br/>[factory.py]"]
  node_progress_rewards["Progress rewards<br/>reward implementation<br/>[progress.py]"]
  node_reward_profiles["Reward profiles &amp; flags<br/>objective data"]
end

subgraph group_training["Training &amp; Persistence"]
  node_train_cli["Training CLI<br/>entry point<br/>[train.py]"]
  node_train_ppo["PPO multimodal trainer<br/>training orchestration"]
  node_paths_flags["Paths &amp; runtime flags<br/>runtime configuration<br/>[paths.py]"]
  node_policy_artifacts[("Policies &amp; telemetry<br/>run artifacts")]
end

subgraph group_observability["Monitoring"]
  node_watch_entry["Watch policy<br/>evaluation entry point"]
  node_live_map_logger["Live map logger<br/>artifact exporter<br/>[live_map_logger.py]"]
  node_map_server["Local map dashboard<br/>visualization server<br/>[map_server.py]"]
  node_map_layout["Map layout<br/>dashboard configuration"]
end

node_train_cli -->|"launches"| node_train_ppo
node_train_ppo -->|"creates"| node_env_wrappers
node_env_wrappers -->|"adapts"| node_gym_env
node_gym_env -->|"drives"| node_emulator_assets
node_gym_env -->|"reads RAM through"| node_memory_access
node_memory_access -->|"supplies"| node_game_memory
node_game_constants -->|"defines addresses"| node_memory_access
node_game_memory -->|"produces features"| node_runtime_model
node_runtime_model -->|"forms structured observations"| node_gym_env
node_gym_env -->|"provides current and prior state"| node_reward_factory
node_reward_factory -->|"constructs"| node_progress_rewards
node_reward_profiles -->|"configures"| node_reward_factory
node_train_ppo -->|"uses"| node_paths_flags
node_paths_flags -.->|"locates"| node_emulator_assets
node_train_ppo -->|"writes"| node_policy_artifacts
node_watch_entry -->|"loads"| node_policy_artifacts
node_policy_artifacts -->|"exports run data to"| node_live_map_logger
node_live_map_logger -->|"emits artifacts for"| node_map_server
node_map_layout -->|"configures"| node_map_server

click node_train_cli "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/cli/train.py"
click node_train_ppo "https://github.com/dolphindasher/kanto-rl/blob/main/tools/train_ppo_multimodal.py"
click node_env_wrappers "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/env/wrappers.py"
click node_gym_env "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/env/environment.py"
click node_game_memory "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/game_data.py"
click node_memory_access "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/memory.py"
click node_game_constants "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/constants.py"
click node_runtime_model "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/runtime.py"
click node_reward_factory "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/rewards/factory.py"
click node_progress_rewards "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/rewards/progress.py"
click node_reward_profiles "https://github.com/dolphindasher/kanto-rl/blob/main/config/reward_presets.json"
click node_paths_flags "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/paths.py"
click node_watch_entry "https://github.com/dolphindasher/kanto-rl/blob/main/tools/watch_latest_policy.py"
click node_live_map_logger "https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/visualization/live_map_logger.py"
click node_map_server "https://github.com/dolphindasher/kanto-rl/blob/main/tools/map_server.py"
click node_map_layout "https://github.com/dolphindasher/kanto-rl/blob/main/config/live_map_layout.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_env_wrappers,node_gym_env,node_emulator_assets toneBlue
class node_game_memory,node_memory_access,node_game_constants,node_runtime_model,node_reward_factory,node_progress_rewards,node_reward_profiles toneAmber
class node_train_cli,node_train_ppo,node_paths_flags,node_policy_artifacts toneMint
class node_watch_entry,node_live_map_logger,node_map_server,node_map_layout toneRose
```

## Components

### Emulator Environment

- [Training wrappers](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/env/wrappers.py): Gym wrappers · `PokemonRL/env/wrappers.py`
- [Pokemon Gym environment](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/env/environment.py): Gymnasium environment · Owns emulator lifecycle, actions, observations, and episode results. · `PokemonRL/env/environment.py`
- **PyBoy, ROM & save state**: external runtime · Local ROM and compatible save state are required and unversioned.

### Game State & Rewards

- [RAM game state](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/game_data.py): state model · Derives player, map, inventory, HP, badges, and event flags from RAM. · `PokemonRL/core/game_data.py`
- [RAM access](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/memory.py): memory adapter · `PokemonRL/core/memory.py`
- [Game constants](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/constants.py): addresses and IDs · `PokemonRL/core/constants.py`
- [Structured runtime model](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/runtime.py): multimodal features · Shared RAM-derived representation for training and monitoring. · `PokemonRL/core/runtime.py`
- [Reward pipeline](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/rewards/factory.py): reward factory · Builds configured progress-oriented rewards. · `PokemonRL/rewards/factory.py`
- [Progress rewards](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/rewards/progress.py): reward implementation · Evaluates milestones, events, exploration, and profile parameters. · `PokemonRL/rewards/progress.py`
- [Reward profiles & flags](https://github.com/dolphindasher/kanto-rl/blob/main/config/reward_presets.json): objective data · External JSON presets, profiles, and event-flag definitions. · `config/reward_presets.json`

### Training & Persistence

- [Training CLI](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/cli/train.py): entry point · `PokemonRL/cli/train.py`
- [PPO multimodal trainer](https://github.com/dolphindasher/kanto-rl/blob/main/tools/train_ppo_multimodal.py): training orchestration · Creates environments, selects profiles, and trains PPO. · `tools/train_ppo_multimodal.py`
- [Paths & runtime flags](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/core/paths.py): runtime configuration · Resolves asset, model, log, and artifact locations. · `PokemonRL/core/paths.py`
- **Policies & telemetry**: run artifacts · Saved policies, checkpoints, logs, and exported run data.

### Monitoring

- [Watch policy](https://github.com/dolphindasher/kanto-rl/blob/main/tools/watch_latest_policy.py): evaluation entry point · `tools/watch_latest_policy.py`
- [Live map logger](https://github.com/dolphindasher/kanto-rl/blob/main/PokemonRL/visualization/live_map_logger.py): artifact exporter · Emits map and progress artifacts from runtime data. · `PokemonRL/visualization/live_map_logger.py`
- [Local map dashboard](https://github.com/dolphindasher/kanto-rl/blob/main/tools/map_server.py): visualization server · `tools/map_server.py`
- [Map layout](https://github.com/dolphindasher/kanto-rl/blob/main/config/live_map_layout.json): dashboard configuration · `config/live_map_layout.json`

## Connections

- 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
