Answers.Body: <blockquote>
    <p>I would like to keep the information saved in the hash memory. Is
    there a way to do that?</p>
  </blockquote>
  
  <p>Yes, there is.</p>
  
  <p>Stockfish is <a href="https://github.com/official-stockfish" rel="nofollow noreferrer">open source</a>. So you can examine the code, modify it, recompile and rebuild.</p>
  
  <p>What you need to do is:</p>
  
  <ol>
  <li>Find where Stockfish creates and uses the cache</li>
  <li>Modify the code to save the cache to disk when the game finishes</li>
  <li>Modify the code to optionally (you won't want to do it for every new game) load the cache from disk for a new game</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-11T17:54:30.490Z
Answers.Text: You're joking, right? There isn't one person in 50,000 that would know how to do this and get it to work correctly.
Answers.UserDisplayName: Randy Minder
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-12T04:33:36.180Z
Answers.Text: @Bryan Towers. Thanks it is a little bit beyond me, but someone must have already made stockfish version vith hash saving capabilities (http://talkchess.com/forum3/viewtopic.php?f=2&t=64720&start=70&sid=b5e4a98942fe85cc6b6d25fbad135701), so I might look into that.
Answers.UserDisplayName: Arturo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-11T11:48:44.147Z
Answers.LastActivityDate: 2020-02-11T11:48:44.147Z
Answers.UserDisplayName: Brian Towers
Answers.Body: <p>The script from that other answer that you've linked to does not appear to be resetting the hash between subsequent games.</p>
  <p>Stockfish uses the UCI protocol, and engines that implement this protocol (most modern engines) will typically clear the hash table when it is issued the <code>ucinewgame</code>, but this is not required of engines that implement the protocol.</p>
  <p><a href="https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.Protocol.play" rel="nofollow noreferrer">The documentation for Python Chess indicates that this is issued based on the <code>game</code> parameter which is supplied to the <code>play()</code> method.</a></p>
  <blockquote>
  <p>game – Optional. An arbitrary object that identifies the game. Will automatically inform the engine if the object is not equal to the previous game (e.g., ucinewgame, new).</p>
  </blockquote>
  <p>My assumption is thus that the <code>ucinewgame</code> command is not issued if this parameter is not specified. Since the script in the linked question does not specify the <code>game</code> parameter anywhere, nor does it appear to do anything else which would send the <code>ucinewgame</code> command, I do not believe that the hash table is being cleared out between games.</p>
  <p>However, if you did want to clear out the hash between games, you would create a new object (type does not matter) every time that you start a game, and pass that into as the <code>game</code> parameter for each move of that game. Whenever the value of this parameter changes between subsequent calls to the method, the <code>ucinewgame</code> command would be sent to Stockfish, informing it that the next commands are for a new game and it should do whatever it needs to do to start a new game (typically this would involve clearing out the hash table).</p>
  <hr />
  <p>I also don't see anything that sets the size of the hash table before the game starts. The specification for the UCI protocol specifies that the default size for the hash table should be very small, so this script would not be making very good use of this table as it is.</p>
  <p><a href="https://github.com/official-stockfish/Stockfish/wiki/Commands#uci" rel="nofollow noreferrer">For Stockfish, the default hash size is 16 MB, and it allows values up to 33554432 MB (~33.5 TB).</a> <a href="https://github.com/official-stockfish/Stockfish/wiki/Stockfish-FAQ#optimal-settings" rel="nofollow noreferrer">They specify that you should optimally set this value to the amount of RAM you have available on your machine - 1 or 2 GB.</a> E.g. if you have 8GB of RAM, set a value of <code>8192 MB - 2048 MB = 6144 MB</code> (allocating 6GB for the hash table).</p>
  <p>To set this in Python, before the <code>for</code> loop, you would call <a href="https://python-chess.readthedocs.io/en/latest/engine.html#options" rel="nofollow noreferrer"><code>engine.configure({&quot;Hash&quot;: 6144})</code></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-16T19:13:15.567Z
Answers.LastActivityDate: 2023-06-16T19:19:47.953Z
Answers.LastEditDate: 2023-06-16T19:19:47.953Z
Answers.UserDisplayName: Nelson O
Body: <p>A while ago I asked a question about how to let Stockfish play 10 games with itself for a limited number of moves:</p>
  <p><a href="https://chess.stackexchange.com/questions/28248/python-script-to-let-stockfish-selfplay-10-games-from-a-given-position/28249#28249">Python script to let stockfish selfplay 10 games from a given position</a></p>
  <p>This question was brilliantly answered by the user Phonon.</p>
  <p>Now I would have a followup question. Since the games are starting from the same position, I would like to avoid to clean the hash memory, as I understand Stockfish does every time it starts a new game. Ideally, even the first game should avoid the hash table clearing since I might have analyzed the position before starting the self match and I would like to keep the information saved in the hash memory.</p>
  <p>Is there a way to do that?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-11T13:46:17.287Z
Comments.Text: I'd try to solve it at the python level (and by communicating with SF differently), as opposed to changing the SF code itself (try to make that your last attempt, as it's a rather difficult task). For a continuously improving evaluation you're probably better off using `engine.analyse` or an [indefinite analysis](https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis) which you keep monitoring. On a related note, you can also [set the engine hash](https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis) to larger ones(in MB).
Comments.UserDisplayName: user929304
Tags: <engines><stockfish>
Title: How to get stockfish to play a selfmatch without clearing the hash table between games
ThreadText: # How to get stockfish to play a selfmatch without clearing the hash table between games
  
  (Asked by: username_0 on 2020-02-11)
  
  A while ago I asked a question about how to let Stockfish play 10 games with itself for a limited number of moves:
  
  [Python script to let stockfish selfplay 10 games from a given position](https://chess.stackexchange.com/questions/28248/python-script-to-let-stockfish-selfplay-10-games-from-a-given-position/28249#28249)
  
  This question was brilliantly answered by the user Phonon.
  
  Now I would have a followup question. Since the games are starting from the same position, I would like to avoid to clean the hash memory, as I understand Stockfish does every time it starts a new game. Ideally, even the first game should avoid the hash table clearing since I might have analyzed the position before starting the self match and I would like to keep the information saved in the hash memory.
  
  Is there a way to do that?
  
  **Tags:** engines, stockfish
  
  > **Comments:**
  > **username_1**: I'd try to solve it at the python level (and by communicating with SF differently), as opposed to changing the SF code itself (try to make that your last attempt, as it's a rather difficult task). For a continuously improving evaluation you're probably better off using `engine.analyse` or an [indefinite analysis](https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis) which you keep monitoring. On a related note, you can also [set the engine hash](https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis) to larger ones(in MB).  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-02-11. Score: 2
  
  > I would like to keep the information saved in the hash memory. Is
  > there a way to do that?
  
  Yes, there is.
  
  Stockfish is [open source](https://github.com/official-stockfish). So you can examine the code, modify it, recompile and rebuild.
  
  What you need to do is:
  
  1. Find where Stockfish creates and uses the cache
  2. Modify the code to save the cache to disk when the game finishes
  3. Modify the code to optionally (you won't want to do it for every new game) load the cache from disk for a new game
  
  > **Comments:**
  > **username_3**: You're joking, right? There isn't one person in 50,000 that would know how to do this and get it to work correctly.  
  > **username_0**: @Bryan username_2. Thanks it is a little bit beyond me, but someone must have already made stockfish version vith hash saving capabilities (http://talkchess.com/forum3/viewtopic.php?f=2&t=64720&start=70&sid=b5e4a98942fe85cc6b6d25fbad135701), so I might look into that.  
  
  ---
  
  ### Answer by username_4 on 2023-06-16. Score: 2
  
  The script from that other answer that you've linked to does not appear to be resetting the hash between subsequent games.
  
  Stockfish uses the UCI protocol, and engines that implement this protocol (most modern engines) will typically clear the hash table when it is issued the `ucinewgame`, but this is not required of engines that implement the protocol.
  
  [The documentation for Python Chess indicates that this is issued based on the `game` parameter which is supplied to the `play()` method.](https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.Protocol.play)
  
  > game – Optional. An arbitrary object that identifies the game. Will automatically inform the engine if the object is not equal to the previous game (e.g., ucinewgame, new).
  
  My assumption is thus that the `ucinewgame` command is not issued if this parameter is not specified. Since the script in the linked question does not specify the `game` parameter anywhere, nor does it appear to do anything else which would send the `ucinewgame` command, I do not believe that the hash table is being cleared out between games.
  
  However, if you did want to clear out the hash between games, you would create a new object (type does not matter) every time that you start a game, and pass that into as the `game` parameter for each move of that game. Whenever the value of this parameter changes between subsequent calls to the method, the `ucinewgame` command would be sent to Stockfish, informing it that the next commands are for a new game and it should do whatever it needs to do to start a new game (typically this would involve clearing out the hash table).
  
  ---
  
  I also don't see anything that sets the size of the hash table before the game starts. The specification for the UCI protocol specifies that the default size for the hash table should be very small, so this script would not be making very good use of this table as it is.
  
  [For Stockfish, the default hash size is 16 MB, and it allows values up to 33554432 MB (~33.5 TB).](https://github.com/official-stockfish/Stockfish/wiki/Commands#uci) [They specify that you should optimally set this value to the amount of RAM you have available on your machine - 1 or 2 GB.](https://github.com/official-stockfish/Stockfish/wiki/Stockfish-FAQ#optimal-settings) E.g. if you have 8GB of RAM, set a value of `8192 MB - 2048 MB = 6144 MB` (allocating 6GB for the hash table).
  
  To set this in Python, before the `for` loop, you would call [`engine.configure({"Hash": 6144})`](https://python-chess.readthedocs.io/en/latest/engine.html#options)
  
  
  ---
  
  