Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when using DiscreteMicrogridEnv.to_normalized #69

Closed
Alfonso00MA opened this issue Jul 3, 2023 · 2 comments
Closed

TypeError when using DiscreteMicrogridEnv.to_normalized #69

Alfonso00MA opened this issue Jul 3, 2023 · 2 comments

Comments

@Alfonso00MA
Copy link

I get a TypeError when trying to use DiscreteMicrogridEnv.to_normalized in a simple env like the one explained in the Quick Start part of the documentation.

Here the code I am using.

import numpy as np
import pandas as pd

np.random.seed(0)

from pymgrid import Microgrid
from pymgrid.modules import (
    BatteryModule,
    LoadModule,
    RenewableModule,
    GridModule)


small_battery = BatteryModule(min_capacity=10,
                              max_capacity=100,
                              max_charge=50,
                              max_discharge=50,
                              efficiency=0.9,
                              init_soc=0.2)

large_battery = BatteryModule(min_capacity=10,
                              max_capacity=1000,
                              max_charge=10,
                              max_discharge=10,
                              efficiency=0.7,
                              init_soc=0.2)

load_ts = 100+100*np.random.rand(24*90) # random load data in the range [100, 200].
pv_ts = 200*np.random.rand(24*90) # random pv data in the range [0, 200].

load = LoadModule(time_series=load_ts)

pv = RenewableModule(time_series=pv_ts)

grid_ts = [0.2, 0.1, 0.5] * np.ones((24*90, 3))

grid = GridModule(max_import=100,
                  max_export=100,
                  time_series=grid_ts)

modules = [
    small_battery,
    large_battery,
    ('pv', pv),
    load,
    grid]

microgrid = Microgrid(modules)

# Copy-pasted from QuickStart documentation until here

discrete_env = DiscreteMicrogridEnv.from_microgrid(microgrid)
discrete_env.reset()


discrete_env.to_normalized(discrete_env.state_dict(), act=False, obs=True)


As the documentation suggests, I am using a dictionary parameter where "dictionary keys are names of the modules while dictionary values are lists containing an action corresponding to all modules with that name".

And here the error trace:


TypeError Traceback (most recent call last)
Cell In[46], line 55
52 discrete_env.reset()
54 print(discrete_env.state_dict())
---> 55 discrete_env.to_normalized(discrete_env.state_dict(), act=False, obs=True)

File /opt/conda/lib/python3.10/site-packages/pymgrid/microgrid/microgrid.py:424, in Microgrid.to_normalized(self, data_dict, act, obs)
405 """
406 Normalize an action or observation.
407
(...)
421 Normalized action.
422 """
423 assert act + obs == 1, 'One of act or obs must be True but not both.'
--> 424 return {module_name: [module.to_normalized(value, act=act, obs=obs) for module, value in zip(module_list, data_dict[module_name])]
425 for module_name, module_list in self._modules.iterdict() if module_name in data_dict}

File /opt/conda/lib/python3.10/site-packages/pymgrid/microgrid/microgrid.py:424, in (.0)
405 """
406 Normalize an action or observation.
407
(...)
421 Normalized action.
422 """
423 assert act + obs == 1, 'One of act or obs must be True but not both.'
--> 424 return {module_name: [module.to_normalized(value, act=act, obs=obs) for module, value in zip(module_list, data_dict[module_name])]
425 for module_name, module_list in self._modules.iterdict() if module_name in data_dict}

File /opt/conda/lib/python3.10/site-packages/pymgrid/microgrid/microgrid.py:424, in (.0)
405 """
406 Normalize an action or observation.
407
(...)
421 Normalized action.
422 """
423 assert act + obs == 1, 'One of act or obs must be True but not both.'
--> 424 return {module_name: [module.to_normalized(value, act=act, obs=obs) for module, value in zip(module_list, data_dict[module_name])]
425 for module_name, module_list in self._modules.iterdict() if module_name in data_dict}

