# future-power-networks/mapdn architecture

> Architecture diagram of the GitHub repository future-power-networks/mapdn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/future-power-networks/mapdn
- Repository: https://github.com/future-power-networks/mapdn

## Overview

##### Project Type and Purpose
- This is a research-oriented full-stack Python project implementing a multi-agent reinforcement learning (MARL) environment and training framework for active voltage control in power distribution networks.
- It bundles:
- A multi-agent environment conforming to PyMARL APIs (“environments/…”)
- Implementations of 10 state-of-the-art actor-critic MARL algorithms (“learning_algorithms/…”, “agents/…”, “critics/…”, “models/…”)
- Configuration and orchestration scripts (“args/…”, `train.py`, `test.py`, bash wrappers)
- Traditional control baselines in MATLAB (“traditional_control/…”).

##### File Structure Analysis
Top-Level Directories
- agents/           – agent policy classes
- critics/          – critic networks for centralized or decentralized learning
- learning_algorithms/ – algorithm wrappers (DDPG, PPO, RL base)
- models/           – high-level algorithm implementations and model registry
- environments/     – voltage control environment and barrier functions
- args/             – YAML configs for algorithms and environments
- utilities/        – replay buffer, trainers, testers
- traditional_control/ – MATLAB scripts using MATPOWER
- img/, README.md, train.py, test.py, code_examples.py

Key Patterns
- Modular separation of environment, agent, critic, model, and algorithm
- Registry patterns for models and voltage barrier functions
- Config-driven orchestration via YAML files and CLI scripts

##### Extracted Architecture Insights
- The core loop: `train.py` / `test.py` load YAML configs → instantiate VoltageControl env → select MARL algorithm from models/model_registry → run training/testing via utilities/trainer or tester → save models, logs.
- Agents and critics interact through learning_algorithms decorators, accessing per-agent observations and centralized state (Dec-POMDP).
- Data inputs: time-series data in `environments/var_voltage_control/data/<scenario>`; MATLAB-based OPF/droop baselines.
- External dependencies: PyTorch/TensorFlow (depending on algorithm), PyMARL API conventions, MATPOWER, Anaconda environments.

##### Components
a. Environment Layer
- VoltageControl Env (environments/var_voltage_control)
- Data loader (scenario files under data/)
- Voltage barrier registry & functions

