Skip to content
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

[RLlib] Action masking example for new API stack. #46146

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Jun 19, 2024

Why are these changes needed?

This PR adds an example for using action masking in the new API stack to the repository. In addition it makes a small change to the SingleAgentEnvRunner to deal with Dict observation spaces.

Related issue number

Closes #44780 #44452

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

…add_env_reset' calls in 'SingleAgentEnvRunner' to deal with 'Dict' observation spaces.

Signed-off-by: simonsays1980 <[email protected]>
@sven1977 sven1977 changed the title [RLlib] - Action masking example for new API stack. [RLlib] Action masking example for new API stack. Jun 19, 2024
@sven1977 sven1977 marked this pull request as ready for review June 19, 2024 19:35
Copy link
Contributor

@sven1977 sven1977 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super cool PR! One more example script TODO down.

We need to add this example to BUILD as well.

@@ -447,7 +447,7 @@ def _sample_episodes(
obs, infos = self.env.reset()
for env_index in range(self.num_envs):
episodes[env_index].add_env_reset(
observation=obs[env_index],
observation=unbatch(obs)[env_index],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange: Shouldn't this already have caused a bug in the existing flatten obs example (which also uses a dict obs space)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, the flatten obs example silently snuck around it: The unbatch(obs) was only forgotten in _sample_episodes and not in _sample_timesteps (has cost me a lot of time to figure it out thoug). Because the flatten obs does no evaluation - it never ran into it :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, yes, makes perfect sense. Thanks for catching this!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With ray==2.24.0, batch_mode="complete_episodes" will cause the bug. And obs = unbatch(obs) should outter the for-loop like what _sample_timesteps do:

        obs, infos = self.env.reset()
        obs = unbatch(obs)
        for env_index in range(self.num_envs):
            episodes[env_index].add_env_reset(
                observation=obs[env_index],

…ngs and comments. Added action-masking and autoregressive actions examples to the BUILD.

Signed-off-by: simonsays1980 <[email protected]>
…ll be deprecated in very near future.

Signed-off-by: simonsays1980 <[email protected]>
@simonsays1980 simonsays1980 requested a review from a team as a code owner June 20, 2024 14:51
Copy link
Contributor

@sven1977 sven1977 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additional fixes and answers @simonsays1980 . Very nice PR!

Copy link
Collaborator

@aslonnie aslonnie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approval for CI files.

@sven1977 sven1977 enabled auto-merge (squash) June 21, 2024 14:36
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jun 21, 2024
@sven1977 sven1977 merged commit 9d66167 into ray-project:master Jun 21, 2024
8 checks passed
@simonsays1980 simonsays1980 self-assigned this Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RLlib] New API Stack: Action masking not working with wrapper, default encoder config issue
4 participants