Skip to content

GensetModule.sample_action() raises TypeError when called via Microgrid.sample_action(strict_bound=True) #119

Description

@petermllrr

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

  • Bug Description:

Calling microgrid.sample_action(strict_bound=True) on any microgrid that contains a GensetModule raises a TypeError at runtime.

  • Steps to reproduce:

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
  • Expected behavior:

Microgrid executes sample_action for the Genset module with a value between 10 and 50 and on_off 0 or 1.

  • Other context:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions