Issue Type
Select one:
[x] Bug report
[] Question
[] Feature Request
[] Other
Fill these out:
- python version: 3.12.3
- python-microgrid version: 1.4.1
Bug Report
Calling microgrid.sample_action(strict_bound=True) on any microgrid that contains a GensetModule raises a TypeError at runtime.
This can be reproduced with the example in the README:
import numpy as np
from pymgrid import Microgrid
from pymgrid.modules import GensetModule, BatteryModule, LoadModule, RenewableModule
genset = GensetModule(running_min_production=10,
running_max_production=50,
genset_cost=0.5)
battery = BatteryModule(min_capacity=0,
max_capacity=100,
max_charge=50,
max_discharge=50,
efficiency=1.0,
init_soc=0.5)
renewable = RenewableModule(time_series=50*np.random.rand(100))
load = LoadModule(time_series=60*np.random.rand(100))
microgrid = Microgrid([genset, battery, ("pv", renewable), load])
for j in range(10):
action = microgrid.sample_action(strict_bound=True)
microgrid.step(action)
This throws a TypeError:
Traceback (most recent call last):
File "reproduce_bug.py", line 30, in <module>
action = microgrid.sample_action(strict_bound=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/pymgrid/microgrid/microgrid.py", line 381, in sample_action
return {module_name: [module.sample_action(strict_bound=strict_bound) for module in module_list]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/pymgrid/modules/genset_module.py", line 364, in sample_action
return np.array([np.random.rand(), super().sample_action(strict_bound=strict_bound)])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/pymgrid/modules/base/base_module.py", line 357, in sample_action
max_bound = self._action_space.normalize(self.max_production)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/pymgrid/utils/space/space.py", line 330, in normalize
self._shape_check(val, 'normalize')
File "src/pymgrid/utils/space/space.py", line 170, in _shape_check
raise TypeError(f'Unable to {func} scalar value, expected array-like of shape {len(low)}')
TypeError: Unable to normalize scalar value, expected array-like of shape 2
Microgrid executes sample_action for the Genset module with a value between 10 and 50 and on_off 0 or 1.
GensetModule has a 2-dimensional action space — [on_off_flag, power_setpoint] — but its sample_action delegates the power component to super().sample_action() (i.e. BaseModule), which expects a scalar action space. When BaseModule tries to normalize self.max_production (a scalar) against an action space of shape (2,), the shape check fails.
Issue Type
Select one:
[x] Bug report
[] Question
[] Feature Request
[] Other
Fill these out:
Bug Report
Bug Description:
Calling
microgrid.sample_action(strict_bound=True)on any microgrid that contains aGensetModuleraises aTypeErrorat runtime.Steps to reproduce:
This can be reproduced with the example in the README:
This throws a TypeError:
Expected behavior:
Microgrid executes sample_action for the
Gensetmodule with a value between 10 and 50 and on_off 0 or 1.Other context:
GensetModulehas a 2-dimensional action space —[on_off_flag, power_setpoint]— but itssample_actiondelegates the power component tosuper().sample_action()(i.e.BaseModule), which expects a scalar action space. WhenBaseModuletries to normalizeself.max_production(a scalar) against an action space of shape(2,), the shape check fails.