Answers.Body: <p>What you are seeing does not necessarily indicate a bug.</p>
  <p>You are searching the position with two different bounds.  Your engine is fail-hard (because you return beta and not eval in the case of a fail-high), so the score that comes back from eval is going to be constrained within the bounds you provide.</p>
  <p>In the second case (using the numbers from your diagram, not your code) you are searching with a bounds of [-20, -5] and your score is -5.  This is fine.</p>
  <p>In the first case you are searching with a bounds of [-20, -15] and your score is -15.  This is also fine.  It would be impossible for this search to return a value of -5 because it is outside the bounds you provided.</p>
  <p>In either case, what the search is telling you is that this position is better (relative to the side to move) than the bounds you provided because in both cases the search is failing high.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-25T19:21:37.313Z
Answers.LastActivityDate: 2020-06-25T19:21:37.313Z
Answers.UserDisplayName: Jeff Wilson
Body: <p>My engine is evaluating the same position at the same depth with a different score inside the search tree, is that normal?</p>
  
  <pre><code>[Title "depth 1   ply 3 alfa -20 beta -15  score -15 (1.Na3 a5 2.Nh3)"]
  [FEN "rnbqkbnr/1ppppppp/8/p7/8/N6N/PPPPPPPP/R1BQKB1R w KQkq - 0 1"]
  </code></pre>
  
  <hr>
  
  <pre><code>    [Title "depth 1   ply 3 alfa -20 beta -5  score -5 (1.Nh3 a5 2.Na3)"]
  [FEN "rnbqkbnr/1ppppppp/8/p7/8/N6N/PPPPPPPP/R1BQKB1R w KQkq - 0 1"]
  </code></pre>
  
  <p><strong>Search code:</strong></p>
  
  <pre><code>        tab.makeMove(mov);
          // first  time eval value is -15 with alpha -30 beta -15 
          // second time eval value is -25 with alpha -30 beta -25
          // this is in the same search
          int eval = -negaMax(depth - 1, -beta, -alfa, ply + 1);
          tab.takeBack(mov);
  
          if (eval &gt;= beta) {
              return beta;
          }
  </code></pre>
  
  <p><strong>engine output:</strong></p>
  
  <pre><code>go
  info depth 1 score cp 50 pv b1c3 
  info depth 2 score cp 0 pv b1c3 b8c6 
  info depth 3 score cp 50 pv b1c3 b8c6 g1f3 
  info depth 4 score cp 0 pv b1c3 b8c6 g1f3 g8f6 
  info depth 5 score cp 40 pv b1c3 b8c6 d2d4 g8f6 g1f3 
  info depth 6 score cp 0 pv b1c3 d7d5 d2d4 b8c6 g1f3 g8f6 
  info depth 7 score cp 35 pv e2e4 b8c6 g1f3 g8f6 e4e5 f6e4 b1c3 
  bestmove e2e4
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T20:54:12.717Z
Comments.Text: Are you using a transposition table? Also, is there any randomness in your engine?
Comments.UserDisplayName: Inertial Ignorance
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T21:02:29.520Z
Comments.Text: No, there is no transposition table just alpha-beta pruning. The engine works fine. I tried to implement TT but I got many blunders for that reason I'm debugging and  I found that the same position has different scores at the same depth in the search tree.
Comments.UserDisplayName: crrlos
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T21:07:42.287Z
Comments.Text: When you get a different score, are you letting the engine evaluate the position again after already evaluating it while it was running? Or are these on completely separate runs of the program?
Comments.UserDisplayName: Inertial Ignorance
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T21:19:32.280Z
Comments.Text: Yes, the engine evaluates the same position again.  I added some code to help to understand where is the problem.
Comments.UserDisplayName: crrlos
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T21:30:12.127Z
Comments.Text: If that piece of code is executed twice in the same run of the program, I would guess you're altering static/global variables. Altering these on the first search could affect the second search. That's why I asked about a TT, since it's one example of static/global data.
Comments.UserDisplayName: Inertial Ignorance
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T22:07:20.773Z
Comments.Text: I checked and I'm sure there are not global/static variables. I updated the example positions, the second is a transposition. I'm going to debug step by step. So, both positions must have the same score, right?
Comments.UserDisplayName: crrlos
Tags: <engines><programming><evaluation>
Title: Engine is evaluating the same position at the same depth with different score
ThreadText: # Engine is evaluating the same position at the same depth with different score
  
  (Asked by: username_0 on 2020-06-02)
  
  My engine is evaluating the same position at the same depth with a different score inside the search tree, is that normal?
  [Title "depth 1   ply 3 alfa -20 beta -15  score -15 (1.Na3 a5 2.Nh3)"]
  [FEN "rnbqkbnr/1ppppppp/8/p7/8/N6N/PPPPPPPP/R1BQKB1R w KQkq - 0 1"]
  
  
      [Title "depth 1   ply 3 alfa -20 beta -5  score -5 (1.Nh3 a5 2.Na3)"]
  [FEN "rnbqkbnr/1ppppppp/8/p7/8/N6N/PPPPPPPP/R1BQKB1R w KQkq - 0 1"]
  
  Search code:
          tab.makeMove(mov);
          // first  time eval value is -15 with alpha -30 beta -15 
          // second time eval value is -25 with alpha -30 beta -25
          // this is in the same search
          int eval = -negaMax(depth - 1, -beta, -alfa, ply + 1);
          tab.takeBack(mov);
  
          if (eval >= beta) {
              return beta;
          }
  
  engine output:
  go
  info depth 1 score cp 50 pv b1c3 
  info depth 2 score cp 0 pv b1c3 b8c6 
  info depth 3 score cp 50 pv b1c3 b8c6 g1f3 
  info depth 4 score cp 0 pv b1c3 b8c6 g1f3 g8f6 
  info depth 5 score cp 40 pv b1c3 b8c6 d2d4 g8f6 g1f3 
  info depth 6 score cp 0 pv b1c3 d7d5 d2d4 b8c6 g1f3 g8f6 
  info depth 7 score cp 35 pv e2e4 b8c6 g1f3 g8f6 e4e5 f6e4 b1c3 
  bestmove e2e4
  
  **Tags:** engines, programming, evaluation
  
  > **Comments:**
  > **username_1**: Are you using a transposition table? Also, is there any randomness in your engine?  
  > **username_0**: No, there is no transposition table just alpha-beta pruning. The engine works fine. I tried to implement TT but I got many blunders for that reason I'm debugging and  I found that the same position has different scores at the same depth in the search tree.  
  > **username_1**: When you get a different score, are you letting the engine evaluate the position again after already evaluating it while it was running? Or are these on completely separate runs of the program?  
  > **username_0**: Yes, the engine evaluates the same position again.  I added some code to help to understand where is the problem.  
  > **username_1**: If that piece of code is executed twice in the same run of the program, I would guess you're altering static/global variables. Altering these on the first search could affect the second search. That's why I asked about a TT, since it's one example of static/global data.  
  > **username_0**: I checked and I'm sure there are not global/static variables. I updated the example positions, the second is a transposition. I'm going to debug step by step. So, both positions must have the same score, right?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-06-25. Score: 3
  
  What you are seeing does not necessarily indicate a bug.
  
  You are searching the position with two different bounds. Your engine is fail-hard (because you return beta and not eval in the case of a fail-high), so the score that comes back from eval is going to be constrained within the bounds you provide.
  
  In the second case (using the numbers from your diagram, not your code) you are searching with a bounds of [-20, -5] and your score is -5. This is fine.
  
  In the first case you are searching with a bounds of [-20, -15] and your score is -15. This is also fine. It would be impossible for this search to return a value of -5 because it is outside the bounds you provided.
  
  In either case, what the search is telling you is that this position is better (relative to the side to move) than the bounds you provided because in both cases the search is failing high.
  
  
  ---
  
  