Skip to content

Commit

Permalink
(v3.0.4) - Sinergym minor Fixes (#360)
Browse files Browse the repository at this point in the history
* Controller RBC 5zone: Summer setpoints updated to the default action space

* updated /bin/python3 /workspaces/sinergym/try_env.py

* Updated try_env.py with code comments

* Fixed logger bug

* Added logger warning in documentation

* env.__str__ changed to env.info(), print(env) now print the env type with all wrappers included

* updated Sinergym version from 3.0.3 to 3.0.4
  • Loading branch information
AlejandroCN7 authored Sep 12, 2023
1 parent 125d312 commit 3a159ad
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 34 deletions.
4 changes: 4 additions & 0 deletions docs/source/pages/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ present as a wrapper attribute and is called in each timestep and
in the end of a episode. This class can be substitute by a new one,
see :ref:`Logger Wrapper personalization/configuration`.

.. warning:: The ``CSVLogger`` requires the info dict with specific keys to
log the information correctly. If you change the info dict structure
in Sinergym, you should check this logger or to use a custom one.

.. note:: Normalized observation methods are only used when environment is
wrapped with normalization previously.

Expand Down
2 changes: 1 addition & 1 deletion examples/getting_env_information.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@
}
],
"source": [
"print(env)"
"env.info()"
]
}
],
Expand Down
5 changes: 4 additions & 1 deletion scripts/try_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import numpy as np

import sinergym
from sinergym.utils.wrappers import LoggerWrapper
from sinergym.utils.wrappers import LoggerWrapper, NormalizeObservation
from gymnasium.wrappers.normalize import NormalizeReward

env = gym.make('Eplus-demo-v1')
env = NormalizeObservation(env)
env = NormalizeReward(env)
env = LoggerWrapper(env)

for i in range(1):
Expand Down
5 changes: 3 additions & 2 deletions sinergym/envs/eplus_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ def idd_path(self) -> str:

# -------------------------------- class print ------------------------------- #

def __str__(self):
return """
def info(self):
print("""
#==================================================================================#
ENVIRONMENT NAME: {}
#==================================================================================#
Expand Down Expand Up @@ -811,3 +811,4 @@ def __str__(self):
self.actuator_handlers,
self.var_handlers,
self.meter_handlers)
)
2 changes: 1 addition & 1 deletion sinergym/utils/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, env: EplusEnv) -> None:
self.observation_variables = env.observation_variables
self.action_varioables = env.action_variables

self.setpoints_summer = (23, 26.0)
self.setpoints_summer = (22.5, 26.0)
self.setpoints_winter = (20.0, 23.5)

def act(self, observation: List[Any]) -> Sequence[Any]:
Expand Down
45 changes: 23 additions & 22 deletions sinergym/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CSVLogger(object):
:param flag: This flag is used to activate (True) or deactivate (False) Logger in real time.
:param steps_data, rewards, powers, etc: These arrays are used to record steps data to elaborate main data for progress.csv later.
:param total_timesteps: Current episode timesteps executed.
:param total_time_elapsed: Current episode time elapsed (simulation seconds).
:param total_time_elapsed: Current episode time elapsed (simulation hours).
:param comfort_violation_timesteps: Current episode timesteps whose comfort_penalty!=0.
:param steps_data: It is a array of str's. Each element belong to a step data.
Expand All @@ -94,6 +94,15 @@ def __init__(
log_progress_file: str,
log_file: Optional[str] = None,
flag: bool = True):
"""CSVLogger constructor
Args:
monitor_header (str): CSV header for sub_run_N/monitor.csv which record interaction step by step.
progress_header (str): CSV header for res_N/progress.csv which record main data episode by episode.
log_progress_file (str): log_file path for progress.csv, there will be only one CSV per whole simulation.
log_file (Optional[str], optional): log_file path for monitor.csv, there will be one CSV per episode. Defaults to None.
flag (bool, optional): Activate (True) or deactivate (False) Logger in real time. Defaults to True.
"""

