-
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Wrappers Documentation (#942)
Co-authored-by: lettera <[email protected]>
- Loading branch information
1 parent
01427b5
commit 882cf84
Showing
12 changed files
with
197 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: Wrapper | ||
--- | ||
|
||
# Wrappers | ||
|
||
## Using Wrappers | ||
|
||
A wrapper is an environment transformation that takes in an environment as input, and outputs a new environment that is similar to the input environment, but with some transformation or validation applied. | ||
|
||
The following wrappers can be used with PettingZoo environments: | ||
|
||
|
||
|
||
[PettingZoo Wrappers](/api/wrappers/pz_wrappers/) include [conversion wrappers](/api/wrappers/pz_wrappers#conversion-wrappers) to convert between the [AEC](/api/aec/) and [Parallel](/api/parallel/) APIs, and a set of simple [utility wrappers](/api/wrappers/pz_wrappers#utility-wrappers) which provide input validation and other convenient reusable logic. | ||
|
||
[Supersuit Wrappers](/api/wrappers/supersuit_wrappers/) include commonly used pre-processing functions such as frame-stacking and color reduction, compatible with both PettingZoo and Gymnasium. | ||
|
||
[Shimmy Compatibility Wrappers](/api/wrappers/shimmy_wrappers/) allow commonly used external reinforcement learning environments to be used with PettingZoo and Gymnasium. | ||
|
||
|
||
```{toctree} | ||
:hidden: | ||
wrappers/pz_wrappers | ||
wrappers/supersuit_wrappers | ||
wrappers/shimmy_wrappers | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--- | ||
title: PettingZoo Wrappers | ||
--- | ||
|
||
# PettingZoo Wrappers | ||
|
||
PettingZoo includes the following types of wrappers: | ||
* [Conversion Wrappers](#conversion-wrappers): wrappers for converting environments between the [AEC](/api/aec/) and [Parallel](/api/parallel/) APIs | ||
* [Utility Wrappers](#utility-wrappers): a set of wrappers which provide convenient reusable logic, such as enforcing turn order or clipping out-of-bounds actions. | ||
|
||
## Conversion wrappers | ||
|
||
### AEC to Parallel | ||
|
||
```{eval-rst} | ||
.. currentmodule:: pettingzoo.utils.conversions | ||
.. automodule:: pettingzoo.utils.conversions | ||
:members: aec_to_parallel | ||
:undoc-members: | ||
``` | ||
|
||
An environment can be converted from an AEC environment to a parallel environment with the `aec_to_parallel` wrapper shown below. Note that this wrapper makes the following assumptions about the underlying environment: | ||
|
||
1. The environment steps in a cycle, i.e. it steps through every live agent in order. | ||
2. The environment does not update the observations of the agents except at the end of a cycle. | ||
|
||
Most parallel environments in PettingZoo only allocate rewards at the end of a cycle. In these environments, the reward scheme of the AEC API an the parallel API is equivalent. If an AEC environment does allocate rewards within a cycle, then the rewards will be allocated at different timesteps in the AEC environment an the Parallel environment. In particular, the AEC environment will allocate all rewards from one time the agent steps to the next time, while the Parallel environment will allocate all rewards from when the first agent stepped to the last agent stepped. | ||
|
||
To convert an AEC environment into a parallel environment: | ||
``` python | ||
from pettingzoo.utils.conversions import aec_to_parallel | ||
from pettingzoo.butterfly import pistonball_v6 | ||
env = pistonball_v6.env() | ||
env = aec_to_parallel(env) | ||
``` | ||
|
||
### Parallel to AEC | ||
|
||
```{eval-rst} | ||
.. currentmodule:: pettingzoo.utils.conversions | ||
.. automodule:: pettingzoo.utils.conversions | ||
:members: parallel_to_aec | ||
:undoc-members: | ||
``` | ||
|
||
Any parallel environment can be efficiently converted to an AEC environment with the `parallel_to_aec` wrapper. | ||
|
||
To convert a parallel environment into an AEC environment: | ||
``` python | ||
from pettingzoo.utils import parallel_to_aec | ||
from pettingzoo.butterfly import pistonball_v6 | ||
env = pistonball_v6.parallel_env() | ||
env = parallel_to_aec(env) | ||
``` | ||
|
||
|
||
## Utility Wrappers | ||
|
||
We wanted our pettingzoo environments to be both easy to use and easy to implement. To combine these, we have a set of simple wrappers which provide input validation and other convenient reusable logic. | ||
|
||
You can apply these wrappers to your environment in a similar manner to the below examples: | ||
|
||
To wrap a Parallel environment. | ||
```python | ||
from pettingzoo.utils import CaptureStdoutWrapper | ||
from pettingzoo.butterfly import pistonball_v6 | ||
parallel_env = pistonball_v6.env() | ||
parallel_env = CaptureStdoutWrapper(parallel_env) | ||
|
||
observations = parallel_env.reset() | ||
|
||
while parallel_env.agents: | ||
actions = {agent: parallel_env.action_space(agent).sample() for agent in parallel_env.agents} # this is where you would insert your policy | ||
observations, rewards, terminations, truncations, infos = parallel_env.step(actions) | ||
``` | ||
|
||
To wrap an AEC environment: | ||
```python | ||
from pettingzoo.utils import TerminateIllegalWrapper | ||
from pettingzoo.classic import rps_v2 | ||
env = rps_v2.env() | ||
env = TerminateIllegalWrapper(env, illegal_reward=-1) | ||
|
||
env.reset() | ||
for agent in env.agent_iter(): | ||
observation, reward, termination, truncation, info = env.last() | ||
if termination or truncation: | ||
action = None | ||
else: | ||
action = env.action_space(agent).sample() # this is where you would insert your policy | ||
env.step(action) | ||
env.close() | ||
``` | ||
Note: Most AEC environments include TerminateIllegalWrapper in their initialization, so this code does not change the environment's behavior. | ||
|
||
```{eval-rst} | ||
.. currentmodule:: pettingzoo.utils.wrappers | ||
.. autoclass:: BaseWrapper | ||
.. autoclass:: TerminateIllegalWrapper | ||
.. autoclass:: CaptureStdoutWrapper | ||
.. autoclass:: AssertOutOfBoundsWrapper | ||
.. autoclass:: ClipOutOfBoundsWrapper | ||
.. autoclass:: OrderEnforcingWrapper | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.