Skip to content

About

Code related to 'Deep Reinforcement Learning for Peak Load Reduction in Aggregated Residential Houses' (IEEE PESGM 2020)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

peakrl

Code related to

V. Taboga, A. Bellahsen, H. Dagdougui, Deep Reinforcement Learning For Peak Load Reduction In Aggregated Residential Houses, IEEE, 2020.

This is not an exact reproduction of the paper's results. It is a clean, modern rewrite of the code behind the paper.

  • Same method, same simulator. The environment, reward and forecaster behave exactly like the 2019 code (checked to round-off against the original), and TRPO uses the same settings.
  • The paper's tables come back only with the 2019 agents. Evaluating the agents trained in 2019 (reference/) in this code gives tables I and II of the paper, up to random noise.
  • Retraining gives different numbers. Agents trained from scratch here reproduce the scenario-1 baseline and the forecaster accuracy, but the peak reductions of scenarios 2 and 3 vary widely from one seed to the next and generally do not match the paper's figures (see From-scratch results). The original training was not seeded, the forecaster training script was not archived, and the TRPO implementation is a port, so the 2019 training runs cannot be replayed exactly.

A single TRPO agent controls the heating/cooling power of a cluster of 1, 2, 4 or 6 houses every 15 minutes, in three scenarios:

  1. keep every house at 21 °C;
  2. same, with a penalty on the aggregated power, larger above a peak threshold;
  3. same as 2, and the agent also observes an LSTM forecast of the aggregated power two hours ahead.

Agents are trained on Montreal weather from April to December 2015 and tested from April to November 2016.

This repository needs Python ≥ 3.10 with numpy, pandas, Gymnasium, PyTorch (CPU) and matplotlib; the 2019 stack (TensorFlow 1, stable-baselines 2, Keras 2.2, MPI) is no longer required.

Quick start

uv sync                                  # creates .venv from uv.lock

uv run peakrl evaluate --run reference   # ~1 min: tables I-II and figure 3 from the agents trained in 2019
uv run peakrl evaluate --run pretrained/seed0   # ~1 min: the same for agents retrained with this code
uv run peakrl reproduce                  # ~25 min on 12 cores: retrain everything from scratch, then evaluate
uv run pytest                            # ~30 s: equivalence with the original code

reproduce writes to runs/default/ (change it with --run). It runs these stages, which can also be called one by one (peakrl train|forecast|evaluate, see peakrl --help):

stage what it does output in the run directory
train --scenarios 1 TRPO, 1M steps per cluster size, then replays 2015 with the trained agent agents/{n}h_s1.npz (+ learning curve .csv), profiles/{n}h.csv.gz
forecast fits the LSTM on the scenario-1 power profile forecasters/{n}h.npz
train --scenarios 2 3 TRPO with the power penalty; the peak threshold and the forecaster come from the two stages above agents/{n}h_s{2,3}.npz
evaluate runs every agent over April-November 2016 results/tables.md, results/metrics.csv, results/fig3_temperatures.png

Useful options: --houses 1 2, --timesteps 200000, --seed 1, --jobs 4.

Results with the 2019 agents

reference/ holds the agents, forecasters and profiles produced in 2019, converted to plain numpy files. Evaluated in this repository's environment they give the paper's tables back (reference/results/tables.md); the remaining differences come from the random non-HVAC load. These are the paper's agents, not agents trained by this code: the table below checks the simulator and the evaluation, not the training.

Temperatures in the four-house cluster during the first two test days

Test period April-November 2016; Δ is the change relative to scenario 1 of the same cluster:

