Answers.Body: <p>Chess algorithms are complicated, but I'm sure static evaluation gets applied before reaching the leaf nodes. For example, it's very common to reduce or even eliminate a branch if it loses materials.</p>
  
  <ul>
  <li>The best way to learn positional evaluation in chess is the Stockfish source code. There's simply nothing better than that. <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/evaluate.cpp" rel="nofollow noreferrer">https://github.com/official-stockfish/Stockfish/blob/master/src/evaluate.cpp</a>.</li>
  <li>If you don't like reading C++ code. There's a page here: <a href="https://hxim.github.io/Stockfish-Evaluation-Guide/" rel="nofollow noreferrer">https://hxim.github.io/Stockfish-Evaluation-Guide/</a>.</li>
  </ul>
  
  <pre>
  function middle_game_evaluation(pos) {
    var v = 0;
    v += piece_value_mg(pos) - piece_value_mg(colorflip(pos));
    v += psqt_mg(pos) - psqt_mg(colorflip(pos));
    v += imbalance_total(pos);
    v += pawns_mg(pos) - pawns_mg(colorflip(pos));
    v += pieces_mg(pos) - pieces_mg(colorflip(pos));
    v += mobility_mg(pos) - mobility_mg(colorflip(pos));
    v += threats_mg(pos) - threats_mg(colorflip(pos));
    v += passed_mg(pos) - passed_mg(colorflip(pos));
    v += space(pos) - space(colorflip(pos));
    v += king_mg(pos) - king_mg(colorflip(pos));
    return v;
  }
  </pre>
  
  <p>Stockfish evaluates a position by linearly add each individual term. That includes piece values, square values, pawn structures, mobility, passed pawns etc. Nothing special here.</p>
  
  <ul>
  <li>How to get those values, known as <strong>parameter tuning</strong>? Nobody knows for sure, we have to adjust the parameters, play many games, look at the results. Sounds dumb? Yes, it is, but that's what all chess programmers do. Google's AlphaZero is no exception.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-10T10:42:39.340Z
Answers.Text: AlphaZero is different in that the parameters are generic and tuned automatically as part of "learning" (where observations influence millions of parameters one game at a time). They didn't start with a term called `passed_mg`, etc. and tune it and other ones with concrete meaning. Instead, there is millions of generic parameters that represent whatever they represent. Some might end up dealing with certain topics like a passed pawn, but others might represent more complicated ideas. It's hard to describe due to the sheer number of parameters.
Answers.UserDisplayName: user904963
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-31T15:21:17.277Z
Answers.LastActivityDate: 2018-01-31T15:21:17.277Z
Answers.UserDisplayName: SmallChess
Body: <p>How do chess engines actually evaluate a position? I am looking for a technical answer, not a general summary of how minimax works, I already know how minimax and alpha-beta, etc, work, so please do not reiterate generalities. I am interested specifically in the evaluation function.</p>
  
  <p>My understanding is that the evaluation of a position is the same as evaluation of the leaf of the decision tree given best play by both sides. So, the problem then is how is each leaf evaluated? Obviously, whatever algorithm is used must be very fast because millions of leafs are evaluated. So, if this is right, then evaluation simply reduces to how a leaf position is evaluated.</p>
  
  <p>The two methods for leaf evaluation that I know of are material balance and king safety. However, I suspect that it must be more complicated than this, otherwise evaluations would not have such fractional values. For example, there are many positions where both sides have the same material and perfectly safe kings. So, in those cases we would expect an evaluation of "0.00". However, in actuality the engine I use (Rybka) <em>never</em> has such an evaluation (unless it is a draw). It's evaluation of such positions is always something like "0.13" or "-0.05" or "0.07". So, obviously it is doing something more than just material+king safety. How does it come up with the number?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-31T15:16:02.147Z
