# andreaskuhnle/simrlfab architecture

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

- Interactive diagram: https://gitdiagram.com/andreaskuhnle/simrlfab
- Repository: https://github.com/andreaskuhnle/simrlfab

## Overview

##### Project Type and Purpose
- SimPyRLFab is a domain‐specific simulation & reinforcement‐learning framework for production planning and control in complex job‐shop manufacturing systems.
- It combines a SimPy‐based discrete‐event simulator (the “environment”) with a Tensorforce‐based RL agent.

##### File Structure
- Top‐level entry points:
- run.py – orchestrates a single training experiment (loads configs, kicks off env & agent loop).
- hyper_tuner.py – wraps multiple run experiments to perform hyperparameter search.
- Config directory: JSON files for agent (agent.json), environment parameters (environment.json), memory and network architecture, plus algorithm presets (ppo1.json, trpo1.json).
- production/envs: Python modules implementing the SimPy environment (initialize_env.py), resources (machines, sources, sinks), transport, reward functions, and dispatching heuristics.
- logger.py + log/ directory: performance‐metric collection and log export.
- docu/: static documentation (e.g., layout.png).

##### README for Key Insights
- Technologies: Python 3.6, SimPy for simulation model, Tensorforce for RL.
- Components: parametrizable environment, benchmark heuristics (FIFO, NJF, EMPTY), RL agent configurations, logging of performance indicators.
- Workflow: configure JSON → run.py → initialize_env.py → training loop → log outputs.

##### Main Components
a. Orchestration Layer
- run.py (entry point)
- hyper_tuner.py (optional wrapper)
- Configuration Manager (reads JSON files from config/)
b. Simulation Environment (SimPy)
- initialize_env.py
- production/envs modules (machines, sources, sinks, transport, reward_functions, heuristics)
c. Reinforcement Learning Agent (Tensorforce)
- agent builder (consumes agent.json, memory.json, network.json)
- action selection & policy update
d. Logger & Data Sink
- logger.py → writes metrics to log/
- log files for post‐run analysis

##### Relationships and Data Flow
- run.py → loads all config JSON → passes env parameters to initialize_env
- run.py → passes agent parameters to Tensorforce agent
- Training loop:
[Environment] --(state)--> [RL Agent] --(action)--> [Environment]
[Environment] --(reward, next state)--> [RL Agent]
[Logger] subscribes to state, action, reward, episode metrics
- hyper_tuner.py invokes run.py repeatedly with varied config files
- config files feed both env and agent components

##### Architectural Patterns and Design Principles
- Modular, configuration‐driven design: clear separation between code and parameters
- Observer pattern for logging: logger.py listens to events from both env and agent

##### Technologies & Frameworks
- Python 3.6
- SimPy (for discrete‐event simulation)
- Tensorforce (for deep RL)
- JSON for configuration
- Standard Python logging or custom logger.py

