Introducing Q-Learning

What is Q-Learning?

Q-Learning is an off-policy value-based method that uses a TD approach to train its action-value function:

Q-Learning is the algorithm we use to train our Q-function, an action-value function that determines the value of being at a particular state and taking a specific action at that state.

Q-function
Given a state and action, our Q Function outputs a state-action value (also called Q-value)

The Q comes from “the Quality” (the value) of that action at that state.

Let’s recap the difference between value and reward:

Internally, our Q-function has a Q-table, a table where each cell corresponds to a state-action pair value. Think of this Q-table as the memory or cheat sheet of our Q-function.

Let’s go through an example of a maze.

Maze example

The Q-table is initialized. That’s why all values are = 0. This table contains, for each state, the four state-action values.

Maze example

Here we see that the state-action value of the initial state and going up is 0:

Maze example

Therefore, Q-function contains a Q-table that has the value of each-state action pair. And given a state and action, our Q-function will search inside its Q-table to output the value.

Q-function

If we recap, Q-Learning is the RL algorithm that:

Link value policy

But, in the beginning, our Q-table is useless since it gives arbitrary values for each state-action pair (most of the time, we initialize the Q-table to 0). As the agent explores the environment and we update the Q-table, it will give us better and better approximations to the optimal policy.

Q-learning
We see here that with the training, our Q-table is better since, thanks to it, we can know the value of each state-action pair.

Now that we understand what Q-Learning, Q-function, and Q-table are, let’s dive deeper into the Q-Learning algorithm.

The Q-Learning algorithm

This is the Q-Learning pseudocode; let’s study each part and see how it works with a simple example before implementing it. Don’t be intimidated by it, it’s simpler than it looks! We’ll go over each step.

Q-learning

Step 1: We initialize the Q-table

Q-learning

We need to initialize the Q-table for each state-action pair. Most of the time, we initialize with values of 0.

Step 2: Choose action using epsilon-greedy strategy

Q-learning

Epsilon greedy strategy is a policy that handles the exploration/exploitation trade-off.

The idea is that we define the initial epsilon ɛ = 1.0:

At the beginning of the training, the probability of doing exploration will be huge since ɛ is very high, so most of the time, we’ll explore. But as the training goes on, and consequently our Q-table gets better and better in its estimations, we progressively reduce the epsilon value since we will need less and less exploration and more exploitation.

Q-learning

Step 3: Perform action At, gets reward Rt+1 and next state St+1

Q-learning

Step 4: Update Q(St, At)

Remember that in TD Learning, we update our policy or value function (depending on the RL method we choose) after one step of the interaction.

To produce our TD target, we used the immediate reward Rt+1R_{t+1} plus the discounted value of the next state best state-action pair (we call that bootstrap).

Q-learning

Therefore, our Q(St,At)Q(S_t, A_t) update formula goes like this:

Q-learning

It means that to update our Q(St,At)Q(S_t, A_t):

How do we form the TD target?

  1. We obtain the reward after taking the action Rt+1R_{t+1}.
  2. To get the best next-state-action pair value, we use a greedy policy to select the next best action. Note that this is not an epsilon-greedy policy, this will always take the action with the highest state-action value.

Then when the update of this Q-value is done, we start in a new state and select our action using a epsilon-greedy policy again.

This is why we say that Q Learning is an off-policy algorithm.

Off-policy vs On-policy

The difference is subtle:

For instance, with Q-Learning, the epsilon-greedy policy (acting policy), is different from the greedy policy that is used to select the best next-state action value to update our Q-value (updating policy).

Off-on policy
Acting Policy

Is different from the policy we use during the training part:

Off-on policy
Updating policy

For instance, with Sarsa, another value-based algorithm, the epsilon-greedy policy selects the next state-action pair, not a greedy policy.

Off-on policy
Sarsa
Off-on policy