Wrappers
POSGGym includes a wrapper API (adopted from Gymnasium) as well as a collection of common wrappers that provide a convenient way to modify an existing environment without having to alter the underlying code directly. Wrappers can be applied to any posggym.Env environment, and can also be chained to combine their effects.
In order to wrap an environment, you must first initialize a base environment. Then you can pass this environment along with (possibly optional) parameters to the wrapper’s constructor.
>>> import posggym
>>> from posggym.wrappers import FlattenObservation
>>> base_env = posggym.make("PursuitEvasion-v1")
>>> base_env.observation_spaces['0']
Tuple(Tuple(Discrete(2), Discrete(2), Discrete(2), Discrete(2), Discrete(2), Discrete(2)), Tuple(Discrete(16), Discrete(16)), Tuple(Discrete(16), Discrete(16)), Tuple(Discrete(16), Discrete(16)))
>>> wrapped_env = FlattenObservation(base_env)
>>> wrapped_env.observation_spaces['0']
Box(0, 1, (108,), int64)
You can access the environment underneath the top-most wrapper by using the posggym.Wrapper.env attribute. Since the posggym.Wrapper class inherits from posggym.Env, the environment from the posggym.Wrapper.env attribute can be another wrapper.
>>> wrapped_env
<FlattenObservation<TimeLimit<OrderEnforcing<PassiveEnvChecker<PursuitEvasionEnv<PursuitEvasion-v0>>>>>>
>>> wrapped_env.env
<TimeLimit<OrderEnforcing<PassiveEnvChecker<PursuitEvasionEnv<PursuitEvasion-v0>>>>>
If you want to get to the environment underneath all of the layers of wrappers, you can use the posggym.Wrapper.unwrapped attribute. If the environment is already a bare environment, this will just return the environment itself.
>>> wrapped_env
<FlattenObservation<TimeLimit<OrderEnforcing<PassiveEnvChecker<PursuitEvasionEnv<PursuitEvasion-v0>>>>>>
>>> wrapped_env.unwrapped
<posggym.envs.grid_world.pursuit_evasion.PursuitEvasionEnv object at 0x7f4a94086d90>
There are three common things you might want a wrapper to do:
Transform actions before applying them to the base environment
Transform observations that are returned by the base environment
Transform rewards that are returned by the base environment
Such wrappers can be easily implemented by inheriting from posggym.ActionWrapper, posggym.ObservationWrapper, or posggym.RewardWrapper and implementing the respective transformation. If you need a wrapper to do more complicated tasks, you can inherit from the posggym.Wrapper class directly.
posggym.Wrapper
- class posggym.Wrapper(env: Env[StateType, ObsType, ActType])
Wraps a
posggym.Envto allow a modular transformation.This class is the base class for all wrappers. Wrappers that inherit from this class can modify
action_spaces,observation_spaces,reward_rangesandmetadataattributes , without changing the underlying environment’s attributes.Moreover, the behavior of the
step()andreset()methods can be changed by these wrappers. Some attributes (spec,render_mode) will point back to the wrapper’s environment (i.e. to the corresponding attributes ofenv).Note
If you inherit from
Wrapper, don’t forget to callsuper().__init__(env)if the subclass overrides the __init__ method.
Methods
- posggym.Wrapper.step(self, actions: Dict[str, WrapperActType]) Tuple[Dict[str, WrapperObsType], Dict[str, float], Dict[str, bool], Dict[str, bool], bool, Dict[str, Dict]]
-
Can be overwritten to change the returned data.
- posggym.Wrapper.reset(self, *, seed: int | None = None, options: Dict[str, Any] | None = None) Tuple[Dict[str, WrapperObsType], Dict[str, Dict]]
-
Can be overwritten to change the returned data.
- posggym.Wrapper.render(self) None | np.ndarray | str | Dict[str, np.ndarray] | Dict[str, str]
-
Can be overwritten to change the returned data.
Attributes
- property Wrapper.possible_agents: Tuple[str, ...]
Returns the
Envpossible_agents.
- property Wrapper.action_spaces: Dict[str, spaces.Space]
Return the
Envaction_spaces.This is the
Envaction_spacesunless it’s overwritten then the wrapperaction_spacesis used.
- property Wrapper.observation_spaces: Dict[str, spaces.Space]
Return the
Envobservation_spaces.This is the
Envobservation_spacesunless it’s overwritten then the wrapperobservation_spacesis used.
- property Wrapper.reward_ranges: Dict[str, Tuple[float, float]]
Return the
Envreward_ranges.This is the
Envreward_ranges, unless it’s overwritten, then the wrapperreward_rangesis used.
- property Wrapper.render_mode: str | None
Return the
Envrender_mode.
- posggym.Wrapper.env
The environment (one level underneath) this wrapper.
This may itself be a wrapped environment. To obtain the environment underneath all layers of wrappers, use
posggym.Wrapper.unwrapped.
- property Wrapper.unwrapped: Env
Returns the base environment of the wrapper.
This will be the bare
posggym.Envenvironment, underneath all layers of wrappers.
POSGGym Wrappers
POSGGym provides a number of commonly used wrappers listed below.
Name |
Type |
Description |
|---|---|---|
Action Wrapper |
An Action wrapper that discretizes continuous action spaces |
|
Action Wrapper |
An Action wrapper for rescaling actions |
|
Observation Wrapper |
An Observation wrapper that flattens the observation |
|
Observation Wrapper |
An Observation wrapper for rescaling observations |
|
Misc Wrapper |
This will produce an error if step or render is called before reset |
|
Misc Wrapper |
Checks that the step, reset and render functions follow the posggym API. |
|
Misc Wrapper |
This wrapper will record videos of rollouts. |
|
Misc Wrapper |
This wrapper will emit a truncated signal if the specified number of steps is exceeded in an episode. |