Constraints¶
masa.common.constraints.base.Constraint ¶
Bases: Protocol
Protocol for stateful constraint monitors.
A Constraint is a monitor that consumes atomic proposition labels
at each step and maintains internal state (e.g., cumulative cost, whether an
LTL automaton is in an accepting/unsafe state, etc.).
Implementations are intended to be lightweight and compatible with
Gymnasium wrappers: call reset at episode start and update
after each environment transition using the label set from info["labels"].
Required interface
Implementations should provide:
reset: clear any episode state.update: incorporate the current label set.constraint_type: a stable identifier string for logging/dispatch.
Metrics interface
The protocol declares:
step_metricepisode_metric
constraint_type
property
¶
A stable identifier for the constraint (e.g., "cmdp", "ltl_safety").
reset ¶
update ¶
Update internal state given the current set of labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Iterable[str]
|
Iterable of atomic proposition strings active at the current
step (typically taken from |
required |
step_metric ¶
Return per-step logging metrics.
Metrics returned here should be:
- cheap to compute,
- non-destructive (do not mutate state),
- meaningful at any time step.
Examples include running cumulative cost, a per-step violation flag, a current probability estimate, etc.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary of scalar metrics (values should be JSON/log friendly). |
Source code in masa/common/constraints/base.py
episode_metric ¶
Return end-of-episode logging metrics.
This is intended to summarize what matters for evaluation/logging at episode termination (terminated or truncated).
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary of scalar metrics (values should be JSON/log friendly). |
Source code in masa/common/constraints/base.py
masa.common.constraints.base.BaseConstraintEnv ¶
Bases: Wrapper, Constraint
Common base wrapper for constraint-aware environments.
This wrapper enforces the MASA convention that the wrapped environment is a
LabelledEnv and provides info["labels"]
as a set (or frozenset) of atomic propositions at each step.
The wrapper:
- Delegates reset/step to the underlying environment.
- Extracts
labels = info.get("labels", set()). - Validates that
labelsis a set-like container of strings. - Calls
self._constraint.update(labels).
Attributes:
| Name | Type | Description |
|---|---|---|
env |
The wrapped Gymnasium environment (must be a |
|
_constraint |
The underlying constraint monitor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Notes
The properties label_fn and cost_fn are convenience accessors
for downstream algorithms. Depending on how wrappers are composed, these
may be None.
Initialize the wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Env
|
Base environment. Must already be wrapped as a
|
required |
constraint
|
Constraint
|
A constraint monitor implementing |
required |
**kw
|
Unused extra keyword arguments (kept for wrapper compatibility). |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in masa/common/constraints/base.py
cost_fn
property
¶
Expose the cost function if available.
Returns:
| Type | Description |
|---|---|
|
The underlying cost function if present on the wrapped stack, else |
|
|
|
label_fn
property
¶
Expose the labelling function if available.
Returns:
| Type | Description |
|---|---|
|
The environment labelling function if present, else |
constraint_type
property
¶
Constraint identifier forwarded from the underlying monitor.
reset ¶
Reset environment and constraint state.
This calls env.reset(...) and then resets and updates the constraint
using the initial label set in info["labels"].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Optional RNG seed forwarded to the base environment. |
None
|
options
|
Dict[str, Any] | None
|
Optional reset options forwarded to the base environment. |
None
|
Returns:
| Type | Description |
|---|---|
|
A tuple |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/constraints/base.py
step ¶
Step environment and update constraint from labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Any
|
Action to pass to the underlying environment. |
required |
Returns:
| Type | Description |
|---|---|
|
A 5-tuple |
|
|
the Gymnasium API. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/constraints/base.py
constraint_step_metrics ¶
Return per-step metrics from the underlying constraint.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary of scalar metrics. |
constraint_episode_metrics ¶
Return end-of-episode metrics from the underlying constraint.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary of scalar metrics. |
Next Steps¶
- CMDP - Budgeted Constrained MDP.
- LTL Safety - Safety fragment of LTL as a monitor and constraint.
- PCTL - A simple Probabilistic Computation Tree Logic constraint.
- Step-wise Probabilistic - Undiscounted probabilistic step-wise safety constraint.
- Reach Avoid - A simple reach-avoid constraint.
- ATL (Multi Agent) - Alternating Time Logic for Multi Agent Systems.