flatland.envs.rail_env_state_machine_wrapper module

flatland.envs.rail_env_state_machine_wrapper module#

flatland.envs.rail_env_state_machine_wrapper.RailEnvStateMachineWrapper(env: AbstractRailEnv, skip_state_machine_update: bool = False) → AbstractRailEnv[source]#

Patches env’s class in place so its step() also updates agent.state/agent.state_machine.

AbstractRailEnv.step() never reads/writes agent.state/agent.state_machine itself - position/ speed/reward/done control flow, get_info_dict()’s state/action_required fields, and every built-in obs builder/predictor are all derived instead via EnvAgent.derived_state() (agent_utils.py), which reconstructs the equivalent TrainState purely from other, always-live agent attributes - unaffected either way, wrapped or not (see its own docstring for the one timing subtlety this has, and when a caller needs to work around it rather than just calling it directly). This wrapper exists only for a caller that needs agent.state/agent.state_machine themselves - real TrainStateMachine transition/signal internals, not just a TrainState value - such as a test asserting on state-machine behavior directly, or the Replay/run_replay_config test framework. _StateMachineUpdateMixin.step() below achieves this by delegating to super().step() and running the state machine update on the way out, after the real step has fully completed - since nothing in the (obs, rewards, dones, info) tuple step() returns depends on agent.state/ agent.state_machine (both derived exclusively via derived_state(), per above), it doesn’t matter whether the update runs before or after that tuple is built, only that it runs once per step. Without wrapping, agent.state_machine itself is never touched at all - permanently frozen at its __init__ default (TrainState.WAITING), not None/undefined (state_machine.is_live is False for exactly this reason). agent.state (the EnvAgent property, not agent.state_machine.state) falls back to derived_state() in that case instead of surfacing the frozen default directly - see its own docstring.

Idempotent: wrapping an already-wrapped env just updates skip_state_machine_update in place rather than double-wrapping. Returns the same instance (not a copy), for chaining convenience.

Parameters#

envAbstractRailEnv

The env to patch in place.

skip_state_machine_updatebool

Wrap but never actually update the state machine - lets a caller keep a uniform “always wrapped” call site while still toggling the behavior off.