self.monitor_header = monitor_header
self.progress_header = progress_header + '\n'
Expand Down Expand Up @@ -157,27 +166,19 @@ def _store_step_information(
"""
# In reset, info this keys are not available
if info.get('reward'):
self.episode_data['rewards'].append(info.get('reward'))
if info.get('abd_energy'):
self.episode_data['powers'].append(info.get('abs_energy'))
if info.get('comfort_term'):
self.episode_data['comfort_penalties'].append(
info.get('comfort_term'))
if info.get('energy_term') is not None:
self.episode_data['power_penalties'].append(
info.get('energy_term'))
if info.get('abs_comfort') is not None:
self.episode_data['abs_comfort'].append(
info.get('abs_comfort'))
if info.get('abs_energy') is not None:
self.episode_data['abs_energy'].append(
info.get('abs_energy'))
if info.get('comfort_term') != 0:
self.episode_data['comfort_violation_timesteps'] += 1
self.episode_data['total_timesteps'] = info.get('timestep')
self.episode_data['total_time_elapsed'] = info.get('time_elapsed')
# In reset (timestep=1), some keys are not available in info
if info['timestep'] > 1:

self.episode_data['rewards'].append(info['reward'])
self.episode_data['powers'].append(info['abs_energy'])
self.episode_data['comfort_penalties'].append(info['comfort_term'])
self.episode_data['power_penalties'].append(info['energy_term'])
self.episode_data['abs_comfort'].append(info['abs_comfort'])
self.episode_data['abs_energy'].append(info['abs_energy'])
if info['comfort_term'] != 0:
self.episode_data['comfort_violation_timesteps'] += 1
self.episode_data['total_time_elapsed'] = info['time_elapsed(hours)']
self.episode_data['total_timesteps'] = info['timestep']

def _reset_logger(self) -> None:
"""Reset relevant data to next episode summary in progress.csv.
Expand Down
6 changes: 3 additions & 3 deletions sinergym/utils/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __init__(
super(LoggerWrapper, self).__init__(env)
# Headers for csv logger
monitor_header_list = monitor_header if monitor_header is not None else ['timestep'] + env.observation_variables + env.action_variables + [
'time (hours)', 'reward', 'power_penalty', 'comfort_penalty', 'abs_comfort', 'abs_energy', 'terminated']
'time (hours)', 'reward', 'energy_penalty', 'comfort_penalty', 'abs_comfort', 'abs_energy', 'terminated']
self.monitor_header = ''
for element_header in monitor_header_list:
self.monitor_header += element_header + ','
Expand All @@ -229,8 +229,8 @@ def __init__(
'mean_comfort_violation',
'std_comfort_violation',
'cumulative_comfort_violation',
'length(timesteps)',
'time_elapsed(seconds)']
'length (timesteps)',
'time_elapsed (hours)']
self.progress_header = ''
for element_header in progress_header_list:
self.progress_header += element_header + ','
Expand Down
2 changes: 1 addition & 1 deletion sinergym/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.3
3.0.4
16 changes: 13 additions & 3 deletions try_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@
import numpy as np

import sinergym
from sinergym.utils.wrappers import LoggerWrapper
from sinergym.utils.wrappers import LoggerWrapper, NormalizeObservation

env = gym.make('Eplus-office-hot-continuous-v1')
# Creating environment and applying wrappers for normalization and logging
env = gym.make('Eplus-5zone-hot-continuous-stochastic-v1')
env = NormalizeObservation(env)
env = LoggerWrapper(env)

for i in range(1):
# Execute interactions during 3 episodes
for i in range(3):
# Reset the environment to start a new episode
obs, info = env.reset()
rewards = []
terminated = False
current_month = 0
while not terminated:
# Random action control
a = env.action_space.sample()
# Read observation and reward
obs, reward, terminated, truncated, info = env.step(a)
rewards.append(reward)
# If this timestep is a new month start
if info['month'] != current_month: # display results every month
current_month = info['month']
# Print information
print('Reward: ', sum(rewards), info)
# Final episode information print
print(
'Episode ',
i,
'Mean reward: ',
np.mean(rewards),
'Cumulative reward: ',
sum(rewards))
# Close the environment
env.close()

0 comments on commit 3a159ad

Please sign in to comment.