PettingZoo Wrappers#

PettingZoo includes the following types of wrappers:

  • Conversion Wrappers: wrappers for converting environments between the AEC and Parallel APIs

  • 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#

pettingzoo.utils.conversions.aec_to_parallel(aec_env: AECEnv) ParallelEnv[source]#

Converts an aec environment to a parallel environment.

In the case of an existing parallel environment wrapped using a parallel_to_aec_wrapper, this function will return the original parallel environment. Otherwise, it will apply the aec_to_parallel_wrapper to convert the environment.

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:

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#

pettingzoo.utils.conversions.parallel_to_aec(par_env: ParallelEnv) AECEnv[source]#

Converts an aec environment to a parallel environment.

In the case of an existing aec environment wrapped using a aec_to_prallel_wrapper, this function will return the original AEC environment. Otherwise, it will apply the parallel_to_aec_wrapper to convert the environment.

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:

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.

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:

from pettingzoo.utils import TerminateIllegalWrapper
from pettingzoo.classic import rps_v2
env = rps_v2.env()
env = TerminateIllegalWrapper(env, illegal_reward=-1)

for agent in env.agent_iter():
    observation, reward, termination, truncation, info = env.last()
    if termination or truncation:
        action = None
        action = env.action_space(agent).sample()  # this is where you would insert your policy

Note: Most AEC environments include TerminateIllegalWrapper in their initialization, so this code does not change the environment’s behavior.

class pettingzoo.utils.wrappers.BaseWrapper(env: AECEnv)[source]#

Creates a wrapper around env parameter.

All AECEnv wrappers should inherit from this base class

class pettingzoo.utils.wrappers.TerminateIllegalWrapper(env: AECEnv, illegal_reward: float)[source]#

This wrapper terminates the game with the current player losing in case of illegal values.


illegal_reward – number that is the value of the player making an illegal move.

class pettingzoo.utils.wrappers.CaptureStdoutWrapper(env: AECEnv)[source]#

Takes an environment which prints to terminal, and gives it an ansi render mode where it captures the terminal output and returns it as a string instead.

class pettingzoo.utils.wrappers.AssertOutOfBoundsWrapper(env: AECEnv)[source]#

Asserts if the action given to step is outside of the action space. Applied in PettingZoo environments with discrete action spaces.

class pettingzoo.utils.wrappers.ClipOutOfBoundsWrapper(env: AECEnv)[source]#

Clips the input action to fit in the continuous action space (emitting a warning if it does so).

Applied to continuous environments in pettingzoo.

class pettingzoo.utils.wrappers.OrderEnforcingWrapper(env: AECEnv)[source]#

Checks if function calls or attribute access are in a disallowed order.

  • error on getting rewards, terminations, truncations, infos, agent_selection before reset

  • error on calling step, observe before reset

  • error on iterating without stepping or resetting environment.

  • warn on calling close before render or reset

  • warn on calling step after environment is terminated or truncated