Logger Wrapper personalization/configuration
We will see on this notebook how to personalize the logger wrapper defined by sinergym.
[1]:
import gym
import numpy as np
import sinergym
from sinergym.utils.wrappers import (LoggerWrapper, MultiObsWrapper,
NormalizeObservation)
from sinergym.utils.constants import RANGES_5ZONE
/usr/local/lib/python3.10/dist-packages/gym/spaces/box.py:73: UserWarning: WARN: Box bound precision lowered by casting to float32
logger.warn(
Step 1 Inherit and modify the CSVloger
First we need to change the CSV logger to modify the values writen into the file on the funtion 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]],
reward: Optional[float],
done: bool,
info: Optional[Dict[str, Any]]) -> List:
if reward is not None:
self.last_10_steps_reward.pop(0)
self.last_10_steps_reward.append(reward)
if info is None: # In a reset
return [0] + list(obs) + list(action) + \
[0, reward, np.mean(self.last_10_steps_reward), None, None, None, done]
else:
return [
info['timestep']] + list(obs) + list(action) + [
info['time_elapsed'],
reward,
np.mean(self.last_10_steps_reward),
info['total_power_no_units'],
info['comfort_penalty'],
info['abs_comfort'],
done]
Step 2 Intanciate 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.variables['observation'] +
env.variables['action'] + ['time (seconds)', 'reward', '10-mean-reward',
'power_penalty', 'comfort_penalty', 'done'])
[2022-08-24 09:20:47,476] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating idf ExternalInterface object if it is not present...
[2022-08-24 09:20:47,477] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating idf Site:Location and SizingPeriod:DesignDay(s) to weather and ddy file...
[2022-08-24 09:20:47,479] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Updating idf OutPut:Variable and variables XML tree model for BVCTB connection.
[2022-08-24 09:20:47,481] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Setting up extra configuration 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.
[4]:
for i in range(1):
obs = env.reset()
rewards = []
done = False
current_month = 0
while not done:
a = env.action_space.sample()
obs, reward, done, 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()
[2022-08-24 09:20:47,833] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:Creating new EnergyPlus simulation episode...
[2022-08-24 09:20:47,845] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus working directory is in /workspaces/sinergym/examples/Eplus-env-demo-v1-res4/Eplus-env-sub_run1
Reward: -0.2841864127941594 {'timestep': 1, 'time_elapsed': 900, 'year': 1991, 'month': 1, 'day': 1, 'hour': 0, 'total_power': 5683.728255883188, 'total_power_no_units': -0.5683728255883188, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [20.09252236706716], 'out_temperature': 1.8, 'action_': [20, 25]}
Reward: -1895.1548592605257 {'timestep': 2976, 'time_elapsed': 2678400, 'year': 1991, 'month': 2, 'day': 1, 'hour': 0, 'total_power': 8380.491234768304, 'total_power_no_units': -0.8380491234768305, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [20.21883172787526], 'out_temperature': -7.0, 'action_': [20, 25]}
Reward: -3633.6399832181337 {'timestep': 5664, 'time_elapsed': 5097600, 'year': 1991, 'month': 3, 'day': 1, 'hour': 0, 'total_power': 978.7532837048899, 'total_power_no_units': -0.097875328370489, 'comfort_penalty': -1.7869800571484404, 'abs_comfort': 1.7869800571484404, 'temperatures': [18.21301994285156], 'out_temperature': 8.1, 'action_': [15, 30]}
Reward: -4757.122309247712 {'timestep': 8640, 'time_elapsed': 7776000, 'year': 1991, 'month': 4, 'day': 1, 'hour': 0, 'total_power': 186.5934720667916, 'total_power_no_units': -0.018659347206679163, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [20.17697338689386], 'out_temperature': 7.7, 'action_': [18, 27]}
Reward: -5552.826655404054 {'timestep': 11520, 'time_elapsed': 10368000, 'year': 1991, 'month': 5, 'day': 1, 'hour': 0, 'total_power': 186.5934720667916, 'total_power_no_units': -0.018659347206679163, 'comfort_penalty': -0.72939902519715, 'abs_comfort': 0.72939902519715, 'temperatures': [19.27060097480285], 'out_temperature': 13.0, 'action_': [17, 28]}
Reward: -6263.697197328473 {'timestep': 14496, 'time_elapsed': 13046400, 'year': 1991, 'month': 6, 'day': 1, 'hour': 0, 'total_power': 386.56918097843, 'total_power_no_units': -0.038656918097843, 'comfort_penalty': -1.7596818210658398, 'abs_comfort': 1.7596818210658398, 'temperatures': [21.24031817893416], 'out_temperature': 18.4, 'action_': [21, 24]}
Reward: -9272.426890062703 {'timestep': 17376, 'time_elapsed': 15638400, 'year': 1991, 'month': 7, 'day': 1, 'hour': 0, 'total_power': 215.8105427091371, 'total_power_no_units': -0.02158105427091371, 'comfort_penalty': -2.199461395033879, 'abs_comfort': 2.199461395033879, 'temperatures': [20.80053860496612], 'out_temperature': 17.7, 'action_': [17, 28]}
Reward: -12578.3581208006 {'timestep': 20352, 'time_elapsed': 18316800, 'year': 1991, 'month': 8, 'day': 1, 'hour': 0, 'total_power': 9159.463676532796, 'total_power_no_units': -0.9159463676532796, 'comfort_penalty': -1.2923781219578103, 'abs_comfort': 1.2923781219578103, 'temperatures': [21.70762187804219], 'out_temperature': 20.6, 'action_': [22, 23]}
Reward: -15903.48380984905 {'timestep': 23328, 'time_elapsed': 20995200, 'year': 1991, 'month': 9, 'day': 1, 'hour': 0, 'total_power': 377.2264848331002, 'total_power_no_units': -0.03772264848331002, 'comfort_penalty': -2.974447422564811, 'abs_comfort': 2.974447422564811, 'temperatures': [20.02555257743519], 'out_temperature': 18.8, 'action_': [20, 25]}
Reward: -18733.26101206708 {'timestep': 26208, 'time_elapsed': 23587200, 'year': 1991, 'month': 10, 'day': 1, 'hour': 0, 'total_power': 186.5934720667916, 'total_power_no_units': -0.018659347206679163, 'comfort_penalty': -0.27470801492377106, 'abs_comfort': 0.27470801492377106, 'temperatures': [19.72529198507623], 'out_temperature': 13.3, 'action_': [17, 28]}
Reward: -19665.3048157153 {'timestep': 29184, 'time_elapsed': 26265600, 'year': 1991, 'month': 11, 'day': 1, 'hour': 0, 'total_power': 2998.063053624272, 'total_power_no_units': -0.2998063053624272, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [21.98445623259406], 'out_temperature': 13.0, 'action_': [22, 23]}
Reward: -20679.519834589526 {'timestep': 32064, 'time_elapsed': 28857600, 'year': 1991, 'month': 12, 'day': 1, 'hour': 0, 'total_power': 6282.049444658835, 'total_power_no_units': -0.6282049444658835, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [20.45374695271586], 'out_temperature': 5.1, 'action_': [22, 22]}
Reward: -22419.718732977875 {'timestep': 35040, 'time_elapsed': 31536000, 'year': 1992, 'month': 1, 'day': 1, 'hour': 0, 'total_power': 12473.66081362551, 'total_power_no_units': -1.2473660813625511, 'comfort_penalty': -0.0, 'abs_comfort': 0.0, 'temperatures': [20.43771976775179], 'out_temperature': -12.0, 'action_': [21, 21]}
Episode 0 Mean reward: -0.639832155621491 Cumulative reward: -22419.718732977875
[2022-08-24 09:21:00,208] EPLUS_ENV_demo-v1_MainThread_ROOT INFO:EnergyPlus simulation closed successfully.