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

[Feature] Add OpenSpielWrapper and OpenSpielEnv #2345

Merged
merged 1 commit into from
Sep 2, 2024

Conversation

kurtamohler
Copy link
Collaborator

@kurtamohler kurtamohler commented Aug 1, 2024

Description

Adds environment wrapper classes for OpenSpiel.

OpenSpielWrapper.reset supports resetting to a specified state.

Motivation and Context

Part of #2133

  • Someone has raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@kurtamohler kurtamohler requested a review from vmoens August 1, 2024 06:35
Copy link

pytorch-bot bot commented Aug 1, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/2345

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures, 7 Unrelated Failures

As of commit 1c90d04 with merge base e82a69f (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 1, 2024
@kurtamohler
Copy link
Collaborator Author

kurtamohler commented Aug 1, 2024

A few notes:

categorical_action_encoding=False is not supported yet.

Also, some of the games in OpenSpiel do not work properly in OpenSpielWrapper because the action spec assumes a discrete space of pyspiel.Game.num_distinct_actions() (see OpenSpiel API reference). However, for some of the games, pyspiel.State.legal_actions() can return more actions than pyspiel.Game.num_distinct_actions(). I suppose to support those games we need to allow the action spec's size to change at each step?

@kurtamohler kurtamohler force-pushed the openspiel-env-0 branch 2 times, most recently from 2ca9acf to da9ad79 Compare August 1, 2024 07:22
@kurtamohler kurtamohler added the Environments Adds or modifies an environment wrapper label Aug 1, 2024
@kurtamohler kurtamohler force-pushed the openspiel-env-0 branch 2 times, most recently from a55b54e to 3aa6ed9 Compare August 1, 2024 07:32
@vmoens vmoens changed the title Add OpenSpielWrapper and OpenSpielEnv [Feature] Add OpenSpielWrapper and OpenSpielEnv Aug 2, 2024
Copy link
Contributor

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

Great work thanks for that, I left a couple of comments already.
Is the TODO blocking on this?

torchrl/envs/libs/openspiel.py Show resolved Hide resolved
torchrl/envs/libs/openspiel.py Outdated Show resolved Hide resolved
torchrl/envs/libs/openspiel.py Outdated Show resolved Hide resolved
done = torch.tensor(
self._env.is_terminal(), device=self.device, dtype=torch.bool
)
state = self._env.serialize()
Copy link
Contributor

Choose a reason for hiding this comment

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

let's put a flag around that to make the state optional. Maybe "stateless=True"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What if we call the flag output_state? I think that name makes it more obvious what exactly it does--it adds the state to the output of reset and step

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the output_state flag, let me know if you'd rather call it stateless or something else

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 renamed to return_state, because that's what PettingZooWrapper calls it

torchrl/envs/libs/openspiel.py Outdated Show resolved Hide resolved
torchrl/envs/libs/openspiel.py Show resolved Hide resolved
torchrl/envs/libs/openspiel.py Outdated Show resolved Hide resolved
torchrl/envs/libs/openspiel.py Show resolved Hide resolved
@kurtamohler
Copy link
Collaborator Author

kurtamohler commented Aug 15, 2024

At the moment, this only supports games where all actions are taken by the players, like in chess or tic-tac-toe. But I've realized that OpenSpiel also has a concept of chance nodes, where a random non-player action is taken. For instance, in Kuhn poker, the initial dealing of the cards is a chance node. In liar's dice, the outcome of rolling dice is a chance node. OpenSpiel has some methods to obtain all the possible chance actions and the associated probability distribution (shown in this example).

For now, I'll raise an error if a loaded game contains chance nodes and leave it as a TODO. I might wait to add support for it in a follow-up PR--unless you would prefer for me to add it in this PR.

@vmoens
Copy link
Contributor

vmoens commented Aug 27, 2024

I suppose to support those games we need to allow the action spec's size to change at each step?

Yes I think having a dynamic space would be the way to go. See #2143

@vmoens
Copy link
Contributor

vmoens commented Aug 27, 2024

OpenSpiel has some methods to obtain all the possible chance actions and the associated probability distribution (shown in this example).

That's an amazing feature. Happy to integrate it separately!

@kurtamohler
Copy link
Collaborator Author

I suppose to support those games we need to allow the action spec's size to change at each step?

Yes I think having a dynamic space would be the way to go. See #2143

Actually, I'm not so sure that we would need dynamic action specs after all. It is true that pyspiel.State.legal_actions() can return a different number of actions than pyspiel.Game.num_distinct_actions(), but I'm pretty sure (not 100% sure) that only happens when it's a chance node, in which case the length of legal_actions() is pyspiel.Game.max_chance_outcomes() instead. Once I add support for chance nodes, it will have its own action spec separate from the players' action specs, and I think all action specs will maintain the same shape throughout the game.

@kurtamohler
Copy link
Collaborator Author

I think I've addressed everything that needed to be fixed so far. Let me know if there is anything else

Copy link
Contributor

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

Amazing thanks a mil for this!

@vmoens vmoens merged commit 94ef901 into pytorch:main Sep 2, 2024
67 of 77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Environments Adds or modifies an environment wrapper
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Integration for OpenSpiel environments [Feature Request] Support Card-Game Environment
4 participants