Answers.Body: <p>You should probably grant reward at the point that the game is logically won. This will help the agent learn more efficiently, by reducing the number of timesteps over which return values need to be backed up.</p>
  <p>Stopping the episode at that point should also be fine, and may add some efficiency too, in that there will be more focused relevant data in the experience replay. It seems like on the surface that there is no benefit to exploring or discovering any policy <em>after</em> the game is won, and from the comments no expectation from you as agent developer that the agent has any kind of behaviour - random actions would be fine.</p>
  <p>It is still possible that the agent could learn more from play <em>after</em> a winning state. It would require certain things to be true about the environment and additional work from you as developer.</p>
  <p>For example, if the game has an end phase where a certain kind of action is more common and it gains something within the game (&quot;victory points&quot;, &quot;gold&quot; or some other numbered token that is part of the game mechanics and could be measured), then additional play where this happened could be of interest. Especially if the moves that gained this measure could also be part of winning moves in the earlier game. To allow the agent to learn this though, it would have to be something that it <em>predicted</em> in addition to winning or losing.</p>
  <p>One way to achieve this is to have a secondary learning system as part of the agent, that learns to predict gains (or totals) of this resource. Such a prediction could either be learned separately (but very similarly to the action value) and fed into the q function as an input, or it could be a neural network that shares early layers with the q function (or policy function) but with a different head. Adding this kind of secondary function to the neural network can also have a regularising effect on the network, because the interim features have to be good for two types of prediction.</p>
  <p>You definitley do not <em>need</em> to consider such an addition. It could be a lot more work. However, for some games it is possible that it helps. Knowing the game, and understanding whether there is any learning experience to be had as a human player to play on beyond winning or losing, might help you decide whether looking into trying to replicate this additional experience for a bot. Even if it works, the effect may be minimal and not worth the difference it makes. For instance running a more basic learning agent for more episodes may still result in a very good agent for the end game. That only costs you more run time for training, not coding effort.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-29T20:06:13.077Z
Answers.LastActivityDate: 2020-06-29T20:06:13.077Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm using the DQN algorithm to train my agent to play a turn-based game. The winner of the game can be known before the game is over. Once the winning condition is satisfied, it cannot be reverted. For example, the game might last 100 turns, but it's possible to know that one of the players won at move 80, because some winning condition was satisfied. The last 20 moves don't change the outcome of the game. If people were playing this game, they, would play it to the very end, but the agent doesn't have to.</p>
  <p>The agent will be using memory replay to learn from the experience. I wonder, is it helpful for the agent to have the experiences after the winning condition was satisfied for a more complete picture? Or is it better to terminate the game immediately, and why? How would this affect agent's learning?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-29T16:41:26.847Z
Comments.Text: What is your reward function? Is this a zero-sum game where the reward is -1 for a loss, 0 for a draw, and 1 for a win?
Comments.UserDisplayName: DeepQZero
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-29T16:58:28.210Z
Comments.Text: Do you want the agent to ever continue playing after the winning condition (perhaps in production against human players)?  If so, is it necessary to play out the game in a particular way in order to "secure" a logically obvious win? Can a winning condition actually be dropped through deliberately bad play? Would playing randomly or "badly" in some way be considered bad game etiquette for other players if e.g. they are playing to see who gets second place?
Comments.UserDisplayName: Neil Slater
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-29T17:50:35.060Z
Comments.Text: It is a zero sum game
Comments.UserDisplayName: mark mark
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-29T17:52:48.617Z
Comments.Text: Once the winning condition is satisfied, it cannot be reverted (random moves would suffice)
Comments.UserDisplayName: mark mark
Tags: <reinforcement-learning><dqn>
Title: Should the agent play the game until the end or until the winner is found?
UserDisplayName: mark mark
ThreadText: # Should the agent play the game until the end or until the winner is found?
  
  (Asked by: username_0 on 2020-06-29)
  
  I'm using the DQN algorithm to train my agent to play a turn-based game. The winner of the game can be known before the game is over. Once the winning condition is satisfied, it cannot be reverted. For example, the game might last 100 turns, but it's possible to know that one of the players won at move 80, because some winning condition was satisfied. The last 20 moves don't change the outcome of the game. If people were playing this game, they, would play it to the very end, but the agent doesn't have to.
  
  The agent will be using memory replay to learn from the experience. I wonder, is it helpful for the agent to have the experiences after the winning condition was satisfied for a more complete picture? Or is it better to terminate the game immediately, and why? How would this affect agent's learning?
  
  **Tags:** reinforcement-learning, dqn
  > **Comments:**
  > **username_1**: What is your reward function? Is this a zero-sum game where the reward is -1 for a loss, 0 for a draw, and 1 for a win?  
  > **username_2**: Do you want the agent to ever continue playing after the winning condition (perhaps in production against human players)?  If so, is it necessary to play out the game in a particular way in order to "secure" a logically obvious win? Can a winning condition actually be dropped through deliberately bad play? Would playing randomly or "badly" in some way be considered bad game etiquette for other players if e.g. they are playing to see who gets second place?  
  > **username_0**: It is a zero sum game  
  > **username_0**: Once the winning condition is satisfied, it cannot be reverted (random moves would suffice)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-06-29. Score: 1 | Accepted answer
  
  You should probably grant reward at the point that the game is logically won. This will help the agent learn more efficiently, by reducing the number of timesteps over which return values need to be backed up.
  
  Stopping the episode at that point should also be fine, and may add some efficiency too, in that there will be more focused relevant data in the experience replay. It seems like on the surface that there is no benefit to exploring or discovering any policy *after* the game is won, and from the comments no expectation from you as agent developer that the agent has any kind of behaviour - random actions would be fine.
  
  It is still possible that the agent could learn more from play *after* a winning state. It would require certain things to be true about the environment and additional work from you as developer.
  
  For example, if the game has an end phase where a certain kind of action is more common and it gains something within the game ("victory points", "gold" or some other numbered token that is part of the game mechanics and could be measured), then additional play where this happened could be of interest. Especially if the moves that gained this measure could also be part of winning moves in the earlier game. To allow the agent to learn this though, it would have to be something that it *predicted* in addition to winning or losing.
  
  One way to achieve this is to have a secondary learning system as part of the agent, that learns to predict gains (or totals) of this resource. Such a prediction could either be learned separately (but very similarly to the action value) and fed into the q function as an input, or it could be a neural network that shares early layers with the q function (or policy function) but with a different head. Adding this kind of secondary function to the neural network can also have a regularising effect on the network, because the interim features have to be good for two types of prediction.
  
  You definitley do not *need* to consider such an addition. It could be a lot more work. However, for some games it is possible that it helps. Knowing the game, and understanding whether there is any learning experience to be had as a human player to play on beyond winning or losing, might help you decide whether looking into trying to replicate this additional experience for a bot. Even if it works, the effect may be minimal and not worth the difference it makes. For instance running a more basic learning agent for more episodes may still result in a very good agent for the end game. That only costs you more run time for training, not coding effort.
  
  
  ---
  
  