# citylearn-project/citylearn architecture

> Architecture diagram of the GitHub repository citylearn-project/citylearn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/citylearn-project/citylearn
- Repository: https://github.com/citylearn-project/citylearn

## Overview

CityLearn is a Gymnasium environment for simulating building-energy communities and evaluating controllers. The principal workflow is: configure a community and dataset, create an environment and agent, reset and repeatedly exchange observations and actions, then evaluate KPIs and optionally export results. The graph includes the documented capabilities for flexible assets, demand response, electrical constraints, community sharing, dynamic topology, and robustness. Some internals are unsampled; edges to those subsystems are based on the environment’s indexed references or README statements, not inspection of their implementation.

Main entry points: [`citylearn/citylearn.py`](https://github.com/citylearn-project/citylearn/blob/master/citylearn/citylearn.py) (CityLearn environment), [`citylearn/building.py`](https://github.com/citylearn-project/citylearn/blob/master/citylearn/building.py) (Building simulation), [`citylearn/agents/base.py`](https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/base.py) (Agent API), [`citylearn/internal/loading.py`](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/loading.py) (Scenario loading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_simulation["Simulation core"]
  node_environment["CityLearn environment<br/>[citylearn.py]"]
  node_loader["Scenario loading<br/>[loading.py]"]
  node_dataset[("Dataset access<br/>[data.py]")]
  node_time["Episode and time state<br/>[base.py]"]
  node_community["Multi-community runs<br/>[multi_community.py]"]
end

subgraph group_assets["Buildings and assets"]
  node_building["Building simulation<br/>[building.py]"]
  node_energy["Energy devices<br/>[energy_model.py]"]
  node_ev["Electric vehicles"]
  node_charger["EV chargers"]
  node_dynamics["Building dynamics<br/>[dynamics.py]"]
end

subgraph group_control["Controllers"]
  node_agent["Agent API<br/>[base.py]"]
  node_baseline["Built-in baselines<br/>[baseline.py]"]
  node_rbc["Rule-based control<br/>[rbc.py]"]
  node_rlagents["RL controllers"]
end

subgraph group_services["Community services"]
  node_demand["Demand response<br/>[demand_response.py]"]
  node_topology["Dynamic topology<br/>[topology.py]"]
  node_robustness["Failure handling<br/>[robustness.py]"]
  node_physics["Electrical constraints"]
end

subgraph group_results["Evaluation and output"]
  node_reward["Reward functions<br/>[reward_function.py]"]
  node_kpi["Community KPIs<br/>[kpi.py]"]
  node_exporter["Result export<br/>[exporter.py]"]
end

node_user(("Simulation user"))

node_user -->|"configures and runs"| node_environment
node_user -->|"selects controller"| node_agent
node_agent -->|"reads observations; sends actions"| node_environment
node_baseline -->|"implements"| node_agent
node_rbc -->|"extends"| node_agent
node_rlagents -.->|"implements"| node_agent
node_environment -->|"loads scenarios"| node_loader
node_loader -->|"retrieves data"| node_dataset
node_environment -->|"coordinates buildings"| node_building
node_environment -->|"tracks episodes"| node_time
node_building -->|"uses device models"| node_energy
node_building -->|"integrates EVs"| node_ev
node_building -->|"uses chargers"| node_charger
node_building -->|"uses dynamics"| node_dynamics
node_environment -.->|"supports synchronized runs"| node_community
node_environment -->|"coordinates response"| node_demand
node_environment -->|"manages topology"| node_topology
node_environment -->|"handles failures"| node_robustness
node_environment -->|"checks constraints"| node_physics
node_environment -->|"computes rewards"| node_reward
node_environment -->|"evaluates outcomes"| node_kpi
node_environment -->|"exports results"| node_exporter
node_environment -->|"uses dataset access"| node_dataset

click node_agent "https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/base.py"
click node_baseline "https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/baseline.py"
click node_rbc "https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/rbc.py"
click node_rlagents "https://github.com/citylearn-project/citylearn/tree/master/citylearn/agents"
click node_environment "https://github.com/citylearn-project/citylearn/blob/master/citylearn/citylearn.py"
click node_loader "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/loading.py"
click node_dataset "https://github.com/citylearn-project/citylearn/blob/master/citylearn/data.py"
click node_time "https://github.com/citylearn-project/citylearn/blob/master/citylearn/base.py"
click node_building "https://github.com/citylearn-project/citylearn/blob/master/citylearn/building.py"
click node_energy "https://github.com/citylearn-project/citylearn/blob/master/citylearn/energy_model.py"
click node_ev "https://github.com/citylearn-project/citylearn/blob/master/citylearn/electric_vehicle.py"
click node_charger "https://github.com/citylearn-project/citylearn/blob/master/citylearn/electric_vehicle_charger.py"
click node_dynamics "https://github.com/citylearn-project/citylearn/blob/master/citylearn/dynamics.py"
click node_community "https://github.com/citylearn-project/citylearn/blob/master/citylearn/multi_community.py"
click node_demand "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/demand_response.py"
click node_topology "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/topology.py"
click node_robustness "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/robustness.py"
click node_physics "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/physics_invariants.py"
click node_reward "https://github.com/citylearn-project/citylearn/blob/master/citylearn/reward_function.py"
click node_kpi "https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/kpi.py"
click node_exporter "https://github.com/citylearn-project/citylearn/blob/master/citylearn/exporter.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_environment,node_loader,node_dataset,node_time,node_community,node_user toneBlue
class node_building,node_energy,node_ev,node_charger,node_dynamics toneAmber
class node_agent,node_baseline,node_rbc,node_rlagents toneMint
class node_demand,node_topology,node_robustness,node_physics toneRose
class node_reward,node_kpi,node_exporter toneIndigo
```

## Components

### Simulation core

- [CityLearn environment](https://github.com/citylearn-project/citylearn/blob/master/citylearn/citylearn.py): `citylearn/citylearn.py`
- [Scenario loading](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/loading.py): `citylearn/internal/loading.py`
- [Dataset access](https://github.com/citylearn-project/citylearn/blob/master/citylearn/data.py): `citylearn/data.py`
- [Episode and time state](https://github.com/citylearn-project/citylearn/blob/master/citylearn/base.py): `citylearn/base.py`
- [Multi-community runs](https://github.com/citylearn-project/citylearn/blob/master/citylearn/multi_community.py): `citylearn/multi_community.py`

### Buildings and assets

- [Building simulation](https://github.com/citylearn-project/citylearn/blob/master/citylearn/building.py): `citylearn/building.py`
- [Energy devices](https://github.com/citylearn-project/citylearn/blob/master/citylearn/energy_model.py): `citylearn/energy_model.py`
- [Electric vehicles](https://github.com/citylearn-project/citylearn/blob/master/citylearn/electric_vehicle.py): `citylearn/electric_vehicle.py`
- [EV chargers](https://github.com/citylearn-project/citylearn/blob/master/citylearn/electric_vehicle_charger.py): `citylearn/electric_vehicle_charger.py`
- [Building dynamics](https://github.com/citylearn-project/citylearn/blob/master/citylearn/dynamics.py): `citylearn/dynamics.py`

### Controllers

- [Agent API](https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/base.py): `citylearn/agents/base.py`
- [Built-in baselines](https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/baseline.py): `citylearn/agents/baseline.py`
- [Rule-based control](https://github.com/citylearn-project/citylearn/blob/master/citylearn/agents/rbc.py): `citylearn/agents/rbc.py`
- [RL controllers](https://github.com/citylearn-project/citylearn/tree/master/citylearn/agents): `citylearn/agents`

### Community services

- [Demand response](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/demand_response.py): `citylearn/internal/demand_response.py`
- [Dynamic topology](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/topology.py): `citylearn/internal/topology.py`
- [Failure handling](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/robustness.py): `citylearn/internal/robustness.py`
- [Electrical constraints](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/physics_invariants.py): `citylearn/internal/physics_invariants.py`

### Evaluation and output

- [Reward functions](https://github.com/citylearn-project/citylearn/blob/master/citylearn/reward_function.py): `citylearn/reward_function.py`
- [Community KPIs](https://github.com/citylearn-project/citylearn/blob/master/citylearn/internal/kpi.py): `citylearn/internal/kpi.py`
- [Result export](https://github.com/citylearn-project/citylearn/blob/master/citylearn/exporter.py): `citylearn/exporter.py`

### Other components

- **Simulation user**

## Connections

- Simulation user → CityLearn environment: configures and runs (evidence: `README.md`)
- Simulation user → Agent API: selects controller (evidence: `README.md`)
- Agent API → CityLearn environment: reads observations; sends actions (evidence: `README.md`)
- Built-in baselines → Agent API: implements (evidence: `citylearn/agents/baseline.py`)
- Rule-based control → Agent API: extends (evidence: `citylearn/agents/rbc.py`)
- RL controllers → Agent API: implements (evidence: `citylearn/agents/baseline.py`)
- CityLearn environment → Scenario loading: loads scenarios (evidence: `citylearn/citylearn.py`)
- Scenario loading → Dataset access: retrieves data (evidence: `citylearn/internal/loading.py`)
- CityLearn environment → Building simulation: coordinates buildings (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Episode and time state: tracks episodes (evidence: `citylearn/citylearn.py`)
- Building simulation → Energy devices: uses device models (evidence: `citylearn/building.py`)
- Building simulation → Electric vehicles: integrates EVs (evidence: `citylearn/building.py`)
- Building simulation → EV chargers: uses chargers (evidence: `citylearn/building.py`)
- Building simulation → Building dynamics: uses dynamics (evidence: `citylearn/building.py`)
- CityLearn environment → Multi-community runs: supports synchronized runs (evidence: `README.md`)
- CityLearn environment → Demand response: coordinates response (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Dynamic topology: manages topology (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Failure handling: handles failures (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Electrical constraints: checks constraints (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Reward functions: computes rewards (evidence: `citylearn/internal/loading.py`)
- CityLearn environment → Community KPIs: evaluates outcomes (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Result export: exports results (evidence: `citylearn/citylearn.py`)
- CityLearn environment → Dataset access: uses dataset access (evidence: `citylearn/citylearn.py`)