File /opt/conda/lib/python3.10/site-packages/pymgrid/modules/base/base_module.py:381, in BaseMicrogridModule.to_normalized(self, value, act, obs)
379 return self._action_space.normalize(value)
380 else:
--> 381 return self._observation_space.normalize(value)

File /opt/conda/lib/python3.10/site-packages/pymgrid/utils/space/space.py:331, in ModuleSpace.normalize(self, val)
328 un_low, un_high = self._unnormalized.low, self._unnormalized.high
330 self._shape_check(val, 'normalize')
--> 331 val = self._bounds_check(val, un_low, un_high)
333 normalized = self._normalized.low + (self._norm_spread / self._unnorm_spread) * (val - un_low)
335 try:

File /opt/conda/lib/python3.10/site-packages/pymgrid/utils/space/space.py:356, in ModuleSpace._bounds_check(self, val, low, high)
355 def _bounds_check(self, val, low, high):
--> 356 clipped = np.clip(val, low, high)
358 if self.verbose or not self.clip_vals and (clipped != val).any():
359 warnings.warn(f'Value {val} resides out of expected bounds of value to be normalized: [{low}, {high}].')

File <array_function internals>:180, in clip(*args, **kwargs)

File /opt/conda/lib/python3.10/site-packages/numpy/core/fromnumeric.py:2154, in clip(a, a_min, a_max, out, **kwargs)
2085 @array_function_dispatch(_clip_dispatcher)
2086 def clip(a, a_min, a_max, out=None, **kwargs):
2087 """
2088 Clip (limit) the values in an array.
2089
(...)
2152
2153 """
-> 2154 return _wrapfunc(a, 'clip', a_min, a_max, out=out, **kwargs)

File /opt/conda/lib/python3.10/site-packages/numpy/core/fromnumeric.py:54, in _wrapfunc(obj, method, *args, **kwds)
52 bound = getattr(obj, method, None)
53 if bound is None:
---> 54 return _wrapit(obj, method, *args, **kwds)
56 try:
57 return bound(*args, **kwds)

File /opt/conda/lib/python3.10/site-packages/numpy/core/fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
41 except AttributeError:
42 wrap = None
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
44 if wrap:
45 if not isinstance(result, mu.ndarray):

File /opt/conda/lib/python3.10/site-packages/numpy/core/_methods.py:160, in _clip(a, min, max, out, casting, **kwargs)
157 return _clip_dep_invoke_with_casting(
158 um.maximum, a, min, out=out, casting=casting, **kwargs)
159 else:
--> 160 return _clip_dep_invoke_with_casting(
161 um.clip, a, min, max, out=out, casting=casting, **kwargs)

File /opt/conda/lib/python3.10/site-packages/numpy/core/_methods.py:114, in _clip_dep_invoke_with_casting(ufunc, out, casting, *args, **kwargs)
112 # try to deal with broken casting rules
113 try:
--> 114 return ufunc(*args, out=out, **kwargs)
115 except _exceptions._UFuncOutputCastingError as e:
116 # Numpy 1.17.0, 2019-02-24
117 warnings.warn(
118 "Converting the output of clip from {!r} to {!r} is deprecated. "
119 "Pass casting=\"unsafe\" explicitly to silence this warning, or "
(...)
122 stacklevel=2
123 )

TypeError: '>=' not supported between instances of 'dict' and 'float'

@ahalev
Copy link
Owner

ahalev commented Jul 5, 2023

You need to pass as_run_output=True to state_dict for the dict to be in the correct format (the format of the output of Microgrid.run):

discrete_env.to_normalized(discrete_env.state_dict(as_run_output=True), act=False, obs=True)

{'balancing': [[]], 'battery': [[ 0.2 20. ], [  0.2 200. ]], 'general': [[104.28825061]], 'grid': [[0.2 0.1 0.5 1. ]], 'load': [[-169.64691856]], 'pv': [[65.35866795]]}

@ahalev ahalev closed this as completed Jul 5, 2023
@ahalev
Copy link
Owner

ahalev commented Jul 5, 2023

Flagging this to rename this kwarg for clarity: #72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants