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

Remove redundant code in tutorials #1159

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 7 additions & 18 deletions docs/code_examples/aec_rps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import functools

import gymnasium
import numpy as np
from gymnasium.spaces import Discrete
Expand Down Expand Up @@ -75,26 +73,17 @@ def __init__(self, render_mode=None):
zip(self.possible_agents, list(range(len(self.possible_agents))))
)

# optional: we can define the observation and action spaces here as attributes to be used in their corresponding methods
self._action_spaces = {agent: Discrete(3) for agent in self.possible_agents}
# we want to define the spaces as fixed objects so we can seed them
self._observation_spaces = {
agent: Discrete(4) for agent in self.possible_agents
}
self.render_mode = render_mode
self._action_spaces = {agent: Discrete(3) for agent in self.possible_agents}

# Observation space should be defined here.
# lru_cache allows observation and action spaces to be memoized, reducing clock cycles required to get each agent's space.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def observation_space(self, agent):
# gymnasium spaces are defined and documented here: https://gymnasium.farama.org/api/spaces/
return Discrete(4)

# Action space should be defined here.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def action_space(self, agent):
return Discrete(3)
# observation and action spaces are defined as functions which take in an agent id
# and returns the relevant spaces.
self.observation_space = lambda agent: self._observation_spaces[agent]
self.action_space = lambda agent: self._action_spaces[agent]
self.render_mode = render_mode

def render(self):
"""
Expand Down
3 changes: 0 additions & 3 deletions docs/code_examples/parallel_rps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import functools

import gymnasium
import numpy as np
from gymnasium.spaces import Discrete
Expand Down Expand Up @@ -76,7 +74,6 @@ def __init__(self, render_mode=None):
self.agent_name_mapping = dict(
zip(self.possible_agents, list(range(len(self.possible_agents))))
)
self.render_mode = render_mode

# Observation space should be defined here.
# lru_cache allows observation and action spaces to be memoized, reducing clock cycles required to get each agent's space.
Expand Down
22 changes: 7 additions & 15 deletions tutorials/CustomEnvironment/tutorial2_adding_game_logic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import random
from copy import copy

Expand Down Expand Up @@ -43,6 +42,13 @@ def __init__(self):
self.timestep = None
self.possible_agents = ["prisoner", "guard"]

self._observation_spaces = {
agent: MultiDiscrete([7 * 7] * 3) for agent in self.possible_agents
}
self.observation_space = lambda agent: self._observation_spaces[agent]
self._action_spaces = {agent: Discrete(4) for agent in self.possible_agents}
self.action_space = lambda agent: self._action_spaces[agent]

def reset(self, seed=None, options=None):
"""Reset set the environment to a starting point.

Expand Down Expand Up @@ -162,17 +168,3 @@ def render(self):
grid[self.guard_y, self.guard_x] = "G"
grid[self.escape_y, self.escape_x] = "E"
print(f"{grid} \n")

# Observation space should be defined here.
# lru_cache allows observation and action spaces to be memoized, reducing clock cycles required to get each agent's space.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def observation_space(self, agent):
# gymnasium spaces are defined and documented here: https://gymnasium.farama.org/api/spaces/
return MultiDiscrete([7 * 7] * 3)

# Action space should be defined here.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def action_space(self, agent):
return Discrete(4)
22 changes: 7 additions & 15 deletions tutorials/CustomEnvironment/tutorial3_action_masking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import random
from copy import copy

Expand Down Expand Up @@ -43,6 +42,13 @@ def __init__(self):
self.timestep = None
self.possible_agents = ["prisoner", "guard"]

self._observation_spaces = {
agent: MultiDiscrete([7 * 7 - 1] * 3) for agent in self.possible_agents
}
self.observation_space = lambda agent: self._observation_spaces[agent]
self._action_spaces = {agent: Discrete(4) for agent in self.possible_agents}
self.action_space = lambda agent: self._action_spaces[agent]

def reset(self, seed=None, options=None):
"""Reset set the environment to a starting point.

Expand Down Expand Up @@ -198,17 +204,3 @@ def render(self):
grid[self.guard_y, self.guard_x] = "G"
grid[self.escape_y, self.escape_x] = "E"
print(f"{grid} \n")

# Observation space should be defined here.
# lru_cache allows observation and action spaces to be memoized, reducing clock cycles required to get each agent's space.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def observation_space(self, agent):
# gymnasium spaces are defined and documented here: https://gymnasium.farama.org/api/spaces/
return MultiDiscrete([7 * 7 - 1] * 3)

# Action space should be defined here.
# If your spaces change over time, remove this line (disable caching).
@functools.lru_cache(maxsize=None)
def action_space(self, agent):
return Discrete(4)
Loading