PIXELBANKv9.1.0
Menu

TD(0) Over a Transition Stream

Problem Statement

Run TD(0) prediction for a single state over a stream of (reward, v_next) transitions, starting from value v0, learning rate alpha, discount gamma. Apply updates sequentially and return the final value.

V←V+α [ r+γ vnextβˆ’V ]V \leftarrow V + \alpha\,[\, r + \gamma\, v_{next} - V\,]

Implement td0_stream(v0, transitions, gamma, alpha) where transitions is a list of (reward, v_next) pairs.

Example:

Input:
td0_stream(0.0, [(1.0, 0.0), (1.0, 0.0)], 0.9, 0.5)
Output:
0.75
Reasoning:
  • Initialize the value estimate VV to the starting value v0=0.0v_0 = 0.0.
  • Process the first transition (r=1.0,vnext=0.0)(r=1.0, v_{next}=0.0): calculate the TD error as 1.0+0.9β‹…0.0βˆ’0.0=1.01.0 + 0.9 \cdot 0.0 - 0.0 = 1.0, then update VV by adding Ξ±β‹…error=0.5β‹…1.0\alpha \cdot \text{error} = 0.5 \cdot 1.0, resulting in V=0.0+0.5=0.5V = 0.0 + 0.5 = 0.5.
  • Process the second transition (r=1.0,vnext=0.0)(r=1.0, v_{next}=0.0): calculate the new TD error as 1.0+0.9β‹…0.0βˆ’0.5=0.51.0 + 0.9 \cdot 0.0 - 0.5 = 0.5, then update VV by adding Ξ±β‹…error=0.5β‹…0.5=0.25\alpha \cdot \text{error} = 0.5 \cdot 0.5 = 0.25, resulting in V=0.5+0.25=0.75V = 0.5 + 0.25 = 0.75.
  • The final output is 0.75

Constraints:

  • Apply transitions in order.
  • Empty stream returns v0 unchanged.
  • Return a float.
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
TD(0) Over a Transition Stream - Medium | PixelBank