Answers.Body: <p>I am assuming you are asking about Monte Carlo simulation for value estimates, perhaps as part of a Monte Carlo control learning agent.</p>
  <p>The basic approach of all value-based methods is to estimate an expected <em>return</em>, often the action value <span class="math-container">$Q(s,a)$</span> which is a sum of expected future reward from taking action <span class="math-container">$a$</span> in state <span class="math-container">$s$</span>. Monte Carlo methods take a direct and simple approach to this, which is to run the environment to the end of an episode and measure the return. This return is a sample out of all possible returns, so it can just be averaged with other observed returns to obtain an estimate. A minor complication is that the return depends on the current policy, and in control scenarios that will change, so the average needs to be recency-weighted for control e.g. using a fixed learning rate <span class="math-container">$\alpha$</span> in an update like <span class="math-container">$Q(s,a) \leftarrow Q(s,a) + \alpha(G - Q(s,a))$</span></p>
  <p>Given this, you can run pretty much any approach that calculates the returns from observed state/action pairs. You will find that the &quot;bubble up&quot; approach is used commonly - the process usually termed <em>backing up</em> - working backwards from the end of the episode.</p>
  <p>If you have an episode from <span class="math-container">$t=0$</span> to <span class="math-container">$t=T$</span> and records of states, actions, rewards <span class="math-container">$s_0, a_0, r_1, s_1, a_1, r_2, s_2, a_2, r_3 . . . s_{T-1}, a_{T-1}, r_T, s_T$</span> (note indexing, reward follows state/action, there is no <span class="math-container">$r_0$</span> and no <span class="math-container">$a_T$</span>), then the following algorithm could be used to calculate individual returns <span class="math-container">$g_t$</span>:</p>
  <blockquote>
  <p><span class="math-container">$g \leftarrow 0$</span></p>
  <p>for <span class="math-container">$t = T-1$</span> down to <span class="math-container">$0$</span>:</p>
  <p><span class="math-container">$\qquad g \leftarrow r_{t+1} + \gamma g$</span></p>
  <p><span class="math-container">$\qquad Q(s_t,a_t) \leftarrow Q(s_t,a_t) + \alpha(g - Q(s_t,a_t))$</span></p>
  </blockquote>
  <p>This working backwards is an efficient way to process rewards and assign them with discounting to action values for all state, action pairs observed in the episode.</p>
  <blockquote>
  <p>Or perhaps these are two different approaches?</p>
  </blockquote>
  <p>It would be valid to calculate only the return for the first state/action, and randomly select state/actions to start from (called exploring starts). Or in fact take any arbitrary set of estimates generated this way. You don't have to use all return estimates, but you do need to have an algorithm that is guaranteed to update values of all state/action pairs in the long term.</p>
  <blockquote>
  <p>Is there a situation where you'd one rather than the other?</p>
  </blockquote>
  <p>Most usually you will see the backed up return estimates to all observed state/action pairs, as this is more sample efficient, and Monte Carlo is already a high variance method that requires lots of samples to get good estimates (especially for early state/action pairs at the start of long episodes).</p>
  <p>However, if you work with function approximation such as neural networks, you need to avoid feeding in correlated data to learn from. A sequence of state/action pairs from a single episode are going to be correlated. There are a few ways to avoid that, but one simple approach could be to take just one sample from each rollout. You <em>might</em> do this if rollouts could be run very fast, possibly simulated on computer. But other alternatives may be better - for instance put all the state, action, return values into a data set, shuffle it after N episodes and learn from everything.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-16T08:54:09.707Z
Answers.Text: Thank you for your excellent answer, Neil. After further thought, if I set gamma to high number, 0.995, then the reward received at the Terminal state would be propagated through the previous experiences, so I think to start with I will simply train on the full experience roll-out with high gamma, rather than backing up the max(rewards) achieved from Terminal states coming from each branch level of the search tree and creating a truncated list of experiences.  I can always experiment later on to see which approach is better. Thanks again.
Answers.UserDisplayName: BigBadMe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-16T08:57:52.507Z
Answers.Text: @BigBadMe: That should be fine. In fact for episodic problems you can often set $\gamma=1$. That partly depends on whether you are using the discount factor to encourage solutions that use less time steps (e.g. a maze with reward +1 to exit and 0 reward otherwise needs $\gamma \lt 1$, but you could have a maze with reward -1 on all steps and use $\gamma = 1$),
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-17T09:25:46.057Z
Answers.Text: Ahh interesting.  I'm sure I'd read somewhere that setting gamma=1 would cause problems, but perhaps as you say it depends on the situation it's used.
Answers.UserDisplayName: BigBadMe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-15T09:46:43.093Z
Answers.LastActivityDate: 2019-09-17T09:40:11.910Z
Answers.LastEditDate: 2020-06-17T09:57:20.710Z
Answers.UserDisplayName: Neil Slater
Body: <p>When you train a model using Monte Carlo-based learning the state and action taken at each step is recorded, and then at some point an end state is reached and the agent receives some reward - what do you do at that point?</p>
  
  <p>Let's say there were 100 steps taken to reach this final reward state, would you update the full rollout of those 100 state/action/rewards and then begin the next episode, or do you then 'bubble up' that final reward to the previous states and update on those as well?</p>
  
  <p>E.g. </p>
  
  <ul>
  <li><p>Process an update for the full 100 experiences. Can either stop here, or...</p></li>
  <li><p>Bubble up the final reward to the 99th step and process an update for the 99 state/action/reward.</p></li>
  <li><p>Bubble up the final reward to the 98th step and process an update for the 98 state/action/reward.</p></li>
  <li><p>and so on right the way to the first step...</p></li>
  </ul>
  
  <p>Or, do you just process an update for the full 100-step roll-out and that's it?</p>
  
  <p>Or perhaps these are two different approaches?  Is there a situation where you'd one rather than the other?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-03T14:03:16.380Z
