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.

  • 9
    $\begingroup$ 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." $\endgroup$ – Ciprian Tomoiagă Jan 17 '17 at 13:50
  • 5
    $\begingroup$ 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 $\endgroup$ – S.N. Jun 7 '18 at 6:22
  • $\begingroup$ @S.N. For many students of reinforcement learning, Sutton and Barto is the first book they read. $\endgroup$ – Neil G Jun 7 '18 at 6:25
  • 3
    $\begingroup$ @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. $\endgroup$ – Douglas De Rizzo Meneghetti Feb 14 '19 at 11:03
  • 1
    $\begingroup$ @AlbertChen "So in this case, it does depend on exploration or not": No, because both algorithms explore. The difference is how Q is updated. $\endgroup$ – Neil G Jul 15 '19 at 20:57

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.


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, we took using our current policy $\pi$.
In off-policy learning the $Q(s,a)$ function is learned from 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'$.


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

  • 1
    $\begingroup$ 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). $\endgroup$ – Neil Slater Sep 1 '17 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."

  • $\begingroup$ if you follow this description, it's not easy to tell why Q-learning is off-policy $\endgroup$ – Albert Chen Jul 10 '19 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.