Answers.Body: <p>The classic working reward scheme for two player zero sum games (i.e. if I win, you lose and vice versa) is simply:</p>
  
  <ul>
  <li><p>+1 for a win</p></li>
  <li><p>0 for a draw</p></li>
  <li><p>-1 for a loss</p></li>
  </ul>
  
  <p>These rewards should be associated with the last move made by the player before the game is resolved.</p>
  
  <blockquote>
    <p>I thought about giving a negativ reward for the move played before the winning move. </p>
  </blockquote>
  
  <p>That is what is normally done.</p>
  
  <blockquote>
    <p>Thinking about this I came to the conclusion that this is a bad idea. There'll be always a looser (except for ties), therefor there always be a last move from the loosing player. This one hasn't to be a bad one. Mistakes could have been made much earlier.</p>
  </blockquote>
  
  <p>It's OK, what the agent is learning is the <em>value</em> of a combination of state and action. It will, eventually, correctly associate the negative reward with a chain of board states that decrease in value and that it will then try to avoid.</p>
  
  <p>This scenario of delayed rewards, not necessarily related to the choice of the current action, is handled correctly by reinforcement learning algorithms. It is still a tough problem though - a more delayed and difficult to predict reward makes for a tougher RL problem. However, even basic learners, such as Q-learning, can eventually solve this.</p>
  
  <p>To get better performance, you might want to look into adding planning algorithms to the RL part.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-18T15:50:23.577Z
Answers.Text: Currently the reward plus 1 is given immediately by the Connect4 env. So I basically wait with adding my [state, action, reward, next state] to the experience replay for one move and choose the reward from [+1,0,-1]? Could you give some more explanation what you mean which adding planning? There are perfect min max algorithms for Connect 4, but this is like cheating. What kind of extra information would you hand the RL network?
Answers.UserDisplayName: Mr.Sh4nnon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-18T16:46:45.243Z
Answers.Text: For deriving correct [+1, 0, -1] it depends on how your agent and environment are structured for multi-players. I don't know what you have set up, so cannot tell you. How to combine RL learning and planning is a whole new question, my comment at the end was a suggestion of what you could research next. Typically add the results of a look-ahead search or rollout - e.g. MCTS, to guide the algorithm and learn faster. This is pretty much what AlphaZero does. It doesn't need to run to perfection, you can decide how to split effort between learning and planning to suit what you are studying.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-18T15:44:08.923Z
Answers.LastActivityDate: 2018-12-18T15:44:08.923Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm using RL to train a Network on the game Connect4. It learns quickly that 4 connected pieces is good. It gets a reward of 1 for this. A zero is rewarded for all other moves.</p>
  <p>It takes quite a time until the AI tries to stop the opponent from winning.</p>
  <p><em>Is there a way this could be further reinforced?</em></p>
  <p>I thought about giving a negative reward for the move played before the winning move. Thinking about this, I came to the conclusion that this is a bad idea. There'll be always a looser (except for ties), therefore there always be a last move from the losing player. This one hasn't to be a bad one. Mistakes could have been made much earlier.</p>
  <p><em>Is there a way to improve this awareness of opponents? Or does it just have to train more?</em></p>
  <p>I'm not perfectly sure if the rewards will propagate back in a way that encourages this behavior with my setup.</p>
  
Tags: <reinforcement-learning><game-ai><rewards><reward-design><connect-four>
Title: How should I define the reward function in the case of Connect Four?
UserDisplayName: Mr.Sh4nnon
ThreadText: # How should I define the reward function in the case of Connect Four?
  
  (Asked by: username_0 on 2018-12-18)
  
  I'm using RL to train a Network on the game Connect4. It learns quickly that 4 connected pieces is good. It gets a reward of 1 for this. A zero is rewarded for all other moves.
  
  It takes quite a time until the AI tries to stop the opponent from winning.
  
  *Is there a way this could be further reinforced?*
  
  I thought about giving a negative reward for the move played before the winning move. Thinking about this, I came to the conclusion that this is a bad idea. There'll be always a looser (except for ties), therefore there always be a last move from the losing player. This one hasn't to be a bad one. Mistakes could have been made much earlier.
  
  *Is there a way to improve this awareness of opponents? Or does it just have to train more?*
  
  I'm not perfectly sure if the rewards will propagate back in a way that encourages this behavior with my setup.
  
  **Tags:** reinforcement-learning, game-ai, rewards, reward-design, connect-four
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-18. Score: 2 | Accepted answer
  
  The classic working reward scheme for two player zero sum games (i.e. if I win, you lose and vice versa) is simply:
  
  * +1 for a win
  * 0 for a draw
  * -1 for a loss
  
  These rewards should be associated with the last move made by the player before the game is resolved.
  
  > I thought about giving a negativ reward for the move played before the winning move.
  
  That is what is normally done.
  
  > Thinking about this I came to the conclusion that this is a bad idea. There'll be always a looser (except for ties), therefor there always be a last move from the loosing player. This one hasn't to be a bad one. Mistakes could have been made much earlier.
  
  It's OK, what the agent is learning is the *value* of a combination of state and action. It will, eventually, correctly associate the negative reward with a chain of board states that decrease in value and that it will then try to avoid.
  
  This scenario of delayed rewards, not necessarily related to the choice of the current action, is handled correctly by reinforcement learning algorithms. It is still a tough problem though - a more delayed and difficult to predict reward makes for a tougher RL problem. However, even basic learners, such as Q-learning, can eventually solve this.
  
  To get better performance, you might want to look into adding planning algorithms to the RL part.
  
  > **Comments:**
  > **username_0**: Currently the reward plus 1 is given immediately by the Connect4 env. So I basically wait with adding my [state, action, reward, next state] to the experience replay for one move and choose the reward from [+1,0,-1]? Could you give some more explanation what you mean which adding planning? There are perfect min max algorithms for Connect 4, but this is like cheating. What kind of extra information would you hand the RL network?  
  > **username_1**: For deriving correct [+1, 0, -1] it depends on how your agent and environment are structured for multi-players. I don't know what you have set up, so cannot tell you. How to combine RL learning and planning is a whole new question, my comment at the end was a suggestion of what you could research next. Typically add the results of a look-ahead search or rollout - e.g. MCTS, to guide the algorithm and learn faster. This is pretty much what AlphaZero does. It doesn't need to run to perfection, you can decide how to split effort between learning and planning to suit what you are studying.  
  
  ---
  
  