Comments.Text: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/117980/discussion-on-question-by-bigbadme-in-monte-carlo-learning-what-do-you-do-when).
Comments.UserDisplayName: nbro
Tags: <reinforcement-learning><monte-carlo-methods>
Title: In Monte Carlo learning, what do you do when an end state is reached, after having recorded the previously visited states and taken actions?
UserDisplayName: BigBadMe
ThreadText: # In Monte Carlo learning, what do you do when an end state is reached, after having recorded the previously visited states and taken actions?
  
  (Asked by: username_0 on 2019-09-14)
  
  When you train a model using Monte Carlo-based learning the state and action taken at each step is recorded, and then at some point an end state is reached and the agent receives some reward - what do you do at that point?
  
  Let's say there were 100 steps taken to reach this final reward state, would you update the full rollout of those 100 state/action/rewards and then begin the next episode, or do you then 'bubble up' that final reward to the previous states and update on those as well?
  
  E.g.
  
  * Process an update for the full 100 experiences. Can either stop here, or...
  * Bubble up the final reward to the 99th step and process an update for the 99 state/action/reward.
  * Bubble up the final reward to the 98th step and process an update for the 98 state/action/reward.
  * and so on right the way to the first step...
  
  Or, do you just process an update for the full 100-step roll-out and that's it?
  
  Or perhaps these are two different approaches? Is there a situation where you'd one rather than the other?
  
  **Tags:** reinforcement-learning, monte-carlo-methods
  > **Comments:**
  > **username_1**: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/117980/discussion-on-question-by-bigbadme-in-monte-carlo-learning-what-do-you-do-when).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-09-15. Score: 3 | Accepted answer
  
  I am assuming you are asking about Monte Carlo simulation for value estimates, perhaps as part of a Monte Carlo control learning agent.
  
  The basic approach of all value-based methods is to estimate an expected *return*, often the action value $Q(s,a)$ which is a sum of expected future reward from taking action $a$ in state $s$. Monte Carlo methods take a direct and simple approach to this, which is to run the environment to the end of an episode and measure the return. This return is a sample out of all possible returns, so it can just be averaged with other observed returns to obtain an estimate. A minor complication is that the return depends on the current policy, and in control scenarios that will change, so the average needs to be recency-weighted for control e.g. using a fixed learning rate $\alpha$ in an update like $Q(s,a) \leftarrow Q(s,a) + \alpha(G - Q(s,a))$
  
  Given this, you can run pretty much any approach that calculates the returns from observed state/action pairs. You will find that the "bubble up" approach is used commonly - the process usually termed *backing up* - working backwards from the end of the episode.
  
  If you have an episode from $t=0$ to $t=T$ and records of states, actions, rewards $s\_0, a\_0, r\_1, s\_1, a\_1, r\_2, s\_2, a\_2, r\_3 . . . s\_{T-1}, a\_{T-1}, r\_T, s\_T$ (note indexing, reward follows state/action, there is no $r\_0$ and no $a\_T$), then the following algorithm could be used to calculate individual returns $g\_t$:
  
  > $g \leftarrow 0$
  >
  > for $t = T-1$ down to $0$:
  >
  > $\qquad g \leftarrow r\_{t+1} + \gamma g$
  >
  > $\qquad Q(s\_t,a\_t) \leftarrow Q(s\_t,a\_t) + \alpha(g - Q(s\_t,a\_t))$
  
  This working backwards is an efficient way to process rewards and assign them with discounting to action values for all state, action pairs observed in the episode.
  
  > Or perhaps these are two different approaches?
  
  It would be valid to calculate only the return for the first state/action, and randomly select state/actions to start from (called exploring starts). Or in fact take any arbitrary set of estimates generated this way. You don't have to use all return estimates, but you do need to have an algorithm that is guaranteed to update values of all state/action pairs in the long term.
  
  > Is there a situation where you'd one rather than the other?
  
  Most usually you will see the backed up return estimates to all observed state/action pairs, as this is more sample efficient, and Monte Carlo is already a high variance method that requires lots of samples to get good estimates (especially for early state/action pairs at the start of long episodes).
  
  However, if you work with function approximation such as neural networks, you need to avoid feeding in correlated data to learn from. A sequence of state/action pairs from a single episode are going to be correlated. There are a few ways to avoid that, but one simple approach could be to take just one sample from each rollout. You *might* do this if rollouts could be run very fast, possibly simulated on computer. But other alternatives may be better - for instance put all the state, action, return values into a data set, shuffle it after N episodes and learn from everything.
  
  > **Comments:**
  > **username_0**: Thank you for your excellent answer, Neil. After further thought, if I set gamma to high number, 0.995, then the reward received at the Terminal state would be propagated through the previous experiences, so I think to start with I will simply train on the full experience roll-out with high gamma, rather than backing up the max(rewards) achieved from Terminal states coming from each branch level of the search tree and creating a truncated list of experiences.  I can always experiment later on to see which approach is better. Thanks again.  
  > **username_2**: @BigBadMe: That should be fine. In fact for episodic problems you can often set $\gamma=1$. That partly depends on whether you are using the discount factor to encourage solutions that use less time steps (e.g. a maze with reward +1 to exit and 0 reward otherwise needs $\gamma \lt 1$, but you could have a maze with reward -1 on all steps and use $\gamma = 1$),  
  > **username_0**: Ahh interesting.  I'm sure I'd read somewhere that setting gamma=1 would cause problems, but perhaps as you say it depends on the situation it's used.  
  
  ---
  
  