# oxwhirl/pymarl architecture

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

- Interactive diagram: https://gitdiagram.com/oxwhirl/pymarl
- Repository: https://github.com/oxwhirl/pymarl

## Overview

#### Purpose and entry points
PyMARL is a PyTorch framework for cooperative deep multi-agent reinforcement learning, primarily targeting SMAC/StarCraft II. The experiment CLI is `src/main.py`, which resolves algorithm and environment presets before dispatching training through `src/run.py`. Containerized execution is supported by `docker/Dockerfile`, `docker/build.sh`, and `run.sh`.

#### Configuration-driven experiment assembly
Algorithm presets select QMIX, VDN, IQL, COMA, or QTRAN behavior; environment presets configure SC2/SMAC. Defaults and overrides are composed at launch, including CLI `with` parameters.
- `src/config/default.yaml`
- `src/config/algs/qmix.yaml`
- `src/config/envs/sc2.yaml`

#### Environment boundary
The framework depends on SMAC as the concrete multi-agent StarCraft II environment; `MultiAgentEnv` defines the internal environment contract runners consume. SC2 installation and maps are provisioned externally, including version-sensitive SC2 binaries.
- `src/envs/multiagentenv.py`
- `src/envs/__init__.py`
- `install_sc2.sh`

#### Execution and rollout stages
Runners own environment interaction, episode lifecycle, evaluation/test-mode rollouts, and collection of timestep-aligned experience. `EpisodeRunner` supports single-environment episodes and replay-oriented execution; `ParallelRunner` scales rollout collection across environments.
- `src/runners/episode_runner.py`
- `src/runners/parallel_runner.py`

#### Controller and action boundary
The basic multi-agent controller converts batched observations, agent state, and action availability into per-agent action values/policies, maintaining recurrent agent hidden state. Action selectors apply exploration or policy sampling separately from network inference.
- `src/controllers/basic_controller.py`
- `src/components/action_selectors.py`
- `src/modules/agents/rnn_agent.py`

#### Experience representation
Rollouts are stored as episode batches, with preprocessing transforms and fields suitable for recurrent sequence learning: observations, actions, rewards, termination, availability masks, and agent state. This is the handoff boundary between runners/controllers and learners.
- `src/components/episode_buffer.py`
- `src/components/transforms.py`

#### Learning algorithms and value factorisation
Learners implement algorithm-specific optimization and target updates. Q-learning variants use per-agent Q outputs and optional centralized mixers: VDN sums values, QMIX learns a monotonic mixing function, and QTRAN applies transformation constraints. COMA instead uses a centralized critic and counterfactual policy-gradient learning.
- `src/learners/q_learner.py`
- `src/learners/coma_learner.py`
- `src/learners/qtran_learner.py`
- `src/modules/mixers/qmix.py`
- `src/modules/critics/coma.py`

#### Runtime outputs and operations
Python/PyTorch is the training runtime; SMAC and StarCraft II are external simulation dependencies. Training metrics, model checkpoints, and optional SC2 replay generation flow through the run/logger layer into per-run result directories. Docker provides the reproducible GPU-oriented deployment path.
- `src/utils/logging.py`
- `docker/Dockerfile`
- `run_interactive.sh`

