# zefang-liu/invagent architecture

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

- Interactive diagram: https://gitdiagram.com/zefang-liu/invagent
- Repository: https://github.com/zefang-liu/invagent

## Overview

##### Project Type and Purpose
- Type: Research codebase for multi-agent reinforcement-learning in supply chains, enhanced by LLM calls.
- Purpose: Simulate inventory environments, run baseline/IPPO/MAPPO policies (zero-shot via LLMs), and compare performance.

##### File Structure
- notebooks/
– autogen.ipynb: orchestration of experiments (data loading, running episodes, result plotting).
- src/
– env.py: defines the supply-chain environment logic (state, step, reset).
– config.py: central configuration loader (agent counts, horizons, LLM settings).
– baseline.py: rule-based inventory policy implementation.
– ippo.py: Independent PPO multi-agent algorithm (no communication).
– mappo.py: Multi-agent PPO with parameter sharing or coordination.
- requirements.txt: external libraries, including OpenAI packages.

##### README for Additional Insights
- LLM Integration: Agents can query OpenAI API (zero-shot prompts) at each decision point.
- Experiment Workflow: Notebook → load config → init env + agents → run episodes → collect metrics.

##### Main Components and Their Interactions
a. Components:
1. External LLM Service (OpenAI API)
2. Experiment Orchestrator (notebooks/autogen.ipynb)
3. Configuration Module (src/config.py)
4. Environment Simulator (src/env.py)
5. Agent Policies:
- Baseline (src/baseline.py)
- IPPO (src/ippo.py)
- MAPPO (src/mappo.py)
6. Results Store / Metrics Logger (in-memory or file outputs)
b. Relationships & Data Flow:
1. The notebook loads config → instantiates env + selected policy class.
2. For each timestep:
– env.py exposes state to policy module.
– If policy uses LLM: policy sends prompt → OpenAI API → returns action.
– Otherwise, policy computes action locally.
– Action flows back to env → env.step() → new state + reward.
3. Logger collects (state, action, reward) → results files/plots.
c. Architectural Patterns:
- Modular policy plugging (Strategy pattern): baseline/IPPO/MAPPO share a common interface.
- Experiment orchestrator decoupled from core logic (Separation of concerns).
- Configuration-driven initialization.
d. Key Technologies:
- Python 3.x
- OpenAI Python SDK (LLM calls)
- RL libraries or NumPy/Pandas (for IPPO/MAPPO math)
- Jupyter notebooks for orchestration and visualization.

##### Detailed Checklist
- Break out each src/*.py as its own component.
- Config.py feeding parameters into env.py and policy constructors.
- Decision loop: state → policy → action → environment.
- Denote zero-shot LLM invocation path separately from local policy logic.
- Notebook’s role in orchestrating runs and gathering plots.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Orchestration"
        Notebook["Experiment Orchestrator\n(notebooks/autogen.ipynb)"]:::orchestrator
    end

    subgraph "Configuration"
        Config["Configuration Module\n(src/config.py)"]:::datastore
    end

    subgraph "Core Modules"
        Env["Environment Simulator\n(src/env.py)"]:::internal
        subgraph "Policy Modules"
            Baseline["Baseline Policy\n(src/baseline.py)"]:::internal
            IPPO["IPPO Policy\n(src/ippo.py)"]:::internal
            MAPPO["MAPPO Policy\n(src/mappo.py)"]:::internal
        end
    end

    ExternalLLM["OpenAI API\n(external LLM)"]:::external
    Results["Results Logger\n(capture state/action/reward)"]:::datastore

    Notebook -->|"loads config"| Config
    Config -->|"init params"| Env
    Config -->|"init params"| Baseline
    Config -->|"init params"| IPPO
    Config -->|"init params"| MAPPO

    Env -->|"state"| Baseline
    Env -->|"state"| IPPO
    Env -->|"state"| MAPPO

    Baseline -->|"action"| Env
    IPPO -->|"action"| Env
    MAPPO -->|"action"| Env

    IPPO -->|"LLM prompt (JSON)"| ExternalLLM
    MAPPO -->|"LLM prompt (JSON)"| ExternalLLM
    ExternalLLM -->|"LLM response (JSON)"| IPPO
    ExternalLLM -->|"LLM response (JSON)"| MAPPO

    Env -->|"log(state,action,reward)"| Results
    Baseline -->|"log(policy)"| Results
    IPPO -->|"log(policy)"| Results
    MAPPO -->|"log(policy)"| Results
    Notebook -->|"retrieves results & plots"| Results

    click Notebook "https://github.com/zefang-liu/invagent/blob/main/notebooks/autogen.ipynb"
    click Config "https://github.com/zefang-liu/invagent/blob/main/src/config.py"
    click Env "https://github.com/zefang-liu/invagent/blob/main/src/env.py"
    click Baseline "https://github.com/zefang-liu/invagent/blob/main/src/baseline.py"
    click IPPO "https://github.com/zefang-liu/invagent/blob/main/src/ippo.py"
    click MAPPO "https://github.com/zefang-liu/invagent/blob/main/src/mappo.py"
    click ExternalLLM "https://github.com/zefang-liu/invagent/blob/main/requirements.txt"

    classDef orchestrator fill:#a8dda8,stroke:#333,stroke-width:1px
    classDef internal fill:#add8e6,stroke:#333,stroke-width:1px
    classDef external fill:#f9c38a,stroke:#333,stroke-width:1px
    classDef datastore fill:#d3d3d3,stroke:#333,stroke-width:1px
```
