1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

ChessEngine: Don't throw away useful branches from last tree

Computation from last turn might have produced some nodes that are still
accurate. Keeping them should make the engine a bit smarter.
This commit is contained in:
Lucas CHOLLET 2022-08-14 16:39:32 +02:00 committed by Andreas Kling
parent 351fc0cce2
commit d5979516b4
4 changed files with 52 additions and 7 deletions

View file

@ -38,7 +38,14 @@ void ChessEngine::handle_go(GoCommand const& command)
auto elapsed_time = Core::ElapsedTimer::start_new();
MCTSTree mcts(m_board);
auto mcts = [this]() -> MCTSTree {
if (!m_last_tree.has_value())
return { m_board };
auto x = m_last_tree.value().child_with_move(m_board.last_move().value());
if (x.has_value())
return move(x.value());
return { m_board };
}();
int rounds = 0;
while (elapsed_time.elapsed() <= command.movetime.value()) {
@ -47,7 +54,10 @@ void ChessEngine::handle_go(GoCommand const& command)
}
dbgln("MCTS finished {} rounds.", rounds);
dbgln("MCTS evaluation {}", mcts.expected_value());
auto best_move = mcts.best_move();
auto& best_node = mcts.best_node();
auto const& best_move = best_node.last_move();
dbgln("MCTS best move {}", best_move.to_long_algebraic());
send_command(BestMoveCommand(best_move));
m_last_tree = move(best_node);
}