Comments.Text: https://github.com/official-stockfish/Stockfish/blob/master/src/evaluate.cpp
Comments.UserDisplayName: RemcoGerlich
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-26T22:29:20.170Z
Comments.Text: This is not a duplicate of the other question given. This question is asking specifically about the eval function, whereas the other question is much broader.
Comments.UserDisplayName: Kef Schecter
Tags: <engines>
Title: How do engines evaluate a position (actual algorithm)?
ThreadText: # How do engines evaluate a position (actual algorithm)?
  
  (Asked by: username_0 on 2018-01-31)
  
  How do chess engines actually evaluate a position? I am looking for a technical answer, not a general summary of how minimax works, I already know how minimax and alpha-beta, etc, work, so please do not reiterate generalities. I am interested specifically in the evaluation function.
  
  My understanding is that the evaluation of a position is the same as evaluation of the leaf of the decision tree given best play by both sides. So, the problem then is how is each leaf evaluated? Obviously, whatever algorithm is used must be very fast because millions of leafs are evaluated. So, if this is right, then evaluation simply reduces to how a leaf position is evaluated.
  
  The two methods for leaf evaluation that I know of are material balance and king safety. However, I suspect that it must be more complicated than this, otherwise evaluations would not have such fractional values. For example, there are many positions where both sides have the same material and perfectly safe kings. So, in those cases we would expect an evaluation of "0.00". However, in actuality the engine I use (Rybka) *never* has such an evaluation (unless it is a draw). It's evaluation of such positions is always something like "0.13" or "-0.05" or "0.07". So, obviously it is doing something more than just material+king safety. How does it come up with the number?
  
  **Tags:** engines
  
  > **Comments:**
  > **username_1**: https://github.com/official-stockfish/Stockfish/blob/master/src/evaluate.cpp  
  > **username_2**: This is not a duplicate of the other question given. This question is asking specifically about the eval function, whereas the other question is much broader.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-01-31. Score: 4
  
  Chess algorithms are complicated, but I'm sure static evaluation gets applied before reaching the leaf nodes. For example, it's very common to reduce or even eliminate a branch if it loses materials.
  
  The best way to learn positional evaluation in chess is the Stockfish source code. There's simply nothing better than that. https://github.com/official-stockfish/Stockfish/blob/master/src/evaluate.cpp.
  If you don't like reading C++ code. There's a page here: https://hxim.github.io/Stockfish-Evaluation-Guide/.
  
  
  function middle_game_evaluation(pos) {
    var v = 0;
    v += piece_value_mg(pos) - piece_value_mg(colorflip(pos));
    v += psqt_mg(pos) - psqt_mg(colorflip(pos));
    v += imbalance_total(pos);
    v += pawns_mg(pos) - pawns_mg(colorflip(pos));
    v += pieces_mg(pos) - pieces_mg(colorflip(pos));
    v += mobility_mg(pos) - mobility_mg(colorflip(pos));
    v += threats_mg(pos) - threats_mg(colorflip(pos));
    v += passed_mg(pos) - passed_mg(colorflip(pos));
    v += space(pos) - space(colorflip(pos));
    v += king_mg(pos) - king_mg(colorflip(pos));
    return v;
  }
  
  Stockfish evaluates a position by linearly add each individual term. That includes piece values, square values, pawn structures, mobility, passed pawns etc. Nothing special here.
  
  How to get those values, known as parameter tuning? Nobody knows for sure, we have to adjust the parameters, play many games, look at the results. Sounds dumb? Yes, it is, but that's what all chess programmers do. Google's AlphaZero is no exception.
  
  > **Comments:**
  > **username_4**: AlphaZero is different in that the parameters are generic and tuned automatically as part of "learning" (where observations influence millions of parameters one game at a time). They didn't start with a term called `passed_mg`, etc. and tune it and other ones with concrete meaning. Instead, there is millions of generic parameters that represent whatever they represent. Some might end up dealing with certain topics like a passed pawn, but others might represent more complicated ideas. It's hard to describe due to the sheer number of parameters.  
  
  ---
  
  