Skip to content

[Bug Report] Text Space has limited support for multi-char symbols #1317

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

Open
1 task done
DanielGardin opened this issue Feb 23, 2025 · 1 comment
Open
1 task done
Labels
bug Something isn't working

Comments

@DanielGardin
Copy link

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
@DanielGardin DanielGardin added the bug Something isn't working label Feb 23, 2025
@pseudo-rnd-thoughts
Copy link
Member

Thanks for the issue

Interestingly, this is not how the space was meant to be used. The intention is that each element of the charset has length == 1.

If you want categorical actions, I would recommend using Discrete with some converter wrapper to change between the categorical words and discrete values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants