# What is the difference between off-policy and on-policy learning?

Artificial intelligence website defines off-policy and on-policy learning as follows:

"An off-policy learner learns the value of the optimal policy independently of the agent's actions. Q-learning is an off-policy learner. An on-policy learner learns the value of the policy being carried out by the agent including the exploration steps."

I would like to ask your clarification regarding this, because they don't seem to make any difference to me. Both the definitions seem like they are identical. What I actually understood are the model-free and model-based learning, and I don't know if they have anything to do with the ones in question.

How is it possible that the optimal policy is learned independently of the agent's actions? Isn't the policy learned when the agent performs the actions?

First of all, there's no reason that an agent has to do the greedy action; Agents can explore or they can follow options. This is not what separates on-policy from off-policy learning.

The reason that Q-learning is off-policy is that it updates its Q-values using the Q-value of the next state $$s'$$ and the greedy action $$a'$$. In other words, it estimates the return (total discounted future reward) for state-action pairs assuming a greedy policy were followed despite the fact that it's not following a greedy policy.

The reason that SARSA is on-policy is that it updates its Q-values using the Q-value of the next state $$s'$$ and the current policy's action $$a''$$. It estimates the return for state-action pairs assuming the current policy continues to be followed.

The distinction disappears if the current policy is a greedy policy. However, such an agent would not be good since it never explores.

Have you looked at the book available for free online? Richard S. Sutton and Andrew G. Barto. Reinforcement learning: An introduction. Second edition, MIT Press, Cambridge, MA, 2018.

• nice explanation! Your example on Q-learning is better formulated that in Sutton's book, which says: "the learned action-value function, Q, directly approximates Q* , the optimal action-value function, independent of the policy being followed. This dramatically simplifies the analysis of the algorithm and enabled early convergence proofs. The policy still has an effect in that it determines which state-action pairs are visited and updated." Jan 17, 2017 at 13:50
• In general, I don't find Sutton and Barto very readable at all. I find the explanations they offer are not very comprehensible. I am not sure why their book gets recommended all over the place
– N.S.
Jun 7, 2018 at 6:22
• @JakubArnold the original Sutton & Barto book is from 1998 and it does not cover deep reinforcement learning. The 2nd edition only mentions things like AlphaGo, but the focus of the book is in more classical approaches. If you want more RL resources, take a look at this list. I suggest David Silver's videos and Puterman's book as they are more approachable. For more theoretical material, I recommend Bertsekas' books. Take a look at the Spinning Up website for DRL algorithms and links to original papers. Feb 14, 2019 at 11:03
• @AlbertChen "So in this case, it does depend on exploration or not": No, because both algorithms explore. The difference is how Q is updated. Jul 15, 2019 at 20:57
• @peterbe Please just ask other questions. There are many errors in your reasoning. Aug 20, 2021 at 8:25

First of all, what actually policy (denoted by $$\pi$$) means?
Policy specifies an action $$a$$, that is taken in a state $$s$$ (or more precisely, $$\pi$$ is a probability, that an action $$a$$ is taken in a state $$s$$).

Second, what types of learning do we have?
1. Evaluate $$Q(s,a)$$ function: predict sum of future discounted rewards, where $$a$$ is an action and $$s$$ is a state.
2. Find $$\pi$$ (actually, $$\pi(a|s)$$), that yields to a maximum reward.

Back to the original question. On-policy and off-policy learning is only related to the first task: evaluating $$Q(s,a)$$.

The difference is this:
In on-policy learning, the $$Q(s,a)$$ function is learned from actions that we took using our current policy $$\pi(a|s)$$.
In off-policy learning, the $$Q(s,a)$$ function is learned from taking different actions (for example, random actions). We even don't need a policy at all!

This is the update function for the on-policy SARSA algorithm: $$Q(s,a) \leftarrow Q(s,a)+\alpha(r+\gamma Q(s',a')-Q(s,a))$$, where $$a'$$ is the action, that was taken according to policy $$\pi$$.

