# pwhiddy/pokemonredexperiments architecture

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

- Interactive diagram: https://gitdiagram.com/pwhiddy/pokemonredexperiments
- Repository: https://github.com/pwhiddy/pokemonredexperiments
- Video tour (about a minute): https://gitdiagram.com/pwhiddy/pokemonredexperiments/video

## Overview

This repository trains reinforcement-learning agents to play Pokémon Red. The principal workflow is: PPO/Ray selects actions, the Gym environment applies them through PyBoy, extracts screens and game-state features, computes exploration/progress rewards, and returns observations for learning. Training can emit checkpoints and TensorBoard metrics, while coordinate streams feed a shared map. Separate recording and visualization tools replay sessions or render movement. Evidence is sampled from the V2 environment, trainers, recorder, and visualization scripts; some documented integrations are represented by their exact tree paths without unsampled call details.

Main entry points: [`v2/red_gym_env_v2.py`](https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/red_gym_env_v2.py) (Red Gym V2), [`v2/baseline_fast_v2.py`](https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/baseline_fast_v2.py) (PPO Learner), [`baselines/ray_exp/red_gym_env_ray.py`](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/ray_exp/red_gym_env_ray.py) (Ray Gym Env), [`baselines/run_pretrained_interactive.py`](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/run_pretrained_interactive.py) (Interactive Runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["RL Training"]
  node_ppo["PPO Learner"]
  node_raytrainer["Ray PPO Trainer<br/>[train_ray.py]"]
  node_metrics["Training Metrics"]
end

subgraph group_environment["Game Environment"]
  node_v2env["Red Gym V2<br/>[red_gym_env_v2.py]"]
  node_legacyenv["Legacy Gym Env<br/>[red_gym_env.py]"]
  node_rayenv["Ray Gym Env<br/>[red_gym_env_ray.py]"]
  node_minimalenv["Minimal Gym Env"]
  node_interactive["Interactive Runner"]
end

subgraph group_broadcast["Live Broadcast"]
  node_streamwrapper["Stream Wrapper"]
end

subgraph group_recording["Recording Replay"]
  node_recorder["Session Recorder<br/>[play.py]"]
  node_sessioncsv[("Session CSV")]
  node_replayrenderer["Replay Renderer<br/>[render_replay.py]"]
end

subgraph group_visualization["Visualization"]
  node_coordrenderer["Coordinate Renderer"]
  node_flowvisualizer["Map Flow Visualizer"]
  node_staticviz["Static Map Tools"]
end

node_user(("Researcher"))
node_pyboy["PyBoy Emulator"]
node_romstate["ROM And States"]
node_globalmap["Shared Global Map"]

node_user -->|"starts training"| node_ppo
node_user -->|"plays interactively"| node_interactive
node_ppo -->|"creates environments"| node_v2env
node_ppo -->|"registers callbacks"| node_metrics
node_v2env -->|"ticks emulator"| node_pyboy
node_v2env -->|"returns observations"| node_ppo
node_ppo -->|"sends actions"| node_v2env
node_raytrainer -->|"configures workers"| node_rayenv
node_rayenv -->|"ticks emulator"| node_pyboy
node_rayenv -->|"returns rewards"| node_raytrainer
node_legacyenv -->|"ticks emulator"| node_pyboy
node_minimalenv -->|"ticks emulator"| node_pyboy
node_interactive -->|"drives emulator"| node_pyboy
node_romstate -->|"initializes game"| node_v2env
node_romstate -->|"loads game"| node_interactive
node_v2env -.->|"emits coordinates"| node_streamwrapper
node_streamwrapper -.->|"broadcasts progress"| node_globalmap
node_recorder -->|"records gameplay"| node_pyboy
node_recorder -->|"writes events"| node_sessioncsv
node_sessioncsv -->|"reads sessions"| node_replayrenderer
node_replayrenderer -->|"replays frames"| node_pyboy
node_sessioncsv -->|"reads coordinates"| node_coordrenderer
node_coordrenderer -->|"renders movement"| node_staticviz
node_sessioncsv -->|"loads trajectories"| node_flowvisualizer
node_flowvisualizer -->|"renders map flow"| node_staticviz

click node_ppo "https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/baseline_fast_v2.py"
click node_raytrainer "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/ray_exp/train_ray.py"
click node_metrics "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/tensorboard_callback.py"
click node_v2env "https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/red_gym_env_v2.py"
click node_legacyenv "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/red_gym_env.py"
click node_rayenv "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/ray_exp/red_gym_env_ray.py"
click node_minimalenv "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/red_gym_env_minimal.py"
click node_streamwrapper "https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/stream_agent_wrapper.py"
click node_recorder "https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/play.py"
click node_replayrenderer "https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/render_replay.py"
click node_coordrenderer "https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/render_coordinates.py"
click node_flowvisualizer "https://github.com/pwhiddy/pokemonredexperiments/blob/master/visualization/BetterMapVis_script_version_FLOW_edge.py"
click node_staticviz "https://github.com/pwhiddy/pokemonredexperiments/tree/master/visualization"
click node_interactive "https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/run_pretrained_interactive.py"

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_ppo,node_raytrainer,node_metrics toneBlue
class node_v2env,node_legacyenv,node_rayenv,node_minimalenv,node_interactive toneAmber
class node_streamwrapper toneMint
class node_recorder,node_sessioncsv,node_replayrenderer toneRose
class node_coordrenderer,node_flowvisualizer,node_staticviz,node_user,node_pyboy,node_romstate,node_globalmap toneIndigo
```

## Components

### RL Training

- [PPO Learner](https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/baseline_fast_v2.py): `v2/baseline_fast_v2.py`
- [Ray PPO Trainer](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/ray_exp/train_ray.py): `baselines/ray_exp/train_ray.py`
- [Training Metrics](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/tensorboard_callback.py): `baselines/tensorboard_callback.py`

### Game Environment

- [Red Gym V2](https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/red_gym_env_v2.py): `v2/red_gym_env_v2.py`
- [Legacy Gym Env](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/red_gym_env.py): `baselines/red_gym_env.py`
- [Ray Gym Env](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/ray_exp/red_gym_env_ray.py): `baselines/ray_exp/red_gym_env_ray.py`
- [Minimal Gym Env](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/red_gym_env_minimal.py): `baselines/red_gym_env_minimal.py`
- [Interactive Runner](https://github.com/pwhiddy/pokemonredexperiments/blob/master/baselines/run_pretrained_interactive.py): `baselines/run_pretrained_interactive.py`

### Live Broadcast

- [Stream Wrapper](https://github.com/pwhiddy/pokemonredexperiments/blob/master/v2/stream_agent_wrapper.py): `v2/stream_agent_wrapper.py`

### Recording Replay

- [Session Recorder](https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/play.py): `jippity-6-test/play.py`
- **Session CSV**
- [Replay Renderer](https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/render_replay.py): `jippity-6-test/render_replay.py`

### Visualization

- [Coordinate Renderer](https://github.com/pwhiddy/pokemonredexperiments/blob/master/jippity-6-test/render_coordinates.py): `jippity-6-test/render_coordinates.py`
- [Map Flow Visualizer](https://github.com/pwhiddy/pokemonredexperiments/blob/master/visualization/BetterMapVis_script_version_FLOW_edge.py): `visualization/BetterMapVis_script_version_FLOW_edge.py`
- [Static Map Tools](https://github.com/pwhiddy/pokemonredexperiments/tree/master/visualization): `visualization`

### Other components

- **Researcher**
- **PyBoy Emulator**
- **ROM And States**
- **Shared Global Map**

## Connections

- Researcher → PPO Learner: starts training
- Researcher → Interactive Runner: plays interactively
- PPO Learner → Red Gym V2: creates environments
- PPO Learner → Training Metrics: registers callbacks
- Red Gym V2 → PyBoy Emulator: ticks emulator
- Red Gym V2 → PPO Learner: returns observations
- PPO Learner → Red Gym V2: sends actions
- Ray PPO Trainer → Ray Gym Env: configures workers
- Ray Gym Env → PyBoy Emulator: ticks emulator
- Ray Gym Env → Ray PPO Trainer: returns rewards
- Legacy Gym Env → PyBoy Emulator: ticks emulator
- Minimal Gym Env → PyBoy Emulator: ticks emulator
- Interactive Runner → PyBoy Emulator: drives emulator
- ROM And States → Red Gym V2: initializes game
- ROM And States → Interactive Runner: loads game
- Red Gym V2 → Stream Wrapper: emits coordinates
- Stream Wrapper → Shared Global Map: broadcasts progress
- Session Recorder → PyBoy Emulator: records gameplay
- Session Recorder → Session CSV: writes events
- Session CSV → Replay Renderer: reads sessions
- Replay Renderer → PyBoy Emulator: replays frames
- Session CSV → Coordinate Renderer: reads coordinates
- Coordinate Renderer → Static Map Tools: renders movement
- Session CSV → Map Flow Visualizer: loads trajectories
- Map Flow Visualizer → Static Map Tools: renders map flow