houses scenario T mean ± std (°C) paper Δ average power paper Δ peak duration paper Δ peak energy paper
1 1 21.36 ± 0.38 21.36 ± 0.37 – – – – – –
1 2 21.10 ± 0.53 21.09 ± 0.53 -2.0 % -1.8 % -34.9 % -35.9 % -35.3 % -36.2 %
1 3 21.03 ± 1.23 21.03 ± 1.24 -3.5 % -3.5 % -70.5 % -71.9 % -71.9 % -73.2 %
2 1 21.15 ± 0.34 21.15 ± 0.33 – – – – – –
2 2 20.57 ± 0.89 20.57 ± 0.89 -3.4 % -3.4 % -62.4 % -62.9 % -63.5 % -63.9 %
2 3 20.67 ± 1.20 20.66 ± 1.20 -3.6 % -3.6 % -57.8 % -59.8 % -58.9 % -60.7 %
4 1 21.23 ± 0.60 21.23 ± 0.60 – – – – – –
4 2 20.95 ± 0.99 20.96 ± 0.99 -2.1 % -2.1 % -44.9 % -42.9 % -45.8 % -44.0 %
4 3 21.14 ± 0.83 21.14 ± 0.82 -0.8 % -0.7 % -13.1 % -11.9 % -13.6 % -12.5 %
6 1 21.24 ± 0.80 21.24 ± 0.80 – – – – – –
6 2 20.93 ± 1.25 20.93 ± 1.30 -1.1 % -1.0 % -25.4 % -26.7 % -26.0 % -27.3 %
6 3 20.95 ± 1.01 20.94 ± 1.00 -1.1 % -1.1 % -26.0 % -26.7 % -26.8 % -27.6 %

Retraining from scratch gives new agents and therefore other numbers: the scenario-1 baselines are reproduced, the peak reductions of scenarios 2 and 3 are reproduced in direction but vary widely from one training run to the next. See From-scratch results.

Layout

src/peakrl/env.py          Gymnasium environment: thermal model, reward, observation
src/peakrl/forecaster.py   LSTM forecaster: inference (numpy) and training (PyTorch)
src/peakrl/trpo.py         TRPO, ported from stable-baselines 2
src/peakrl/agents.py       loading agents, roll-outs
src/peakrl/experiment.py   the stages above, for one cluster size
src/peakrl/report.py       tables I and II, figure 3
src/peakrl/cli.py          command line, runs cluster sizes in parallel
data/                      Montreal weather 2015-2016, daily occupant profiles
reference/                 2019 agents, forecasters, profiles; paper tables; their evaluation
pretrained/seed{0,1,2}/    agents, forecasters and profiles retrained with this code; their evaluation
scripts/convert_original.py  how data/ and reference/ were extracted from the original archive
tests/                     equivalence tests against the original environment and Keras models

Fidelity to the 2019 code

  • Environment. For fixed action sequences, observations and rewards equal those of the original, unmodified real_building_cc.py to 1e-6, for 1 to 6 houses, the three scenarios, both splits and across the end of the training year.
  • Forecaster. The numpy LSTM equals real Keras running the original .hdf5 files to 2e-6.
  • Constants. The peak thresholds (10064, 19728, 39435, 58977 W) and the power scales hard-coded in the original environment are recomputed exactly from the archived profiles.
  • Tables. See above.

What differs from the 2019 code:

  • TRPO implementation. trpo.py is a PyTorch port of the stable-baselines 2.8 TRPO (TensorFlow 1, MPI) for the single-environment case, written from its source: same networks and initialisation, GAE, Fisher-vector products on one sample in five, conjugate gradient, line search accepting KL ≤ 1.5 × 0.01, value function fitted with Adam, and the same defaults. It is not bit-identical to the original (different random streams and framework). sb3-contrib's TRPO was tried first with the same settings; in a small comparison (six runs against four) its scenario-1 agents were less consistent from seed to seed, and the port removes the dependency.
  • Thermal step. The 1000 Euler sub-steps per house are computed in closed form (a matrix power), equal to round-off and orders of magnitude faster.
  • Forecaster training. The original training script was not in the archive; only the trained models were. train_forecaster is a reconstruction. Recovered from the saved models and the environment: architecture, input features and their (absence of) scaling, power scaling, loss, optimiser, learning rate, 546 batches per epoch, and 37 to 73 epochs depending on the cluster. Guessed: batch size 32, a chronological 66/14/20 % train/validation/test split (which gives 546 batches), early stopping with a patience of 10 epochs.
  • Forecaster lags. The 16 past powers given to the LSTM are selected by auto-correlation, as the paper describes. On the 2019 profiles this rule selects offsets 0-7 and 91-98, while the 2019 environment used 0-7 and 90-97 (0-8 and 91-97 for four houses); the 2019 lists are kept with the reference forecasters.
  • Randomness. Training, the environment noise and the evaluation are seeded (--seed), so a run can be repeated; the 2019 code was not seeded.

