# pku-marl/harl architecture

> Architecture diagram of the GitHub repository pku-marl/harl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pku-marl/harl
- Repository: https://github.com/pku-marl/harl

## Overview

##### Main Components
- Environment Interfaces and Wrappers:
– These are implemented under the “harl/envs” directory and subdirectories (e.g. “dexhands”, “football”, “gym”, “lag”, “mamujoco”, “pettingzoo_mpe”, “smac”, “smacv2”).
– Their role is to provide a unified API (wrap-reset-step-render interfaces) that hide environment-specific details.
- HARL Algorithms:
– Located under “harl/algorithms” (with further segregation into “actors” for policy updates and “critics” for value functions) and base classes under “harl/algorithms/actors/on_policy_base.py” and “off_policy_base.py”.
– This is where heterogeneous update schemes (e.g. the sequential update scheme) are implemented.
- Model Definitions:
– Found in “harl/models”, this includes policy models (under “policy_models”) and value function models (in “value_function_models”).
– They are built using standard deep network architectures (CNNs, MLPs, RNNs, etc.), as defined in “harl/models/base”.
- Training Runners:
– Under “harl/runners”, these files (such as “off_policy_base_runner.py”, “on_policy_base_runner.py”, etc.) orchestrate training by interacting with the algorithm implementations, environment interfaces, and models.
– They manage the training loop, logging, saving, and (optionally) rendering.
- Configuration Management:
– “harl/configs” houses configuration files (YAML) for algorithms and for environment settings.
– Additionally, tuned configuration files are provided under “tuned_configs” for reproducing experiments.
- Utilities and Helpers:
– The “harl/utils” folder contains configuration tools, logging and data transformation utilities to support smooth integration between components.
- Logging and Experiment Management:
– Loggers are implemented under “harl/common/base_logger.py” and several environment-specific loggers exist in each environment folder.
– They output feedback and metrics during training.
- Examples and Entry Points:
– The “examples” folder contains the main training scripts (train.py, train.sh) that users execute.
– They tie together configuration, algorithm, runner, and environment layers to launch training experiments.

##### Architectural Patterns and Design Principles
- Modularity/Separation of Concerns:
– Each major package (algorithms, models, environments, configurations) is clearly segregated, making it easy for researchers to modify or extend specific parts without impacting others.
- Abstraction and Interface Hiding:
– The environment interfaces abstract away details from external systems such as SMAC, Bi-DexterousHands, etc.
– The runner and configuration tools are designed for easy reproduction of experiments.
- Extensibility:
– New algorithms or environments can be plugged into the existing training loop by following the implemented abstractions.

