Skip to content

Commit

Permalink
speed up bot async mode by running 25 iterations per chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Nov 24, 2019
1 parent ad08b8a commit 3cd5667
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/ai/mcts-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
};

Expand Down
3 changes: 2 additions & 1 deletion src/client/debug/ai/AI.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@
client.reset();
botAction = null;
iterationCounter = 0;
Exit();
}
function OnKeyDown(e) {
// ESC.
if (e.keyCode == 27) {
Reset();
Exit();
}
}
Expand Down

0 comments on commit 3cd5667

Please sign in to comment.