# mpc-based-reinforcement-learning/mpc4rl architecture

> Architecture diagram of the GitHub repository mpc-based-reinforcement-learning/mpc4rl, 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/mpc-based-reinforcement-learning/mpc4rl
- Repository: https://github.com/mpc-based-reinforcement-learning/mpc4rl

## Overview

- Core Libraries and Utilities:
- A “common” component that includes shared utilities such as integrators, policies, and other helper functions.
- Simulation Environments:
- The “rlmpc/gym” directory encapsulates simulation environments (e.g., continuous_cartpole, evaporation_process, and linear_system) that adapt gymnasium for these control problems.
- Model Predictive Control (MPC) Solvers:
- The “rlmpc/mpc” directory subdivides into subfolders (cartpole, chain_mass, evaporation_process, linear_system) along with a general MPC module. These components utilize acados to generate MPC solvers.
- Reinforcement Learning Algorithms:
- Notably, the “rlmpc/qlearning”, “rlmpc/ppo”, and “rlmpc/td3” directories represent different RL approaches to approximate or augment MPC control.
- Examples & Scripts:
- The “rlmpc/examples” along with the standalone “scripts” directory offer example implementations and experiments running various control strategies.
- Configuration & Assets:
- Testing:
- The “tests” directory holds unit tests to ensure correctness of implementations.

##### Architectural patterns and design principles
- Modular Separation:
- Clearly separate the environment (gym) from control strategies (MPC, RL algorithms), which facilitates extensibility and isolated testing.
- Integration Points:
- Integration areas where different control modules (MPC and RL) might run in closed-loop configurations, as evidenced in the scripts (e.g., “cartpole_mpc_as_td3_agent_closed_loop.py”).
- Use of Configuration Files:
- That the “config” module supplies parameters to the simulation environments and control algorithms.
- Multiple Approaches:
- That the project supports a variety of control methods (MPC, Q-learning, PPO, TD3), making it a hybrid control framework.

##### Relevant technologies and tools
- Acados is central for solving MPC problems.
- Casadi is used for symbolic and algorithmic differentiation.
- Gymnasium acts as the simulation backbone, ensuring that environments like cartpole or linear systems are standardized.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    subgraph "External Dependencies"
        ED1["acados"]:::ext
        ED2["casadi"]:::ext
        ED3["gymnasium"]:::ext
        ED4["stable-baselines3"]:::ext
    end

    %% Core Modules
    subgraph "Core Modules"
        CU["Common Utilities"]:::common
        SE["Gym Environments"]:::simulation
        MPC["MPC Solvers"]:::control
        subgraph "Reinforcement Learning Algorithms"
            RL_Q["Q-Learning"]:::control
            RL_PPO["PPO"]:::control
            RL_TD3["TD3"]:::control
        end
        subgraph "Experiments"
            EX["Examples"]:::entry
            SC["Scripts"]:::entry
        end
        CF["Configuration Files"]:::config
        AS["Assets"]:::config
        TS["Testing"]:::testing
    end

    %% External Dependencies to Core Modules
    ED1 --> MPC
    ED2 --> MPC
    ED3 --> SE
    ED4 --> RL_Q
    ED4 --> RL_PPO
    ED4 --> RL_TD3

    %% Common Utilities influence control modules
    CU --> MPC
    CU --> RL_Q
    CU --> RL_PPO
    CU --> RL_TD3

    %% Gym Environments interact with control modules
    SE --> MPC
    SE --> RL_Q
    SE --> RL_PPO
    SE --> RL_TD3

    %% Experiments trigger simulation environments
    EX --> SE
    SC --> SE

    %% Experiments also drive control modules (via simulation)
    EX --> MPC
    EX --> RL_Q
    EX --> RL_PPO
    EX --> RL_TD3
    SC --> MPC
    SC --> RL_Q
    SC --> RL_PPO
    SC --> RL_TD3

    %% Configuration feeds into simulation and control modules
    CF --> SE
    CF --> MPC
    CF --> RL_Q
    CF --> RL_PPO
    CF --> RL_TD3

    %% Testing validates control and common modules
    TS --> CU
    TS --> MPC
    TS --> RL_Q
    TS --> RL_PPO
    TS --> RL_TD3

    %% Click Events for Component Mapping
    click CU "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/common"
    click SE "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/gym"
    click MPC "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/mpc"
    click RL_Q "https://github.com/mpc-based-reinforcement-learning/mpc4rl/blob/main/rlmpc/qlearning/q_learning.py"
    click RL_PPO "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/ppo"
    click RL_TD3 "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/td3"
    click EX "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/rlmpc/examples"
    click SC "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/scripts"
    click CF "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/config"
    click AS "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/assets"
    click TS "https://github.com/mpc-based-reinforcement-learning/mpc4rl/tree/main/tests"

    %% Class Definitions for Styling
    classDef ext fill:#cccccc,stroke:#333333,stroke-width:2px;
    classDef common fill:#ffa500,stroke:#333333,stroke-width:2px;
    classDef simulation fill:#add8e6,stroke:#333333,stroke-width:2px;
    classDef control fill:#90ee90,stroke:#333333,stroke-width:2px;
    classDef entry fill:#ffff99,stroke:#333333,stroke-width:2px;
    classDef config fill:#d3d3d3,stroke:#333333,stroke-width:2px;
    classDef testing fill:#f4a7b9,stroke:#333333,stroke-width:2px;
```
