19. Logger Wrapper personalization/configuration

We will see on this notebook how to personalize the logger wrapper defined by sinergym.

[5]:
import gymnasium as gym
import numpy as np
import sinergym
from sinergym.utils.wrappers import (LoggerWrapper, MultiObsWrapper,
                                     NormalizeObservation)
from sinergym.utils.constants import RANGES_5ZONE

19.1. Step 1 Inherit and modify the CSVloger

First we need to change the CSV logger to modify the values written into the file on the function create_row_contents

[6]:

from sinergym.utils.logger import CSVLogger from typing import Any, Dict, Optional, Sequence, Tuple, Union, List class CustomCSVLogger(CSVLogger): def __init__( self, monitor_header: str, progress_header: str, log_progress_file: str, log_file: Optional[str] = None, flag: bool = True): super(CustomCSVLogger, self).__init__(monitor_header,progress_header,log_progress_file,log_file,flag) self.last_10_steps_reward = [0]*10 def _create_row_content( self, obs: List[Any], action: Union[int, np.ndarray, List[Any]], terminated: bool, info: Optional[Dict[str, Any]]) -> List: if info.get('reward') is not None: self.last_10_steps_reward.pop(0) self.last_10_steps_reward.append(info['reward']) return [ info.get('timestep',0)] + list(obs) + list(action) + [ info.get('time_elapsed',0), info.get('reward',None), np.mean(self.last_10_steps_reward), info.get('total_power_no_units'), info.get('comfort_penalty'), info.get('abs_comfort'), terminated]

19.2. Step 2 Instantiate the LoggerWrapper

now we need to instantiate the loggerwrapper and specify the new headers of our file and the csvlogger class we want to use.

[7]:
env=gym.make('Eplus-demo-v1')
env=LoggerWrapper(env,logger_class=CustomCSVLogger,monitor_header = ['timestep'] + env.variables['observation'] +
                env.variables['action'] + ['time (seconds)', 'reward', '10-mean-reward',
                'power_penalty', 'comfort_penalty', 'done'])
[2023-05-26 08:49:39,767] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating Building model ExternalInterface object if it is not present...
[2023-05-26 08:49:39,767] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating Building model ExternalInterface object if it is not present...
[2023-05-26 08:49:39,768] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating Building model Site:Location and SizingPeriod:DesignDay(s) to weather and ddy file...
[2023-05-26 08:49:39,768] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating Building model Site:Location and SizingPeriod:DesignDay(s) to weather and ddy file...
[2023-05-26 08:49:39,770] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating building model OutPut:Variable and variables XML tree model for BVCTB connection.
[2023-05-26 08:49:39,770] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating building model OutPut:Variable and variables XML tree model for BVCTB connection.
[2023-05-26 08:49:39,771] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Setting up extra configuration in building model if exists...
[2023-05-26 08:49:39,771] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Setting up extra configuration in building model if exists...
[2023-05-26 08:49:39,771] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Setting up action definition in building model if exists...
[2023-05-26 08:49:39,771] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Setting up action definition in building model if exists...

Now, you can see in Sinergym output folder that you will have available progress.csv file and monitor.csv files in each episode.

[8]:
for i in range(1):
    obs, info = env.reset()
    rewards = []
    terminated = False
    current_month = 0
    while not terminated:
        a = env.action_space.sample()
        obs, reward, terminated, truncated, info = env.step(a)
        rewards.append(reward)
        if info['month'] != current_month:  # display results every month
            current_month = info['month']
            print('Reward: ', sum(rewards), info)
    print('Episode ', i, 'Mean reward: ', np.mean(
        rewards), 'Cumulative reward: ', sum(rewards))
