From 3cd56670aa92aaaafc73ff6bb50253cb0e7556d3 Mon Sep 17 00:00:00 2001 From: Nicolo Davis Date: Fri, 22 Nov 2019 20:45:33 +0800 Subject: [PATCH] speed up bot async mode by running 25 iterations per chunk --- src/ai/mcts-bot.js | 22 +++++++++++++++++----- src/client/debug/ai/AI.svelte | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/ai/mcts-bot.js b/src/ai/mcts-bot.js index b80e02802..981f922b5 100644 --- a/src/ai/mcts-bot.js +++ b/src/ai/mcts-bot.js @@ -8,6 +8,12 @@ import { CreateGameReducer } from '../core/reducer'; import { Bot } from './bot'; +/** + * The number of iterations to run before yielding to + * the JS event loop (in async mode). + */ +const CHUNK_SIZE = 25; + /** * Bot that uses Monte-Carlo Tree Search to find promising moves. */ @@ -229,11 +235,17 @@ export class MCTSBot extends Bot { return new Promise(resolve => { const iteration = () => { - const leaf = this.select(root); - const child = this.expand(leaf); - const result = this.playout(child); - this.backpropagate(child, result); - this.iterationCounter++; + for ( + let i = 0; + i < CHUNK_SIZE && this.iterationCounter < numIterations; + i++ + ) { + const leaf = this.select(root); + const child = this.expand(leaf); + const result = this.playout(child); + this.backpropagate(child, result); + this.iterationCounter++; + } this.iterationCallback(this.iterationCounter); }; diff --git a/src/client/debug/ai/AI.svelte b/src/client/debug/ai/AI.svelte index 71637f0f1..d914d8e97 100644 --- a/src/client/debug/ai/AI.svelte +++ b/src/client/debug/ai/AI.svelte @@ -86,12 +86,13 @@ client.reset(); botAction = null; iterationCounter = 0; + Exit(); } function OnKeyDown(e) { // ESC. if (e.keyCode == 27) { - Reset(); + Exit(); } }