Skip to content

Commit

Permalink
remove old Normalize and refs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahalev committed Dec 27, 2022
1 parent 1a7ee47 commit 812d47a
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 175 deletions.
72 changes: 2 additions & 70 deletions src/pymgrid/modules/base/base_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from pymgrid.utils.logger import ModularLogger
from pymgrid.utils.space import ModuleSpace
from pymgrid.modules.utils.normalize import Normalize, IdentityNormalize
from pymgrid.utils.serialize import add_numpy_pandas_representers, add_numpy_pandas_constructors, dump_data


Expand Down Expand Up @@ -38,59 +37,16 @@ class BaseMicrogridModule(yaml.YAMLObject):
def __init__(self,
raise_errors,
provided_energy_name='provided_energy',
absorbed_energy_name='absorbed_energy',
normalize_pos=...
absorbed_energy_name='absorbed_energy'
):

self.raise_errors = raise_errors
self._current_step = 0
self._obs_normalizer = self._get_normalizer(normalize_pos, obs=True)
self._act_normalizer = self._get_normalizer(normalize_pos, act=True)
self._action_space = self._get_action_spaces()
self._observation_space = self._get_observation_spaces()
self.provided_energy_name, self.absorbed_energy_name = provided_energy_name, absorbed_energy_name
self._logger = ModularLogger()
self.name = (None, None) # set by ModularMicrogrid

def _get_normalizer(self, normalize_pos, obs=False, act=False):
assert obs+act == 1, 'Must be initiating normalizer for obs or act but not both or neither.'
_str = 'obs' if obs else 'act'
try:
if obs:
val_min, val_max = self.min_obs, self.max_obs
else:
val_min, val_max = self.min_act, self.max_act

except AttributeError:
script_logger.debug(f'min_{_str} and max_{_str} attributes not found for module {self.__class__.__name__}. '
f'Returning identity normalizer')

return IdentityNormalize()

try:
normalize_position = normalize_pos['obs' if obs else 'act']
val_min, val_max = val_min[normalize_position], val_max[normalize_position]
except TypeError:
pass

if val_min is None or np.isnan(val_min).any() or val_max is None or np.isnan(val_max).any():
script_logger.debug(f'One of min_{_str} or max_{_str} attributes is None or NaN for module {self.__class__.__name__}. Returni'
f'ng identity normalizer')
return IdentityNormalize()

elif np.isinf(np.array(val_min)).any() or np.isinf(np.array(val_max)).any():
script_logger.debug(f'One of min_{_str} or max_{_str} attributes is Infinity for module {self.__class__.__name__}. Returni'
f'ng identity normalizer')
return IdentityNormalize()
elif isinstance(val_min, np.ndarray) and isinstance(val_max, np.ndarray) and len(val_min) == 0 and len(val_max) == 0:
return IdentityNormalize()

try:
assert val_max >= val_min, f'max_{_str} must be greater than min_{_str}'
except ValueError:
assert (val_max >= val_min).all(), f'max_{_str} must be greater than min_{_str}'

return Normalize(val_min, val_max)
self.name = (None, None) # set by ModularMicrogrid

def _get_action_spaces(self):
unnormalized_low = self.min_act if isinstance(self.min_act, np.ndarray) else np.array([self.min_act])
Expand Down Expand Up @@ -537,30 +493,6 @@ def current_step(self):
"""
return self._current_step

@property
def act_normalizer(self):
"""
Module's action normalizer.
Returns
-------
Normalize
"""
return self._act_normalizer

@property
def obs_normalizer(self):
"""
Module's observation normalizer.
Returns
-------
Normalize
"""
return self._obs_normalizer

@property
@abstractmethod
def min_obs(self):
Expand Down
6 changes: 2 additions & 4 deletions src/pymgrid/modules/base/timeseries/base_timeseries_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def __init__(self,
]
}

super().__init__(raise_errors,
provided_energy_name=provided_energy_name,
absorbed_energy_name=absorbed_energy_name,
normalize_pos=normalize_pos)
super().__init__(raise_errors, provided_energy_name=provided_energy_name,
absorbed_energy_name=absorbed_energy_name)

def _set_time_series(self, time_series):
_time_series = np.array(time_series)
Expand Down
4 changes: 1 addition & 3 deletions src/pymgrid/modules/battery_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ def __init__(self,
self.init_charge, self.init_soc = init_charge, init_soc
self._current_charge, self._soc = self._init_battery(init_charge, init_soc)
self.name = ('battery', None)
super().__init__(raise_errors,
provided_energy_name='discharge_amount',
absorbed_energy_name='charge_amount')
super().__init__(raise_errors, provided_energy_name='discharge_amount', absorbed_energy_name='charge_amount')

def _init_battery(self, init_charge, init_soc):
if init_charge is not None:
Expand Down
5 changes: 1 addition & 4 deletions src/pymgrid/modules/genset_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def __init__(self,
self._steps_until_up, self._steps_until_down = self._reset_up_down_times()
self.name = ('genset', None)

super().__init__(raise_errors,
provided_energy_name=provided_energy_name,
absorbed_energy_name=None,
normalize_pos=dict(obs=..., act=-1))
super().__init__(raise_errors, provided_energy_name=provided_energy_name, absorbed_energy_name=None)

def step(self, action, normalized=True):
goal_status = action[0]
Expand Down
4 changes: 1 addition & 3 deletions src/pymgrid/modules/unbalanced_energy_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def __init__(self,
overgeneration_cost=2.0
):

super().__init__(raise_errors,
provided_energy_name='loss_load',
absorbed_energy_name='overgeneration')
super().__init__(raise_errors, provided_energy_name='loss_load', absorbed_energy_name='overgeneration')
self.loss_load_cost, self.overgeneration_cost = loss_load_cost, overgeneration_cost
self.name = ('unbalanced_energy', None)

Expand Down
Empty file.
91 changes: 0 additions & 91 deletions src/pymgrid/modules/utils/normalize.py

This file was deleted.

0 comments on commit 812d47a

Please sign in to comment.