mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:47:44 +00:00
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.
This commit is contained in:
parent
5f13a87ce7
commit
351fc0cce2
3 changed files with 15 additions and 4 deletions
|
@ -259,6 +259,16 @@ Board::Board()
|
||||||
set_piece(Square("h8"), { Color::Black, Type::Rook });
|
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
|
String Board::to_fen() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
|
|
@ -117,6 +117,7 @@ struct Move {
|
||||||
class Board {
|
class Board {
|
||||||
public:
|
public:
|
||||||
Board();
|
Board();
|
||||||
|
Board clone_without_history() const;
|
||||||
|
|
||||||
Piece get_piece(Square const&) const;
|
Piece get_piece(Square const&) const;
|
||||||
Piece set_piece(Square const&, Piece const&);
|
Piece set_piece(Square const&, Piece const&);
|
||||||
|
|
|
@ -39,10 +39,10 @@ MCTSTree& MCTSTree::expand()
|
||||||
VERIFY(!expanded() || m_children.size() == 0);
|
VERIFY(!expanded() || m_children.size() == 0);
|
||||||
|
|
||||||
if (!m_moves_generated) {
|
if (!m_moves_generated) {
|
||||||
m_board->generate_moves([&](Chess::Move move) {
|
m_board->generate_moves([&](Chess::Move chess_move) {
|
||||||
Chess::Board clone = *m_board;
|
auto clone = m_board->clone_without_history();
|
||||||
clone.apply_move(move);
|
clone.apply_move(chess_move);
|
||||||
m_children.append(make<MCTSTree>(clone, this));
|
m_children.append(make<MCTSTree>(move(clone), this));
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
m_moves_generated = true;
|
m_moves_generated = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue