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(); } }