PIXELBANKv9.1.0
Menu

Problem Statement

Expected SARSA replaces the sampled next action with the expectation under the next-state policy:

target=r+γ∑aπ(a∣s′) Q(s′,a)target = r + \gamma \sum_a \pi(a\mid s')\, Q(s', a)

Given reward r, discount gamma, next-state policy pi_next and action values q_next (aligned), implement expected_sarsa_target(reward, gamma, pi_next, q_next) returning a float.

Example:

Input:
expected_sarsa_target(1.0, 0.9, [0.5, 0.5], [2.0, 4.0])
Output:
3.7
Reasoning:
  • Calculate the expected action value for the next state by taking the dot product of the policy probabilities and the corresponding Q-values: 0.5×2.0+0.5×4.0=1.0+2.0=3.00.5 \times 2.0 + 0.5 \times 4.0 = 1.0 + 2.0 = 3.0.
  • Multiply this expected value by the discount factor γ\gamma to account for the temporal distance of future rewards: 0.9×3.0=2.70.9 \times 3.0 = 2.7.
  • Add the immediate reward to the discounted expected future value to form the target: 1.0+2.7=3.71.0 + 2.7 = 3.7.
  • The final output is 3.7

Constraints:

  • len(pi_next) == len(q_next), pi_next sums to 1.
  • 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.
Expected SARSA Target - Medium | PixelBank