Answers.Body: <p>Possibilities:</p>
  
  <ul>
  <li>Number of nodes</li>
  <li>Fixed depth</li>
  <li>Fixed time</li>
  <li>Divide a fixed percentage of remaining time</li>
  <li>By the complexity of the position</li>
  </ul>
  
  <p>Let's take a quick look at Stockfish. Briefly, the file <a href="https://github.com/official-stockfish/Stockfish/blob/d8f683760c9eb6d2c4714ec83e717dd2143de55c/src/timeman.cpp" rel="nofollow noreferrer">timemann.cpp</a> calculates the minimum time allowed for a move. The default minimum time allowed is about 20 seconds.</p>
  
  <p>Later, during the <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp" rel="nofollow noreferrer">search</a> it does:</p>
  
  <pre><code> // Stop the search if only one legal move is available, or if all
   // of the available time has been used, or if we matched an easyMove
   // from the previous search and just did a fast verification.
  
   ...
  
   if (   rootMoves.size() == 1
                || Time.elapsed() &gt; Time.optimum() * unstablePvFactor * improvingFactor / 628
                || (mainThread-&gt;easyMovePlayed = doEasyMove, doEasyMove))
  </code></pre>
  
  <p>If the position requires longer thinking (unstable position -
   <code>unstablePvFactor</code>), the engine would extend the search. How long it extend depend on calibrated parameters.</p>
  
  <p>If there is an obvious move (<code>doEasyMove</code>), Stockfish plays it immediately.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-13T18:26:49.573Z
Answers.Text: What is an obvious move ?
Answers.UserDisplayName: Saikat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-28T21:20:04.970Z
Answers.Text: @Saikat e.g. if it's a one-move checkmate or taking a queen without consequences
Answers.UserDisplayName: Nosrep
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-11T06:54:16.757Z
Answers.LastActivityDate: 2017-05-11T11:08:04.317Z
Answers.LastEditDate: 2017-05-11T11:08:04.317Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>Tom Kerrigan's simple chess program uses a scheme to just check every 1024 nodes if a certain amount of time has passed</p>
  
  <p>basically there is a function called think that has the following lines which we jump back to when the time is up...stop_search is set by a function called checkup()</p>
  
  <pre><code>/* some code that lets us longjmp back here and return
      from think() when our time is up */
      stop_search = FALSE;
      setjmp(env);
      if (stop_search) {
  
          /* make sure to take back the line we were searching */
          while (ply)
              takeback();
          return;
      }    
      start_time = get_ms();
      stop_time = start_time + max_time;    
      ply = 0;
      nodes = 0;
  </code></pre>
  
  <p>the following lines are part of the search function that gets called by think()</p>
  
  <pre><code>/* do some housekeeping every 1024 nodes */
  if ((nodes &amp; 1023) == 0)
      checkup();
  </code></pre>
  
  <p>You can see that search() calls checkup() every 1024 nodes. In a more complex scheme the think function could dynamically set the amount of time for the searches. </p>
  
  <pre><code>void checkup()
  {
      /* is the engine's time up? if so, longjmp back to the
         beginning of think() */
      if (get_ms() &gt;= stop_time) {
          stop_search = TRUE;
          longjmp(env, 0);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-10T17:03:33.887Z
Answers.LastActivityDate: 2017-05-10T17:03:33.887Z
Answers.UserDisplayName: nak3c
Body: <p>I was just watching a video on YouTube that analysed a game in between two chess engines. So, chess engines do find good moves but how do they manage their time when they're playing a game ? Is there any algorithm that tells them how much longer they must continue thinking till they make a move, keeping the amount of time left for them in the game in mind ?</p>
  
Tags: <engines>
Title: How do chess engines manage time when they play real games?
ThreadText: # How do chess engines manage time when they play real games?
  
  (Asked by: username_0 on 2017-05-10)
  
  I was just watching a video on YouTube that analysed a game in between two chess engines. So, chess engines do find good moves but how do they manage their time when they're playing a game ? Is there any algorithm that tells them how much longer they must continue thinking till they make a move, keeping the amount of time left for them in the game in mind ?
  
  **Tags:** engines
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-05-11. Score: 4
  
  Possibilities:
  
  * Number of nodes
  * Fixed depth
  * Fixed time
  * Divide a fixed percentage of remaining time
  * By the complexity of the position
  
  Let's take a quick look at Stockfish. Briefly, the file [timemann.cpp](https://github.com/official-stockfish/Stockfish/blob/d8f683760c9eb6d2c4714ec83e717dd2143de55c/src/timeman.cpp) calculates the minimum time allowed for a move. The default minimum time allowed is about 20 seconds.
  
  Later, during the [search](https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp) it does:
  
  ```
   // Stop the search if only one legal move is available, or if all
   // of the available time has been used, or if we matched an easyMove
   // from the previous search and just did a fast verification.
  
   ...
  
   if (   rootMoves.size() == 1
                || Time.elapsed() > Time.optimum() * unstablePvFactor * improvingFactor / 628
                || (mainThread->easyMovePlayed = doEasyMove, doEasyMove))
  
  ```
  
  If the position requires longer thinking (unstable position -
  `unstablePvFactor`), the engine would extend the search. How long it extend depend on calibrated parameters.
  
  If there is an obvious move (`doEasyMove`), Stockfish plays it immediately.
  
  > **Comments:**
  > **username_0**: What is an obvious move ?  
  > **username_2**: @username_0 e.g. if it's a one-move checkmate or taking a queen without consequences  
  
  ---
  
  ### Answer by username_3 on 2017-05-10. Score: 2
  
  Tom Kerrigan's simple chess program uses a scheme to just check every 1024 nodes if a certain amount of time has passed
  
  basically there is a function called think that has the following lines which we jump back to when the time is up...stop\_search is set by a function called checkup()
  
  ```
  /* some code that lets us longjmp back here and return
      from think() when our time is up */
      stop_search = FALSE;
      setjmp(env);
      if (stop_search) {
  
          /* make sure to take back the line we were searching */
          while (ply)
              takeback();
          return;
      }    
      start_time = get_ms();
      stop_time = start_time + max_time;    
      ply = 0;
      nodes = 0;
  
  ```
  
  the following lines are part of the search function that gets called by think()
  
  ```
  /* do some housekeeping every 1024 nodes */
  if ((nodes & 1023) == 0)
      checkup();
  
  ```
  
  You can see that search() calls checkup() every 1024 nodes. In a more complex scheme the think function could dynamically set the amount of time for the searches.
  
  ```
  void checkup()
  {
      /* is the engine's time up? if so, longjmp back to the
         beginning of think() */
      if (get_ms() >= stop_time) {
          stop_search = TRUE;
          longjmp(env, 0);
      }
  }
  
  ```
  
  
  ---
  
  