Where the paper and the code disagree

paper code (kept)
Temperature reward, eq. (9) −exp((T−T_target)²) − 0.1·(distance outside [T_min, T_max]) **+**exp(−0.5·(T−T_target)²) − 0.1·(distance outside 21 ± 0.5 °C), summed over houses
Power coefficient ν, eq. (10) 5·10⁻⁶ 5*10e-6 = 5·10⁻⁵
Reward scale divided by the number of houses
Peak indicator, eq. (11) "positive when the peak is exceeded" P_peak − max(forecast), i.e. negative when a peak is forecast; divided by the cluster's maximum power
Peak threshold top 10 % of the scenario-1 power top 10 % of the scenario-1 power from July to December 2015 (the notebook skips the first 90 days of the April-December profile)
Total energy, table II e.g. 319 922 kWh for one house ten times too large (p/4*10e-3): 5.463 kW × 5856 h = 31 992 kWh. Percentages are unaffected
Average consumption, table II kW 4 and 6 houses are printed in W
Envelope resistance R_th computed as area/(λ·thickness) = 5000 K/W instead of thickness/(λ·area); the direct envelope loss is therefore negligible and heat leaves through the wall node
Test actions sampled from the policy (model.predict(obs) without deterministic=True)

Other facts worth knowing: the peak threshold is computed on 2015 and applied to 2016, which is why scenario 1 spends 4.3 % of the test above it rather than 10 %; the two "occupant profiles" differ only by their non-HVAC load curve (the set-point is 21 °C all day in both); the non-HVAC load is the profile times 5571 W plus Gaussian noise (σ = 250 W) per house; and the forecaster receives raw, unscaled weather features (°F, %, hour, quarter-hour index 1-96).

From-scratch results

uv run peakrl reproduce retrains the 12 agents and the 4 forecasters and evaluates them, in 25 minutes on 12 laptop cores. Like the paper, it trains each configuration once. It was run with three seeds (--seed 0, 1, 2); cells below read seed 0 / seed 1 / seed 2. The resulting agents, forecasters, learning curves and tables are in pretrained/seed{0,1,2}/; uv run peakrl evaluate --run pretrained/seed1 --seed 1 regenerates the tables of a seed exactly (evaluation is seeded, so pass the seed that matches the directory).

Scenario 1 is reproduced. The baseline consumption is within 2.5 % of the paper and the time spent above the peak threshold within 5 %:

houses scenario 1 average power (kW) paper scenario 1 peak duration (min) paper
1 5.494 / 5.447 / 5.329 5.463 15,540 / 15,735 / 15,405 15,225
2 10.827 / 10.841 / 10.781 10.841 15,090 / 15,060 / 15,435 15,030
4 21.391 / 21.460 / 21.748 21.840 15,240 / 16,020 / 15,525 15,330
6 32.571 / 32.844 / 32.526 32.797 15,030 / 15,105 / 15,240 15,480

The forecasters are reproduced. Test MAPE between 5.1 and 7.1 % over the 12 fits (paper: 5.47 % on average).

The effect of scenarios 2 and 3 is reproduced in direction, not in size. Peak energy decreases in 20 of the 24 trained agents, is unchanged in one and increases in three. The size of the change depends as much on the seed as on the scenario or the cluster, so the per-cluster percentages of table II should be read as one draw from a wide distribution:

houses scenario T mean ± std (°C) paper Δ average power (%) paper Δ peak energy (%) paper
1 1 21.28 ± 0.49 / 21.20 ± 0.43 / 21.05 ± 0.52 21.36 ± 0.37 – – – –
1 2 20.92 ± 0.50 / 21.81 ± 1.20 / 20.21 ± 1.22 21.09 ± 0.53 -3.3 / +7.7 / -4.5 -1.8 -52.5 / +43.9 / -79.5 -36.2
1 3 19.56 ± 2.32 / 19.26 ± 3.11 / 18.51 ± 3.64 21.03 ± 1.24 -11.7 / -12.6 / -12.0 -3.5 -99.2 / -99.9 / -74.6 -73.2
2 1 21.12 ± 0.37 / 21.17 ± 0.37 / 21.09 ± 0.43 21.15 ± 0.33 – – – –
2 2 21.01 ± 0.52 / 21.06 ± 0.40 / 21.20 ± 0.47 20.57 ± 0.89 -1.2 / -0.6 / +0.8 -3.4 -24.3 / -17.2 / +7.5 -63.9
2 3 20.94 ± 0.59 / 20.98 ± 0.68 / 21.15 ± 0.39 20.66 ± 1.20 -1.3 / -1.5 / +0.4 -3.6 -22.3 / -32.7 / +12.1 -60.7
4 1 20.89 ± 0.77 / 20.99 ± 0.65 / 21.17 ± 0.50 21.23 ± 0.60 – – – –
4 2 20.74 ± 1.09 / 21.07 ± 0.86 / 21.05 ± 0.60 20.96 ± 0.99 -0.6 / +0.5 / -0.8 -2.1 -21.0 / +0.0 / -15.8 -44.0
4 3 20.82 ± 0.90 / 20.93 ± 0.72 / 21.13 ± 0.61 21.14 ± 0.82 -0.1 / -0.0 / -0.3 -0.7 -10.4 / -0.8 / -3.6 -12.5
6 1 21.10 ± 1.01 / 21.32 ± 0.77 / 21.08 ± 0.77 21.24 ± 0.80 – – – –
6 2 20.69 ± 1.73 / 21.16 ± 0.81 / 20.70 ± 1.42 20.93 ± 1.30 -2.4 / -0.4 / -2.2 -1.0 -60.3 / -16.1 / -53.3 -27.3
6 3 20.59 ± 1.69 / 21.10 ± 0.90 / 20.92 ± 1.22 20.94 ± 1.00 -2.7 / -0.8 / -0.9 -1.1 -58.7 / -28.2 / -34.1 -27.6

Two things stand out:

  • With one house, scenario 3 removes 75 to 100 % of the peak energy in all three seeds, as in the paper (73 %), but by letting the house get colder (18.5 to 19.6 °C on average, against 21.0 °C in the paper).
  • The learning curves (agents/*.csv) of scenarios 2 and 3 are still rising after 1M steps for four and six houses: the agents are not converged at the paper's training budget, which is a likely source of the run-to-run differences. --timesteps sets the budget.

Provenance

data/ and reference/ were extracted from the original archive by scripts/convert_original.py (needs uv sync --group convert). Only data/Building4.csv (weather; the five building files share it) and data/user_habit_cst.csv were used by the experiments. The discrete-action environment, the PPO notebook and the 8-house models of the archive are not part of the paper and were left out.

Citation

If you use this code, please cite the paper:

@inproceedings{taboga2020peakload,
  author    = {Taboga, Vincent and Bellahsen, Amine and Dagdougui, Hanane},
  title     = {Deep Reinforcement Learning for Peak Load Reduction in Aggregated Residential Houses},
  booktitle = {2020 IEEE Power \& Energy Society General Meeting (PESGM)},
  year      = {2020},
  doi       = {10.1109/PESGM41954.2020.9281946}
}

About

Code related to 'Deep Reinforcement Learning for Peak Load Reduction in Aggregated Residential Houses' (IEEE PESGM 2020)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages