Skip to content

Fix: parallel_api_test accepts action spaces of shapes other than Discrete#1313

Open
duncan-hintz wants to merge 1 commit intoFarama-Foundation:masterfrom
duncan-hintz:master
Open

Fix: parallel_api_test accepts action spaces of shapes other than Discrete#1313
duncan-hintz wants to merge 1 commit intoFarama-Foundation:masterfrom
duncan-hintz:master

Conversation

@duncan-hintz
Copy link
Copy Markdown

Description

The test function parallel_api_test failed and threw an error when testing an environment that had an action space other than Discrete when action masked. This is due to the flattening of the action mask to sample from. This resulted in a single numerical sample consistent with the action mask, which is correct for Discrete but fails for actions spaces such as MultiDiscrete which expect an input as a tuple, e.g. [107, 26] instead of [133]. Motivated by a testing of a custom env that uses a MultiDiscrete action space. Similar issue was found in #1298 where a user reported failing a test due to sampling a Dict space. They ask if there is a proper way to action mask other than the examples, but I believe they masked correctly and the test failed due to this flattening.
New implementation uses the existing sample function from Gymnasium.spaces that takes a mask as a parameter. This will make it easier for users to simply reference the Gymnasium sampling and action-masking examples when creating new environments.
New implementation will not work for Box spaces, as these are unsupported for masking by Gymnasium.

Fixes #1298

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Screenshots

Before:
image
After:
image

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have run pytest -v and no errors are present.
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I solved any possible warnings that pytest -v has generated that are related to my code to the best of my knowledge.
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

parallel_api_test in parallel_test.py to
account for action-masked spaces
that are not Discrete.
New implementation uses the existing sample
function from Gymnasium.spaces that takes a mask
as a parameter.
Previous implementation fails for
MultiDiscrete and Dict spaces (and perhaps others)
as seen in issue Farama-Foundation#1298.
New implementation will not work for Box spaces,
as these are unsupported for masking by
Gymnasium.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Question] How to mask actions in Dict space

2 participants