Answers.Body: <p>Most RL algorithms assume a discretization of time (although RL can also be applied to continuous-time problems [<a href="http://incompleteideas.net/book/RLbook2020.pdf" rel="nofollow noreferrer">1</a>]), i.e., in theory, it doesn't really matter what the actual time between consecutive time steps is, but, in practice, you may have delays in the rewards or observations, so you cannot perform e.g. the TD updates immediately. One natural solution to your problem would be to keep track (e.g. in a buffer) of the reward obtained and the next state that the agent ended up in after having taken a certain action in a certain state, or use some kind of synchronization mechanism (note that I've just come up with these solutions, so I don't know if this has been done or not to solve problems). In practice, this may not work (in all cases), for example, during real-time inference, where you need to decide quickly what you need to do even without full information about the current state or reward.</p>
  <p>Note that, in RL, rewards are often said to be <a href="https://ai.stackexchange.com/a/12190/2444"><em>delayed</em></a>, in the sense that</p>
  <ol>
  <li>you may know the consequences of an action only many time-steps after you have taken it (determining the consequences of an action is known as the <a href="https://ai.stackexchange.com/q/12908/2444">credit assignment problem</a>), or</li>
  <li>you may get a non-zero reward only when the agent gets to a goal/final state (in this last case, these rewards are also known as <em>sparse</em>).</li>
  </ol>
  <p>These two problems are common in RL. However, if I understand correctly your concerns, this is a bit different than your problem, because your problem also involves the potential delay of the state or even reward that was supposed to arrive at a previous time step, which can be due e.g. to an erratic or broken sensor/actuator. For instance, if you are using <a href="https://www.cs.toronto.edu/%7Evmnih/docs/dqn.pdf" rel="nofollow noreferrer">DQN</a>, which typically builds an approximation of the current state by concatenating the last frames captured by your camera, if you have delays in the frames that cause the natural order of the frames to be changed, this could lead to a bad approximation of the current state, which could actually lead to a catastrophic event. So, yes, this is an important problem that needs to be tackled.</p>
  <p>Given that I am not really familiar with the actual existing solutions, I'll refer you to the paper <a href="https://arxiv.org/pdf/1904.12901.pdf" rel="nofollow noreferrer">Challenges of Real-World Reinforcement Learning</a> that I read a few weeks ago, which mentions this issue and points you to other research work that attempted to address it. Take a look at <a href="https://ai.stackexchange.com/a/7344/2444">this answer</a> too, if you're more interested in delayed/sparse rewards.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-14T22:38:32.517Z
