Answers.Body: <p>A typical and practical way to measure the convergence to <strong>some</strong> solution (so not necessarily the optimal one!) of any numerical iterative algorithm (such as RL algorithms) is to check if the current solution has not changed (much) with respect to the previous one. In your case, the solutions are value functions, so you could check if your algorithm has converged to some value function e.g. as follows</p>
  <p><span class="math-container">$$
  c(q_t, q_{t-1}, \epsilon) =
  \begin{cases}
  1,  &amp;\text{if } |q_t(s, a) - q_{t-1}(s, a)| &lt; \epsilon, \forall s \in S, a \in A \\
  0, &amp; \text{otherwise}
  \end{cases},
  \tag{1}\label{1}
  $$</span>
  where</p>
  <ul>
  <li><span class="math-container">$c$</span> is the &quot;convergence&quot; function (aka termination condition) that returns <span class="math-container">$1$</span> (true) if your RL algorithm has converged to some small enough neighbourhood of value functions (where those value functions are &quot;indistinguishable&quot;), and <span class="math-container">$0$</span> otherwise</li>
  <li><span class="math-container">$q_t$</span> is the value function at iteration <span class="math-container">$t$</span></li>
  <li><span class="math-container">$\epsilon$</span> is a threshold (aka precision or tolerance) value, which is a hyper-parameter that you can set depending on your &quot;tolerance&quot; (hence the name); this value is typically something like <span class="math-container">$10^{-6}$</span></li>
  </ul>
  <p>Of course, this requires that you keep track of two value functions.</p>
  <p>You can also define your &quot;convergence&quot; function <span class="math-container">$c$</span> in \ref{1} differently. For example, rather than using the absolute value, you could use the relative error, i.e. <span class="math-container">$\left|\frac{q_t(s, a) - q_{t-1}(s, a)}{q_t(s, a)} \right|$</span>. Moreover, given that RL algorithms are exploratory (i.e. stochastic) algorithms, the value function may not change (much) from one iteration to the other, but, in the next one, it could significantly change because of your exploratory/behavioural actions, so you may also want to take into account more iterations, i.e. after e.g. <span class="math-container">$N &gt; 1$</span> iterations, if the value function does not change much, then you could say (maybe probabilistically) that your RL algorithm has converged to some small neighbourhood of value functions in the space of value functions.</p>
  <p>Note that these approaches do not guarantee that your RL algorithm has converged to the <strong>global optimal</strong> value function, but to <strong>some locally optimal</strong> value function (or, more precisely, small neighborhood of value functions). <a href="https://ai.stackexchange.com/q/11679/2444">Q-learning is guaranteed to converge to the optimal value function in the tabular setting</a> (your setting), but this is in the limit; in practice, it is more difficult to know if Q-learning has converged to an optimal or near-optimal value function.</p>
  <p>Maybe you can also have a look at episodic returns of the policy derived from your final value function, but without upper and lower bounds on the optimal returns, you don't know much about the global optimality of your policy/value function.</p>
  <p>Yes, you can check if the policy makes the agent reach the goal, but many policies could do that job, i.e. that does not say that the policy is the best (or optimal) one, i.e. that's a necessary (provided the goal is reachable and the reward function models your actual goal) but not sufficient condition (for optimality). The optimality here is usually a function of the return (given that is what you are usually trying to optimize).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-14T15:10:04.563Z
Answers.Text: This answer is based on my general knowledge of numerical algorithms and not much on how people (in the RL community) determine when their Q-learning (or, in general, RL) algorithms converge. It's possible that people may be using other approaches to determine the convergence in practice. Right now, I don't recall serious approaches: typically, they just comment on the collected returns/rewards plots. So, I encourage other people that are familiar with RL research to give other answers, if they know/remember other approaches.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-14T15:01:49.167Z
Answers.LastActivityDate: 2020-10-14T15:43:52.777Z
Answers.LastEditDate: 2020-10-14T15:43:52.777Z
Answers.UserDisplayName: nbro
Body: <p>I am using Q-learning and SARSA to solve a problem. The agent learns to go from the start to the goal without falling in the holes.</p>
  <p>At each state, I can choose the action corresponding to the maximum Q value at the state (the greedy action that the agent would take). And all the actions connect some states together. I think that would show me a road from start to goal, which means the result converges.</p>
  <p>But some others think that as long as the agent learns how to reach the goal, the result converges. Sometimes the success rate is very high but we cannot get the road from Q table. I don't know which one means the agent is trained totally and what the converged result means.</p>
  
