Permalink
Browse files

Use adaptive search depth at higher levels of play.

So far this is very simple.  If the search doesn't take very long at
all, it will be retried at the next depth.  And again, if it still
doesn't take long.

This is slightly wasteful, in that the original search is thrown away,
and the level is recalibrated every move, not estimated based on
previous moves.  But this waste is not huge, given that each deeper
search is almost always an order of magnitude slower than the
shallower one, so the waste is in the order of a few percent.
  • Loading branch information...
1 parent ea0c569 commit 76b814cdb97636953227c5cda4c1704825fd7bba @douglasbagnall committed Mar 27, 2012
Showing with 19 additions and 3 deletions.
  1. +2 −0 src/display-config.js
  2. +15 −2 src/display.js
  3. +2 −1 src/fen-test.html
View
2 src/display-config.js
@@ -1,5 +1,7 @@
var ROTATE_BOARD = true;
var LEVELS = ['stupid', 'middling', 'slow', 'slowest'];
+var DEFAULT_LEVEL = 2;
+var ADAPTIVE_LEVELS = true;
var SQUEEZE_INTO_BOX = true;
var IMAGE_NAMES = [
View
17 src/display.js
@@ -14,7 +14,7 @@ var input = {
board_state: p4_new_game(),
players: ['human', 'computer'], //[white, black] controllers
pawn_becomes: 0, //index into PROMOTIONS array
- computer_level: 2
+ computer_level: DEFAULT_LEVEL
};
var PROMOTIONS = ['queen', 'rook', 'knight', 'bishop'];
@@ -88,7 +88,20 @@ function computer_move(){
auto_play_timeout_ID = undefined;
var state = input.board_state;
var s, e, mv;
- mv = p4_findmove(state, input.computer_level + 1);
+ var depth = input.computer_level + 1;
+ var start_time = Date.now();
+ mv = p4_findmove(state, depth);
+ var delta = Date.now() - start_time;
+ console.log("findmove took", delta);
+ if (ADAPTIVE_LEVELS && depth > 2){
+ var min_time = 25 * depth;
+ while (delta < min_time){
+ depth++;
+ mv = p4_findmove(state, depth);
+ delta = Date.now() - start_time;
+ console.log("retry at depth", depth, " total time:", delta);
+ }
+ }
s = mv[0], e = mv[1];
var move_result = p4_move(state, s, e);
if (move_result){
View
3 src/fen-test.html
@@ -13,8 +13,9 @@
<script src="display.js"></script>
<script>
var FEN = "r3kb1r/ppBnp1pp/5p2/1N1n1b2/2BP4/5NP1/P4P1P/R1R3K1 b kq - 1 16";
+var FEN2 = "4k3/4n3/8/3N1R2/4R2p/7P/1r3BK1/8 b - - 6 42";
-p4_fen2state(FEN, input.board_state);
+p4_fen2state(FEN2, input.board_state);
refresh(0);
next_move();
</script>

0 comments on commit 76b814c

Please sign in to comment.