Open
Description
Describe the bug
I was messing around with categorical actions, where one word out of a predefined word set could be selected (e.g. "sit", "walk", "fly"). My first attempt as to define the charset as the whole words and limit the size to 1
S = frozenset(['sit', 'walk', 'fly'])
space = Text(max_length=1, charset=S)
It actually worked, and I was very glad this could be supported. However it seems like the contains implementation for this space has the supposition that all strings in charset have length 1 (which makes sense as its primary use is to store characters). Therefore, space.contains('sit')
always returns False, even if it should be True.
...
def contains(self, x: Any) -> bool:
"""Return boolean specifying if x is a valid member of this space."""
if isinstance(x, str):
if self.min_length <= len(x) <= self.max_length:
return all(c in self.character_set for c in x)
return False
Should Text allow multi-char symbols?
Code example
from gymnasium.spaces import Text
S = frozenset(['sit', 'walk', 'fly'])
space = Text(max_length=1, charset=S)
assert space.contains('sit') # Assertion Error
System info
Gymnasium was installed via pip in a conda environment
Gymnasium version: 1.0.0
OS: Ubuntu 20.04
Python version: 3.11.11
Additional context
No response
Checklist
- I have checked that there is no similar issue in the repo