Q-learning and SARSA updates

ImplementEasy15 min13 checks

reinforcement-learningq-learningsarsaexploration

Problem

Tabular Q-learning and SARSA learn a table of action values from one transition at a time. Both move Q[s, a] toward a target. Q-learning uses the best next action in the target. SARSA uses the next action that the agent really takes. An epsilon-greedy policy picks the actions while the table learns.

Task

Write three functions.

  1. q_learning_update(Q, s, a, r, s_next, done, alpha=0.1, gamma=0.99) returns the table after one Q-learning update.
  2. sarsa_update(Q, s, a, r, s_next, a_next, done, alpha=0.1, gamma=0.99) returns the table after one SARSA update.
  3. epsilon_greedy(Q, s, epsilon, rng) returns the action for state s.

Both updates change only the entry Q[s, a]:

Q[s, a] \leftarrow Q[s, a] + \alpha \left(y - Q[s, a]\right)

The target y for Q-learning is r + \gamma \max_{a'} Q[s', a']. The target for SARSA is r + \gamma \, Q[s', a_{next}]. When done is True, the target is r only, with no term for the next state. The target uses the values of Q before the update.

epsilon_greedy uses the generator rng in this exact order:

  1. Draw u = rng.random().
  2. If u < epsilon, draw and return rng.integers(n_actions).
  3. Else return the action with the largest Q[s]. On a tie, return the lowest index.

Input and output

NameTypeShapeMeaning
Qfloat array(n_states, n_actions)the action values
s, s_nextintthe state and the next state
a, a_nextintthe action and the next action
rfloatthe reward of the transition
doneboolTrue when s_next is terminal
alphafloatthe step size, from 0 to 1
gammafloatthe discount, from 0 to 1
epsilonfloatthe probability of a random action, from 0 to 1
rngnp.random.Generatorthe random generator
  • q_learning_update and sarsa_update return a new float array of shape (n_states, n_actions).
  • epsilon_greedy returns a Python int.

Constraints

  • 1 <= n_states <= 1000 and 1 <= n_actions <= 20.
  • s_next can be equal to s.
  • Do not change the input table. Return a new table.
  • Use numpy only.

Examples

pythonQ = np.array([[0.0, 1.0], [2.0, -1.0], [0.5, 0.5]])
q_learning_update(Q, 0, 1, -1.0, 1, False, alpha=0.5, gamma=0.9)
sarsa_update(Q, 0, 1, -1.0, 1, 1, False, alpha=0.5, gamma=0.9)
q_learning_update(Q, 0, 1, -1.0, 1, True, alpha=0.5, gamma=0.9)
text[[ 0.   0.9]
 [ 2.  -1. ]
 [ 0.5  0.5]]
[[ 0.   -0.45]
 [ 2.   -1.  ]
 [ 0.5   0.5 ]]
[[ 0.   0. ]
 [ 2.  -1. ]
 [ 0.5  0.5]]
pythonrng = np.random.default_rng(7)
Q = np.array([[1.0, 3.0, 3.0, 0.0]])
[epsilon_greedy(Q, 0, 0.5, rng) for _ in range(12)]
text[1, 1, 1, 0, 1, 1, 1, 1, 3, 1, 3, 1]

Hints

4 hints

    Solution and notes

    Locked

    Pass every check with Submit to open the reference solution and the notes.

    Run it on your computer

    Clone the repository, install numpy and run the same checks in a terminal:

    python3 scripts/practice/run.py q-learning-update my_answer.py
    python3 scripts/practice/run.py q-learning-update my_answer.py --examples

    Start my_answer.py from starter.py.

    Your code

    Python loads on the first run

    Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.

    Keyboard shortcuts
    Ctrl Enter
    Run the examples
    Ctrl Shift Enter
    Submit
    Tab / Shift Tab
    Indent or outdent the lines
    Esc then Tab
    Move the focus out of the editor
    Alt H
    Show the next hint

    On a Mac, use Cmd in place of Ctrl.

    Select Run to check the examples, or Submit to run every check.