Tags: <reinforcement-learning><q-learning><convergence><temporal-difference-methods><sarsa>
Title: How to determine if Q-learning has converged in practice？
UserDisplayName: WANGWANGZI
ThreadText: # How to determine if Q-learning has converged in practice？
  
  (Asked by: username_0 on 2020-10-14)
  
  I am using Q-learning and SARSA to solve a problem. The agent learns to go from the start to the goal without falling in the holes.
  
  At each state, I can choose the action corresponding to the maximum Q value at the state (the greedy action that the agent would take). And all the actions connect some states together. I think that would show me a road from start to goal, which means the result converges.
  
  But some others think that as long as the agent learns how to reach the goal, the result converges. Sometimes the success rate is very high but we cannot get the road from Q table. I don't know which one means the agent is trained totally and what the converged result means.
  
  **Tags:** reinforcement-learning, q-learning, convergence, temporal-difference-methods, sarsa
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-14. Score: 4
  
  A typical and practical way to measure the convergence to **some** solution (so not necessarily the optimal one!) of any numerical iterative algorithm (such as RL algorithms) is to check if the current solution has not changed (much) with respect to the previous one. In your case, the solutions are value functions, so you could check if your algorithm has converged to some value function e.g. as follows
  
  $$
  c(q\_t, q\_{t-1}, \epsilon) =
  \begin{cases}
  1, &\text{if } |q\_t(s, a) - q\_{t-1}(s, a)| < \epsilon, \forall s \in S, a \in A \\
  0, & \text{otherwise}
  \end{cases},
  \tag{1}\label{1}
  $$
  where
  
  * $c$ is the "convergence" function (aka termination condition) that returns $1$ (true) if your RL algorithm has converged to some small enough neighbourhood of value functions (where those value functions are "indistinguishable"), and $0$ otherwise
  * $q\_t$ is the value function at iteration $t$
  * $\epsilon$ is a threshold (aka precision or tolerance) value, which is a hyper-parameter that you can set depending on your "tolerance" (hence the name); this value is typically something like $10^{-6}$
  
  Of course, this requires that you keep track of two value functions.
  
  You can also define your "convergence" function $c$ in \ref{1} differently. For example, rather than using the absolute value, you could use the relative error, i.e. $\left|\frac{q\_t(s, a) - q\_{t-1}(s, a)}{q\_t(s, a)} \right|$. Moreover, given that RL algorithms are exploratory (i.e. stochastic) algorithms, the value function may not change (much) from one iteration to the other, but, in the next one, it could significantly change because of your exploratory/behavioural actions, so you may also want to take into account more iterations, i.e. after e.g. $N > 1$ iterations, if the value function does not change much, then you could say (maybe probabilistically) that your RL algorithm has converged to some small neighbourhood of value functions in the space of value functions.
  
  Note that these approaches do not guarantee that your RL algorithm has converged to the **global optimal** value function, but to **some locally optimal** value function (or, more precisely, small neighborhood of value functions). [Q-learning is guaranteed to converge to the optimal value function in the tabular setting](https://ai.stackexchange.com/q/11679/2444) (your setting), but this is in the limit; in practice, it is more difficult to know if Q-learning has converged to an optimal or near-optimal value function.
  
  Maybe you can also have a look at episodic returns of the policy derived from your final value function, but without upper and lower bounds on the optimal returns, you don't know much about the global optimality of your policy/value function.
  
  Yes, you can check if the policy makes the agent reach the goal, but many policies could do that job, i.e. that does not say that the policy is the best (or optimal) one, i.e. that's a necessary (provided the goal is reachable and the reward function models your actual goal) but not sufficient condition (for optimality). The optimality here is usually a function of the return (given that is what you are usually trying to optimize).
  
  > **Comments:**
  > **username_1**: This answer is based on my general knowledge of numerical algorithms and not much on how people (in the RL community) determine when their Q-learning (or, in general, RL) algorithms converge. It's possible that people may be using other approaches to determine the convergence in practice. Right now, I don't recall serious approaches: typically, they just comment on the collected returns/rewards plots. So, I encourage other people that are familiar with RL research to give other answers, if they know/remember other approaches.  
  
  ---
  
  