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

Fix syntax/imports for python blocks in /docs #1075

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ from pettingzoo.butterfly import pistonball_v6
parallel_env = pistonball_v6.parallel_env(render_mode="human")
observations, infos = parallel_env.reset(seed=42)

while env.agents:
while parallel_env.agents:
# this is where you would insert your policy
actions = {agent: parallel_env.action_space(agent).sample() for agent in parallel_env.agents}

Expand Down
7 changes: 5 additions & 2 deletions docs/api/wrappers/pz_wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ We wanted our pettingzoo environments to be both easy to use and easy to impleme
You can apply these wrappers to your environment in a similar manner to the below examples:

To wrap a Parallel environment.
```python
```python notest
from pettingzoo.utils import CaptureStdoutWrapper
from pettingzoo.butterfly import pistonball_v6
parallel_env = pistonball_v6.env()

parallel_env = pistonball_v6.parallel_env(render_mode="human")
# TODO: parallel_env render_mode doesn't exist here
parallel_env = CaptureStdoutWrapper(parallel_env)

observations, infos = parallel_env.reset()
Expand All @@ -90,6 +92,7 @@ for agent in env.agent_iter():
action = None
else:
action = env.action_space(agent).sample() # this is where you would insert your policy
#TODO RPS observe() returns an np.array instead of dict, triggering an assert statement in TerminateIllegalWrapper
elliottower marked this conversation as resolved.
Show resolved Hide resolved
env.step(action)
env.close()
```
Expand Down
10 changes: 6 additions & 4 deletions docs/api/wrappers/shimmy_wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ The [Shimmy](https://shimmy.farama.org/) package (`pip install shimmy`) allows c

To load a DeepMind Control [multi-agent soccer game](https://github.com/deepmind/dm_control/blob/main/dm_control/locomotion/soccer/README.md):

```python
```python notest
from shimmy import DmControlMultiAgentCompatibilityV0
#TODO ModuleNotFoundError: No module named 'dm_control'
Copy link
Contributor

Choose a reason for hiding this comment

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

This one requires pip install shimmy[dm-control] or all, I think it’s possible to make a pytest fixture or some sort of thing to basically say when doing import shimmy, we have to install it (or mock it but ideally it would actually test it)

Copy link
Contributor

Choose a reason for hiding this comment

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

I’d have to test locally but my guess is if the pytest docs can run the pettingzoo scripts then we can have it install shimmy as well.

from dm_control.locomotion import soccer as dm_soccer

env = dm_soccer.load(team_size=2)
Expand All @@ -41,11 +42,11 @@ while env.agents:

To load an OpenSpiel game of [backgammon](https://github.com/deepmind/open_spiel/blob/master/docs/games.md#backgammon):
```python
from shimmy import OpenspielCompatibilityV0
from shimmy import OpenSpielCompatibilityV0
import pyspiel

env = pyspiel.load_game("backgammon")
env = OpenspielCompatibilityV0(game=env, render_mode=None)
env = OpenSpielCompatibilityV0(env=env, render_mode=None)

env.reset()
for agent in env.agent_iter():
Expand All @@ -61,7 +62,8 @@ for agent in env.agent_iter():