Answers.LastActivityDate: 2020-12-14T23:24:42.207Z
Answers.LastEditDate: 2020-12-14T23:24:42.207Z
Answers.UserDisplayName: nbro
Answers.Body: <p>This problem has been formally termed as Delayed MDP (<a href="https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read" rel="nofollow noreferrer">Katsikopoulos &amp; Engelbrecht, 2003</a>)[<a href="https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read" rel="nofollow noreferrer">1</a>] - the actions generated are not instantly applied to the environment and/or the captured observations are not immediately seen by the agent, as expected in an MDP. The delay can either be:</p>
  <p><strong>a) A constant delay</strong> - Constant Delayed MDP (CDMDP) (<a href="https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf" rel="nofollow noreferrer">Learning and planning in en-
  vironments with delayed feedback, 2008</a>)[<a href="https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf" rel="nofollow noreferrer">2</a>]</p>
  <ul>
  <li>The delay introduced by the environment is constant and known. Like the example you provide in the question.</li>
  </ul>
  <p><strong>b) A random delay</strong> - Random Delayed MDP</p>
  <ul>
  <li>We don't know what delay to expect, or when the delay will occur. This is more realistic for a real-world case.</li>
  </ul>
  <hr />
  <p><em>TRIED APPROACHES</em></p>
  <p><strong>1. Just ignore the delay</strong></p>
  <p>This assumes the CDMDP is an MDP. We then attempt to search for the policy that best ignores the delay</p>
  <p><span class="math-container">$$
  \pi(I_k)={\pi^∗(s)|I_k=(s, a_1,···,a_k)}
  $$</span></p>
  <p><span class="math-container">$k$</span> is the number of time-steps between acting on the current state, and receiving feedback. This is simple and can give a reasonable result if the delay is small compared to the state transition magnitude [<a href="https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf" rel="nofollow noreferrer">2</a>].</p>
  <p><strong>2. Reconstruct the MDP from the CDMDP (Augmented approach)</strong></p>
  <p>The corresponding optimal policy for the reconstructed MDP will then the optimal policy for the CDMDP. This is however made intractable by the size of the action buffer growing with the delay length. It's therefore limited to small constant delays. [<a href="https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read" rel="nofollow noreferrer">1</a>] defines how to do the re-construction.</p>
  <p><strong>3. Predicting the delayed observation</strong></p>
  <p>This tries to &quot;undelay&quot; the environment by using a predictive model <span class="math-container">$P$</span> to approximate what the delayed observation would be, instead of keeping the agent patient to the end of the delay (<a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwjj-7yLhcnwAhUcQUEAHafeAJkQFjADegQIDhAD&amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.115.677%26rep%3Drep1%26type%3Dpdf&amp;usg=AOvVaw3iFfuHVw4m2fSrEvjnuX2d" rel="nofollow noreferrer">Learning and planning in envs with delayed feedback, (2008)</a>[<a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwjj-7yLhcnwAhUcQUEAHafeAJkQFjADegQIDhAD&amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.115.677%26rep%3Drep1%26type%3Dpdf&amp;usg=AOvVaw3iFfuHVw4m2fSrEvjnuX2d" rel="nofollow noreferrer">5</a>], <a href="https://arxiv.org/pdf/1810.07286.pdf" rel="nofollow noreferrer">At Human Speed: Deep RL with action delay, (2018)</a>][<a href="https://arxiv.org/pdf/1810.07286.pdf" rel="nofollow noreferrer">6</a>]).</p>
  <p><em>e.g., For a delayed action</em>:</p>
  <p><span class="math-container">$$
  s_{t+i} = P(s_{t + i -1}, a_{t + i - delay})
  $$</span></p>
  <p>If the delay is on the observation, the most recent K actions can be used with the most recent observation to approximate the current state[<a href="https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf" rel="nofollow noreferrer">2</a>]. However, this also means the delays are part of the state - it's possible to have a better approach, as next described in the random delays part.</p>
  <p>Estimating the current state enables the agent to act conditioned an estimate of the true state, on which the action is executed. The limitation is that it assumes a constant delay.</p>
  <hr />
  <p>All the above approaches assume a constant delay</p>
  <p><em>Handling random delays</em>*</p>
  <p><strong>4. Partial Trajectory resampling (PTR)</strong></p>
  <p>A method that recursively resamples actions in the buffer, replacing them with on-policy actions. It uses the delay dynamics to simulate their effect on the current policy.</p>
  <p>If random delays exist on an MDP, some actions present in the off-policy replay buffer will not influence the later delayed observations and rewards, for a number of timesteps. This will allow generation of on-policy sub-trajectories from the off-policy samples by recursively resampling the most recent actions from the action buffer. It will not invalidate the sub-trajectory.</p>
  <p>The resampled action will be valid as long as the observation delay <span class="math-container">$\omega$</span> and the action delay <span class="math-container">$\alpha$</span> are both greater than the current time step <span class="math-container">$t$</span> i.e., No delayed observation depends on a resampled action.</p>
  <p><span class="math-container">$$
  \omega_t + \alpha_t &gt;= t
  $$</span></p>
  <p><a href="https://i.sstatic.net/ND2pF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ND2pF.png" alt="Partial trajectory resampling" /></a>
  <em>Illustration of PTR, (<a href="https://arxiv.org/abs/2010.02966" rel="nofollow noreferrer">3</a>)</em></p>
  <p>The benefits of this method:</p>
  <ol>
  <li><p>Allows discarding of outdated information. e.g If the delay is such that the are no observations for <span class="math-container">$5$</span> timesteps, which then all arrive at step <span class="math-container">$5$</span>, we can safely discard the transitions of steps <span class="math-container">$(1,2, 3,4)$</span>. This is because this info will be compressed in the most recent observation.</p>
  </li>
  <li><p>Provides information on the &quot;age&quot; of an observation and the actions applied next</p>
  </li>
  <li><p>Efficient credit assignment</p>
  </li>
  </ol>
  <p>These factors give partial trajectory resampling better performance compared to concatenating the past <span class="math-container">$K$</span> actions with the most recent observation to estimate the current delayed state.</p>
  <p><a href="https://openreview.net/forum?id=QFYnKlBJYR" rel="nofollow noreferrer">RL with random delays (2020)</a>[<a href="https://arxiv.org/abs/2010.02966" rel="nofollow noreferrer">3</a>] describes and applies Partial Trajectory Resampling in RL. It's the only work I've managed to come across that handles both random and constant delayed MDPs.</p>
  <p><strong>What of the reward delay?</strong></p>
  <p>Reward delay, in this context, is referring to the rewards attributed to a delayed observation and action.</p>
  <p>A solution to a delayed MDP will involve tackling the <a href="https://ai.stackexchange.com/questions/12908/what-is-the-credit-assignment-problem/12909">credit assignment problem</a>. In this case, this is deciding what to do with the delayed rewards and how to give credit to the delayed actions and states.</p>
  <p>This has had different approaches. For instance:</p>
  <blockquote>
  <p>&quot; ...we chose to accumulate the rewards corresponding to the [excessively delayed transitions we drop]. When an observation gets repeated
  because no new observation is available, the corresponding reward is 0, and when a new observation arrives, the corresponding reward contains the sum of intermediate rewards in lost [dropped] transitions.&quot; [<a href="https://arxiv.org/abs/2010.02966" rel="nofollow noreferrer">3</a>]</p>
  </blockquote>
  <p><a href="https://www.cs.utexas.edu/%7Epstone/Courses/394Rfall19/resources/Ch8-TEXPLORE.pdf" rel="nofollow noreferrer">Real time sample efficint RL for robots, (2013)</a>[<a href="https://www.cs.utexas.edu/%7Epstone/Courses/394Rfall19/resources/Ch8-TEXPLORE.pdf" rel="nofollow noreferrer">9</a>] on the other hand, uses a decision tree to assign credit to an MDP reconstructed from a CMDP. The tree learns which delayed actions are relevant.</p>
  <hr />
  <p>Related reads:</p>
  <ol>
  <li><a href="https://arxiv.org/pdf/1911.04448.pdf" rel="nofollow noreferrer">Real-time Reinforcement Learning (2019)</a></li>
  <li><a href="https://arxiv.org/abs/2004.06089" rel="nofollow noreferrer">Thinking while moving: Deep reinforcement learning with concurrent control, (2020)</a></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-14T11:41:47.370Z
Answers.LastActivityDate: 2021-05-14T11:50:16.460Z
Answers.LastEditDate: 2021-05-14T11:50:16.460Z
Answers.UserDisplayName: mugoh
Body: <p>I have a question regarding the time delay in reinforcement learning (RL).</p>
  <p>In the RL, one has state, reward and action. It is usually assumed that (as far as I understand it) when the action is executed on the system, the state changes immediately and that the new state can then be analysed (influencing the reward) to determine the next action. However, what if there is a time delay in this process. For example, when some action is executed at time <span class="math-container">$t_1$</span>, we can only get its effect on the system at <span class="math-container">$t_2$</span> (You can imagine a flow: the actuator is in the upstream region and the sensor is in the downstream region, so that there will be a time delay between the action and the state). How do we deal with this time delay in RL?</p>
  
Tags: <reinforcement-learning><time-series><delayed-rewards>
Title: How to deal with the time delay in reinforcement learning?
UserDisplayName: jengmge
ThreadText: # How to deal with the time delay in reinforcement learning?
  
  (Asked by: username_0 on 2020-12-14)
  
  I have a question regarding the time delay in reinforcement learning (RL).
  
  In the RL, one has state, reward and action. It is usually assumed that (as far as I understand it) when the action is executed on the system, the state changes immediately and that the new state can then be analysed (influencing the reward) to determine the next action. However, what if there is a time delay in this process. For example, when some action is executed at time $t\_1$, we can only get its effect on the system at $t\_2$ (You can imagine a flow: the actuator is in the upstream region and the sensor is in the downstream region, so that there will be a time delay between the action and the state). How do we deal with this time delay in RL?
  
  **Tags:** reinforcement-learning, time-series, delayed-rewards
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-12-14. Score: 2
  
  Most RL algorithms assume a discretization of time (although RL can also be applied to continuous-time problems [[1](http://incompleteideas.net/book/RLbook2020.pdf)]), i.e., in theory, it doesn't really matter what the actual time between consecutive time steps is, but, in practice, you may have delays in the rewards or observations, so you cannot perform e.g. the TD updates immediately. One natural solution to your problem would be to keep track (e.g. in a buffer) of the reward obtained and the next state that the agent ended up in after having taken a certain action in a certain state, or use some kind of synchronization mechanism (note that I've just come up with these solutions, so I don't know if this has been done or not to solve problems). In practice, this may not work (in all cases), for example, during real-time inference, where you need to decide quickly what you need to do even without full information about the current state or reward.
  
  Note that, in RL, rewards are often said to be [*delayed*](https://ai.stackexchange.com/a/12190/2444), in the sense that
  
  1. you may know the consequences of an action only many time-steps after you have taken it (determining the consequences of an action is known as the [credit assignment problem](https://ai.stackexchange.com/q/12908/2444)), or
  2. you may get a non-zero reward only when the agent gets to a goal/final state (in this last case, these rewards are also known as *sparse*).
  
  These two problems are common in RL. However, if I understand correctly your concerns, this is a bit different than your problem, because your problem also involves the potential delay of the state or even reward that was supposed to arrive at a previous time step, which can be due e.g. to an erratic or broken sensor/actuator. For instance, if you are using [DQN](https://www.cs.toronto.edu/%7Evmnih/docs/dqn.pdf), which typically builds an approximation of the current state by concatenating the last frames captured by your camera, if you have delays in the frames that cause the natural order of the frames to be changed, this could lead to a bad approximation of the current state, which could actually lead to a catastrophic event. So, yes, this is an important problem that needs to be tackled.
  
  Given that I am not really familiar with the actual existing solutions, I'll refer you to the paper [Challenges of Real-World Reinforcement Learning](https://arxiv.org/pdf/1904.12901.pdf) that I read a few weeks ago, which mentions this issue and points you to other research work that attempted to address it. Take a look at [this answer](https://ai.stackexchange.com/a/7344/2444) too, if you're more interested in delayed/sparse rewards.
  
  
  ---
  
  ### Answer by username_2 on 2021-05-14. Score: 2
  
  This problem has been formally termed as Delayed MDP ([Katsikopoulos & Engelbrecht, 2003](https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read))[[1](https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read)] - the actions generated are not instantly applied to the environment and/or the captured observations are not immediately seen by the agent, as expected in an MDP. The delay can either be:
  
  **a) A constant delay** - Constant Delayed MDP (CDMDP) ([Learning and planning in en-
  vironments with delayed feedback, 2008](https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf))[[2](https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf)]
  
  * The delay introduced by the environment is constant and known. Like the example you provide in the question.
  
  **b) A random delay** - Random Delayed MDP
  
  * We don't know what delay to expect, or when the delay will occur. This is more realistic for a real-world case.
  
  ---
  
  *TRIED APPROACHES*
  
  **1. Just ignore the delay**
  
  This assumes the CDMDP is an MDP. We then attempt to search for the policy that best ignores the delay
  
  $$
  \pi(I\_k)={\pi^∗(s)|I\_k=(s, a\_1,···,a\_k)}
  $$
  
  $k$ is the number of time-steps between acting on the current state, and receiving feedback. This is simple and can give a reasonable result if the delay is small compared to the state transition magnitude [[2](https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf)].
  
  **2. Reconstruct the MDP from the CDMDP (Augmented approach)**
  
  The corresponding optimal policy for the reconstructed MDP will then the optimal policy for the CDMDP. This is however made intractable by the size of the action buffer growing with the delay length. It's therefore limited to small constant delays. [[1](https://www.researchgate.net/publication/3031436_Engelbrecht_SE_Markov_decision_processes_with_delays_and_asynchronous_cost_collection_IEEE_Trans_Autom_Control_484_568-574#read)] defines how to do the re-construction.
  
  **3. Predicting the delayed observation**
  
  This tries to "undelay" the environment by using a predictive model $P$ to approximate what the delayed observation would be, instead of keeping the agent patient to the end of the delay ([Learning and planning in envs with delayed feedback, (2008)](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjj-7yLhcnwAhUcQUEAHafeAJkQFjADegQIDhAD&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.115.677%26rep%3Drep1%26type%3Dpdf&usg=AOvVaw3iFfuHVw4m2fSrEvjnuX2d)[[5](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjj-7yLhcnwAhUcQUEAHafeAJkQFjADegQIDhAD&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.115.677%26rep%3Drep1%26type%3Dpdf&usg=AOvVaw3iFfuHVw4m2fSrEvjnuX2d)], [At Human Speed: Deep RL with action delay, (2018)](https://arxiv.org/pdf/1810.07286.pdf)][[6](https://arxiv.org/pdf/1810.07286.pdf)]).
  
  *e.g., For a delayed action*:
  
  $$
  s\_{t+i} = P(s\_{t + i -1}, a\_{t + i - delay})
  $$
  
  If the delay is on the observation, the most recent K actions can be used with the most recent observation to approximate the current state[[2](https://link.springer.com/content/pdf/10.1007%2F978-3-540-74958-5_41.pdf)]. However, this also means the delays are part of the state - it's possible to have a better approach, as next described in the random delays part.
  
  Estimating the current state enables the agent to act conditioned an estimate of the true state, on which the action is executed. The limitation is that it assumes a constant delay.
  
  ---
  
  All the above approaches assume a constant delay
  
  *Handling random delays*\*
  
  **4. Partial Trajectory resampling (PTR)**
  
  A method that recursively resamples actions in the buffer, replacing them with on-policy actions. It uses the delay dynamics to simulate their effect on the current policy.
  
  If random delays exist on an MDP, some actions present in the off-policy replay buffer will not influence the later delayed observations and rewards, for a number of timesteps. This will allow generation of on-policy sub-trajectories from the off-policy samples by recursively resampling the most recent actions from the action buffer. It will not invalidate the sub-trajectory.
  
  The resampled action will be valid as long as the observation delay $\omega$ and the action delay $\alpha$ are both greater than the current time step $t$ i.e., No delayed observation depends on a resampled action.
  
  $$
  \omega\_t + \alpha\_t >= t
  $$
  
  [![Partial trajectory resampling](https://i.sstatic.net/ND2pF.png)](https://i.sstatic.net/ND2pF.png)
  *Illustration of PTR, ([3](https://arxiv.org/abs/2010.02966))*
  
  The benefits of this method:
  
  1. Allows discarding of outdated information. e.g If the delay is such that the are no observations for $5$ timesteps, which then all arrive at step $5$, we can safely discard the transitions of steps $(1,2, 3,4)$. This is because this info will be compressed in the most recent observation.
  2. Provides information on the "age" of an observation and the actions applied next
  3. Efficient credit assignment
  
  These factors give partial trajectory resampling better performance compared to concatenating the past $K$ actions with the most recent observation to estimate the current delayed state.
  
  [RL with random delays (2020)](https://openreview.net/forum?id=QFYnKlBJYR)[[3](https://arxiv.org/abs/2010.02966)] describes and applies Partial Trajectory Resampling in RL. It's the only work I've managed to come across that handles both random and constant delayed MDPs.
  
  **What of the reward delay?**
  
  Reward delay, in this context, is referring to the rewards attributed to a delayed observation and action.
  
  A solution to a delayed MDP will involve tackling the [credit assignment problem](https://ai.stackexchange.com/questions/12908/what-is-the-credit-assignment-problem/12909). In this case, this is deciding what to do with the delayed rewards and how to give credit to the delayed actions and states.
  
  This has had different approaches. For instance:
  
  > " ...we chose to accumulate the rewards corresponding to the [excessively delayed transitions we drop]. When an observation gets repeated
  > because no new observation is available, the corresponding reward is 0, and when a new observation arrives, the corresponding reward contains the sum of intermediate rewards in lost [dropped] transitions." [[3](https://arxiv.org/abs/2010.02966)]
  
  [Real time sample efficint RL for robots, (2013)](https://www.cs.utexas.edu/%7Epstone/Courses/394Rfall19/resources/Ch8-TEXPLORE.pdf)[[9](https://www.cs.utexas.edu/%7Epstone/Courses/394Rfall19/resources/Ch8-TEXPLORE.pdf)] on the other hand, uses a decision tree to assign credit to an MDP reconstructed from a CMDP. The tree learns which delayed actions are relevant.
  
  ---
  
  Related reads:
  
  1. [Real-time Reinforcement Learning (2019)](https://arxiv.org/pdf/1911.04448.pdf)
  2. [Thinking while moving: Deep reinforcement learning with concurrent control, (2020)](https://arxiv.org/abs/2004.06089)
  
  
  ---
  
  