diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2022-08-14 15:00:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-08-22 21:20:41 +0200 |
commit | 351fc0cce235755f9f74e7f5891c8d7778033e4e (patch) | |
tree | 02a286d2f0c197de174c3a62b5562e6ca5eb28e7 /Userland/Libraries | |
parent | 5f13a87ce77c5f2cba0707c122f8cf3f584347cf (diff) | |
download | serenity-351fc0cce235755f9f74e7f5891c8d7778033e4e.zip |
ChessEngine: Use reduced Board objects in MCTSTree
Monte-Carlo methods are known to intensively create nodes and in our
case each leaf of the tree stores a board. However, for this use case,
we don't need a full board object that also contains game information.
This patch adds a `clone_cleared()` method that return a clone without
game information and uses it when constructing the tree.
It allows the ChessEngine much more possibility before getting out of
memory.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibChess/Chess.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibChess/Chess.h | 1 |
2 files changed, 11 insertions, 0 deletions
diff --git a/Userland/Libraries/LibChess/Chess.cpp b/Userland/Libraries/LibChess/Chess.cpp index 577e82fcb6..9cfbe3817e 100644 --- a/Userland/Libraries/LibChess/Chess.cpp +++ b/Userland/Libraries/LibChess/Chess.cpp @@ -259,6 +259,16 @@ Board::Board() set_piece(Square("h8"), { Color::Black, Type::Rook }); } +Board Board::clone_without_history() const +{ + // Note: When used in the MCTSTree, the board doesn't need to have all information about previous states. + // It spares a huge amount of memory. + auto result = *this; + result.m_moves.clear(); + result.m_previous_states.clear(); + return result; +} + String Board::to_fen() const { StringBuilder builder; diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h index a67e173a93..44a542c8dd 100644 --- a/Userland/Libraries/LibChess/Chess.h +++ b/Userland/Libraries/LibChess/Chess.h @@ -117,6 +117,7 @@ struct Move { class Board { public: Board(); + Board clone_without_history() const; Piece get_piece(Square const&) const; Piece set_piece(Square const&, Piece const&); |