v9.1.1
Menu

SARSA vs Q-Learning Targets

mediumPROTemporal-Difference Learning and ControlReinforcement LearningRL 1: Foundations

Problem Statement

Compute both control targets for a transition, given the next-state action values q_next (a list over actions), the on-policy next action index a_next, reward r and discount gamma:

  • SARSA (on-policy): target = r + gamma * q_next[a_next]
  • Q-Learning (off-policy): target = r + gamma * max(q_next)

Implement control_targets(reward, gamma, q_next, a_next) returning the tuple (sarsa, qlearning).

Example

Input
control_targets(1.0, 0.9, [2.0, 5.0], 0)
Output
(2.8, 5.5)
Reasoning
  • Identify the input parameters: reward r=1.0r = 1.0, discount factor γ=0.9\gamma = 0.9, next-state action values qnext=[2.0,5.0]q_{\text{next}} = [2.0, 5.0], and the on-policy action index anext=0a_{\text{next}} = 0.
  • Compute the SARSA target by using the value of the specific action taken in the next state (index 0): sarsa=1.0+0.9×2.0=1.0+1.8=2.8sarsa = 1.0 + 0.9 \times 2.0 = 1.0 + 1.8 = 2.8.
  • Compute the Q-Learning target by using the maximum value among all possible next actions to represent the optimal policy: qlearning=1.0+0.9×max⁡(2.0,5.0)=1.0+0.9×5.0=1.0+4.5=5.5q_{\text{learning}} = 1.0 + 0.9 \times \max(2.0, 5.0) = 1.0 + 0.9 \times 5.0 = 1.0 + 4.5 = 5.5.
  • The final output is (2.8, 5.5)

Constraints

  • 0 <= a_next < len(q_next).
  • Return a tuple of two floats.

Learn this in

More from RL 1: Foundations

PRO

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

Sign in to run your code against the test cases.