research29 min studyLesson 641 in Engineering

Environment transitions: preserve the boundary that the learner needs

Define observation, action, reward, termination, and truncation records before deriving bootstrap targets or resetting an episode.

On this page

What you’ll understand

  • Distinguish task termination from an external collection cutoff.
  • Construct bootstrap targets from the correct final observation.
  • Specify trajectory identity and observation semantics for a learner.

A learner can consume valid-shaped arrays and still learn the wrong task if a collector merges every episode ending into one Boolean. Gymnasium 1.1.1 step returns an observation, reward, terminated flag, truncated flag, and information dictionary; reset has a different role and returns a new initial observation. Preserve these distinctions through serialization, vectorization, and storage. We discuss value bootstrapping for a task whose continuation after an external cutoff remains meaningful, rather than assuming every invalid simulator state should receive a value estimate.

A transition record is a semantic contract

Record the observation or history used to choose the action, the actual executed action, the resulting reward, and the post-action observation before a new episode replaces it. Include environment identity, episode identity, timestep, action mask where relevant, and preprocessing version. An observation can be partial: a value function of the observation alone then need not represent a Markov state value. Recurrent policies require the appropriate history or hidden-state contract. A tensor’s shape says nothing about whether it belongs to the correct episode or follows the executed action.

Validate action and observation spaces, dtypes, reward conventions, and exceptional outcomes. A simulator timeout, a successful task completion, and an infrastructure failure are not interchangeable rewards. Decide whether a failure produces a valid task transition, an externally cut fragment, or unusable data, and report it separately. Logging privileged simulator state in an information dictionary does not mean the acting policy may consume it. Keep actor-visible inputs distinct from diagnostics so that offline evaluation does not accidentally grant the policy information unavailable during deployment.

Gt=∑k=0T−t−1γkrt+kG_t=\sum_{k=0}^{T-t-1}\gamma^k r_{t+k}
For a finite task episode ending at T, rewards use one stated unit and 0≤γ≤1 is dimensionless. This episodic return contains the task’s rewards only; a collector fragment ending before task termination needs an explicitly defined continuation estimate.↔ Scroll wide equations horizontally.

Termination and truncation imply different bootstrap choices

Termination means the task’s defined terminal condition has been reached. External truncation stops collection without necessarily ending the underlying decision process, so a continuing-task value target can bootstrap from the true post-action observation. A finite horizon that is part of the task is different: its end is task termination, and remaining time must be represented when needed for the Markov description. Replacing both flags with “done” loses the information required to distinguish those objectives. If both flags are true, the terminal condition still removes continuation under the target defined here.

A bootstrap mask is not permission to connect two episodes. At an external cutoff, the target can use the value of the old episode’s final observation while the next collected observation comes from reset. Recurrent state and multistep traces must respect that episode boundary. Likewise, a fixed-length collector fragment may bootstrap without requiring an environment reset. Define the task, reset boundary, and stored sequence boundary separately, then test them with observations carrying unmistakable episode and timestep identifiers.

yt=rt+γbtV(ot+1+),bt=1−1[terminatedt]y_t=r_t+\gamma b_tV(o^{+}_{t+1}),\qquad b_t=1-\mathbf1[\mathrm{terminated}_t]
This one-step target assumes a meaningful continuation value at the true post-action observation o+, with reward and V in compatible discounted-return units. External truncation alone retains bootstrapping; a task terminal state does not. It is not a rule to bootstrap through invalid simulator failures or into reset observations.↔ Scroll wide equations horizontally.

Test boundary arithmetic before measuring learning curves

Create fixtures for ordinary continuation, terminal success, terminal failure, external time limit, and collector-only fragment cuts. Check the stored final observation and masks directly, not only a noisy episode-return chart. Include a case where reset observations have very different values from final observations, since that makes accidental substitution obvious. A one-step contract error can contaminate every return and advantage afterward. Preserve enough provenance to replay the transition adapter independently from the stochastic optimizer and policy update.

The Markov lab illustrates only a deterministic absorbing-state limit. Reset, explicitly set transition p=1 and q=0, predict the probability of state one after one Step, then Step once. For a separate comparison Reset and explicitly set p=0 and q=0 before predicting and taking one Step. The probabilities are one and zero from the fixed initial state zero. This two-state distribution model has no actions, rewards, Gymnasium flags, or value targets; it cannot validate the collector contract or decide which task condition should terminate.

One reward can have two different valid targets

A transition gives reward two with discount 0.9. Its true final observation has value ten. Compare a continuing task externally truncated by collection with a genuinely terminated task; assume the value estimate is otherwise suitable.

  1. For external truncation without termination, the bootstrap mask is one. The target is 2+0.9×10=11. Resetting collection afterward does not erase the continuing task value represented at this final observation.

  2. For true termination, the mask is zero and the target is two. A finite task horizon belongs to this case when it is genuinely part of the task definition, rather than an external sampling limit.

  3. Neither target uses a new reset observation. If a vector adapter returns the new initial observation in the ordinary observation slot, retrieve the actual final observation through the pinned adapter contract before constructing the target.

The result

The targets are eleven for the stated external cutoff and two for task termination. Their difference encodes the objective, not an arbitrary API preference.

Explore the model

Interactive lab

Interactive lab · markov

An absorbing limit is a small state-transition illustration

Use independent Reset → explicitly set BOTH p and q → predict → Step runs for (1,0) and (0,0). One Step advances one transition from initial state zero, giving state-one probabilities one and zero. No actions, rewards, episode flags, or bootstrapping are modeled.

An absorbing limit is a small state-transition illustrationExact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Current readings: P(state 1) at step 0: 0%; Stationary P(state 1): 60%; Chain behavior: Monotone convergence. All parameters are adjustable with the labeled controls below.Two-state Markov distribution after 0 steps: state zero probability 1, state one probability 0.Initial distribution (1, 0) · step 0p = 0.3q = 0.201100%0%1−p1−qP(0)P(1)Monotone convergence
P(state 0)P(state 1)Stationary distribution
P(state 1) at step 0
0%
Stationary P(state 1)
60%
Chain behavior
Monotone convergence
t = 0 s

Exact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA mathematical transition model is useful only after the real task and collection boundaries are defined.

Check your understanding

A continuing task reaches an external time limit without termination, and its valid final observation is available. Which target matches the stated contract?

Try it yourself

A transition has reward one, discount 0.8, and suitable final-observation value five. Compute the target for external truncation and for termination. Why is the value of a new reset observation irrelevant?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Apply the mask to continuation, then distinguish two observations that can share the same tensor shape.

Compare with the solution

External truncation gives 1+0.8×5=5. Termination gives one. A reset observation begins a different episode and is not the post-action continuation of the stored transition. Substituting it changes the target even though array shapes remain valid. Record task horizon, reset mode, final-observation provenance, and any recurrent-state boundary so the learner can construct the intended target consistently. Infrastructure errors lacking a meaningful continuation state need their own explicit data policy.

Take it with you

  • Preserve task termination and external truncation separately.
  • Bootstrap from the true post-action observation when the task permits continuation.
  • Keep episode, reset, and collector-fragment boundaries distinct.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.