Answers.Body: <p>This is known as <strong>Quiescence Search</strong>.</p>
  
  <ul>
  <li><a href="https://chessprogramming.wikispaces.com/Quiescence+Search" rel="noreferrer">https://chessprogramming.wikispaces.com/Quiescence+Search</a></li>
  </ul>
  
  <blockquote>
    <p>The purpose of this search is to only evaluate "quiet" positions, or
    positions where there are no winning tactical moves to be made. This
    search is needed to avoid the horizon effect. Simply stopping your
    search when you reach the desired depth and then evaluate, is very
    dangerous. Consider the situation where the last move you consider is
    QxP. If you stop there and evaluate, you might think that you have won
    a pawn. But what if you were to search one move deeper and find that
    the next move is PxQ? You didn't win a pawn, you actually lost a
    queen. Hence the need to make sure that you are evaluating only
    quiescent (quiet) positions.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T00:26:33.443Z
Answers.LastActivityDate: 2018-03-09T00:26:33.443Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>I can see more than one reason for that.</p>
  
  <p>One reason is that after a check there are very few moves available: usually there are 30 moves available, but after a check there are usually at most 4 or 5. So that move doesn't count as a full ply if you want to measure the size of the resulting tree, and in the same search time you can go deeper.  </p>
  
  <p>Another reason, and I think it is the most important, is that a check is a forcing move, and consequences of a forcing move must be considered more deeply. So it is good to go deeper after a check.</p>
  
  <p>Note that in this way, the program will tend to analyze all forced sequences of consecutive checks very deep, in this way it can easily find all perpetual checks and many forced mates. So this is can be very useful.  </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T06:02:15.507Z
Answers.Text: But how does this eliminates horizon effect. I mean what you said can be applied to other valuable pieces as well. Like if at the end of depth we take a pawn but the next move can be a queen capture by other side. So in that way increasing the depth should be done for every major piece. But we can see that no matter how much depth we go there will always be a horizon.
Answers.UserDisplayName: Dipendra bhatt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T06:37:39.463Z
Answers.Text: A Quiescence search is used to evaluate the position when there are no captures, checks, and serious threats.  This reduces most of the horizon effect, and makes it a lot easier to evaluate the position.
Answers.UserDisplayName: Fred Knight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T09:18:40.543Z
Answers.Text: Okay..i think I got it...thanks for help...
Answers.UserDisplayName: Dipendra bhatt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-08T23:14:52.940Z
Answers.LastActivityDate: 2018-03-08T23:14:52.940Z
Answers.UserDisplayName: LeibnizGW
Body: <p>I was following a tutorial for making a chess engine and in that, while writing the alpha beta pruning function the tutorial before checking for all possible moves generated , checks weather the current side is in check or not. If in check the depth was increased and i couldn't understand the reason. I am fairly new to chess engine programming and have searched about this but couldn't find anything relevant.
  Any help will be much appreciated.Thanks.</p>
  
  <p>Here is the code for alpha beta pruning.</p>
  
  <pre><code>function AlphaBeta(depth, alpha, beta)
  {
      SearchController.nodes++;
      if (depth &lt;= 0)
      {
          return Evaluate();
      }
  
      //fiftyMove rule or if a position is Repeated
      if ((IsRepeated() || GameBoard.fiftyMove &gt;= 100) &amp;&amp; GameBoard.hisPly != 0)
          return 0;
  
      if (GameBoard.ply &gt; MAXDEPTH - 1 )
          return Evaluate();
  
      //this is the line i am talking about
      var InCheck = SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1 );
      if (InCheck)  {
          depth++; //&lt;---------HERE
      }
  
      var score = -INFINITE;
  
      GenerateMoves();
  
      var Move = NOMOVE, MoveNum = 0, BestMove = NOMOVE;
      var legal = 0; // To see weather any legal moves are possible
      var start = GameBoard.moveListStart[GameBoard.ply], end = GameBoard.moveListStart[GameBoard.ply + 1];
  
      for(MoveNum = start; MoveNum &lt; end; MoveNum++)
      {
          Move = GameBoard.moveList[MoveNum];
  
          if (!MakeMove(Move))
              continue;
  
          legal++;
  
          score = -AlphaBeta(depth - 1, -beta, -alpha);
  
          TakeMoveBack();
  
  
          if (score &gt; alpha)
          {
              if(score &gt;= beta)
                  return beta;
  
              alpha = score;
              BestMove = Move;
          }
  
          console.log('For move : ' + PrMove(Move) + ' Score : ' + score );
      }
  
      if (BestMove != NOMOVE)
      {
          StorePvMove(BestMove);
      }
  
      if (legal == 0)
      {
          if (SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1))
          {
              return MATE - GameBoard.ply;
          }
          else return 0;
      }
  
      console.log("");
      console.log("Best Move at Height : " + depth + ' Move : ' + PrMove(BestMove) + ' Score : ' + alpha);
      return alpha;
  
  }
  </code></pre>
  