– Core algorithm components (policy, critics, runners)
– External interfaces (environments)
– Support utilities (logging, configuration management)
- Notes or small callouts about key design decisions such as “sequential update scheme” or “unified environment interface” to capture the distinctive ideas of HARL.
- Detail the flow:
1. Experiment setup (configs, tuned_configs, CLI parameters)
2. Training Runner initializes algorithm objects and model networks
3. Algorithm modules interact with environment interfaces to generate experiences
4. Experiences are processed by utility modules (e.g., buffers, loggers) and update the model parameters
5. Logging and performance metrics are pushed back to the user

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User/Experiment Setup"
        U1["\"harl/configs\""]:::infra
        U2["\"tuned_configs\""]:::infra
        U3["\"examples/train.py\""]:::infra
        U4["\"examples/train.sh\""]:::infra
    end

    R["Training Runner\n(harl/runners)"]:::core

    subgraph "Algorithms Core"
        A1["HARL Algorithms\n(harl/algorithms)"]:::core
        A2["Actors\n(harl/algorithms/actors)"]:::core
        A3["Critics\n(harl/algorithms/critics)"]:::core
        M1["Models\n(harl/models)"]:::core
        M2["Base Models\n(harl/models/base)"]:::core
        M3["Policy Models\n(harl/models/policy_models)"]:::core
        M4["Value Function Models\n(harl/models/value_function_models)"]:::core
    end

    subgraph "Environment Interfaces"
        E1["DexterousHands\n(harl/envs/dexhands)"]:::infra
        E2["Football\n(harl/envs/football)"]:::infra
        E3["Gym\n(harl/envs/gym)"]:::infra
        E4["LAG\n(harl/envs/lag)"]:::infra
        E5["MAMuJoCo\n(harl/envs/mamujoco)"]:::infra
        E6["PettingZoo MPE\n(harl/envs/pettingzoo_mpe)"]:::infra
        E7["SMAC\n(harl/envs/smac)"]:::infra
        E8["SMACv2\n(harl/envs/smacv2)"]:::infra
    end

    subgraph "Utilities & Logging"
        UTL1["Utilities\n(harl/utils)"]:::util
        UTL2["Base Logger\n(harl/common/base_logger.py)"]:::util
    end

    %% Data and Control Flows
    U1 -->|"config_input"| R
    U2 -->|"config_input"| R
    U3 -->|"entry_point"| R
    U4 -->|"script_trigger"| R

    R -->|"initialize"| A1
    A1 -->|"delegates_to"| A2
    A1 -->|"delegates_to"| A3
    A2 -->|"uses"| M1
    A3 -->|"uses"| M1
    M1 -->|"builds_with"| M2
    M1 -->|"builds_with"| M3
    M1 -->|"builds_with"| M4

    A1 -->|"action_obs"| E1
    A1 -->|"action_obs"| E2
    A1 -->|"action_obs"| E3
    A1 -->|"action_obs"| E4
    A1 -->|"action_obs"| E5
    A1 -->|"action_obs"| E6
    A1 -->|"action_obs"| E7
    A1 -->|"action_obs"| E8

    E1 -->|"provide_experience"| R
    E2 -->|"provide_experience"| R
    E3 -->|"provide_experience"| R
    E4 -->|"provide_experience"| R
    E5 -->|"provide_experience"| R
    E6 -->|"provide_experience"| R
    E7 -->|"provide_experience"| R
    E8 -->|"provide_experience"| R

    R -->|"logging"| UTL2
    R -->|"utility_calls"| UTL1

    %% Click Events
    click U1 "https://github.com/pku-marl/harl/tree/main/harl/configs"
    click U2 "https://github.com/pku-marl/harl/tree/main/tuned_configs"
    click U3 "https://github.com/pku-marl/harl/blob/main/examples/train.py"
    click U4 "https://github.com/pku-marl/harl/blob/main/examples/train.sh"
    click R "https://github.com/pku-marl/harl/tree/main/harl/runners"
    click A1 "https://github.com/pku-marl/harl/tree/main/harl/algorithms"
    click A2 "https://github.com/pku-marl/harl/tree/main/harl/algorithms/actors"
    click A3 "https://github.com/pku-marl/harl/tree/main/harl/algorithms/critics"
    click M1 "https://github.com/pku-marl/harl/tree/main/harl/models"
    click M2 "https://github.com/pku-marl/harl/tree/main/harl/models/base"
    click M3 "https://github.com/pku-marl/harl/tree/main/harl/models/policy_models"
    click M4 "https://github.com/pku-marl/harl/tree/main/harl/models/value_function_models"
    click E1 "https://github.com/pku-marl/harl/tree/main/harl/envs/dexhands"
    click E2 "https://github.com/pku-marl/harl/tree/main/harl/envs/football"
    click E3 "https://github.com/pku-marl/harl/tree/main/harl/envs/gym"
    click E4 "https://github.com/pku-marl/harl/tree/main/harl/envs/lag"
    click E5 "https://github.com/pku-marl/harl/tree/main/harl/envs/mamujoco"
    click E6 "https://github.com/pku-marl/harl/tree/main/harl/envs/pettingzoo_mpe"
    click E7 "https://github.com/pku-marl/harl/tree/main/harl/envs/smac"
    click E8 "https://github.com/pku-marl/harl/tree/main/harl/envs/smacv2"
    click UTL1 "https://github.com/pku-marl/harl/tree/main/harl/utils"
    click UTL2 "https://github.com/pku-marl/harl/blob/main/harl/common/base_logger.py"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef infra fill:#90EE90,stroke:#000,stroke-width:2px,stroke-dasharray:5,5;
    classDef util fill:#FFB6C1,stroke:#000,stroke-width:2px,stroke-dasharray:2,2;
```
