Core Wrappers¶
API Reference¶
masa.common.wrappers.TimeLimit ¶
Bases: ConstraintPersistentWrapper
Episode time-limit wrapper compatible with constraint persistence.
This is a minimal time-limit wrapper similar in spirit to Gymnasium's
time-limit handling. It sets the truncated flag to True once the
number of elapsed steps reaches _max_episode_steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Base environment to wrap. |
required |
max_episode_steps
|
int
|
Maximum number of steps per episode. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
_max_episode_steps |
Configured time limit in steps. |
|
_elapsed_steps |
Counter of steps elapsed in the current episode. |
Source code in masa/common/wrappers.py
step ¶
Step the environment and apply time-limit truncation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Action forwarded to the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
|
A 5-tuple |
|
|
the time limit is reached, |
Source code in masa/common/wrappers.py
reset ¶
Reset the environment and the elapsed step counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Forwarded to the underlying environment's |
{}
|
Returns:
| Type | Description |
|---|---|
|
The underlying environment's |
Source code in masa/common/wrappers.py
masa.common.wrappers.ConstraintMonitor ¶
Bases: ConstraintPersistentWrapper
Monitor that injects constraint metadata and metrics into info.
This wrapper requires the wrapped environment to be a
masa.common.constraints.base.BaseConstraintEnv, so it can query:
masa.common.constraints.base.BaseConstraintEnv.constraint_typemasa.common.constraints.base.BaseConstraintEnv.constraint_step_metricsmasa.common.constraints.base.BaseConstraintEnv.constraint_episode_metrics
On each step, the wrapper writes:
info["constraint"]["type"]: the constraint type stringinfo["constraint"]["step"]: step-level metrics (cheap, safe)info["constraint"]["episode"]: episode-level metrics (when available)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Constraint environment to wrap. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in masa/common/wrappers.py
reset ¶
Reset and populate initial constraint metadata in info.
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 and populate constraint metrics in info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Action forwarded to the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
|
A 5-tuple |
|
|
The returned |
|
|
class docstring. |
Source code in masa/common/wrappers.py
_step_metrics ¶
Read step-level constraint metrics.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
A dictionary of step-level metrics. If the underlying constraint raises |
Dict[str, float]
|
an exception, returns an empty dictionary. |
Source code in masa/common/wrappers.py
_episode_metrics ¶
Read episode-level constraint metrics.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
A dictionary of episode-level metrics. If the underlying constraint raises |
Dict[str, float]
|
an exception, returns an empty dictionary. |
Source code in masa/common/wrappers.py
masa.common.wrappers.RewardMonitor ¶
Bases: ConstraintPersistentWrapper
Monitor that injects reward/length metrics into info.
This wrapper tracks:
- per-step immediate reward in
info["metrics"]["step"]["reward"] - episode return/length at episode end in
info["metrics"]["episode"]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Base environment to wrap. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
total_reward |
Accumulated episode reward since last reset. |
|
total_steps |
Number of steps taken since last reset. |
Source code in masa/common/wrappers.py
reset ¶
Reset reward counters and forward reset to the underlying env.
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 update reward metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Action forwarded to the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
|
A 5-tuple |
|
|
On episode end, |
|
|
episode return and length. |
Source code in masa/common/wrappers.py
_episode_metrics ¶
Compute episode-level reward metrics.
Returns:
| Type | Description |
|---|---|
|
A dictionary with keys: |
|
|
|
|