src.plangym.videogames.nes#

Environment for playing Mario bros using gym-super-mario-bros.

Attributes#

Classes#

NESWrapper

A wrapper for the NES environment.

JoypadSpace

An environment wrapper to convert binary to discrete action space.

NesEnv

Environment for working with the NES-py emulator.

MarioEnv

Interface for using gym-super-mario-bros in plangym.

Module Contents#

src.plangym.videogames.nes.RIGHT_ONLY = [['NOOP'], ['right'], ['right', 'A'], ['right', 'B'], ['right', 'A', 'B']]#
src.plangym.videogames.nes.SIMPLE_MOVEMENT = [['NOOP'], ['right'], ['right', 'A'], ['right', 'B'], ['right', 'A', 'B'], ['A'], ['left']]#
src.plangym.videogames.nes.COMPLEX_MOVEMENT = [['NOOP'], ['right'], ['right', 'A'], ['right', 'B'], ['right', 'A', 'B'], ['A'], ['left'],...#
src.plangym.videogames.nes.ObsType#
src.plangym.videogames.nes.ActType#
src.plangym.videogames.nes.RenderFrame#
class src.plangym.videogames.nes.NESWrapper(wrapped)[source]#

A wrapper for the NES environment.

_wrapped#
__getattr__(name)[source]#

Get an attribute from the wrapped object.

__setattr__(name, value)[source]#

Set an attribute on the wrapped object.

__delattr__(name)[source]#

Delete an attribute from the wrapped object.

step(action)[source]#

Modify the env after calling step() using self.observation().

Parameters:

action (ActType)

Return type:

tuple[gymnasium.core.WrapperObsType, gymnasium.core.SupportsFloat, bool, bool, dict[str, Any]]

reset(*, seed=None, options=None)[source]#

Modify the env after calling reset(), returning a modified observation.

Parameters:
  • seed (int | None)

  • options (dict[str, Any] | None)

Return type:

tuple[gymnasium.core.WrapperObsType, dict[str, Any]]

observation(observation)[source]#

Return a modified observation.

Parameters:

observation (ObsType) – The env observation

Returns:

The modified observation

Return type:

gymnasium.core.WrapperObsType

class src.plangym.videogames.nes.JoypadSpace(env, actions)[source]#

Bases: gymnasium.Wrapper

An environment wrapper to convert binary to discrete action space.

Parameters:
  • env (gymnasium.Env)

  • actions (list)

_button_map#
classmethod buttons()[source]#

Return the buttons that can be used as actions.

Return type:

list

action_space#

Return the Env action_space unless overwritten then the wrapper action_space is used.

_action_map#
_action_meanings#
step(action)[source]#

Take a step using the given action.

Parameters:

action (int) – the discrete action to perform

Returns:

  • (numpy.ndarray) the state as a result of the action

  • (float) the reward achieved by taking the action

  • (bool) a flag denoting whether the episode has ended

  • (dict) a dictionary of extra information

Return type:

a tuple of

get_keys_to_action()[source]#

Return the dictionary of keyboard keys to actions.

get_action_meanings()[source]#

Return a list of actions meanings.

class src.plangym.videogames.nes.NesEnv(name, frameskip=5, episodic_life=False, autoreset=True, delay_setup=False, remove_time_limit=True, obs_type='rgb', render_mode=None, wrappers=None, **kwargs)[source]#

Bases: plangym.videogames.env.VideogameEnv

Environment for working with the NES-py emulator.

Parameters:
  • name (str)

  • frameskip (int)

  • episodic_life (bool)

  • autoreset (bool)

  • delay_setup (bool)

  • remove_time_limit (bool)

  • obs_type (str)

  • render_mode (str | None)

  • wrappers (Iterable[plangym.core.wrap_callable] | None)

property nes_env: NESEnv#

Access the underlying NESEnv.

Return type:

NESEnv

get_image()[source]#

Return a numpy array containing the rendered view of the environment.

Square matrices are interpreted as a greyscale image. Three-dimensional arrays are interpreted as RGB images with channels (Height, Width, RGB)

Return type:

numpy.ndarray

get_ram()[source]#

Return a copy of the emulator environment.

Return type:

numpy.ndarray

get_state(state=None)[source]#

Recover the internal state of the simulation.

A state must completely describe the Environment at a given moment.

Parameters:

state (numpy.ndarray | None)

Return type:

numpy.ndarray

set_state(state)[source]#

Set the internal state of the simulation.

Parameters:

state (numpy.ndarray) – Target state to be set in the environment.

Returns:

None

Return type:

None

close()[source]#

Close the underlying gym.Env.

Return type:

None

__del__()[source]#

Tear down the environment.

render(mode='rgb_array')[source]#

Render the environment.

class src.plangym.videogames.nes.MarioEnv(name, movement_type='simple', original_reward=False, **kwargs)[source]#

Bases: NesEnv

Interface for using gym-super-mario-bros in plangym.

Parameters:
  • name (str)

  • movement_type (str)

  • original_reward (bool)

AVAILABLE_OBS_TYPES#
MOVEMENTS#
_movement_type#
_original_reward#
get_state(state=None)[source]#

Recover the internal state of the simulation.

A state must completely describe the Environment at a given moment.

Parameters:

state (numpy.ndarray | None)

Return type:

numpy.ndarray

init_gym_env()[source]#

Initialize the NESEnv` instance that the current class is wrapping.

Return type:

gymnasium.Env

_update_info(info)[source]#
Parameters:

info (dict[str, Any])

Return type:

dict[str, Any]

_get_info()[source]#
get_coords_obs(obs, info=None, **kwargs)[source]#

Return the information contained in info as an observation if obs_type == “info”.

Parameters:
  • obs (numpy.ndarray)

  • info (dict[str, Any] | None)

Return type:

numpy.ndarray

process_reward(reward, info, **kwargs)[source]#

Return a custom reward based on the x, y coordinates and level mario is in.

Return type:

float

process_terminal(terminal, info, **kwargs)[source]#

Return True if terminal or mario is dying.

Return type:

bool

process_info(info, **kwargs)[source]#

Add additional data to the info dictionary.

Return type:

dict[str, Any]