Compare it with the update function for the off-policy Q-learning algorithm: $$Q(s,a) \leftarrow Q(s,a)+\alpha(r+\gamma \max_{a'}Q(s',a')-Q(s,a))$$, where $$a'$$ are all actions, that were probed in state $$s'$$.

• "In off-policy learning, the $Q(s,a)$ function is learned from taking different actions (for example, random actions). We even don't need a policy at all!" - How can you not have a policy? Isn't even taking random actions technically a policy? Also it would be helpful if you could illucidate the difference in the Q update between SARSA and Q-Learning that show what makes either on policy or off policy.
– alex
Jun 8, 2020 at 3:43
• @alex If I understand correctly, a policy is a function of current state and environment, while taking random actions would not take current state/environment into account. I guess you could have a function that just outputs random actions no matter the input, but then whether that's an actual "policy" is debatable. Mar 16, 2021 at 21:49
• So can we say that algorithms like policy gradient are a mix of on and off-policy learning? Because there is this exploration-exploitation rate that tells the RL when to be greedy and when to explore Nov 11, 2021 at 18:26
• I don't really understand this distinction. Isn't $a' = \pi(a'|s')$ just equal to $a' = max_{a'} Q(s', a')$ in the off policy case? That's still a policy, just not one you need to store separately from the Q function. Does the distinction lie in the way we often keep a separate target Q in some flavors of RL that we only update with information from the working Q every $k$ iterations? Nov 15, 2021 at 17:12
• @PavelKomarov Let's assume we use $\varepsilon$-greedy policy. Then in the on-policy formula $a'$ in $Q(s', a')$ stands for the $\varepsilon$-greedy action which is different from $\mathrm{argmax}_{a'} Q(s', a')$. Mar 4 at 19:09

On-policy methods estimate the value of a policy while using it for control.

In off-policy methods, the policy used to generate behaviour, called the behaviour policy, may be unrelated to the policy that is evaluated and improved, called the estimation policy.

An advantage of this seperation is that the estimation policy may be deterministic (e.g. greedy), while the behaviour policy can continue to sample all possible actions.

For further details, see sections 5.4 and 5.6 of the book Reinforcement Learning: An Introduction by Barto and Sutton, first edition.

The difference between Off-policy and On-policy methods is that with the first you do not need to follow any specific policy, your agent could even behave randomly and despite this, off-policy methods can still find the optimal policy. On the other hand on-policy methods are dependent on the policy used. In the case of Q-Learning, which is off-policy, it will find the optimal policy independent of the policy used during exploration, however this is true only when you visit the different states enough times. You can find in the original paper by Watkins the actual proof that shows this very nice property of Q-Learning. There is however a trade-off and that is off-policy methods tend to be slower than on-policy methods. Here a link with other interesting summary of the properties of both types of methods

• Off-policy methods are not only slower, but can be unstable when combined with bootstrapping (i.e. how Q-learning builds estimates from each other) and function approximators (e.g. neural networks). Sep 1, 2017 at 16:50

From the Sutton book: "The on-policy approach in the preceding section is actually a compromise—it learns action values not for the optimal policy, but for a near-optimal policy that still explores. A more straightforward approach is to use two policies, one that is learned about and that becomes the optimal policy, and one that is more exploratory and is used to generate behavior. The policy being learned about is called the target policy, and the policy used to generate behavior is called the behavior policy. In this case we say that learning is from data “o↵” the target policy, and the overall process is termed o↵-policy learning."

• if you follow this description, it's not easy to tell why Q-learning is off-policy Jul 10, 2019 at 12:46

This is the recursive version of the Q-function (according to Bellman equation):

$$Q_\pi(s_t,a_t)=\mathbb{E}_{\,r_t,\,s_{t+1}\,\sim\,E}\left[r(s_t,a_t)+\gamma\,\mathbb{E}_{\,a_{t+1}\,\sim\,\pi}\left[Q_\pi(s_{t+1}, a_{t+1})\right]\right]$$

Notice that the outer expectation exists because the current reward and the next state are sampled ($$\sim)$$ from the environment ($$E$$). The inner expectation exists because the Q-value for the next state depends on the next action. If you your policy is deterministic, there is no inner expectation, our $$a_{t+1}$$ is a known value that depends only on the next state, let's call it $$A(s_{t+1})$$:

$$Q_{det}(s_t,a_t)=\mathbb{E}_{\,r_t,\,s_{t+1}\,\sim\,E}\left[r(s_t,a_t)+\gamma\,Q_{det}(s_{t+1}, A(s_{t+1})\right]$$

This means the Q-value depends only on the environment for deterministic policies.

The optimal policy is always deterministic (it always take the action that leads to higher expected reward) and Q-learning directly approximates the optimal policy. Therefore the Q-values of this greedy agent depends only on the environment.

Well, if the Q-values depends only on the environment, it doesn't matter how I explore the environment, that is, I can use an exploratory behaviour policy.

On-policy learning: The same (ϵ-greedy) policy that is evaluated and improved is also used to select actions. For eg. SARSA TD Learning Algorithm

Off-policy learning: The (greedy) policy that is evaluated and improved is different from the (ϵ-greedy) policy that is used to select actions. For eg. Q-Learning Algorithm