Tags: <engines><chess-ai>
Title: Why do we increase depth of search for a mate detection in alpha beta pruning?
ThreadText: # Why do we increase depth of search for a mate detection in alpha beta pruning?
  
  (Asked by: username_0 on 2018-03-08)
  
  I was following a tutorial for making a chess engine and in that, while writing the alpha beta pruning function the tutorial before checking for all possible moves generated , checks weather the current side is in check or not. If in check the depth was increased and i couldn't understand the reason. I am fairly new to chess engine programming and have searched about this but couldn't find anything relevant.
  Any help will be much appreciated.Thanks.
  
  Here is the code for alpha beta pruning.
  
  ```
  function AlphaBeta(depth, alpha, beta)
  {
      SearchController.nodes++;
      if (depth <= 0)
      {
          return Evaluate();
      }
  
      //fiftyMove rule or if a position is Repeated
      if ((IsRepeated() || GameBoard.fiftyMove >= 100) && GameBoard.hisPly != 0)
          return 0;
  
      if (GameBoard.ply > MAXDEPTH - 1 )
          return Evaluate();
  
      //this is the line i am talking about
      var InCheck = SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1 );
      if (InCheck)  {
          depth++; //<---------HERE
      }
  
      var score = -INFINITE;
  
      GenerateMoves();
  
      var Move = NOMOVE, MoveNum = 0, BestMove = NOMOVE;
      var legal = 0; // To see weather any legal moves are possible
      var start = GameBoard.moveListStart[GameBoard.ply], end = GameBoard.moveListStart[GameBoard.ply + 1];
  
      for(MoveNum = start; MoveNum < end; MoveNum++)
      {
          Move = GameBoard.moveList[MoveNum];
  
          if (!MakeMove(Move))
              continue;
  
          legal++;
  
          score = -AlphaBeta(depth - 1, -beta, -alpha);
  
          TakeMoveBack();
  
          if (score > alpha)
          {
              if(score >= beta)
                  return beta;
  
              alpha = score;
              BestMove = Move;
          }
  
          console.log('For move : ' + PrMove(Move) + ' Score : ' + score );
      }
  
      if (BestMove != NOMOVE)
      {
          StorePvMove(BestMove);
      }
  
      if (legal == 0)
      {
          if (SqAttacked(GameBoard.pieceList[Kings[GameBoard.side] * 10] , GameBoard.side ^ 1))
          {
              return MATE - GameBoard.ply;
          }
          else return 0;
      }
  
      console.log("");
      console.log("Best Move at Height : " + depth + ' Move : ' + PrMove(BestMove) + ' Score : ' + alpha);
      return alpha;
  
  }
  
  ```
  
  **Tags:** engines, chess-ai
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-03-08. Score: 5 | Accepted answer
  
  I can see more than one reason for that.
  
  One reason is that after a check there are very few moves available: usually there are 30 moves available, but after a check there are usually at most 4 or 5. So that move doesn't count as a full ply if you want to measure the size of the resulting tree, and in the same search time you can go deeper.
  
  Another reason, and I think it is the most important, is that a check is a forcing move, and consequences of a forcing move must be considered more deeply. So it is good to go deeper after a check.
  
  Note that in this way, the program will tend to analyze all forced sequences of consecutive checks very deep, in this way it can easily find all perpetual checks and many forced mates. So this is can be very useful.
  
  > **Comments:**
  > **username_0**: But how does this eliminates horizon effect. I mean what you said can be applied to other valuable pieces as well. Like if at the end of depth we take a pawn but the next move can be a queen capture by other side. So in that way increasing the depth should be done for every major piece. But we can see that no matter how much depth we go there will always be a horizon.  
  > **username_3**: A Quiescence search is used to evaluate the position when there are no captures, checks, and serious threats.  This reduces most of the horizon effect, and makes it a lot easier to evaluate the position.  
  > **username_0**: Okay..i think I got it...thanks for help...  
  
  ---
  
  ### Answer by username_1 on 2018-03-09. Score: 5
  
  This is known as **Quiescence Search**.
  
  * <https://chessprogramming.wikispaces.com/Quiescence+Search>
  
  > The purpose of this search is to only evaluate "quiet" positions, or
  > positions where there are no winning tactical moves to be made. This
  > search is needed to avoid the horizon effect. Simply stopping your
  > search when you reach the desired depth and then evaluate, is very
  > dangerous. Consider the situation where the last move you consider is
  > QxP. If you stop there and evaluate, you might think that you have won
  > a pawn. But what if you were to search one move deeper and find that
  > the next move is PxQ? You didn't win a pawn, you actually lost a
  > queen. Hence the need to make sure that you are evaluating only
  > quiescent (quiet) positions.
  
  
  ---
  
  