b. Agent & Network Layer
- Agent classes (agents/mlp_agent.py, rnn_agent.py variants)
- Critic classes (critics/*.py)
- Models (models/*.py & model_registry)

c. Algorithm Layer
- Learning algorithm wrappers (learning_algorithms/…)
- RL core (actor_critic, rl_algorithms, ppo, ddpg)

d. Orchestration & Configuration
- CLI scripts (`train.py`, `test.py`, code_examples.py)
- Config files (args/env_args/*.yaml, args/alg_args/*.yaml)
- Bash wrappers (train_case*.sh)

e. Utilities & Support
- utilities/trainer.py → training loop, gradient updates
- utilities/replay_buffer.py → experience storage
- utilities/tester.py → evaluation pipelines
- utilities/util.py → common helpers

f. Traditional Control (optional view)
- MATLAB scripts (traditional_control/*.m)
- MATPOWER dependency

##### Relationships and Data Flows
- At runtime, `train.py` reads algorithm + env config → creates VoltageControl env instance → queries number of agents, action/state spaces → instantiates Agent & Critic networks via model_registry → enters training loop in utilities/trainer → for each step:
1. env.get_obs() / step(actions)
2. collect transitions → replay_buffer
3. sample batch → compute actor/critic losses → backprop via learning_algorithms
4. update model parameters
- Models and logs are saved to disk (`--save-path`).
- `test.py` loads saved models, runs evaluation in utilities/tester, optionally renders via rendering_voltage_control_env.py.

##### Architectural Patterns and Principles
- Modular layering: environment ↔ agent/critic ↔ algorithm wrapper ↔ orchestration scripts
- Registry and factory patterns (model_registry, voltage_barrier_registry) for extensibility
- Config-driven design: YAML files decouple code from parameters
- Separation of concerns: MATLAB baselines isolated under traditional_control

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Layer
    ConfigFiles["Config Files\n(args/*.yaml)"]:::orchestration
    ConfigFiles --> TrainScript
    ConfigFiles --> TestScript
    ConfigFiles --> CodeExamples

    %% Orchestration & Configuration
    TrainScript["train.py"]:::orchestration
    TestScript["test.py"]:::orchestration
    CodeExamples["code_examples.py"]:::orchestration
    BashWrappers["Bash Wrappers\n(train_case*.sh)"]:::orchestration

    TrainScript --> VoltageEnv
    TestScript --> VoltageEnv
    CodeExamples --> VoltageEnv
    BashWrappers -.-> MATLAB

    %% Environment Layer
    VoltageEnv["VoltageControl Env"]:::env
    DataLoader["Data Loader\n(data/)"]:::env
    BarrierRegistry["Voltage Barrier Registry"]:::env
    BarrierFunctions["Voltage Barrier Functions"]:::env

    VoltageEnv --> DataLoader
    VoltageEnv --> BarrierRegistry
    BarrierRegistry --> BarrierFunctions

    VoltageEnv --> Agents
    VoltageEnv --> Critics

    %% Agent & Network Layer
    Agents["Agent Policies"]:::agent
    Critics["Critic Networks"]:::agent
    Models["Models & Registry"]:::agent

    Agents --> AlgoWrappers
    Critics --> AlgoWrappers
    Models --> AlgoWrappers

    %% Algorithm Layer
    AlgoWrappers["Learning Algorithm Wrappers"]:::algo
    RLCores["RL Core\n(actor_critic, ddpg, ppo)"]:::algo

    AlgoWrappers --> RLCores
    RLCores --> Trainer
    RLCores --> Tester

    %% Utilities & Support
    Trainer["Trainer"]:::utilities
    Tester["Tester"]:::utilities
    ReplayBuffer["Replay Buffer"]:::utilities
    Util["Utilities Helpers"]:::utilities

    Trainer --> ReplayBuffer
    Trainer --> Util
    Tester --> Util

    Trainer --> Storage
    Tester --> Storage

    %% Traditional Control Baselines
    MATLAB["MATLAB Baselines\n(traditional_control/*.m)"]:::external
    MATLAB -.-> DataLoader

    %% Storage
    Storage[(Models & Logs)]:::storage

    %% Click Events
    click VoltageEnv "https://github.com/future-power-networks/mapdn/blob/main/environments/var_voltage_control/voltage_control_env.py"
    click DataLoader "https://github.com/future-power-networks/mapdn/tree/main/environments/var_voltage_control/data/"
    click BarrierFunctions "https://github.com/future-power-networks/mapdn/tree/main/environments/var_voltage_control/voltage_barrier"
    click BarrierRegistry "https://github.com/future-power-networks/mapdn/blob/main/environments/var_voltage_control/voltage_barrier/voltage_barrier_registry.py"
    click Agents "https://github.com/future-power-networks/mapdn/tree/main/agents"
    click Critics "https://github.com/future-power-networks/mapdn/tree/main/critics"
    click Models "https://github.com/future-power-networks/mapdn/tree/main/models"
    click AlgoWrappers "https://github.com/future-power-networks/mapdn/tree/main/learning_algorithms"
    click ConfigFiles "https://github.com/future-power-networks/mapdn/tree/main/args"
    click TrainScript "https://github.com/future-power-networks/mapdn/blob/main/train.py"
    click TestScript "https://github.com/future-power-networks/mapdn/blob/main/test.py"
    click CodeExamples "https://github.com/future-power-networks/mapdn/blob/main/code_examples.py"
    click Trainer "https://github.com/future-power-networks/mapdn/blob/main/utilities/trainer.py"
    click Tester "https://github.com/future-power-networks/mapdn/blob/main/utilities/tester.py"
    click ReplayBuffer "https://github.com/future-power-networks/mapdn/blob/main/utilities/replay_buffer.py"
    click Util "https://github.com/future-power-networks/mapdn/blob/main/utilities/util.py"
    click MATLAB "https://github.com/future-power-networks/mapdn/tree/main/traditional_control"

    %% Styles
    classDef orchestration fill:#ccc,stroke:#333,stroke-width:1px
    classDef env fill:#ccf,stroke:#333,stroke-width:1px
    classDef agent fill:#cfc,stroke:#333,stroke-width:1px
    classDef algo fill:#f96,stroke:#333,stroke-width:1px
    classDef utilities fill:#ff9,stroke:#333,stroke-width:1px
    classDef storage fill:#eee,stroke:#333,stroke-width:1px
    classDef external fill:#fff,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
