Skip to content

Commit

Permalink
Fix ListSpace with empty elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nhuet authored and g-poveda committed Feb 20, 2025
1 parent c52704a commit acca9e6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion skdecide/hub/space/gym/gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import gymnasium as gym
import gymnasium.spaces as gym_spaces
import numpy as np
from gymnasium.spaces.space import T_cov

from skdecide import EnumerableSpace, SamplableSpace, SerializableSpace, T

Expand Down Expand Up @@ -355,7 +356,11 @@ def __init__(self, elements: Iterable[T]) -> None:
elements: The list of elements for creating the Gym Discrete space (gym.spaces.Discrete) to wrap.
"""
self._elements = list(elements)
gym_space = gym_spaces.Discrete(len(self._elements))
if len(self._elements) > 0:
gym_space = gym_spaces.Discrete(len(self._elements))
else:
gym_space = Empty()

super().__init__(gym_space)

def contains(self, x: T) -> bool:
Expand Down Expand Up @@ -550,3 +555,15 @@ def from_unwrapped(self, sample_n: Iterable) -> Iterable[T]:

def __repr__(self):
return f"RepeatedSpace({self._gym_space}, max_len={self.max_len})"


class Empty(gym.spaces.Space):
@property
def is_np_flattenable(self) -> bool:
return False

def sample(self, mask: Any | None = None) -> T_cov:
raise RuntimeError("Cannot sample an empty space.")

def contains(self, x: Any) -> bool:
return False

0 comments on commit acca9e6

Please sign in to comment.