Skip to content

Commit

Permalink
(v3.6.8) - OU process migration (#457)
Browse files Browse the repository at this point in the history
* Added ou process function in common module

* Create test for ou process in test_common.py

* Adapt modeling module to apply weather variability with this function

* Weather Forecasting wrapper: Miograted ou process with common module call. Added observation variables, updated tests, improved some implementations aspects

* EnergyCost wrapper: Migrated ou process with common module call. Update tests and improved some implementation aspects

* Wrappers: Improved redifinition of observation spaces, now read from old observation spaces relevant information

* Fix code

* Update Sinergym version from 3.6.7 to 3.6.8
  • Loading branch information
AlejandroCN7 authored Nov 14, 2024
1 parent 87e8def commit a0ba77e
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 203 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package-mode = true
name = "sinergym"

version = "3.6.7"
version = "3.6.8"
description = "The goal of sinergym is to create an environment following OpenAI Gym interface for wrapping simulation engines for building control using deep reinforcement learning."
license = "MIT"

Expand Down
35 changes: 7 additions & 28 deletions sinergym/config/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from eppy.modeleditor import IDF
from epw.weather import Weather

from sinergym.utils.common import eppy_element_to_dict, get_delta_seconds
from sinergym.utils.common import (eppy_element_to_dict, get_delta_seconds,
ornstein_uhlenbeck_process)
from sinergym.utils.constants import (CWD, LOG_MODEL_LEVEL, PKG_DATA_PATH,
WEEKDAY_ENCODING, YEAR)
from sinergym.utils.logger import TerminalLogger
Expand Down Expand Up @@ -326,38 +327,16 @@ def apply_weather_variability(
Returns:
str: New EPW file path generated in simulator working path in that episode or current EPW path if variation is not defined.
"""
# deepcopy for weather_data
weather_data_mod = deepcopy(self.weather_data)

filename = self._weather_path.split('/')[-1]
weather_data_mod = deepcopy(self.weather_data)

# Apply variation to EPW if exists
if weather_variability is not None:

T = 1. # Total time.
# All the columns are going to have the same num of rows since they are
# in the same dataframe
# get first column of df
n = weather_data_mod.dataframe.shape[0]
dt = T / n
# t = np.linspace(0., T, n) # Vector of times.

for variable, variation in weather_variability.items():

sigma = variation[0] # Standard deviation.
mu = variation[1] # Mean.
tau = variation[2] # Time constant.

sigma_bis = sigma * np.sqrt(2. / tau)
sqrtdt = np.sqrt(dt)

# Create noise
noise = np.zeros(n)
for i in range(n - 1):
noise[i + 1] = noise[i] + dt * (-(noise[i] - mu) / tau) + \
sigma_bis * sqrtdt * np.random.randn()

# Add noise
weather_data_mod.dataframe[variable] += noise
weather_data_mod.dataframe = ornstein_uhlenbeck_process(
data=self.weather_data.dataframe,
variability_config=weather_variability)

self.logger.info(
'Weather noise applied in columns: {}'.format(
Expand Down
48 changes: 48 additions & 0 deletions sinergym/utils/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Common utilities."""

from copy import deepcopy
from datetime import datetime, timedelta
from typing import Any, Dict, Optional, Tuple, Type, Union

import gymnasium as gym
import numpy as np
import pandas as pd
import xlsxwriter
from eppy.modeleditor import IDF

Expand Down Expand Up @@ -180,6 +182,52 @@ def export_schedulers_to_excel(
object_num += 1
workbook.close()

# ------------------------ Ornstein Unhelbeck Process ------------------------ #


def ornstein_uhlenbeck_process(
data: pd.DataFrame,
variability_config: Dict[str, Tuple[float, float, float]]) -> pd.DataFrame:
"""Add noise to the data using the Ornstein-Uhlenbeck process.
Args:
data (pd.DataFrame): Data to be modified.
variability_config (Dict[str, Tuple[float, float, float]]): Dictionary with the variability configuration for each variable (sigma, mu and tau constants).
Returns:
pd.DataFrame: Data with noise added.
"""

# deepcopy for weather_data
data_mod = deepcopy(data)

# Total time.
T = 1.
# get first column of df
n = data_mod.shape[0]
dt = T / n
# t = np.linspace(0., T, n) # Vector of times.

for variable, variation in variability_config.items():

sigma = variation[0] # Standard deviation.
mu = variation[1] # Mean.
tau = variation[2] # Time constant.

sigma_bis = sigma * np.sqrt(2. / tau)
sqrtdt = np.sqrt(dt)

# Create noise
noise = np.zeros(n)
for i in range(n - 1):
noise[i + 1] = noise[i] + dt * (-(noise[i] - mu) / tau) + \
sigma_bis * sqrtdt * np.random.randn()

# Add noise
data_mod[variable] += noise

return data_mod

# ------------------ Reading JSON environment configuration ------------------ #


Expand Down
2 changes: 1 addition & 1 deletion sinergym/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def write(
# Log all metrics
wandb.log(metrics_to_log)
except ImportError:
class WandBOutputFormat():
class WandBOutputFormat(): # pragma: no cover
"""WandBOutputFormat class for logging in WandB from SB3 logger.
"""

Expand Down
Loading

0 comments on commit a0ba77e

Please sign in to comment.