mirror of
https://github.com/nolen777/eagle0.git
synced 2026-07-29 02:48:40 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d27a991f8 |
@@ -134,34 +134,52 @@ auto AbstractMCTSAI::BuildMCTSTree(
|
||||
std::mutex treeMutex;
|
||||
std::vector<std::future<void>> futures;
|
||||
|
||||
// TEMPORARY DEBUG
|
||||
printf("[DEBUG BuildMCTSTree] Starting multithreaded MCTS with %d threads, deadline in "
|
||||
"%lld ms\n",
|
||||
config_.numThreads,
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
deadline - std::chrono::steady_clock::now())
|
||||
.count());
|
||||
|
||||
futures.reserve(config_.numThreads);
|
||||
for (int threadId = 0; threadId < config_.numThreads; ++threadId) {
|
||||
futures.push_back(std::async(std::launch::async, [&] {
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
// Selection and Expansion (with lock - tree modification must be serialized)
|
||||
MCTSNode* expanded;
|
||||
{
|
||||
std::lock_guard lock(treeMutex);
|
||||
auto* selected = MCTSSelection(root.get());
|
||||
if (!selected) break;
|
||||
try {
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
// Selection and Expansion (with lock - tree modification must be
|
||||
// serialized)
|
||||
MCTSNode* expanded;
|
||||
{
|
||||
std::lock_guard lock(treeMutex);
|
||||
auto* selected = MCTSSelection(root.get());
|
||||
if (!selected) { break; }
|
||||
|
||||
// Expansion modifies tree structure - must be inside lock
|
||||
expanded = MCTSExpansion(selected, engine);
|
||||
}
|
||||
// Expansion modifies tree structure - must be inside lock
|
||||
expanded = MCTSExpansion(selected, engine);
|
||||
}
|
||||
|
||||
// Simulation can run in parallel (doesn't modify tree)
|
||||
// Simulation can run in parallel (doesn't modify tree)
|
||||
|
||||
// Backpropagation (with lock - modifies node statistics)
|
||||
{
|
||||
const double reward = MCTSSimulation(
|
||||
engine,
|
||||
*expanded->gameState,
|
||||
playerId_,
|
||||
expanded->playerFlips);
|
||||
std::lock_guard lock(treeMutex);
|
||||
MCTSBackpropagation(expanded, reward, config_.backpropagationPolicy);
|
||||
iterations.fetch_add(1);
|
||||
// Backpropagation (with lock - modifies node statistics)
|
||||
{
|
||||
const double reward = MCTSSimulation(
|
||||
engine,
|
||||
*expanded->gameState,
|
||||
playerId_,
|
||||
expanded->playerFlips);
|
||||
std::lock_guard lock(treeMutex);
|
||||
MCTSBackpropagation(expanded, reward, config_.backpropagationPolicy);
|
||||
iterations.fetch_add(1);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
// Exception during MCTS iteration - exit gracefully to let other threads
|
||||
// continue
|
||||
return;
|
||||
} catch (...) {
|
||||
// Unknown exception - exit gracefully
|
||||
return;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user