Skip to content

Commit

Permalink
Rename class agent_selector -> AgentSelector (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
dm-ackerman authored Apr 1, 2024
1 parent 45f7f14 commit 849414d
Show file tree
Hide file tree
Showing 23 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docs/api/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Base class which is used by [CaptureStdoutWrapper](https://pettingzoo.farama.org

The agent selector utility allows for easy cycling of agents in an AEC environment. At any time it can be reset or reinitialized with a new order, allowing for changes in turn order or handling a dynamic number of agents (see [Knights-Archers-Zombies](https://pettingzoo.farama.org/environments/butterfly/knights_archers_zombies/) for an example of spawning/killing agents)

Note: while many PettingZoo environments use agent_selector to manage agent cycling internally, it is not intended to be used externally when interacting with an environment. Instead, use `for agent in env.agent_iter()` (see [AEC API Usage](https://pettingzoo.farama.org/api/aec/#usage)).
Note: while many PettingZoo environments use AgentSelector to manage agent cycling internally, it is not intended to be used externally when interacting with an environment. Instead, use `for agent in env.agent_iter()` (see [AEC API Usage](https://pettingzoo.farama.org/api/aec/#usage)).

```{eval-rst}
.. currentmodule:: pettingzoo.utils
Expand Down
6 changes: 3 additions & 3 deletions docs/code_examples/aec_rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from gymnasium.spaces import Discrete

from pettingzoo import AECEnv
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers

ROCK = 0
PAPER = 1
Expand Down Expand Up @@ -156,9 +156,9 @@ def reset(self, seed=None, options=None):
self.observations = {agent: NONE for agent in self.agents}
self.num_moves = 0
"""
Our agent_selector utility allows easy cyclic stepping through the agents list.
Our AgentSelector utility allows easy cyclic stepping through the agents list.
"""
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.next()

def step(self, action):
Expand Down
6 changes: 3 additions & 3 deletions docs/content/environment_creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ The utils directory also contain some classes which are only helpful for develop

### Agent selector

The `agent_selector` class steps through agents in a cycle
The `AgentSelector` class steps through agents in a cycle

It can be used as follows to cycle through the list of agents:

```python
from pettingzoo.utils import agent_selector
from pettingzoo.utils import AgentSelector
agents = ["agent_1", "agent_2", "agent_3"]
selector = agent_selector(agents)
selector = AgentSelector(agents)
agent_selection = selector.reset()
# agent_selection will be "agent_1"
for i in range(100):
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/butterfly/cooperative_pong/cooperative_pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
from pettingzoo.butterfly.cooperative_pong.manual_policy import ManualPolicy
from pettingzoo.butterfly.cooperative_pong.paddle import Paddle
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector
from pettingzoo.utils.conversions import parallel_wrapper_fn

FPS = 15
Expand Down Expand Up @@ -370,7 +370,7 @@ def __init__(self, **kwargs):

self.agents = self.env.agents[:]
self.possible_agents = self.agents[:]
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()
# spaces
self.action_spaces = dict(zip(self.agents, self.env.action_space))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
from pettingzoo.butterfly.knights_archers_zombies.src.players import Archer, Knight
from pettingzoo.butterfly.knights_archers_zombies.src.weapons import Arrow, Sword
from pettingzoo.butterfly.knights_archers_zombies.src.zombie import Zombie
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn

sys.dont_write_bytecode = True
Expand Down Expand Up @@ -370,7 +370,7 @@ def __init__(
self.floor_patch3 = get_image(os.path.join("img", "patch3.png"))
self.floor_patch4 = get_image(os.path.join("img", "patch4.png"))

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.reinit()

def observation_space(self, agent):
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/butterfly/pistonball/pistonball.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

from pettingzoo import AECEnv
from pettingzoo.butterfly.pistonball.manual_policy import ManualPolicy
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn

_image_library = {}
Expand Down Expand Up @@ -180,7 +180,7 @@ def __init__(
self.agents = ["piston_" + str(r) for r in range(self.n_pistons)]
self.possible_agents = self.agents[:]
self.agent_name_mapping = dict(zip(self.agents, list(range(self.n_pistons))))
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

self.observation_spaces = dict(
zip(
Expand Down
6 changes: 3 additions & 3 deletions pettingzoo/classic/chess/chess.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
from pettingzoo import AECEnv
from pettingzoo.classic.chess import chess_utils
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env(**kwargs):
Expand Down Expand Up @@ -144,7 +144,7 @@ def __init__(self, render_mode: str | None = None, screen_height: int | None = 8
self.agents = [f"player_{i}" for i in range(2)]
self.possible_agents = self.agents[:]

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

self.action_spaces = {name: spaces.Discrete(8 * 8 * 73) for name in self.agents}
self.observation_spaces = {
Expand Down Expand Up @@ -238,7 +238,7 @@ def reset(self, seed=None, options=None):

self.board = chess.Board()

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

self.rewards = {name: 0 for name in self.agents}
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/classic/connect_four/connect_four.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def get_image(path):
Expand Down Expand Up @@ -220,7 +220,7 @@ def reset(self, seed=None, options=None):
self.truncations = {i: False for i in self.agents}
self.infos = {i: {} for i in self.agents}

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

self.agent_selection = self._agent_selector.reset()

Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/classic/go/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
from pettingzoo import AECEnv
from pettingzoo.classic.go import coords, go_base
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def get_image(path):
Expand Down Expand Up @@ -191,7 +191,7 @@ def __init__(
[spaces.Discrete(self._N * self._N + 1) for _ in range(self.num_agents)]
)

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

self.board_history = np.zeros((self._N, self._N, 16), dtype=bool)

Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/classic/hanabi/hanabi.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env(**kwargs):
Expand Down Expand Up @@ -441,7 +441,7 @@ def reset(self, seed=None, options=None):
self.truncations = self.hanabi_env.truncations
self.infos = self.hanabi_env.infos

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

def step(
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/classic/rps/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
from gymnasium.utils import EzPickle

from pettingzoo import AECEnv
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn


Expand Down Expand Up @@ -419,7 +419,7 @@ def close(self):

def reset(self, seed=None, options=None):
self.agents = self.possible_agents[:]
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.next()
self.rewards = {agent: 0 for agent in self.agents}
self._cumulative_rewards = {agent: 0 for agent in self.agents}
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/classic/tictactoe/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

from pettingzoo import AECEnv
from pettingzoo.classic.tictactoe.board import Board
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers


def get_image(path):
Expand Down Expand Up @@ -143,7 +143,7 @@ def __init__(
self.truncations = {i: False for i in self.agents}
self.infos = {i: {"legal_moves": list(range(0, 9))} for i in self.agents}

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

self.render_mode = render_mode
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/mpe/_mpe_utils/simple_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pettingzoo import AECEnv
from pettingzoo.mpe._mpe_utils.core import Agent
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
agent.name: idx for idx, agent in enumerate(self.world.agents)
}

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

# set spaces
self.action_spaces = dict()
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/sisl/multiwalker/multiwalker.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
from pettingzoo import AECEnv
from pettingzoo.sisl.multiwalker.multiwalker_base import FPS
from pettingzoo.sisl.multiwalker.multiwalker_base import MultiWalkerEnv as _env
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn


Expand Down Expand Up @@ -156,7 +156,7 @@ def __init__(self, *args, **kwargs):
self.agent_name_mapping = dict(
zip(self.agents, list(range(self.env.n_walkers)))
)
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
# spaces
self.action_spaces = dict(zip(self.agents, self.env.action_space))
self.observation_spaces = dict(zip(self.agents, self.env.observation_space))
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/sisl/pursuit/pursuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from pettingzoo import AECEnv
from pettingzoo.sisl.pursuit.manual_policy import ManualPolicy
from pettingzoo.sisl.pursuit.pursuit_base import Pursuit as _env
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn

__all__ = ["ManualPolicy", "env", "parallel_env", "raw_env"]
Expand Down Expand Up @@ -118,7 +118,7 @@ def __init__(self, *args, **kwargs):
self.agents = ["pursuer_" + str(a) for a in range(self.env.num_agents)]
self.possible_agents = self.agents[:]
self.agent_name_mapping = dict(zip(self.agents, list(range(self.num_agents))))
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
# spaces
self.n_act_agents = self.env.act_dims[0]
self.action_spaces = dict(zip(self.agents, self.env.action_space))
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/sisl/waterworld/waterworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
from pettingzoo import AECEnv
from pettingzoo.sisl.waterworld.waterworld_base import FPS
from pettingzoo.sisl.waterworld.waterworld_base import WaterworldBase as _env
from pettingzoo.utils import agent_selector, wrappers
from pettingzoo.utils import AgentSelector, wrappers
from pettingzoo.utils.conversions import parallel_wrapper_fn


Expand Down Expand Up @@ -171,7 +171,7 @@ def __init__(self, *args, **kwargs):
self.agents = ["pursuer_" + str(r) for r in range(self.env.num_agents)]
self.possible_agents = self.agents[:]
self.agent_name_mapping = dict(zip(self.agents, list(range(self.num_agents))))
self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)

# spaces
self.action_spaces = dict(zip(self.agents, self.env.action_space))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env():
Expand Down Expand Up @@ -105,7 +105,7 @@ def reset(self, seed=None, options=None):
for i in range(5):
self.add_agent(self.np_random.choice(self.types))

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

# seed observation and action spaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env():
Expand Down Expand Up @@ -107,7 +107,7 @@ def reset(self, seed=None, options=None):
for i in range(5):
self.add_agent(self.np_random.choice(self.types))

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

# seed observation and action spaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env():
Expand Down Expand Up @@ -99,7 +99,7 @@ def reset(self, seed=None, options=None):
for i in range(5):
self.add_agent(self.np_random.choice(self.types))

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

# seed observation and action spaces
Expand Down
4 changes: 2 additions & 2 deletions pettingzoo/test/example_envs/generated_agents_env_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pettingzoo import AECEnv
from pettingzoo.utils import wrappers
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector


def env():
Expand Down Expand Up @@ -99,7 +99,7 @@ def reset(self, seed=None, options=None):
for i in range(5):
self.add_agent(self.np_random.choice(self.types))

self._agent_selector = agent_selector(self.agents)
self._agent_selector = AgentSelector(self.agents)
self.agent_selection = self._agent_selector.reset()

# seed observation and action spaces
Expand Down
2 changes: 1 addition & 1 deletion pettingzoo/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pettingzoo.utils.agent_selector import agent_selector
from pettingzoo.utils.agent_selector import AgentSelector
from pettingzoo.utils.average_total_reward import average_total_reward
from pettingzoo.utils.conversions import (
aec_to_parallel,
Expand Down
Loading

0 comments on commit 849414d

Please sign in to comment.