Answers.Body: <p>This is a tricky question, and I'm sure there's no definitive solution.</p>
  
  <p>Lichess uses this definition (<a href="https://lichess.org/qa/102/how-are-the-mid-game-and-end-game-dividers-determined" rel="noreferrer">https://lichess.org/qa/102/how-are-the-mid-game-and-end-game-dividers-determined</a>)</p>
  
  <pre><code>Opening is obvious, starts on move 1.
  
  Mid-game is less obvious, and is still in testing. The long-and-short of the current heuristics are that the mid-game occurs when either:
  - The position is adequately complex and enough pieces are developed.
  - 2 sets of minor or major pieces have been taken off the board. (This is mainly useful for when the position is quite open)
  
  End-game is slightly more straight forward, when there are less than 7 minor and major pieces on the board the end-game has begun (so, perhaps 2 rooks and a bishop Vs 2 bishops and a knight). This is not technically where everyone would draw the line, but it's a good indicator that either 1. The end-game has begun 2. The end-game will begin soon.
  </code></pre>
  
  <p>Stockfish uses interpolation between the middlegame and endgame. This is done by counting non-pawn materials (<a href="https://github.com/official-stockfish/Stockfish/blob/03a9b3bd8d79e1677f1c74cf7d8f8036795675c6/src/material.cpp" rel="noreferrer">https://github.com/official-stockfish/Stockfish/blob/03a9b3bd8d79e1677f1c74cf7d8f8036795675c6/src/material.cpp</a>)</p>
  
  <pre><code>// Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
  e-&gt;gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
  </code></pre>
  
  <p>You will need to make a decision on what material counts you want to use.</p>
  
  <p>Suggestions:</p>
  
  <ul>
  <li>If your engine supports opening book, you might want to use that</li>
  <li>You might use the move counts (it's in the FEN string) for opening vs middlgame</li>
  <li>Endgame is generally defined as number of X pieces and N pawns on the board (you decide what X and N should be). N&lt;=3 is almost always an endgame. </li>
  <li>Lichess's definitions have worked quite well practically.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-03T11:46:02.387Z
Answers.Text: In your third dot, do you rather mean X<=3 ?
Answers.UserDisplayName: Evargalo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-03T11:47:43.240Z
Answers.Text: @Evargalo No. When the number of pawns is no more than three, we generally think it's an endgame. It's unusual to have no more than three pawns for each side in a "middlegame". But there's no right or wrong here.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-04T07:01:56.403Z
Answers.Text: X<=3 is a pretty good indicator that you're in the endgame as well, though :D
Answers.UserDisplayName: Annatar
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-04T07:10:37.467Z
Answers.Text: @Annatar Ok.. I'm wrong. X and N <= 3.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-04T08:39:42.260Z
Answers.Text: @SmallChess You're not wrong. X *or* N <= 3 is enough.
Answers.UserDisplayName: Annatar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-20T05:11:34.133Z
Answers.Text: [your link](https://lichess.org/qa/102/how-are-the-mid-game-and-end-game-dividers-determined) just redirects to this https://lichess.org/faq what's going on?
Answers.UserDisplayName: BCLC
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-20T05:31:47.380Z
Answers.Text: but here's another [link](https://lichess.org/forum/game-analysis/how-does-the-analysis-tool-know-where-the-middle-game-and-end-game-start) that says '6 Major/Minor pieces left on table'. another comment says 7 but both unsourced (w/rt lichess. of course the other comment is semi-sourced re wiki)
Answers.UserDisplayName: BCLC
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-03T02:48:12.287Z
Answers.LastActivityDate: 2017-12-03T02:53:14.020Z
Answers.LastEditDate: 2017-12-03T02:53:14.020Z
Answers.UserDisplayName: SmallChess
Body: <p>What is the best or alternative ways to detect game phase by a chess AI? What is a good algorithm to use? <a href="https://chessprogramming.wikispaces.com/Game+Phases" rel="nofollow noreferrer">Chess programming wiki</a> says to count material or pieces? What would be the expected piece or material counts to use?</p>
  
  <p>I have an idea to use average pieces on a file and its standard deviation but that’s intensive math to use? And I’m unsure of its applicability?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T22:03:59.223Z
Comments.Text: What do you mean with "best", it's completely subjective for humans too. What do you want to use the information for?
Comments.UserDisplayName: RemcoGerlich
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-03T00:45:00.017Z
Comments.Text: Your right. I’m not exactly looking for best but alternative methods to use. I basically want to benchmark different ideas and pick something that is relatively accurate and light weight. My purpose is to add modified terms in the evaluation that depend on game phase. Something like stockfishes tapered eval. Taper evaal for me is too heavy in performance. You basically triple the fuction calls.
Comments.UserDisplayName: Νικόλαος Μανωλακος
Tags: <evaluation><chess-algorithms>
Title: Calculation for game phase
ThreadText: # Calculation for game phase
  
  (Asked by: username_0 on 2017-12-02)
  
  What is the best or alternative ways to detect game phase by a chess AI? What is a good algorithm to use? [Chess programming wiki](https://chessprogramming.wikispaces.com/Game+Phases) says to count material or pieces? What would be the expected piece or material counts to use?
  
  I have an idea to use average pieces on a file and its standard deviation but that’s intensive math to use? And I’m unsure of its applicability?
  
  **Tags:** evaluation, chess-algorithms
  
  > **Comments:**
  > **username_1**: What do you mean with "best", it's completely subjective for humans too. What do you want to use the information for?  
  > **username_0**: Your right. I’m not exactly looking for best but alternative methods to use. I basically want to benchmark different ideas and pick something that is relatively accurate and light weight. My purpose is to add modified terms in the evaluation that depend on game phase. Something like stockfishes tapered eval. Taper evaal for me is too heavy in performance. You basically triple the fuction calls.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-12-03. Score: 3
  
  This is a tricky question, and I'm sure there's no definitive solution.
  
  Lichess uses this definition (<https://lichess.org/qa/102/how-are-the-mid-game-and-end-game-dividers-determined>)
  
  ```
  Opening is obvious, starts on move 1.
  
  Mid-game is less obvious, and is still in testing. The long-and-short of the current heuristics are that the mid-game occurs when either:
  - The position is adequately complex and enough pieces are developed.
  - 2 sets of minor or major pieces have been taken off the board. (This is mainly useful for when the position is quite open)
  
  End-game is slightly more straight forward, when there are less than 7 minor and major pieces on the board the end-game has begun (so, perhaps 2 rooks and a bishop Vs 2 bishops and a knight). This is not technically where everyone would draw the line, but it's a good indicator that either 1. The end-game has begun 2. The end-game will begin soon.
  
  ```
  
  Stockfish uses interpolation between the middlegame and endgame. This is done by counting non-pawn materials (<https://github.com/official-stockfish/Stockfish/blob/03a9b3bd8d79e1677f1c74cf7d8f8036795675c6/src/material.cpp>)
  
  ```
  // Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
  e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
  
  ```
  
  You will need to make a decision on what material counts you want to use.
  
  Suggestions:
  
  * If your engine supports opening book, you might want to use that
  * You might use the move counts (it's in the FEN string) for opening vs middlgame
  * Endgame is generally defined as number of X pieces and N pawns on the board (you decide what X and N should be). N<=3 is almost always an endgame.
  * Lichess's definitions have worked quite well practically.
  
  > **Comments:**
  > **username_3**: In your third dot, do you rather mean X<=3 ?  
  > **username_2**: @username_3 No. When the number of pawns is no more than three, we generally think it's an endgame. It's unusual to have no more than three pawns for each side in a "middlegame". But there's no right or wrong here.  
  > **username_4**: X<=3 is a pretty good indicator that you're in the endgame as well, though :D  
  > **username_2**: @username_4 Ok.. I'm wrong. X and N <= 3.  
  > **username_4**: @username_2 You're not wrong. X *or* N <= 3 is enough.  
  > **username_5**: [your link](https://lichess.org/qa/102/how-are-the-mid-game-and-end-game-dividers-determined) just redirects to this https://lichess.org/faq what's going on?  
  > **username_5**: but here's another [link](https://lichess.org/forum/game-analysis/how-does-the-analysis-tool-know-where-the-middle-game-and-end-game-start) that says '6 Major/Minor pieces left on table'. another comment says 7 but both unsourced (w/rt lichess. of course the other comment is semi-sourced re wiki)  
  
  ---
  
  