From 9c37c2d2c1cd545cca1ef04f5704c0908e2c10ec Mon Sep 17 00:00:00 2001 From: Alejandro Campoy Nieves Date: Thu, 27 Jun 2024 12:59:08 +0200 Subject: [PATCH] (v3.3.6) - Sinergym normalization fix for DRL evaluations (#419) * Fix evalLoggerCallback for deactivate normalization update and update mean and var calibration from training env automatically (when NormalizeObservation was activated) * Update Sinergym version from 3.3.5 to 3.3.6 * Fix opyplus update inconsistency * Dockerfile: Updated Ubuntu and Python version (24.04 and 3.12.3) * Added version documentation --- .devcontainer/Dockerfile | 5 +++-- Dockerfile | 5 +++-- INSTALL.md | 1 + docs/source/pages/installation.rst | 2 ++ scripts/train/train_agent.py | 3 ++- sinergym/utils/callbacks.py | 16 ++++++++++++++++ sinergym/utils/common.py | 2 +- sinergym/version.txt | 2 +- 8 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 0e8336fe7c..3105822f68 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,6 +1,6 @@ # Base on nrel/energyplus from Nicholas Long but using # Ubuntu, Python 3.10 and BCVTB -ARG UBUNTU_VERSION=22.04 +ARG UBUNTU_VERSION=24.04 FROM ubuntu:${UBUNTU_VERSION} # Arguments for EnergyPlus version (default values of version 8.6.0 if is not specified) @@ -12,7 +12,8 @@ ARG ENERGYPLUS_SHA=87ed9199d4 ARG SINERGYM_EXTRAS=[extras] # Argument for choosing Python version -ARG PYTHON_VERSION=3.10 +ARG PYTHON_VERSION=3.12 +ENV PIP_BREAK_SYSTEM_PACKAGES=1 # WANDB_API_KEY ARG WANDB_API_KEY diff --git a/Dockerfile b/Dockerfile index 0e8336fe7c..3105822f68 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Base on nrel/energyplus from Nicholas Long but using # Ubuntu, Python 3.10 and BCVTB -ARG UBUNTU_VERSION=22.04 +ARG UBUNTU_VERSION=24.04 FROM ubuntu:${UBUNTU_VERSION} # Arguments for EnergyPlus version (default values of version 8.6.0 if is not specified) @@ -12,7 +12,8 @@ ARG ENERGYPLUS_SHA=87ed9199d4 ARG SINERGYM_EXTRAS=[extras] # Argument for choosing Python version -ARG PYTHON_VERSION=3.10 +ARG PYTHON_VERSION=3.12 +ENV PIP_BREAK_SYSTEM_PACKAGES=1 # WANDB_API_KEY ARG WANDB_API_KEY diff --git a/INSTALL.md b/INSTALL.md index c3988a8d68..a39c57b6d6 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -13,6 +13,7 @@ The table below provides a summary of the versions supported by *Sinergym* acros | **1.9.5** | **22.04 LTS** | **3.10.6** | 9.5.0 | IDF | | **2.4.0** | 22.04 LTS | 3.10.6 | 9.5.0 | **epJSON** | | **2.5.0** | 22.04 LTS | 3.10.6 | **23.1.0** | epJSON | +| **3.3.6** | **24.04 LTS** | **3.12.3** | 23.1.0 | epJSON | We recommend always using the latest version of *Sinergym* that is supported by the container. This approach helps you avoid the complexities of the installation process. However, diff --git a/docs/source/pages/installation.rst b/docs/source/pages/installation.rst index 17f2533fbd..2f17f00f4b 100644 --- a/docs/source/pages/installation.rst +++ b/docs/source/pages/installation.rst @@ -20,6 +20,8 @@ The table below provides a summary of the versions supported by *Sinergym* acros +----------------------+--------------------+--------------------+------------------------+---------------------------+ | **2.5.0** | 22.04 LTS | 3.10.6 | **23.1.0** | epJSON | +----------------------+--------------------+--------------------+------------------------+---------------------------+ +| **3.3.6** | **24.04 LTS** | **3.12.3** | 23.1.0 | epJSON | ++----------------------+--------------------+--------------------+------------------------+---------------------------+ We recommend always using the latest version of *Sinergym* that is supported by the container. This approach helps you avoid the complexities of the installation process. However, diff --git a/scripts/train/train_agent.py b/scripts/train/train_agent.py index 610a53d31f..c53fefa678 100644 --- a/scripts/train/train_agent.py +++ b/scripts/train/train_agent.py @@ -284,7 +284,8 @@ def process_algorithm_parameters(alg_params: dict): # Set up Evaluation and saving best model if conf.get('evaluation'): eval_callback = LoggerEvalCallback( - eval_env, + eval_env=eval_env, + train_env=env, best_model_save_path=eval_env.get_wrapper_attr('workspace_path') + '/best_model/', log_path=eval_env.get_wrapper_attr('workspace_path') + diff --git a/sinergym/utils/callbacks.py b/sinergym/utils/callbacks.py index 6107edf2ad..b13dda4de0 100644 --- a/sinergym/utils/callbacks.py +++ b/sinergym/utils/callbacks.py @@ -241,6 +241,7 @@ class LoggerEvalCallback(EventCallback): def __init__( self, eval_env: Union[gym.Env, VecEnv], + train_env: Union[gym.Env, VecEnv], callback_on_new_best: Optional[BaseCallback] = None, callback_after_eval: Optional[BaseCallback] = None, n_eval_episodes: int = 5, @@ -273,6 +274,7 @@ def __init__( # eval_env = DummyVecEnv([lambda: eval_env]) self.eval_env = eval_env + self.train_env = train_env self.best_model_save_path = best_model_save_path # Logs will be written in ``evaluations.npz`` if log_path is not None: @@ -351,6 +353,7 @@ def _on_step(self) -> bool: "Training and eval env are not wrapped the same way, " "see https://stable-baselines3.readthedocs.io/en/master/guide/callbacks.html#evalcallback " "and warning above.") from e + self._sync_envs() # Reset success rate buffer self._is_success_buffer = [] @@ -489,3 +492,16 @@ def update_child_locals(self, locals_: Dict[str, Any]) -> None: """ if self.callback: self.callback.update_locals(locals_) + + def _sync_envs(self): + # normalization + if is_wrapped( + self.train_env, + NormalizeObservation) and is_wrapped( + self.eval_env, + NormalizeObservation): + self.eval_env.get_wrapper_attr('deactivate_update')() + self.eval_env.get_wrapper_attr('set_mean')( + self.train_env.get_wrapper_attr('mean')) + self.eval_env.get_wrapper_attr('set_var')( + self.train_env.get_wrapper_attr('var')) diff --git a/sinergym/utils/common.py b/sinergym/utils/common.py index 99a574b61d..3222b50a83 100644 --- a/sinergym/utils/common.py +++ b/sinergym/utils/common.py @@ -10,7 +10,7 @@ import pandas as pd import xlsxwriter from eppy.modeleditor import IDF -from opyplus.epm.record import Record +from opyplus.epgm.record import Record import sinergym from sinergym.utils.constants import YEAR diff --git a/sinergym/version.txt b/sinergym/version.txt index 0163af7e86..eedb52bac2 100644 --- a/sinergym/version.txt +++ b/sinergym/version.txt @@ -1 +1 @@ -3.3.5 \ No newline at end of file +3.3.6 \ No newline at end of file