Answers.Body: <blockquote>
    <p>First, some preliminary questions: in this case, what is the optimal policy?</p>
  </blockquote>
  
  <p>It is the policy that maximises <em>return</em> from any given time step <span class="math-container">$G_t$</span>. You need to be careful with your definition of <em>return</em> with continuing environments. The simple expected sum of future rewards is likely to be positive or negative infinity.</p>
  
  <p>There are three basic approaches:</p>
  
  <ul>
  <li><p>Set an arbitrary finite horizon <span class="math-container">$h$</span>, so <span class="math-container">$G_t = \sum_{k=1}^{h} R_{t+k}$</span></p></li>
  <li><p>Use discounting, with discount factor <span class="math-container">$\gamma &lt; 1$</span>, so <span class="math-container">$G_t = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}$</span></p></li>
  <li><p>Use average reward per time step,  <span class="math-container">$\bar{R} = \lim\limits_{h \to \infty}\frac{1}{h} \sum_{k=1}^{h} R_{t+k}$</span>, which leads to thinking in terms of <em>differential return</em> <span class="math-container">$G_t = \sum_{k=1}^{\infty} (R_{t+k} - \bar{R})$</span></p></li>
  </ul>
  
  <p>With large enough horizon (so that state is ergodic) or large enough <span class="math-container">$\gamma$</span> (close to <span class="math-container">$1$</span>), these approaches are similar and should result in approximately the same policy. The difference is in how you construct an agent in detail to solve the problem of maximising the value.</p>
  
  <blockquote>
    <p>Given the infinite horizon there is no terminal state but only an objective to maximise the rewards, so I can't run more than one episode, is it correct?</p>
  </blockquote>
  
  <p>The term <em>episode</em> becomes meaningless. It may be simpler to think of this differently though - you are trying to solve a non-episodic problem here, in that there is no natural separation of the process into separate meaningful episodes. No physical process is actually infinite, that's just a theoretical nicety.</p>
  
  <p>In practice, if you can run your environment in simulation, or multiple versions of it for training purposes, then you do start and stop pseudo-episodes. You don't treat them as episodes mathematically - i.e. there is no terminal state, and you can never obtain a simple episodic return value. However, you can decide to stop an environment and start a new one from a different state.</p>
  
  <p>Even if there is only one real environment, you can sample sections of it for training, or attempt to use different agents over time, each of which is necessarily finite in nature.</p>
  
  <blockquote>
    <p>Consequently, what is the difference between on-policy and off-policy learning given this framework?</p>
  </blockquote>
  
  <p>The notions of on-policy and off-policy are entirely separate from episodic vs continuing environments. </p>
  
  <ul>
  <li><p>On-policy agents use a single policy both to select actions (the behaviour policy), and as the learning target. When the learnt policy is updated with new information that immediately affects the behaviour of the agent.</p></li>
  <li><p>Off-policy agents use two or more policies. That is one or more <em>behaviour</em> policies that select actions (the behaviour policy), and a target policy that is learned, typically the best guess at the optimal policy given data so far.</p></li>
  </ul>
  
  <p>These things do not change between episodic and continuing tasks, and many algorithms remain identical when solving episodic vs continuing problems. For example, DQN requires no special changes to support continuing tasks, you can just set a high enough discount factor and use it as-is.</p>
  
  <p>You cannot wait until the end of an episode, so certain update methods won't work. However, value bootstrapping used in temporal difference (TD) learning still works.</p>
  
  <p>In some cases, you will want to address the differences in the definition of return. Using an average reward setting typically means looking at a differential return for calculating TD targets for example.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-19T09:51:58.643Z
Answers.Text: So for example if I want to run a Deep Q-learning  with experience replay algorithm, having only one real environment, I should simply not consider the "for" loop of episodes?
Answers.UserDisplayName: unter_983
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-19T11:20:05.743Z
Answers.Text: @aandre_90: You may still want to arbitrarily stop and reset state whilst continuing to train, so could keep some kind of outer loop for that. You would never see an episode complete though, so terminating the inner loop needs to be based on something else for that to be useful. Otherwise, yes you could drop that outer loop per episode.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-19T09:19:06.267Z
Answers.LastActivityDate: 2020-05-19T19:46:15.063Z
Answers.LastEditDate: 2020-05-19T19:46:15.063Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm trying to understand RL applied to time series (so with infinite horizon) which have a continous state space and a discrete action space.</p>
  
  <p>First, some preliminary questions: in this case, what is the optimal policy? Given the infinite horizon there is no terminal state but only an objective to maximise the rewards, so I can't run more than one episode, is it correct?</p>
  
  <p>Consequently, what is the difference between on-policy and off-policy learning given this framework? </p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-19T19:21:57.787Z
