PIXELBANKv9.1.0
Menu

Async In-Place Value Iteration Sweep

Problem Statement

Perform one in-place (asynchronous) value-iteration sweep: states are updated left to right, and each update immediately uses the freshest values of earlier states. For each state you are given a list of actions, each {"reward": r, "probs": [...], "next": [...]} where next are next-state indices aligned with probs.

V(s)←max⁡a[r+γ∑iprobsi V(nexti)]V(s) \leftarrow \max_a \left[ r + \gamma \sum_i probs_i\, V(next_i) \right]

Mutate and return the values list. Implement vi_sweep_inplace(states, gamma, V).

Example:

Input:
vi_sweep_inplace([[{"reward":0.0,"probs":[1.0],"next":[1]}],[{"reward":1.0,"probs":[1.0],"next":[1]}]], 0.9, [0.0, 0.0])
Output:
[0.0, 1.0]
Reasoning:
  • Initialize State 0: The sweep begins with state s=0s=0. It has one action with reward r=0.0r=0.0, probability 1.01.0, and next state 11. Since the update is in-place, it uses the current value of state 1, which is initially V[1]=0.0V[1] = 0.0.
  • Compute Value for State 0: Calculate the Q-value for the single action: Q=0.0+0.9×(1.0×0.0)=0.0Q = 0.0 + 0.9 \times (1.0 \times 0.0) = 0.0. This becomes the new value for state 0, so V[0]V[0] is updated to 0.00.0.
  • Process State 1: Move to state s=1s=1. It has one action with reward r=1.0r=1.0, probability 1.01.0, and next state 11 (a self-loop). The calculation uses the current value of state 1, which is still V[1]=0.0V[1] = 0.0 because state 1 has not been updated yet in this sweep.
  • Compute Value for State 1: Calculate the Q-value: Q=1.0+0.9×(1.0×0.0)=1.0Q = 1.0 + 0.9 \times (1.0 \times 0.0) = 1.0. This becomes the new value for state 1, so V[1]V[1] is updated to 1.01.0.
  • The final output is [0.0, 1.0]

Constraints:

  • len(states) == len(V); update states 0..n-1 in order, in place.
  • Later states in the same sweep see earlier states' new values.
  • Return the same (mutated) list.
🔒

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.
Async In-Place Value Iteration Sweep - Medium | PixelBank