Wrappers¶
Environment wrappers for MASA-Safe-RL.
This module contains small, composable gymnasium.Wrapper utilities that
(1) preserve access to constraint-related objects through wrapper chains,
(2) inject monitoring/metrics into info, (3) apply potential-based reward
shaping for DFA-based constraints, and (4) provide basic observation/reward
normalization and light-weight vector-environment helpers.
Key conventions¶
- Constraint-enabled environments expose a
_constraintobject and (often)label_fn/cost_fnattributes. Seemasa.common.constraints.base.BaseConstraintEnv. - Monitoring wrappers add structured dictionaries under
info["constraint"]and/orinfo["metrics"]. - Vector wrappers in this file use a simple Python list API:
observations, rewards, terminals, truncations, infos are lists of length
VecEnvWrapperBase.n_envs.
Notes¶
For potential-based shaping, the shaped cost inserted into info is of the
form
where \(q_t\) is the DFA state, \(c_t\) is the original constraint cost, \(\Phi\) is the potential function, and \(\gamma\) is the shaping discount factor.
API Reference¶
Base Class¶
masa.common.wrappers.ConstraintPersistentWrapper ¶
Bases: Wrapper
Base wrapper that persists access to constraint-related attributes.
Many Gymnasium wrappers shadow attributes by changing self.env. This
wrapper provides stable properties for:
_constraint(if present on the underlying env)cost_fn(if exposed by the constraint)label_fn(if present on the underlying env)
Subclasses can rely on these properties even when stacked with additional wrappers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Base environment to wrap. |
required |
Source code in masa/common/wrappers.py
_constraint
property
¶
The underlying constraint object, if present.
Returns:
| Type | Description |
|---|---|
|
The object stored in |
|
|
|
cost_fn
property
¶
Cost function exposed by the underlying constraint, if available.
If self._constraint exists and it exposes cost_fn, this returns
that callable-like object (often a masa.common.ltl.DFACostFn).
Otherwise returns None.
Returns:
| Type | Description |
|---|---|
|
A cost function-like object or |
label_fn
property
¶
Labelling function exposed by the underlying environment, if available.
Returns:
| Type | Description |
|---|---|
|
The object stored in |
|
|
|
masa.common.wrappers.ConstraintPersistentObsWrapper ¶
Bases: ConstraintPersistentWrapper
Base class for wrappers that transform observations while preserving constraint access.
Subclasses must implement _get_obs which maps raw observations to the
wrapped observation representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Base environment to wrap. |
required |
Source code in masa/common/wrappers.py
_get_obs ¶
Transform a raw observation into the wrapped observation.
Subclasses must implement this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs
|
Any
|
Raw observation from the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Transformed observation. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not implemented by the subclass. |
Source code in masa/common/wrappers.py
reset ¶
Reset the environment and transform the returned observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed forwarded to the underlying environment. |
None
|
options
|
Dict[str, Any] | None
|
Reset options forwarded to the underlying environment. |
None
|
Returns:
| Type | Description |
|---|---|
|
A tuple |
Source code in masa/common/wrappers.py
step ¶
Step the environment and transform the returned observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Action forwarded to the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
|
A 5-tuple |
|
|
is transformed via |
Source code in masa/common/wrappers.py
Helpers¶
masa.common.wrappers.is_wrapped ¶
Check whether env is wrapped (anywhere in its wrapper chain) by
wrapper_class.
This helper walks through typical wrapper chains:
- Gymnasium-style wrappers via
.env. - Vector-env style wrappers via a
.venvattribute (commonly used by vectorized environments and some third-party libraries).
Cycle protection is included: if the wrapper chain loops, this function
returns False rather than looping forever.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Environment or wrapper to inspect. |
required |
wrapper_class
|
Wrapper
|
Wrapper type to search for. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Source code in masa/common/wrappers.py
masa.common.wrappers.get_wrapped ¶
Return the first wrapper instance of type wrapper_class found in env's
wrapper chain.
The traversal rules match is_wrapped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Environment or wrapper to inspect. |
required |
wrapper_class
|
Wrapper
|
Wrapper type to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Env
|
The first encountered instance of |
Env
|
or |
Source code in masa/common/wrappers.py
Next Steps¶
- Core Wrappers - API reference for core wrappers.
- Misc Wrappers - API reference for miscellanious wrappers.
- Vectorized Envs - API refernce for synchronous vectorized environments and wrappers.