Comments.Text: Although all your questions are related, please, next time, ask one question per post! You're asking at least 3 distinct questions here.
Comments.UserDisplayName: nbro
Tags: <reinforcement-learning><comparison><q-learning><off-policy-methods><on-policy-methods>
Title: What is the difference between on-policy and off-policy for continuous environments?
UserDisplayName: unter_983
ThreadText: # What is the difference between on-policy and off-policy for continuous environments?
  
  (Asked by: username_0 on 2020-05-18)
  
  I'm trying to understand RL applied to time series (so with infinite horizon) which have a continous state space and a discrete action space.
  
  First, some preliminary questions: in this case, what is the optimal policy? Given the infinite horizon there is no terminal state but only an objective to maximise the rewards, so I can't run more than one episode, is it correct?
  
  Consequently, what is the difference between on-policy and off-policy learning given this framework?
  
  **Tags:** reinforcement-learning, comparison, q-learning, off-policy-methods, on-policy-methods
  > **Comments:**
  > **username_1**: Although all your questions are related, please, next time, ask one question per post! You're asking at least 3 distinct questions here.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-05-19. Score: 3 | Accepted answer
  
  > First, some preliminary questions: in this case, what is the optimal policy?
  
  It is the policy that maximises *return* from any given time step $G\_t$. You need to be careful with your definition of *return* with continuing environments. The simple expected sum of future rewards is likely to be positive or negative infinity.
  
  There are three basic approaches:
  
  * Set an arbitrary finite horizon $h$, so $G\_t = \sum\_{k=1}^{h} R\_{t+k}$
  * Use discounting, with discount factor $\gamma < 1$, so $G\_t = \sum\_{k=0}^{\infty} \gamma^k R\_{t+k+1}$
  * Use average reward per time step, $\bar{R} = \lim\limits\_{h \to \infty}\frac{1}{h} \sum\_{k=1}^{h} R\_{t+k}$, which leads to thinking in terms of *differential return* $G\_t = \sum\_{k=1}^{\infty} (R\_{t+k} - \bar{R})$
  
  With large enough horizon (so that state is ergodic) or large enough $\gamma$ (close to $1$), these approaches are similar and should result in approximately the same policy. The difference is in how you construct an agent in detail to solve the problem of maximising the value.
  
  > Given the infinite horizon there is no terminal state but only an objective to maximise the rewards, so I can't run more than one episode, is it correct?
  
  The term *episode* becomes meaningless. It may be simpler to think of this differently though - you are trying to solve a non-episodic problem here, in that there is no natural separation of the process into separate meaningful episodes. No physical process is actually infinite, that's just a theoretical nicety.
  
  In practice, if you can run your environment in simulation, or multiple versions of it for training purposes, then you do start and stop pseudo-episodes. You don't treat them as episodes mathematically - i.e. there is no terminal state, and you can never obtain a simple episodic return value. However, you can decide to stop an environment and start a new one from a different state.
  
  Even if there is only one real environment, you can sample sections of it for training, or attempt to use different agents over time, each of which is necessarily finite in nature.
  
  > Consequently, what is the difference between on-policy and off-policy learning given this framework?
  
  The notions of on-policy and off-policy are entirely separate from episodic vs continuing environments.
  
  * On-policy agents use a single policy both to select actions (the behaviour policy), and as the learning target. When the learnt policy is updated with new information that immediately affects the behaviour of the agent.
  * Off-policy agents use two or more policies. That is one or more *behaviour* policies that select actions (the behaviour policy), and a target policy that is learned, typically the best guess at the optimal policy given data so far.
  
  These things do not change between episodic and continuing tasks, and many algorithms remain identical when solving episodic vs continuing problems. For example, DQN requires no special changes to support continuing tasks, you can just set a high enough discount factor and use it as-is.
  
  You cannot wait until the end of an episode, so certain update methods won't work. However, value bootstrapping used in temporal difference (TD) learning still works.
  
  In some cases, you will want to address the differences in the definition of return. Using an average reward setting typically means looking at a differential return for calculating TD targets for example.
  
  > **Comments:**
  > **username_0**: So for example if I want to run a Deep Q-learning  with experience replay algorithm, having only one real environment, I should simply not consider the "for" loop of episodes?  
  > **username_2**: @aandre_90: You may still want to arbitrarily stop and reset state whilst continuing to train, so could keep some kind of outer loop for that. You would never see an episode complete though, so terminating the inner loop needs to be based on something else for that to be useful. Otherwise, yes you could drop that outer loop per episode.  
  
  ---
  
  