Skip to content

Terminations

Termination functions: does a transition land in a terminal state?

An Environment's termination_fn has the signature shared across this module, navix.rewards and navix.events:

fn(prev_state: State, action: Array, state: State) -> Array
  • prev_state is $s_t$ (before the action), action is $a_t$, state is $s_{t+1}$ (after). Most conditions only need state; a few need prev_state to detect a change this step (e.g. a door going closed -> open), because navix clears the per-step event record every step.
  • The return is a boolean scalar bool[] (True = terminate). Truncation at max_steps is added separately by Environment.termination, so a termination function only reports genuine absorbing states.

Most functions here are thin, correctly-typed wrappers around the matching detector in navix.events (which holds the precise semantics); compose ORs several into one, and DEFAULT_TERMINATION is the goal / lava / ball-hit combination MiniGrid uses.

The termination_fn an Environment uses unless overridden: the episode ends on reaching the goal, falling into lava, or being hit by a moving ball (MiniGrid's default terminal conditions).

Merges a termination flag and a truncation flag into a single StepType value. Used by Environment.termination.

Parameters:

Name Type Description Default
terminated Array

bool[], True if termination_fn fired.

required
truncated Array

bool[], True if t >= max_steps.

required

Returns:

Name Type Description
Array Array

i32[] - 2 (TERMINATION) if terminated, else 1

Array

(TRUNCATION) if truncated, else 0 (TRANSITION).

Array

Termination wins over truncation.

Combines several termination functions into one.

Parameters:

Name Type Description Default
*term_functions Callable

termination functions to combine, each (prev_state, action, state) -> bool[].

()
operator Callable

reduces the stacked results to a scalar. Default jnp.any - terminate if any condition fires.

any

Returns:

Name Type Description
Callable Callable

a single (prev_state, action, state) -> bool[]

Callable

function. This is how DEFAULT_TERMINATION and the per-task

Callable

combinations are built.

Fetch's termination: any Key/Ball pickup this step ends the episode, right or wrong (see rewards.on_target_fetched for which one determines the reward).

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.

Check if the ball has hit something using the ball_hit event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the ball has hit something.

Check if any ball was picked up this step, using the ball_pickup event - for environments (e.g. DynamicObstacles) where Ball represents a hazard to avoid touching, not an object to carry: composing this alongside on_ball_hit makes picking one up end the episode the same way walking into it already does, rather than silently removing it from play now that Ball is Pickable.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether any ball was picked up.

Check if any box was picked up this step, using the box_pickup event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether any box was picked up.

Check if the action done has been called in front of a Door object with the correct colour.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the action done has been called in front of a Door object with the correct colour.

Check if any door was opened this step, using the door_opening event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether any door was opened.

Check if the goal has been reached using the goal_reached event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the goal has been reached.

Check if the lava has fallen using the lava_fall event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the lava has fallen.

Check if the player reached Memory's wrong (failure) position, using the on_memory_failure event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the wrong position was reached.

Check if the player reached Memory's target position, using the on_memory_success event.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array indicating whether the target was reached.

RedBlueDoors' termination: ends the episode as soon as blue opens, whether that was a success (red already open) or a failure (blue opened first).

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.

PutNear's termination on a genuine drop attempt (success or failure - see rewards.on_put_near_success for which one this was).

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.

PutNear's failure-on-pickup termination: the wrong object was picked up this step.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.

GoToObject's success termination: the done action was taken while the player is orthogonally adjacent to the mission's target object - facing it is not required (matches MiniGrid; see events.on_target_done).

Returns:

Name Type Description
Array Array

bool[].

ObstructedMaze's termination: only picking up the specific mission-target Key/Ball ends the episode - unlike Fetch's on_any_target_pickup, a wrong pickup (the blocking Ball, say) does not end it here, matching MiniGrid's actual ObstructedMazeEnv (reward + termination only on picking up the target).

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.

GoToObject's failure termination: the toggle action was used at all.

Parameters:

Name Type Description Default
prev_state State

The previous state of the game.

required
action Array

The action taken by the player.

required
state State

The current state of the game.

required

Returns:

Name Type Description
Array Array

A boolean array.