Main entry points: [`src/run.py`](https://github.com/oxwhirl/pymarl/blob/master/src/run.py) (Run dispatcher), [`src/components/episode_buffer.py`](https://github.com/oxwhirl/pymarl/blob/master/src/components/episode_buffer.py) (Episode batch), [`src/main.py`](https://github.com/oxwhirl/pymarl/blob/master/src/main.py) (Experiment CLI), [`src/runners/episode_runner.py`](https://github.com/oxwhirl/pymarl/blob/master/src/runners/episode_runner.py) (Episode runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_launch["Experiment launch"]
  node_cli["Experiment CLI<br/>Python entry point<br/>[main.py]"]
  node_run["Run dispatcher<br/>training orchestration<br/>[run.py]"]
  node_config["Preset configuration<br/>YAML presets<br/>[default.yaml]"]
  node_smac_config["SC2 environment preset<br/>YAML preset<br/>[sc2.yaml]"]
  node_qmix_config["QMIX algorithm preset<br/>YAML preset<br/>[qmix.yaml]"]
end

subgraph group_interaction["Interaction and experience"]
  node_env_contract["Multi-agent environment contract<br/>environment interface<br/>[multiagentenv.py]"]
  node_smac{{"SMAC / StarCraft II<br/>external simulator<br/>[install_sc2.sh]"}}
  node_episode_runner["Episode runner<br/>single-env rollout<br/>[episode_runner.py]"]
  node_parallel_runner["Parallel runner<br/>multi-env rollout<br/>[parallel_runner.py]"]
  node_episode_batch[("Episode batch<br/>sequence experience<br/>[episode_buffer.py]")]
  node_transforms["Experience transforms<br/>preprocessing<br/>[transforms.py]"]
end

subgraph group_learning["Control and learning"]
  node_controller["Basic MAC<br/>multi-agent controller"]
  node_rnn_agent["RNN agent<br/>per-agent network<br/>[rnn_agent.py]"]
  node_action_selector["Action selector<br/>exploration policy"]
  node_q_learner["Q learner<br/>value-factorisation learner<br/>[q_learner.py]"]
  node_qmix["QMIX mixer<br/>monotonic value mixer<br/>[qmix.py]"]
  node_coma_learner["COMA learner<br/>policy-gradient learner<br/>[coma_learner.py]"]
  node_coma_critic["COMA critic<br/>centralized critic<br/>[coma.py]"]
  node_qtran_learner["QTRAN learner<br/>constraint learner<br/>[qtran_learner.py]"]
end

subgraph group_operations["Runtime and operations"]
  node_logging[("Run logger<br/>metrics and checkpoints<br/>[logging.py]")]
  node_docker["Docker runtime<br/>container deployment"]
  node_interactive["Interactive launcher<br/>container launcher<br/>[run_interactive.sh]"]
end

node_cli -->|"loads defaults"| node_config
node_cli -->|"selects algorithm"| node_qmix_config
node_cli -->|"selects environment"| node_smac_config
node_cli -->|"dispatches"| node_run
node_run -->|"runs rollouts"| node_episode_runner
node_run -->|"runs parallel rollouts"| node_parallel_runner
node_run -.->|"selects learner"| node_q_learner
node_run -.->|"selects learner"| node_coma_learner
node_run -.->|"selects learner"| node_qtran_learner
node_episode_runner -->|"interacts through"| node_env_contract
node_parallel_runner -->|"interacts through"| node_env_contract
node_env_contract -->|"implemented by"| node_smac
node_episode_runner -->|"requests actions"| node_controller
node_parallel_runner -->|"requests actions"| node_controller
node_controller -->|"infers with"| node_rnn_agent
node_controller -->|"selects actions via"| node_action_selector
node_episode_runner -->|"collects"| node_episode_batch
node_parallel_runner -->|"collects"| node_episode_batch
node_transforms -->|"preprocesses"| node_episode_batch
node_episode_batch -->|"trains"| node_q_learner
node_episode_batch -->|"trains"| node_coma_learner
node_episode_batch -->|"trains"| node_qtran_learner
node_q_learner -.->|"optionally mixes values"| node_qmix
node_coma_learner -->|"optimizes with"| node_coma_critic
node_run -->|"records outputs"| node_logging
node_docker -.->|"containerizes execution"| node_cli
node_interactive -->|"launches"| node_docker

click node_cli "https://github.com/oxwhirl/pymarl/blob/master/src/main.py"
click node_run "https://github.com/oxwhirl/pymarl/blob/master/src/run.py"
click node_config "https://github.com/oxwhirl/pymarl/blob/master/src/config/default.yaml"
click node_smac_config "https://github.com/oxwhirl/pymarl/blob/master/src/config/envs/sc2.yaml"
click node_qmix_config "https://github.com/oxwhirl/pymarl/blob/master/src/config/algs/qmix.yaml"
click node_env_contract "https://github.com/oxwhirl/pymarl/blob/master/src/envs/multiagentenv.py"
click node_smac "https://github.com/oxwhirl/pymarl/blob/master/install_sc2.sh"
click node_episode_runner "https://github.com/oxwhirl/pymarl/blob/master/src/runners/episode_runner.py"
click node_parallel_runner "https://github.com/oxwhirl/pymarl/blob/master/src/runners/parallel_runner.py"
click node_episode_batch "https://github.com/oxwhirl/pymarl/blob/master/src/components/episode_buffer.py"
click node_transforms "https://github.com/oxwhirl/pymarl/blob/master/src/components/transforms.py"
click node_controller "https://github.com/oxwhirl/pymarl/blob/master/src/controllers/basic_controller.py"
click node_rnn_agent "https://github.com/oxwhirl/pymarl/blob/master/src/modules/agents/rnn_agent.py"
click node_action_selector "https://github.com/oxwhirl/pymarl/blob/master/src/components/action_selectors.py"
click node_q_learner "https://github.com/oxwhirl/pymarl/blob/master/src/learners/q_learner.py"
click node_qmix "https://github.com/oxwhirl/pymarl/blob/master/src/modules/mixers/qmix.py"
click node_coma_learner "https://github.com/oxwhirl/pymarl/blob/master/src/learners/coma_learner.py"
click node_coma_critic "https://github.com/oxwhirl/pymarl/blob/master/src/modules/critics/coma.py"
click node_qtran_learner "https://github.com/oxwhirl/pymarl/blob/master/src/learners/qtran_learner.py"
click node_logging "https://github.com/oxwhirl/pymarl/blob/master/src/utils/logging.py"
click node_docker "https://github.com/oxwhirl/pymarl/blob/master/docker/Dockerfile"
click node_interactive "https://github.com/oxwhirl/pymarl/blob/master/run_interactive.sh"

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_cli,node_run,node_config,node_smac_config,node_qmix_config toneBlue
class node_env_contract,node_smac,node_episode_runner,node_parallel_runner,node_episode_batch,node_transforms toneAmber
class node_controller,node_rnn_agent,node_action_selector,node_q_learner,node_qmix,node_coma_learner,node_coma_critic,node_qtran_learner toneMint
class node_logging,node_docker,node_interactive toneRose
```

## Components

### Experiment launch

- [Experiment CLI](https://github.com/oxwhirl/pymarl/blob/master/src/main.py): Python entry point · Resolves presets and CLI overrides. · `src/main.py`
- [Run dispatcher](https://github.com/oxwhirl/pymarl/blob/master/src/run.py): training orchestration · Assembles experiments and drives training. · `src/run.py`
- [Preset configuration](https://github.com/oxwhirl/pymarl/blob/master/src/config/default.yaml): YAML presets · Default, QMIX, and SC2 settings compose each run. · `src/config/default.yaml`
- [SC2 environment preset](https://github.com/oxwhirl/pymarl/blob/master/src/config/envs/sc2.yaml): YAML preset · `src/config/envs/sc2.yaml`
- [QMIX algorithm preset](https://github.com/oxwhirl/pymarl/blob/master/src/config/algs/qmix.yaml): YAML preset · `src/config/algs/qmix.yaml`

### Interaction and experience

- [Multi-agent environment contract](https://github.com/oxwhirl/pymarl/blob/master/src/envs/multiagentenv.py): environment interface · Internal contract consumed by runners. · `src/envs/multiagentenv.py`
- [SMAC / StarCraft II](https://github.com/oxwhirl/pymarl/blob/master/install_sc2.sh): external simulator · Concrete multi-agent simulation dependency. · `install_sc2.sh`
- [Episode runner](https://github.com/oxwhirl/pymarl/blob/master/src/runners/episode_runner.py): single-env rollout · Manages episode lifecycle, evaluation, and collection. · `src/runners/episode_runner.py`
- [Parallel runner](https://github.com/oxwhirl/pymarl/blob/master/src/runners/parallel_runner.py): multi-env rollout · Scales rollout collection across environments. · `src/runners/parallel_runner.py`
- [Episode batch](https://github.com/oxwhirl/pymarl/blob/master/src/components/episode_buffer.py): sequence experience · Timestep-aligned recurrent learning data. · `src/components/episode_buffer.py`
- [Experience transforms](https://github.com/oxwhirl/pymarl/blob/master/src/components/transforms.py): preprocessing · `src/components/transforms.py`

### Control and learning

- [Basic MAC](https://github.com/oxwhirl/pymarl/blob/master/src/controllers/basic_controller.py): multi-agent controller · Maintains recurrent state and produces agent outputs. · `src/controllers/basic_controller.py`
- [RNN agent](https://github.com/oxwhirl/pymarl/blob/master/src/modules/agents/rnn_agent.py): per-agent network · `src/modules/agents/rnn_agent.py`
- [Action selector](https://github.com/oxwhirl/pymarl/blob/master/src/components/action_selectors.py): exploration policy · Separates sampling and exploration from inference. · `src/components/action_selectors.py`
- [Q learner](https://github.com/oxwhirl/pymarl/blob/master/src/learners/q_learner.py): value-factorisation learner · Optimizes IQL, VDN, or QMIX-style Q learning. · `src/learners/q_learner.py`
- [QMIX mixer](https://github.com/oxwhirl/pymarl/blob/master/src/modules/mixers/qmix.py): monotonic value mixer · `src/modules/mixers/qmix.py`
- [COMA learner](https://github.com/oxwhirl/pymarl/blob/master/src/learners/coma_learner.py): policy-gradient learner · Uses a centralized critic and counterfactual updates. · `src/learners/coma_learner.py`
- [COMA critic](https://github.com/oxwhirl/pymarl/blob/master/src/modules/critics/coma.py): centralized critic · `src/modules/critics/coma.py`
- [QTRAN learner](https://github.com/oxwhirl/pymarl/blob/master/src/learners/qtran_learner.py): constraint learner · `src/learners/qtran_learner.py`

### Runtime and operations

- [Run logger](https://github.com/oxwhirl/pymarl/blob/master/src/utils/logging.py): metrics and checkpoints · Writes metrics, models, and optional replays per run. · `src/utils/logging.py`
- [Docker runtime](https://github.com/oxwhirl/pymarl/blob/master/docker/Dockerfile): container deployment · Reproducible GPU-oriented execution path. · `docker/Dockerfile`
- [Interactive launcher](https://github.com/oxwhirl/pymarl/blob/master/run_interactive.sh): container launcher · `run_interactive.sh`

## Connections

- Experiment CLI → Preset configuration: loads defaults
- Experiment CLI → QMIX algorithm preset: selects algorithm
- Experiment CLI → SC2 environment preset: selects environment
- Experiment CLI → Run dispatcher: dispatches
- Run dispatcher → Episode runner: runs rollouts
- Run dispatcher → Parallel runner: runs parallel rollouts
- Run dispatcher → Q learner: selects learner
- Run dispatcher → COMA learner: selects learner
- Run dispatcher → QTRAN learner: selects learner
- Episode runner → Multi-agent environment contract: interacts through
- Parallel runner → Multi-agent environment contract: interacts through
- Multi-agent environment contract → SMAC / StarCraft II: implemented by
- Episode runner → Basic MAC: requests actions
- Parallel runner → Basic MAC: requests actions
- Basic MAC → RNN agent: infers with
- Basic MAC → Action selector: selects actions via
- Episode runner → Episode batch: collects
- Parallel runner → Episode batch: collects
- Experience transforms → Episode batch: preprocesses
- Episode batch → Q learner: trains
- Episode batch → COMA learner: trains
- Episode batch → QTRAN learner: trains
- Q learner → QMIX mixer: optionally mixes values
- COMA learner → COMA critic: optimizes with
- Run dispatcher → Run logger: records outputs
- Docker runtime → Experiment CLI: containerizes execution
- Interactive launcher → Docker runtime: launches
