21. Logger Wrapper personalization/configuration
We will see on this notebook how to personalize the logger wrapper defined by sinergym.
[1]:
import gymnasium as gym
import numpy as np
import sinergym
from sinergym.utils.wrappers import (LoggerWrapper, MultiObsWrapper,
NormalizeObservation)
21.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
.
[2]:
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,
truncated: 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(hours)',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,
truncated]
21.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.
[3]:
env=gym.make('Eplus-demo-v1')
env=LoggerWrapper(env,logger_class=CustomCSVLogger,monitor_header = ['timestep'] + env.get_wrapper_attr('observation_variables') +
env.get_wrapper_attr('action_variables') + ['time (hours)', 'reward', '10-mean-reward',
'power_penalty', 'comfort_penalty', 'terminated', 'truncated'])
#==============================================================================================#
[ENVIRONMENT] (INFO) : Creating Gymnasium environment... [demo-v1]
#==============================================================================================#
[MODELING] (INFO) : Experiment working directory created [/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042]
[MODELING] (INFO) : runperiod established: {'start_day': 1, 'start_month': 1, 'start_year': 1991, 'end_day': 31, 'end_month': 12, 'end_year': 1991, 'start_weekday': 1, 'n_steps_per_hour': 4}
[MODELING] (INFO) : Episode length (seconds): 31536000.0
[MODELING] (INFO) : timestep size (seconds): 900.0
[MODELING] (INFO) : timesteps per episode: 35040
[MODELING] (INFO) : Model Config is correct.
[REWARD] (INFO) : Reward function initialized.
[ENVIRONMENT] (INFO) : Environment demo-v1 created successfully.
[WRAPPER LoggerWrapper] (INFO) : Wrapper initialized.
Now, you can see in Sinergym output folder that you will have available progress.csv
file and monitor.csv
files in each episode.
[4]:
for i in range(1):
obs, info = env.reset()
rewards = []
truncated = terminated = False
current_month = 0
while not (terminated or truncated):
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()
#----------------------------------------------------------------------------------------------#
[ENVIRONMENT] (INFO) : Starting a new episode... [demo-v1] [Episode 1]
#----------------------------------------------------------------------------------------------#
[MODELING] (INFO) : Episode directory created [/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042/Eplus-env-sub_run1]
[MODELING] (INFO) : Weather file USA_PA_Pittsburgh-Allegheny.County.AP.725205_TMY3.epw used.
[MODELING] (INFO) : Updated building model with whole Output:Variable available names
[MODELING] (INFO) : Updated building model with whole Output:Meter available names
[MODELING] (INFO) : Extra config: runperiod updated to {'apply_weekend_holiday_rule': 'No', 'begin_day_of_month': 1, 'begin_month': 1, 'begin_year': 1991, 'day_of_week_for_start_day': 'Tuesday', 'end_day_of_month': 1, 'end_month': 3, 'end_year': 1991, 'use_weather_file_daylight_saving_period': 'Yes', 'use_weather_file_holidays_and_special_days': 'Yes', 'use_weather_file_rain_indicators': 'Yes', 'use_weather_file_snow_indicators': 'Yes'}
[MODELING] (INFO) : Updated episode length (seconds): 5184000.0
[MODELING] (INFO) : Updated timestep size (seconds): 3600.0
[MODELING] (INFO) : Updated timesteps per episode: 1440
[MODELING] (INFO) : Adapting weather to building model. [USA_PA_Pittsburgh-Allegheny.County.AP.725205_TMY3.epw]
[ENVIRONMENT] (INFO) : Saving episode output path... [/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042/Eplus-env-sub_run1/output]
/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(
[SIMULATOR] (INFO) : Running EnergyPlus with args: ['-w', '/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042/Eplus-env-sub_run1/USA_PA_Pittsburgh-Allegheny.County.AP.725205_TMY3.epw', '-d', '/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042/Eplus-env-sub_run1/output', '/workspaces/sinergym/examples/Eplus-env-demo-v1-res35042/Eplus-env-sub_run1/5ZoneAutoDXVAV.epJSON']
[ENVIRONMENT] (INFO) : Episode 1 started.
[SIMULATOR] (INFO) : handlers initialized.
[SIMULATOR] (INFO) : handlers are ready.
[SIMULATOR] (INFO) : System is ready.
[WRAPPER LoggerWrapper] (INFO) : Creating monitor.csv for current episode (episode 1) if logger is active
Reward: -0.7359103431230598 {'time_elapsed(hours)': 2.5, 'month': 1, 'day': 1, 'hour': 1, 'is_raining': False, 'action': array([18.217491, 27.886911], dtype=float32), 'timestep': 2, 'reward': -0.7359103431230598, 'energy_term': -0.04774477368803297, 'comfort_term': -0.6881655694350268, 'reward_weight': 0.5, 'abs_energy': 954.8954737606593, 'abs_comfort': 1.3763311388700536, 'energy_values': [954.8954737606593], 'temp_values': [18.623668861129946]}
Reward: -183.89998888619655 {'time_elapsed(hours)': 745.3333333333334, 'month': 2, 'day': 1, 'hour': 0, 'is_raining': False, 'action': array([17.045027, 24.73458 ], dtype=float32), 'timestep': 745, 'reward': -0.04774477368803296, 'energy_term': -0.04774477368803296, 'comfort_term': -0.0, 'reward_weight': 0.5, 'abs_energy': 954.8954737606592, 'abs_comfort': 0.0, 'energy_values': [954.8954737606592], 'temp_values': [20.268929570656645]}
Reward: -301.35254046509186 {'time_elapsed(hours)': 1417.3333333333333, 'month': 3, 'day': 1, 'hour': 0, 'is_raining': False, 'action': array([15.840368, 25.000128], dtype=float32), 'timestep': 1417, 'reward': -0.10365036504194708, 'energy_term': -0.020063419083978743, 'comfort_term': -0.08358694595796834, 'reward_weight': 0.5, 'abs_energy': 401.2683816795749, 'abs_comfort': 0.16717389191593668, 'energy_values': [401.2683816795749], 'temp_values': [19.832826108084063]}
Episode 0 Mean reward: -0.21284685762769534 Cumulative reward: -306.49947498388144
[WRAPPER LoggerWrapper] (INFO) : End of episode, recording summary (progress.csv) if logger is active
[ENVIRONMENT] (INFO) : Environment closed. [demo-v1]