env.close()
[2023-05-26 08:49:39,868] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Creating new EnergyPlus simulation episode...
[2023-05-26 08:49:39,868] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Creating new EnergyPlus simulation episode...
[2023-05-26 08:49:39,983] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus working directory is in /workspaces/sinergym/examples/Eplus-env-demo-v1-res4/Eplus-env-sub_run1
[2023-05-26 08:49:39,983] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus working directory is in /workspaces/sinergym/examples/Eplus-env-demo-v1-res4/Eplus-env-sub_run1
/usr/local/lib/python3.10/dist-packages/opyplus/weather_data/weather_data.py:493: FutureWarning: the 'line_terminator'' keyword is deprecated, use 'lineterminator' instead.
  epw_content = self._headers_to_epw(use_datetimes=use_datetimes) + df.to_csv(
Reward:  -0.830348444547302 {'timestep': 1, 'time_elapsed': 900, 'year': 1991, 'month': 1, 'day': 1, 'hour': 0, 'action': [22, 22], 'reward': -0.830348444547302, 'reward_energy': -1.660696889094604, 'reward_comfort': -0.0, 'total_energy': 16606.96889094604, 'abs_comfort': 0.0, 'temperatures': [21.96118062548524]}
Reward:  -1939.4734928711111 {'timestep': 2976, 'time_elapsed': 2678400, 'year': 1991, 'month': 2, 'day': 1, 'hour': 0, 'action': [18, 27], 'reward': -1.3496876205409685, 'reward_energy': -0.6809266783718777, 'reward_comfort': -2.0184485627100592, 'total_energy': 6809.266783718776, 'abs_comfort': 2.0184485627100592, 'temperatures': [17.98155143728994]}
Reward:  -3933.292969923887 {'timestep': 5664, 'time_elapsed': 5097600, 'year': 1991, 'month': 3, 'day': 1, 'hour': 0, 'action': [17, 28], 'reward': -0.4423907986821893, 'reward_energy': -0.07470093861900816, 'reward_comfort': -0.8100806587453704, 'total_energy': 747.0093861900816, 'abs_comfort': 0.8100806587453704, 'temperatures': [19.18991934125463]}
Reward:  -5209.694205226271 {'timestep': 8640, 'time_elapsed': 7776000, 'year': 1991, 'month': 4, 'day': 1, 'hour': 0, 'action': [21, 24], 'reward': -0.3403952831281551, 'reward_energy': -0.6807905662563102, 'reward_comfort': -0.0, 'total_energy': 6807.905662563102, 'abs_comfort': 0.0, 'temperatures': [21.01577085247379]}
Reward:  -6129.786035211225 {'timestep': 11520, 'time_elapsed': 10368000, 'year': 1991, 'month': 5, 'day': 1, 'hour': 0, 'action': [22, 22], 'reward': -0.21151713282790238, 'reward_energy': -0.42303426565580476, 'reward_comfort': -0.0, 'total_energy': 4230.342656558048, 'abs_comfort': 0.0, 'temperatures': [22.0005172479412]}
Reward:  -6923.35654386654 {'timestep': 14496, 'time_elapsed': 13046400, 'year': 1991, 'month': 6, 'day': 1, 'hour': 0, 'action': [22, 22], 'reward': -0.7077540598512679, 'reward_energy': -0.40819585123366664, 'reward_comfort': -1.0073122684688691, 'total_energy': 4081.958512336666, 'abs_comfort': 1.0073122684688691, 'temperatures': [21.99268773153113]}
Reward:  -9185.974719868711 {'timestep': 17376, 'time_elapsed': 15638400, 'year': 1991, 'month': 7, 'day': 1, 'hour': 0, 'action': [17, 28], 'reward': -0.9071546622746904, 'reward_energy': -0.0077768882715716125, 'reward_comfort': -1.8065324362778092, 'total_energy': 77.76888271571612, 'abs_comfort': 1.8065324362778092, 'temperatures': [21.19346756372219]}
Reward:  -11638.751082560155 {'timestep': 20352, 'time_elapsed': 18316800, 'year': 1991, 'month': 8, 'day': 1, 'hour': 0, 'action': [22, 22], 'reward': -1.3316746509137833, 'reward_energy': -1.483453734303027, 'reward_comfort': -1.1798955675245395, 'total_energy': 14834.53734303027, 'abs_comfort': 1.1798955675245395, 'temperatures': [21.82010443247546]}
Reward:  -14045.633847103463 {'timestep': 23328, 'time_elapsed': 20995200, 'year': 1991, 'month': 9, 'day': 1, 'hour': 0, 'action': [21, 21], 'reward': -1.0123429118514469, 'reward_energy': -0.02472531084889493, 'reward_comfort': -1.999960512853999, 'total_energy': 247.2531084889493, 'abs_comfort': 1.999960512853999, 'temperatures': [21.000039487146]}
Reward:  -16369.721380758147 {'timestep': 26208, 'time_elapsed': 23587200, 'year': 1991, 'month': 10, 'day': 1, 'hour': 0, 'action': [22, 22], 'reward': -0.47218446810824116, 'reward_energy': -0.9443689362164823, 'reward_comfort': -0.0, 'total_energy': 9443.689362164823, 'abs_comfort': 0.0, 'temperatures': [21.96825068629957]}
Reward:  -17684.768171663854 {'timestep': 29184, 'time_elapsed': 26265600, 'year': 1991, 'month': 11, 'day': 1, 'hour': 0, 'action': [21, 24], 'reward': -0.2166495718253857, 'reward_energy': -0.4332991436507714, 'reward_comfort': -0.0, 'total_energy': 4332.991436507714, 'abs_comfort': 0.0, 'temperatures': [20.98067880717298]}
Reward:  -18858.210940693767 {'timestep': 32064, 'time_elapsed': 28857600, 'year': 1991, 'month': 12, 'day': 1, 'hour': 0, 'action': [19, 26], 'reward': -0.15978920038153083, 'reward_energy': -0.02546548051228989, 'reward_comfort': -0.29411292025077174, 'total_energy': 254.6548051228989, 'abs_comfort': 0.29411292025077174, 'temperatures': [19.70588707974923]}
Reward:  -20587.4981381546 {'timestep': 35040, 'time_elapsed': 31536000, 'year': 1992, 'month': 1, 'day': 1, 'hour': 0, 'action': [21, 24], 'reward': -0.980047007751289, 'reward_energy': -1.960094015502578, 'reward_comfort': -0.0, 'total_energy': 19600.94015502578, 'abs_comfort': 0.0, 'temperatures': [20.98596614281954]}
Episode  0 Mean reward:  -0.587542755084327 Cumulative reward:  -20587.4981381546
[2023-05-26 08:49:53,585] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus simulation closed successfully.
[2023-05-26 08:49:53,585] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus simulation closed successfully.