To load a Melting Pot [prisoner's dilemma in the matrix](https://github.com/deepmind/meltingpot/blob/main/docs/substrate_scenario_details.md#prisoners-dilemma-in-the-matrix) substrate:

```python
```python notest
#TODO ImportError: Melting Pot or PettingZoo is not installed, run `pip install 'shimmy[melting-pot]' and install Melting Pot via https://github.com/deepmind/meltingpot#installation`
from shimmy import MeltingPotCompatibilityV0
env = MeltingPotCompatibilityV0(substrate_name="prisoners_dilemma_in_the_matrix__arena", render_mode="human")
observations, infos = env.reset()
Expand Down
2 changes: 1 addition & 1 deletion docs/api/wrappers/supersuit_wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [SuperSuit](https://github.com/Farama-Foundation/SuperSuit) companion packag

To convert [space invaders](/environments/atari/space_invaders/) to a greyscale observation space and stack the last 4 frames:

``` python
``` python notest
from pettingzoo.atari import space_invaders_v2
elliottower marked this conversation as resolved.
Show resolved Hide resolved
from supersuit import color_reduction_v0, frame_stack_v1

Expand Down
23 changes: 19 additions & 4 deletions docs/content/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,33 @@ env = pistonball_v6.env()
Environments are generally highly configurable via arguments at creation, i.e.:

``` python
cooperative_pong.env(ball_speed=18, left_paddle_speed=25,
right_paddle_speed=25, is_cake_paddle=True, max_cycles=900, bounce_randomness=False)
from pettingzoo.butterfly import cooperative_pong_v5

cooperative_pong_v5.env(ball_speed=18, left_paddle_speed=25,
right_paddle_speed=25, cake_paddle=True, max_cycles=900, bounce_randomness=False)
```

## Interacting With Environments

Environments can be interacted with using a similar interface to Gymnasium:

``` python
env.reset()
from pettingzoo.butterfly import cooperative_pong_v5

env = cooperative_pong_v5.env(render_mode="human")
env.reset(seed=42)

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

if termination or truncation:
action = None
else:
# this is where you would insert your policy
action = env.action_space(agent).sample()

env.step(action)
env.close()
```

The commonly used methods are:
Expand Down Expand Up @@ -116,6 +129,8 @@ When an agent is terminated or truncated, it's removed from `agents`, so when th
If you have a wrapped environment, and you want to get the unwrapped environment underneath all the layers of wrappers (so that you can manually call a function or change some underlying aspect of the environment), you can use the `.unwrapped` attribute. If the environment is already a base environment, the `.unwrapped` attribute will just return itself.

``` python
from pettingzoo.butterfly import knights_archers_zombies_v10

base_env = knights_archers_zombies_v10.env().unwrapped
```

Expand Down
4 changes: 2 additions & 2 deletions docs/content/environment_creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ from pettingzoo.utils.deprecated_module import DeprecatedModule
knights_archers_zombies_v0 = DeprecatedModule("knights_archers_zombies", "v0", "v10")
```
This declaration tells the user that `knights_archers_zombies_v0` is deprecated and `knights_archers_zombies_v10` should be used instead. In particular, it gives the following error:
``` python
``` python notest
from pettingzoo.butterfly import knights_archers_zombies_v0
knights_archers_zombies_v0.env()
# pettingzoo.utils.deprecated_module.DeprecatedEnv: knights_archers_zombies_v0 is now deprecated, use knights_archers_zombies_v10 instead

elliottower marked this conversation as resolved.
Show resolved Hide resolved
```
13 changes: 7 additions & 6 deletions docs/content/environment_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ The seed test takes in a function that creates a pettingzoo environment. For exa
from pettingzoo.test import seed_test, parallel_seed_test
from pettingzoo.butterfly import pistonball_v6
env_fn = pistonball_v6.env
seed_test(env_fn, num_cycles=10, test_kept_state=True)
seed_test(env_fn, num_cycles=10)

# or for parallel environments
parallel_env_fn = pistonball_v6.parallel_env
parallel_seed_test(parallel_env_fn, num_cycles=10, test_kept_state=True)
parallel_seed_test(parallel_env_fn)
```

Internally, there are two separate tests.
Expand All @@ -61,22 +61,23 @@ The second optional argument, `test_kept_state` allows the user to disable the s

The max cycles test tests that the `max_cycles` environment argument exists and the resulting environment actually runs for the correct number of cycles. If your environment does not take a `max_cycles` argument, you should not run this test. The reason this test exists is that many off-by-one errors are possible when implementing `max_cycles`. An example test usage looks like:

``` python
``` python notest
from pettingzoo.test import max_cycles_test
from pettingzoo.butterfly import pistonball_v6
env = pistonball_v6.env()
#TODO AttributeError: 'OrderEnforcingWrapper' object has no attribute 'parallel_env'
elliottower marked this conversation as resolved.
Show resolved Hide resolved
max_cycles_test(env)
```

## Render Test

The render test checks that rendering 1) does not crash and 2) produces output of the correct type when given a mode (only supports `'human'`, `'ansi'`, and `'rgb_array'` modes).

``` python
from pettingzoo.test import render_test
from pettingzoo.butterfly import pistonball_v6
env = pistonball_v6.env()
render_test(env)
env_func = pistonball_v6.env
#TODO TypeError: 'OrderEnforcingWrapper' object is not callable
elliottower marked this conversation as resolved.
Show resolved Hide resolved
render_test(env_func)
```

The render test method takes in an optional argument `custom_tests` that allows for additional tests in non-standard modes.
Expand Down
2 changes: 1 addition & 1 deletion docs/environments/atari.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pip install supersuit

Here is some example usage for the Atari preprocessing:

``` python
``` python notest
elliottower marked this conversation as resolved.
Show resolved Hide resolved
import supersuit
from pettingzoo.atari import space_invaders_v1

Expand Down
2 changes: 1 addition & 1 deletion docs/environments/butterfly.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ for agent in env.agent_iter():
observation, reward, termination, truncation, info = env.last()

if termination or truncation:
action = None
action = None
elif agent == manual_policy.agent:
# get user input (controls are WASD and space)
action = manual_policy(observation, agent)
Expand Down
5 changes: 4 additions & 1 deletion pettingzoo/test/render_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from typing import Callable

import numpy as np
from gymnasium.core import Env


def collect_render_results(env):
Expand All @@ -24,7 +27,7 @@ def collect_render_results(env):
return results


def render_test(env_fn, custom_tests={}):
def render_test(env_fn: Callable[[], Env], custom_tests={}):
env = env_fn(render_mode="human")
render_modes = env.metadata.get("render_modes")[:]
assert (
Expand Down