forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
midgame.c
1457 lines (1238 loc) · 40.2 KB
/
midgame.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: midgame.c
Created: July 1, 1997
Modified: November 23, 2002
Author: Gunnar Andersson ([email protected])
Contents: Search routines designated to be used in the
midgame phase of the game.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "autoplay.h"
#include "constant.h"
#include "display.h"
#include "eval.h"
#include "getcoeff.h"
#include "globals.h"
#include "hash.h"
#include "macros.h"
#include "midgame.h"
#include "moves.h"
#include "myrandom.h"
#include "patterns.h"
#include "pcstat.h"
#include "probcut.h"
#include "search.h"
#include "texts.h"
#include "timer.h"
/* Enable/disable Multi-ProbCut */
#define USE_MPC 1
/* Number of nodes searched between consecutive time checks */
#ifdef _WIN32_WCE
#define EVENT_CHECK_INTERVAL 10000
#else
#define EVENT_CHECK_INTERVAL 100000
#endif
/* The eval bonus given if the hash table move is found during the
shallow search. */
#define HASH_MOVE_BONUS 10000
/* The depth from which where hash tables lookups are performed. */
#define HASH_THRESHOLD 2
/* The depth where shallow searches are first performed. */
#define PRE_SEARCH_THRESHOLD 3
/* The depth to which all variations must be searched */
#define FULL_WIDTH_DEPTH 8
/* The depth where shallow searches are performed to depth 2. */
#define DEPTH_TWO_DEPTH 10
/* Default aspiration window parameters. These values are currently
really huge as usage of a small windows tends to slow down
the search. */
#define USE_WINDOW FALSE
#define ALPHA_WINDOW 2048
#define BETA_WINDOW 2048
#define WIPEOUT_THRESHOLD 60
static int allow_midgame_hash_probe;
static int allow_midgame_hash_update;
static int best_mid_move, best_mid_root_move;
static int midgame_abort;
static int do_check_midgame_abort = TRUE;
static int counter_phase;
static int apply_perturbation = TRUE;
static int perturbation_amplitude = 0;
static int stage_reached[62] = {};
static int stage_score[62] ={};
static int score_perturbation[100];
static int feas_index_list[64][64];
/*
SETUP_MIDGAME
Sets up some search parameters.
*/
void
setup_midgame( void ) {
int i;
allow_midgame_hash_probe = TRUE;
allow_midgame_hash_update = TRUE;
for ( i = 0; i <= 61; i++ )
stage_reached[i] = FALSE;
calculate_perturbation();
}
/*
CLEAR_MIDGAME_ABORT
IS_MIDGAME_ABORT
SET_MIDGAME_ABORT
TOGGLE_MIDGAME_ABORT_CHECK
These functions handle the midgame abort system which kicks in
when it is estimated that the next iteration in the iterative
deepening would take too long.
*/
void
clear_midgame_abort( void ) {
midgame_abort = FALSE;
}
int
is_midgame_abort( void ) {
return midgame_abort;
}
void
set_midgame_abort( void ) {
midgame_abort = do_check_midgame_abort;
}
void
toggle_midgame_abort_check( int toggle ) {
do_check_midgame_abort = toggle;
}
/*
TOGGLE_MIDGAME_HASH_USAGE
Toggles hash table access in the midgame search on/off.
*/
void
toggle_midgame_hash_usage( int allow_read, int allow_write ) {
allow_midgame_hash_probe = allow_read;
allow_midgame_hash_update = allow_write;
}
/*
CALCULATE_PERTURBATION
Determines the score perturbations (if any) to the root moves.
*/
void
calculate_perturbation( void ) {
int i;
int shift;
if ( !apply_perturbation || (perturbation_amplitude == 0) )
for ( i = 0; i < 100; i++ )
score_perturbation[i] = 0;
else {
shift = perturbation_amplitude / 2;
for ( i = 0; i < 100; i++ )
score_perturbation[i] =
(abs( my_random()) % perturbation_amplitude) - shift;
}
}
/*
SET_PERTURBATION
Set the amplitude of the score perturbation applied by
CALCULATE_PERTURBATION.
*/
void
set_perturbation( int amplitude ) {
perturbation_amplitude = amplitude;
}
/*
TOGGLE_PERTURBATION_USAGE
Toggle usage of score perturbations on/off.
*/
void
toggle_perturbation_usage( int toggle ) {
apply_perturbation = toggle;
}
/*
ADVANCE_MOVE
Swaps a move and its predecessor in the move list if it's
not already first in the list.
*/
INLINE static
void
advance_move( int index ) {
int temp_move;
if ( index > 0 ) {
temp_move = sorted_move_order[disks_played][index];
sorted_move_order[disks_played][index] =
sorted_move_order[disks_played][index - 1];
sorted_move_order[disks_played][index - 1] = temp_move;
}
}
/*
STATIC_OR_TERMINAL_EVALUATION
Invokes the proper evaluation function depending on whether the
board is filled or not.
*/
INLINE static int
static_or_terminal_evaluation( int side_to_move ) {
if ( disks_played == 60 )
return terminal_evaluation( side_to_move );
else
return static_evaluation( side_to_move );
}
/*
FAST_TREE_SEARCH
The recursive tree search function. It uses negascout for
tree pruning.
*/
static int
fast_tree_search( int level, int max_depth, int side_to_move, int alpha,
int beta, int allow_hash, int void_legal ) {
int curr_val, best;
int move_index, move;
int best_move_index, best_move;
int first;
int remains;
int use_hash, new_use_hash;
int curr_alpha;
int empties_remaining;
HashEntry entry;
INCREMENT_COUNTER( nodes );
if ( level >= max_depth )
return static_or_terminal_evaluation( side_to_move );
/* Check the hash table */
remains = max_depth - level;
use_hash = (remains >= HASH_THRESHOLD) && USE_HASH_TABLE && allow_hash;
if ( use_hash && allow_midgame_hash_probe ) {
find_hash( &entry, MIDGAME_MODE );
if ( (entry.draft >= remains) &&
(entry.selectivity == 0) &&
valid_move( entry.move[0], side_to_move ) &&
(entry.flags & MIDGAME_SCORE) &&
((entry.flags & EXACT_VALUE) ||
((entry.flags & LOWER_BOUND) && entry.eval >= beta) ||
((entry.flags & UPPER_BOUND) && entry.eval <= alpha)) ) {
best_mid_move = entry.move[0];
return entry.eval;
}
}
/* Reorder the move lists now and then to keep the empty squares up front */
if ( (nodes.lo & 4095) == 0 )
reorder_move_list( disks_played );
/* Search */
first = TRUE;
best_move = PASS;
best_move_index = -1;
best = -INFINITE_EVAL;
if ( remains == 1 ) { /* Plain alpha-beta last ply */
empties_remaining = 60 - disks_played;
for ( move_index = 0; move_index < MOVE_ORDER_SIZE; move_index++ ) {
move = sorted_move_order[disks_played][move_index];
if ( board[move] == EMPTY ) {
if ( make_move_no_hash( side_to_move, move ) != 0 ) {
curr_val = -static_or_terminal_evaluation( OPP( side_to_move ) );
unmake_move_no_hash( side_to_move, move );
INCREMENT_COUNTER( nodes );
if ( curr_val > best ) {
best = curr_val;
best_move_index = move_index;
best_move = move;
if ( curr_val >= beta ) {
advance_move( move_index );
best_mid_move = best_move;
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, best, best_move,
MIDGAME_SCORE | LOWER_BOUND, remains, 0 );
return best;
}
}
first = FALSE;
}
empties_remaining--;
if ( empties_remaining == 0 )
break;
}
}
}
else { /* Principal variation search for deeper searches */
new_use_hash = (remains >= HASH_THRESHOLD + 1) && use_hash;
curr_alpha = alpha;
empties_remaining = 60 - disks_played;
for ( move_index = 0; move_index < MOVE_ORDER_SIZE; move_index++ ) {
move = sorted_move_order[disks_played][move_index];
if ( board[move] == EMPTY ) {
if ( make_move( side_to_move, move, new_use_hash ) != 0 ) {
if ( first ) {
best = curr_val =
-fast_tree_search( level + 1, max_depth, OPP( side_to_move ),
-beta, -curr_alpha, allow_hash, TRUE );
best_move = move;
best_move_index = move_index;
}
else {
curr_alpha = MAX( best, curr_alpha );
curr_val =
-fast_tree_search( level + 1, max_depth, OPP( side_to_move ),
-(curr_alpha + 1), -curr_alpha, allow_hash,
TRUE );
if ( (curr_val > curr_alpha) && (curr_val < beta) )
curr_val =
-fast_tree_search( level + 1, max_depth, OPP( side_to_move ),
-beta, INFINITE_EVAL, allow_hash, TRUE );
if ( curr_val > best ) {
best_move = move;
best_move_index = move_index;
best = curr_val;
}
}
unmake_move( side_to_move, move );
if ( best >= beta ) {
advance_move( move_index );
best_mid_move = best_move;
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, best, best_move,
MIDGAME_SCORE | LOWER_BOUND, remains, 0 );
return best;
}
first = FALSE;
}
empties_remaining--;
if ( empties_remaining == 0 )
break;
}
}
}
if ( !first ) {
advance_move( best_move_index );
best_mid_move = best_move;
if ( use_hash && allow_midgame_hash_update ) {
if ( best > alpha )
add_hash( MIDGAME_MODE, best, best_move,
MIDGAME_SCORE | EXACT_VALUE, remains, 0 );
else
add_hash( MIDGAME_MODE, best, best_move,
MIDGAME_SCORE | UPPER_BOUND, remains, 0 );
}
return best;
}
else if ( void_legal ) { /* I pass, other player's turn now */
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
curr_val = -fast_tree_search( level, max_depth, OPP( side_to_move ),
-beta, -alpha, allow_hash, FALSE );
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
return curr_val;
}
else { /* Both players had to pass ==> evaluate board as final */
curr_val = terminal_evaluation( side_to_move );
return curr_val;
}
}
#define VERBOSE 0
/*
UPDATE_BEST_LIST
*/
static void
update_best_list( int *best_list, int move, int best_list_index,
int best_list_length ) {
int i;
#if VERBOSE
printf( "move=%2d index=%d length=%d ", move, best_list_index,
best_list_length );
printf( "Before: " );
for ( i = 0; i < 4; i++ )
printf( "%2d ", best_list[i] );
#endif
if ( best_list_index < best_list_length )
for ( i = best_list_index; i >= 1; i-- )
best_list[i] = best_list[i - 1];
else
for ( i = 3; i >= 1; i-- )
best_list[i] = best_list[i - 1];
best_list[0] = move;
#if VERBOSE
printf( " After: " );
for ( i = 0; i < 4; i++ )
printf( "%2d ", best_list[i] );
puts( "" );
#endif
}
/*
TREE_SEARCH
The recursive tree search function. It uses negascout for
tree pruning.
*/
int
tree_search( int level,
int max_depth,
int side_to_move,
int alpha,
int beta,
int allow_hash,
int allow_mpc,
int void_legal ) {
int i, j;
int curr_val, best, pre_best;
int searched;
int move, hash_move;
int move_index, best_move_index;
int empties_remaining;
int hash_hit;
int pre_depth;
int update_pv;
int remains, shallow_remains;
int use_hash, pre_search_done;
int curr_alpha;
int best_index, best_score;
int best_list_index, best_list_length;
int selectivity, cut;
int best_list[4];
HashEntry entry;
#if CHECK_HASH_CODES && defined( TEXT_BASED )
unsigned int h1, h2;
#endif
if ( level >= max_depth ) {
INCREMENT_COUNTER( nodes );
return static_or_terminal_evaluation( side_to_move );
}
remains = max_depth - level;
if ( remains < PRE_SEARCH_THRESHOLD ) {
curr_val = fast_tree_search( level, max_depth, side_to_move, alpha,
beta, allow_hash, void_legal );
pv_depth[level] = level + 1;
pv[level][level] = best_mid_move;
return curr_val;
}
INCREMENT_COUNTER( nodes );
/* Check the hash table */
use_hash = (remains >= HASH_THRESHOLD) && USE_HASH_TABLE && allow_hash;
if ( USE_MPC && allow_mpc )
selectivity = 1;
else
selectivity = 0;
#if CHECK_HASH_CODES && defined( TEXT_BASED )
if ( use_hash ) {
h1 = hash1;
h2 = hash2;
}
#endif
if ( use_hash && allow_midgame_hash_probe ) {
find_hash( &entry, MIDGAME_MODE );
if ( (entry.draft >= remains) &&
(entry.selectivity <= selectivity) &&
valid_move( entry.move[0], side_to_move ) &&
(entry.flags & MIDGAME_SCORE) &&
((entry.flags & EXACT_VALUE) ||
((entry.flags & LOWER_BOUND) && entry.eval >= beta) ||
((entry.flags & UPPER_BOUND) && entry.eval <= alpha)) ) {
pv[level][level] = entry.move[0];
pv_depth[level] = level + 1;
return entry.eval;
}
}
hash_hit = (use_hash && allow_midgame_hash_probe);
if ( hash_hit )
hash_move = entry.move[0];
else
hash_move = 44;
pre_search_done = FALSE;
/* Use multi-prob-cut to selectively prune the tree */
if ( USE_MPC && allow_mpc && (remains <= MAX_CUT_DEPTH) ) {
int alpha_test = TRUE;
int beta_test = TRUE;
for ( cut = 0; cut < mpc_cut[remains].cut_tries; cut++ ) {
/* Determine the fail-high and fail-low bounds */
int bias = mpc_cut[remains].bias[cut][disks_played];
int window = mpc_cut[remains].window[cut][disks_played];
int alpha_bound = alpha + bias - window;
int beta_bound = beta + bias + window;
/* Don't use an MPC cut which results in the full-width depth
being less than some predefined constant */
shallow_remains = mpc_cut[remains].cut_depth[cut];
if ( level + shallow_remains < FULL_WIDTH_DEPTH )
continue;
if ( shallow_remains > 1 ) { /* "Deep" shallow search */
if ( cut == 0 ) {
/* Use static eval to decide if a one- or two-sided
MPC test is to be performed. */
int static_eval = static_evaluation( side_to_move );
if ( static_eval <= alpha_bound )
beta_test = FALSE;
else if ( static_eval >= beta_bound )
alpha_test = FALSE;
}
assert( alpha_test || beta_test );
if ( alpha_test && beta_test ) {
/* Test for likely fail-low or likely fail-high. */
int shallow_val =
tree_search( level, level + shallow_remains, side_to_move,
alpha_bound, beta_bound, allow_hash, FALSE,
void_legal );
if ( shallow_val >= beta_bound ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, beta, pv[level][level],
MIDGAME_SCORE | LOWER_BOUND, remains, selectivity );
return beta;
}
else if ( shallow_val <= alpha_bound ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, alpha, pv[level][level],
MIDGAME_SCORE | UPPER_BOUND, remains, selectivity );
return alpha;
}
else {
/* Use information learned from the failed cut test to decide
if a one or a two-sided test is to be performed next. */
int mid = (alpha_bound + beta_bound) / 2;
#if 0
if ( abs( shallow_val - alpha_bound ) < abs( shallow_val - mid ) )
beta_test = FALSE;
if ( abs( shallow_val - beta_bound ) < abs( shallow_val - mid ) )
alpha_test = FALSE;
#elif 1
if ( shallow_val < mid )
beta_test = FALSE;
else
alpha_test = FALSE;
#else
int low_threshold = (2 * mid + alpha_bound) / 3;
int high_threshold = (2 * mid + beta_bound) / 3;
if ( shallow_val <= low_threshold )
beta_test = FALSE;
else if ( shallow_val >= high_threshold )
alpha_test = FALSE;
else
break; /* Unlikely that there is any selective cutoff. */
#endif
}
}
else if ( beta_test ) {
/* Fail-high with high probability? */
if ( tree_search( level, level + shallow_remains, side_to_move,
beta_bound - 1, beta_bound, allow_hash, FALSE,
void_legal ) >= beta_bound ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, beta, pv[level][level],
MIDGAME_SCORE | LOWER_BOUND, remains, selectivity );
return beta;
}
}
else if ( alpha_test ) {
/* Fail-low with high probability? */
if ( tree_search( level, level + shallow_remains, side_to_move,
alpha_bound, alpha_bound + 1, allow_hash, FALSE,
void_legal ) <= alpha_bound ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, alpha, pv[level][level],
MIDGAME_SCORE | UPPER_BOUND, remains, selectivity );
return alpha;
}
}
}
else { /* All-in-one MPC one-ply search and move ordering */
move_count[disks_played] = 0;
best = alpha_bound;
empties_remaining = 60 - disks_played;
for ( move_index = 0; move_index < MOVE_ORDER_SIZE; move_index++ ) {
move = sorted_move_order[disks_played][move_index];
if ( board[move] == EMPTY ) {
if ( make_move_no_hash( side_to_move, move ) != 0 ) {
curr_val = -static_or_terminal_evaluation( OPP( side_to_move ) );
unmake_move_no_hash( side_to_move, move );
INCREMENT_COUNTER( nodes );
if ( curr_val > best ) {
best = curr_val;
if ( best >= beta_bound ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, beta, pv[level][level],
MIDGAME_SCORE | LOWER_BOUND, remains,
selectivity );
return beta;
}
}
evals[disks_played][move] = curr_val;
if ( move == hash_move ) /* Always try hash table move first */
evals[disks_played][move] += HASH_MOVE_BONUS;
feas_index_list[disks_played][move_count[disks_played]] =
move_index;
move_count[disks_played]++;
}
empties_remaining--;
if ( empties_remaining == 0 )
break;
}
}
if ( (best == alpha_bound) && (move_count[disks_played] > 0) ) {
if ( use_hash && allow_midgame_hash_update )
add_hash( MIDGAME_MODE, alpha, pv[level][level],
MIDGAME_SCORE | UPPER_BOUND, remains, selectivity );
return alpha;
}
pre_search_done = TRUE;
}
}
}
/* Full negascout search */
searched = 0;
best = -INFINITE_EVAL;
best_move_index = -1;
curr_alpha = alpha;
best_list_length = 0;
for ( i = 0; i < 4; i++ )
best_list[i] = 0;
if ( !pre_search_done ) {
move_count[disks_played] = 0;
if ( hash_hit )
for ( i = 0; i < 4; i++ )
if ( valid_move( entry.move[i], side_to_move ) )
best_list[best_list_length++] = entry.move[i];
}
for ( i = 0, best_list_index = 0; TRUE; i++, best_list_index++ ) {
/* Try the hash table move(s) first if feasible */
if ( !pre_search_done && (best_list_index < best_list_length) ) {
move_count[disks_played]++;
move_index = 0;
while ( sorted_move_order[disks_played][move_index] !=
best_list[best_list_index] )
move_index++;
}
else { /* Otherwise use information from shallow searches */
if ( !pre_search_done ) {
if ( remains < DEPTH_TWO_DEPTH )
pre_depth = 1;
else
pre_depth = 2;
pre_best = -INFINITE_EVAL;
empties_remaining = 60 - disks_played;
for ( move_index = 0; move_index < MOVE_ORDER_SIZE; move_index++ ) {
int already_checked;
move = sorted_move_order[disks_played][move_index];
already_checked = FALSE;
for ( j = 0; j < best_list_length; j++ )
if ( move == best_list[j] )
already_checked = TRUE;
if ( board[move] == EMPTY ) {
if ( !already_checked &&
(make_move( side_to_move, move, TRUE ) != 0 ) ) {
curr_val = -tree_search( level + 1, level + pre_depth,
OPP( side_to_move ), -INFINITE_EVAL,
-pre_best, FALSE, FALSE, TRUE );
pre_best = MAX( pre_best, curr_val );
unmake_move( side_to_move, move );
evals[disks_played][move] = curr_val;
feas_index_list[disks_played][move_count[disks_played]] =
move_index;
move_count[disks_played]++;
}
empties_remaining--;
if ( empties_remaining == 0 )
break;
}
}
pre_search_done = TRUE;
}
if ( i == move_count[disks_played] ) /* No moves left to try? */
break;
best_index = i;
best_score =
evals[disks_played][sorted_move_order[disks_played][feas_index_list[disks_played][i]]];
for ( j = i + 1; j < move_count[disks_played]; j++ ) {
int cand_move;
cand_move =
sorted_move_order[disks_played][feas_index_list[disks_played][j]];
if ( evals[disks_played][cand_move] > best_score ) {
best_score =
evals[disks_played][cand_move];
best_index = j;
}
}
move_index = feas_index_list[disks_played][best_index];
feas_index_list[disks_played][best_index] =
feas_index_list[disks_played][i];
}
move = sorted_move_order[disks_played][move_index];
counter_phase = (counter_phase + 1) & 63;
if ( counter_phase == 0 ) {
double node_val;
adjust_counter( &nodes );
node_val = counter_value( &nodes );
if ( node_val - last_panic_check >= EVENT_CHECK_INTERVAL ) {
/* Time abort? */
last_panic_check = node_val;
check_panic_abort();
/* Display available search information */
if ( echo )
display_buffers();
/* Check for events */
handle_event( TRUE, FALSE, TRUE );
if ( is_panic_abort() || force_return )
return SEARCH_ABORT;
}
}
(void) make_move( side_to_move, move, TRUE );
update_pv = FALSE;
if ( searched == 0 ) {
best = curr_val =
-tree_search( level + 1, max_depth, OPP( side_to_move ), -beta,
-curr_alpha, allow_hash, allow_mpc, TRUE );
best_move_index = move_index;
update_pv = TRUE;
}
else {
curr_alpha = MAX( best, curr_alpha );
curr_val =
-tree_search( level + 1, max_depth, OPP( side_to_move ),
-(curr_alpha + 1), -curr_alpha, allow_hash,
allow_mpc, TRUE );
if ( (curr_val > curr_alpha) && (curr_val < beta) ) {
curr_val =
-tree_search( level + 1, max_depth, OPP( side_to_move ), -beta,
INFINITE_EVAL, allow_hash, allow_mpc, TRUE );
if ( curr_val > best ) {
best = curr_val;
best_move_index = move_index;
update_pv = TRUE;
}
}
else if ( curr_val > best ) {
best = curr_val;
best_move_index = move_index;
update_pv = TRUE;
}
}
unmake_move( side_to_move, move );
if ( is_panic_abort() || force_return )
return SEARCH_ABORT;
evals[disks_played][move] = curr_val;
if ( update_pv ) {
update_best_list( best_list, move, best_list_index, best_list_length );
pv[level][level] = move;
pv_depth[level] = pv_depth[level + 1];
for ( j = level + 1; j < pv_depth[level + 1]; j++)
pv[level][j] = pv[level + 1][j];
}
if ( best >= beta ) {
advance_move( move_index );
if ( use_hash && allow_midgame_hash_update )
add_hash_extended( MIDGAME_MODE, best, best_list,
MIDGAME_SCORE | LOWER_BOUND, remains, selectivity );
return best;
}
searched++;
}
/* Post-processing */
#if CHECK_HASH_CODES && defined( TEXT_BASED )
if ( use_hash )
if ( (h1 != hash1) || (h2 != hash2) )
printf( "%s: %x%x %s: %x%x", HASH_BEFORE, h1, h2,
HASH_AFTER, hash1, hash2 );
#endif
if ( move_count[disks_played] > 0 ) {
advance_move( best_move_index );
if ( use_hash && allow_midgame_hash_update ) {
if ( best > alpha )
add_hash_extended( MIDGAME_MODE, best, best_list,
MIDGAME_SCORE | EXACT_VALUE, remains, selectivity );
else
add_hash_extended( MIDGAME_MODE, best, best_list,
MIDGAME_SCORE | UPPER_BOUND, remains, selectivity );
}
return best;
}
else if ( void_legal ) { /* No feasible moves */
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
curr_val = -tree_search( level, max_depth, OPP( side_to_move ), -beta,
-alpha, allow_hash, allow_mpc, FALSE );
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
return curr_val;
}
else {
pv_depth[level] = level;
return terminal_evaluation( side_to_move );
}
}
/*
PERTURB_SCORE
Perturbs SCORE by PERTURBATION if it doesn't appear to be
a midgame win.
*/
static int
perturb_score( int score, int perturbation ) {
if ( abs( score ) < ALMOST_MIDGAME_WIN )
return score + perturbation;
else
return score;
}
/*
ROOT_TREE_SEARCH
The recursive tree search function that is to be called only
for the root of the search tree.
*/
int
root_tree_search( int level, int max_depth, int side_to_move, int alpha,
int beta, int allow_hash, int allow_mpc, int void_legal ) {
char buffer[32];
int i, j;
int curr_val, best, pre_best;
int searched;
int move;
int move_index, best_move_index;
int hash_hit;
int pre_depth;
int update_pv;
int remains;
int use_hash, pre_search_done;
int curr_alpha;
int best_index, best_score;
int best_list_index, best_list_length;
int selectivity;
int offset;
int best_list[4];
HashEntry entry;
#if CHECK_HASH_CODES && defined( TEXT_BASED )
unsigned int h1, h2;
#endif
remains = max_depth - level;
INCREMENT_COUNTER( nodes );
use_hash = (remains >= HASH_THRESHOLD) && USE_HASH_TABLE && allow_hash;
if ( USE_MPC && allow_mpc )
selectivity = 1;
else
selectivity = 0;
#if CHECK_HASH_CODES && defined( TEXT_BASED )
if ( use_hash ) {
h1 = hash1;
h2 = hash2;
}
#endif
/* Hash strategy at the root: Only use hash table information for
move ordering purposes. This guarantees that score perturbation
is applied for all moves. */
hash_hit = FALSE;
if ( use_hash && allow_midgame_hash_probe ) {
find_hash( &entry, MIDGAME_MODE );
if ( entry.draft != NO_HASH_MOVE )
hash_hit = TRUE;
}
pre_search_done = FALSE;
if ( !get_ponder_move() ) {
if ( (alpha <= -MIDGAME_WIN) && (beta >= MIDGAME_WIN) )
sprintf( buffer, "[-inf,inf]:" );
else if ( (alpha <= -MIDGAME_WIN) && (beta < MIDGAME_WIN) )
sprintf( buffer, "[-inf,%.1f]:", beta / 128.0 );
else if ( (alpha > -MIDGAME_WIN) && (beta >= MIDGAME_WIN) )
sprintf( buffer, "[%.1f,inf]:", alpha / 128.0 );
else
sprintf( buffer, "[%.1f,%.1f]:", alpha / 128.0, beta / 128.0 );
clear_sweep();
send_sweep( "%-14s ", buffer );
}
/* Full negascout search */
searched = 0;
best = -INFINITE_EVAL;
best_move_index = -1,
curr_alpha = alpha;
best_list_length = 0;
for ( i = 0; i < 4; i++ )
best_list[i] = 0;
if ( !pre_search_done ) {
move_count[disks_played] = 0;
if ( hash_hit )
for ( i = 0; i < 4; i++ )
if ( valid_move( entry.move[i], side_to_move ) )
best_list[best_list_length++] = entry.move[i];
}
for ( i = 0, best_list_index = 0; TRUE; i++, best_list_index++ ) {