##### Final Checklist
- [ ] Clear, descriptive labels for each component and sub‐module
- [ ] Call out key technologies on each component (e.g., “SimPy”, “Tensorforce”, “JSON Config”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Layer
    subgraph "Configuration Files"
        direction TB
        ConfigDir["config/"]:::config
        AgentConf["agent.json"]:::config
        EnvConf["environment.json"]:::config
        MemConf["memory.json"]:::config
        NetConf["network.json"]:::config
        PPOConf["ppo1.json"]:::config
        TRPOConf["trpo1.json"]:::config
    end

    %% Orchestration Layer
    subgraph "Orchestration Layer"
        direction TB
        Run["run.py"]:::orchestration
        Hyper["hyper_tuner.py"]:::orchestration
    end

    %% Simulation Environment
    subgraph "Simulation Environment (SimPy)"
        direction TB
        InitEnv["initialize_env.py"]:::environment
        ProdEnv["production_env.py"]:::environment
        Machines["machine.py"]:::environment
        Orders["order.py"]:::environment
        Sources["source.py"]:::environment
        Sinks["sink.py"]:::environment
        Transport["transport.py"]:::environment
        TimeCalc["time_calc.py"]:::environment
        Resources["resources.py"]:::environment
        Reward["reward_functions.py"]:::environment
        Heuristics["heuristics.py"]:::environment
    end

    %% RL Agent Layer
    subgraph "Reinforcement Learning Agent (Tensorforce)"
        direction TB
        AgentBuilder["Agent Builder"]:::agent
        ActionPolicy["Action Selection & Policy Update"]:::agent
    end

    %% Logging Layer
    subgraph "Logging & Data Sink"
        direction TB
        Logger["logger.py"]:::logging
        LogDir["log/"]:::logging
    end

    %% Documentation & Dependencies
    Docu["docu/"]:::config
    Req["requirements.txt"]:::config

    %% Data Flows
    ConfigDir --> AgentConf
    ConfigDir --> EnvConf
    ConfigDir --> MemConf
    ConfigDir --> NetConf
    ConfigDir --> PPOConf
    ConfigDir --> TRPOConf

    AgentConf --> Run
    EnvConf --> Run
    MemConf --> Run
    NetConf --> Run
    PPOConf --> Run
    TRPOConf --> Hyper
    Hyper --> Run

    Run --> InitEnv
    Run --> AgentBuilder

    InitEnv --> ProdEnv
    ProdEnv --> Machines
    ProdEnv --> Orders
    ProdEnv --> Sources
    ProdEnv --> Sinks
    ProdEnv --> Transport
    ProdEnv --> TimeCalc
    ProdEnv --> Resources
    ProdEnv --> Reward
    ProdEnv --> Heuristics

    ProdEnv -->|state| AgentBuilder
    AgentBuilder -->|action| ProdEnv
    ProdEnv -->|reward,next state| AgentBuilder

    ProdEnv --> Logger
    AgentBuilder --> Logger
    Logger --> LogDir

    Run --> Req
    Run --> Docu

    %% Click Events
    click Run "https://github.com/andreaskuhnle/simrlfab/blob/master/run.py"
    click Hyper "https://github.com/andreaskuhnle/simrlfab/blob/master/hyper_tuner.py"
    click ConfigDir "https://github.com/andreaskuhnle/simrlfab/tree/master/config/"
    click AgentConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/agent.json"
    click EnvConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/environment.json"
    click MemConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/memory.json"
    click NetConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/network.json"
    click PPOConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/ppo1.json"
    click TRPOConf "https://github.com/andreaskuhnle/simrlfab/blob/master/config/trpo1.json"
    click InitEnv "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/initialize_env.py"
    click ProdEnv "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/production_env.py"
    click Machines "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/machine.py"
    click Orders "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/order.py"
    click Sources "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/source.py"
    click Sinks "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/sink.py"
    click Transport "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/transport.py"
    click TimeCalc "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/time_calc.py"
    click Resources "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/resources.py"
    click Reward "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/reward_functions.py"
    click Heuristics "https://github.com/andreaskuhnle/simrlfab/blob/master/production/envs/heuristics.py"
    click Logger "https://github.com/andreaskuhnle/simrlfab/blob/master/logger.py"
    click LogDir "https://github.com/andreaskuhnle/simrlfab/tree/master/log/"
    click Docu "https://github.com/andreaskuhnle/simrlfab/tree/master/docu/"
    click Req "https://github.com/andreaskuhnle/simrlfab/blob/master/requirements.txt"

    %% Styles
    classDef orchestration fill:#f96,stroke:#333,stroke-width:1px
    classDef config fill:#ff9,stroke:#333,stroke-width:1px
    classDef environment fill:#9cf,stroke:#333,stroke-width:1px
    classDef agent fill:#9f9,stroke:#333,stroke-width:1px
    classDef logging fill:#ccc,stroke:#333,stroke-width:1px
```
