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¶
- class masa.common.wrappers.ConstraintPersistentWrapper(env: Env)[source]¶
Bases:
WrapperBase 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:
env – Base environment to wrap.
Wraps an environment to allow a modular transformation of the
step()andreset()methods.- Parameters:
env – The environment to wrap
- property _constraint¶
The underlying constraint object, if present.
- Returns:
The object stored in
self.env._constraintif it exists, otherwiseNone.
- property cost_fn¶
Cost function exposed by the underlying constraint, if available.
If
self._constraintexists and it exposescost_fn, this returns that callable-like object (often amasa.common.ltl.DFACostFn). Otherwise returnsNone.- Returns:
A cost function-like object or
None.
- property label_fn¶
Labelling function exposed by the underlying environment, if available.
- Returns:
The object stored in
self.env.label_fnif it exists, otherwiseNone.
- class masa.common.wrappers.ConstraintPersistentObsWrapper(env: Env)[source]¶
Bases:
ConstraintPersistentWrapperBase 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:
env – Base environment to wrap.
Wraps an environment to allow a modular transformation of the
step()andreset()methods.- Parameters:
env – The environment to wrap
- _get_obs() Any[source]¶
Transform a raw observation into the wrapped observation.
Subclasses must implement this.
- Parameters:
obs – Raw observation from the underlying environment.
- Returns:
Transformed observation.
- Raises:
NotImplementedError – If not implemented by the subclass.
- reset(*, seed: int | None = None, options: Dict[str, Any] | None = None)[source]¶
Reset the environment and transform the returned observation.
- Parameters:
seed – Random seed forwarded to the underlying environment.
options – Reset options forwarded to the underlying environment.
- Returns:
A tuple
(obs, info)whereobsis transformed via_get_obs().
- step(action)[source]¶
Step the environment and transform the returned observation.
- Parameters:
action – Action forwarded to the underlying environment.
- Returns:
A 5-tuple
(obs, reward, terminated, truncated, info)whereobsis transformed via_get_obs().
Helpers¶
- wrappers.is_wrapped(wrapper_class: Wrapper) bool¶
Check whether
envis wrapped (anywhere in its wrapper chain) bywrapper_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
Falserather than looping forever.- Parameters:
env – Environment or wrapper to inspect.
wrapper_class – Wrapper type to search for.
- Returns:
Trueif an instance ofwrapper_classappears in the wrapper chain;Falseotherwise.
- wrappers.get_wrapped(wrapper_class: Wrapper) Env¶
Return the first wrapper instance of type
wrapper_classfound inenv’s wrapper chain.The traversal rules match
is_wrapped().- Parameters:
env – Environment or wrapper to inspect.
wrapper_class – Wrapper type to retrieve.
- Returns:
The first encountered instance of
wrapper_classin the wrapper chain, orNoneif it is not present (or if a cycle is detected).
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.