This repository provides a Python-based simulation framework for studying distributed coordination of renewable energy resources using multi-agent reinforcement learning (MARL).
The system models the interaction between solar generation, wind generation, battery storage, controllable loads, and the main utility grid, applying Q-learning with discretized state-action spaces.
The codebase is designed for engineers and researchers familiar with Python and reinforcement learning, aiming to facilitate reproducibility and extensibility.
- Multi-agent simulation of energy systems under dynamic conditions
- Modular architecture with specialized agent classes
- Discretization of state variables into configurable bins
- Q-learning implementation with per-agent policies
- Automatic generation of:
- Evolution logs (
CSV) - Episode metrics (
Excel) - Q-table snapshots (
Excel) - High-resolution and vector plots (
SVG)
- Evolution logs (
- Analysis and visualization utilities
Recommended platform: Linux Ubuntu 22.04.5 LTS
Python version: 3.11.7
Environment manager: Anaconda (conda 24.9.1)
conda create -n marl_env python=3.11.7conda activate marl_envpip install -r requirements.txt(Make sure requirements.txt includes numpy, pandas, matplotlib, openpyxl, etc.)
The main entry point is main.py.
python main.pyParameters such as:
- Number of episodes
- Exploration factor (
epsilon) - Input dataset (
csv_filename)
are set in the Simulation class instantiation.
Details of parameter values are described in the accompanying scientific article.
Running the simulation will:
- Initialize the environment and all agents
- Train agents via Q-learning
- Generate results in the
results/directory - Output performance metrics to the console
After a simulation run, your project will look like this:
results/
├── evolution/
│ └── learning_<episode>.csv # Detailed logs per episode
├── plots/
│ ├── IAE_over_episodes.svg
│ ├── Var_dif_over_episodes.svg
│ ├── Q_Norm_<agent>.svg
│ └── env_plot.svg
└── metrics_episode.xlsx # Episode summary metrics
To evaluate coordination effectiveness, the system computes:
- Energy Balance (ΔP): Instantaneous difference between generation and demand
- ISE: Integral Square Error
- IAE: Integral Absolute Error
- REP: Renewable Energy Penetration (%)
- GEP: Grid Energy Penetration (%)
To evaluate learning, it also calculates:
- Average Reward per Episode
- Average Cumulative Reward
Additional stability metrics are implemented and will be detailed in the publication.
You can create new agents or customize the environment:
- Subclass
BaseAgent - Implement:
get_discretized_state(): Define state representationinitialize_q_table(): Configure state-action spacecalculate_reward(): Design the reward function
- Subclass
MultiAgentEnvto load different datasets or apply new discretization schemes.
This design supports flexible experimentation without altering core components.
The module analysis_tools.py provides helper functions:
| Function | Purpose |
|---|---|
load_latest_evolution_csv() |
Load the most recent simulation log |
plot_metric() |
Generate metric plots |
compute_q_diff_norm() |
Compute L2 norm between Q-tables |
check_stability() |
Evaluate stability over episodes |
process_evolution_data() |
Prepare logs for visualization |
plot_coordination() |
Generate multi-panel plots of agent behavior |
clear_results_directories() |
Clean all files under results/ |
Note:
clear_results_directories()removes previous outputs before a new simulation run.
By default, the simulation generates:
- Time series plots of power and energy balance
- SVG graphics of learning progress (
IAE,Var_dif,Q Norms) - Per-agent Q-tables and rewards
Visual outputs help validate whether agents learn effective coordination strategies.
- Input datasets should be placed in
assets/datasets/in CSV format. - Default time resolution: 1 hour per time step
- For parameter explanations and case studies, see the accompanying article.
This project is released for academic research purposes.
Please cite appropriately if used in publications.