-
Notifications
You must be signed in to change notification settings - Fork 4
/
osfbook.c
4722 lines (3945 loc) · 124 KB
/
osfbook.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: osfbook.c
Created: December 31, 1997
Modified: December 30, 2004
Author: Gunnar Andersson ([email protected])
Contents: A module which implements the book algorithm which
simply tries to maximize the first evaluation out
of book by means of nega-maxing. The details get
a bit hairy as all transpositions are kept track of
using a hash table.
*/
#include "porting.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32_WCE
#include <time.h>
#endif
#include "autoplay.h"
#include "constant.h"
#include "counter.h"
#include "display.h"
#include "end.h"
#include "error.h"
#include "eval.h"
#include "game.h"
#include "getcoeff.h"
#include "hash.h"
#include "macros.h"
#include "magic.h"
#include "midgame.h"
#include "moves.h"
#include "myrandom.h"
#include "opname.h"
#include "osfbook.h"
#include "patterns.h"
#include "safemem.h"
#include "search.h"
#include "texts.h"
#include "timer.h"
#define STAGE_WINDOW 8
#define ROOT 0
#define DEFAULT_SEARCH_DEPTH 2
#define DEFAULT_SLACK 0
#define INFINIT_BATCH_SIZE 10000000
#define DEFAULT_GAME_MODE PRIVATE_GAME
#define DEFAULT_DRAW_MODE OPPONENT_WINS
#define DEFAULT_FORCE_BLACK FALSE
#define DEFAULT_FORCE_WHITE FALSE
/* Data structure definitions */
#define NODE_TABLE_SLACK 1000
#define EMPTY_HASH_SLOT -1
#define NOT_AVAILABLE -1
#define MAX_HASH_FILL 0.80
/* Tree search parameters */
#define HASH_BITS 19
#define RANDOMIZATION 0
/* The depth for reevaluation when the hash table should be cleared */
#define HASH_CLEAR_DEPTH 8
/* Get rid of some ugly warnings by disallowing usage of the
macro version of tolower (not time-critical anyway). */
#ifdef toupper
#undef toupper
#endif
typedef struct {
int hash_val1;
int hash_val2;
short black_minimax_score;
short white_minimax_score;
short best_alternative_move;
short alternative_score;
unsigned short flags;
} BookNode;
typedef struct {
const char *out_file_name;
double prob;
int max_diff;
int max_depth;
} StatisticsSpec;
/* Local variables */
static const char *correction_script_name = NULL;
static double deviation_bonus;
static int search_depth;
static int node_table_size, hash_table_size;
static int total_game_count, book_node_count;
static int evaluated_count, evaluation_stage;
static int max_eval_count;
static int max_batch_size;
static int exhausted_node_count;
static int max_slack;
static int low_deviation_threshold, high_deviation_threshold;
static int min_eval_span, max_eval_span;
static int min_negamax_span, max_negamax_span;
static int leaf_count, bad_leaf_count, really_bad_leaf_count;
static int unreachable_count;
static int candidate_count;
static int force_black, force_white;
static int used_slack[3];
static int b1_b1_map[100], g1_b1_map[100], g8_b1_map[100], b8_b1_map[100];
static int a2_b1_map[100], a7_b1_map[100], h7_b1_map[100], h2_b1_map[100];
static int exact_count[61], wld_count[61];
static int exhausted_count[61], common_count[61];
static int *symmetry_map[8], *inv_symmetry_map[8];
static int line_hash[2][8][6561];
static int *book_hash_table = NULL;
static DrawMode draw_mode = DEFAULT_DRAW_MODE;
static GameMode game_mode = DEFAULT_GAME_MODE;
static BookNode *node = NULL;
static CandidateMove candidate_list[60];
/*
INIT_MAPS
Initializes the 8 symmetry maps.
Notice that the order of these MUST coincide with the returned
orientation value from get_hash() OR YOU WILL LOSE BIG.
*/
static void
init_maps( void ) {
int i, j, k, pos;
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
b1_b1_map[pos] = pos;
g1_b1_map[pos] = 10 * i + (9 - j);
g8_b1_map[pos] = 10 * (9 - i) + (9 - j);
b8_b1_map[pos] = 10 * (9 - i) + j;
a2_b1_map[pos] = 10 * j + i;
a7_b1_map[pos] = 10 * j + (9 - i);
h7_b1_map[pos] = 10 * (9 - j) + (9 - i);
h2_b1_map[pos] = 10 * (9 - j) + i;
}
symmetry_map[0] = b1_b1_map;
inv_symmetry_map[0] = b1_b1_map;
symmetry_map[1] = g1_b1_map;
inv_symmetry_map[1] = g1_b1_map;
symmetry_map[2] = g8_b1_map;
inv_symmetry_map[2] = g8_b1_map;
symmetry_map[3] = b8_b1_map;
inv_symmetry_map[3] = b8_b1_map;
symmetry_map[4] = a2_b1_map;
inv_symmetry_map[4] = a2_b1_map;
symmetry_map[5] = a7_b1_map;
inv_symmetry_map[5] = h2_b1_map;
symmetry_map[6] = h7_b1_map;
inv_symmetry_map[6] = h7_b1_map;
symmetry_map[7] = h2_b1_map;
inv_symmetry_map[7] = a7_b1_map;
for ( i = 0; i < 8; i++ )
symmetry_map[i][NULL_MOVE] = NULL_MOVE;
for ( i = 0; i < 8; i++ )
for ( j = 1; j <= 8; j++ )
for ( k = 1; k <= 8; k++ ) {
pos = 10 * j + k;
if ( inv_symmetry_map[i][symmetry_map[i][pos]] != pos )
fatal_error( "Error in map %d: inv(map(%d))=%d\n",
i, pos, inv_symmetry_map[i][symmetry_map[i][pos]] );
}
}
/*
SELECT_HASH_SLOT
Finds a slot in the hash table for the node INDEX
using linear probing.
*/
static void
select_hash_slot( int index ) {
int slot;
slot = node[index].hash_val1 % hash_table_size;
while ( book_hash_table[slot] != EMPTY_HASH_SLOT )
slot = (slot + 1) % hash_table_size;
book_hash_table[slot] = index;
}
/*
PROBE_HASH_TABLE
Search for a certain hash code in the hash table.
*/
static int
probe_hash_table( int val1, int val2 ) {
int slot;
if ( hash_table_size == 0 )
return NOT_AVAILABLE;
else {
slot = val1 % hash_table_size;
while ( (book_hash_table[slot] != EMPTY_HASH_SLOT) &&
(node[book_hash_table[slot]].hash_val2 != val2 ||
node[book_hash_table[slot]].hash_val1 != val1) )
slot = (slot + 1) % hash_table_size;
return slot;
}
}
/*
CREATE_HASH_REFERENCEE
Takes the node list and fills the hash table with indices
into the node list.
*/
static void
create_hash_reference( void ) {
int i;
for ( i = 0; i < hash_table_size; i++ )
book_hash_table[i] = EMPTY_HASH_SLOT;
for ( i = 0; i < book_node_count; i++ )
select_hash_slot( i );
}
/*
REBUILD_HASH_TABLE
Resize the hash table for a requested number of nodes.
*/
static void
rebuild_hash_table( int requested_items ) {
int new_size, new_memory;
new_size = 2 * requested_items;
new_memory = new_size * sizeof(int);
if ( hash_table_size == 0 )
book_hash_table = (int *) safe_malloc( new_memory );
else
book_hash_table = (int *) safe_realloc(book_hash_table, new_memory);
if ( book_hash_table == NULL )
fatal_error( "%s %d\n", BOOK_HASH_ALLOC_ERROR, new_memory, new_size );
hash_table_size = new_size;
create_hash_reference();
}
/*
PREPARE_HASH
Compute the position hash codes.
*/
static void
prepare_hash( void ) {
int i, j, k;
/* The hash keys are static, hence the same keys must be
produced every time the program is run. */
my_srandom( 0 );
for ( i = 0; i < 2; i++ )
for ( j = 0; j < 8; j++ )
for ( k = 0; k < 6561; k++ )
line_hash[i][j][k] = (my_random() % 2) ? my_random() :-my_random();
hash_table_size = 0;
}
/*
GET_HASH
Return the hash values for the current board position.
The position is rotated so that the 64-bit hash value
is minimized among all rotations. This ensures detection
of all transpositions.
See also init_maps().
*/
void
get_hash( int *val0, int *val1, int *orientation ) {
int i, j;
int min_map;
int min_hash0, min_hash1;
int out[8][2];
/* Calculate the 8 different 64-bit hash values for the
different rotations. */
compute_line_patterns( board );
for ( i = 0; i < 8; i++ )
for ( j = 0; j < 2; j++ )
out[i][j] = 0;
for ( i = 0; i < 8; i++ ) {
/* b1 -> b1 */
out[0][0] ^= line_hash[0][i][row_pattern[i]];
out[0][1] ^= line_hash[1][i][row_pattern[i]];
/* g1 -> b1 */
out[1][0] ^= line_hash[0][i][flip8[row_pattern[i]]];
out[1][1] ^= line_hash[1][i][flip8[row_pattern[i]]];
/* g8 -> b1 */
out[2][0] ^= line_hash[0][i][flip8[row_pattern[7 - i]]];
out[2][1] ^= line_hash[1][i][flip8[row_pattern[7 - i]]];
/* b8 -> b1 */
out[3][0] ^= line_hash[0][i][row_pattern[7 - i]];
out[3][1] ^= line_hash[1][i][row_pattern[7 - i]];
/* a2 -> b1 */
out[4][0] ^= line_hash[0][i][col_pattern[i]];
out[4][1] ^= line_hash[1][i][col_pattern[i]];
/* a7 -> b1 */
out[5][0] ^= line_hash[0][i][flip8[col_pattern[i]]];
out[5][1] ^= line_hash[1][i][flip8[col_pattern[i]]];
/* h7 -> b1 */
out[6][0] ^= line_hash[0][i][flip8[col_pattern[7 - i]]];
out[6][1] ^= line_hash[1][i][flip8[col_pattern[7 - i]]];
/* h2 -> b1 */
out[7][0] ^= line_hash[0][i][col_pattern[7 - i]];
out[7][1] ^= line_hash[1][i][col_pattern[7 - i]];
}
/* Find the rotation minimizing the hash index.
If two hash indices are equal, map number is implicitly used
as tie-breaker. */
min_map = 0;
min_hash0 = out[0][0];
min_hash1 = out[0][1];
for ( i = 1; i < 8; i++)
if ( (out[i][0] < min_hash0) ||
((out[i][0] == min_hash0) && (out[i][1] < min_hash1)) ) {
min_map = i;
min_hash0 = out[i][0];
min_hash1 = out[i][1];
}
*val0 = abs( min_hash0 );
*val1 = abs( min_hash1 );
*orientation = min_map;
}
/*
SET_ALLOCATION
Changes the number of nodes for which memory is allocated.
*/
static void
set_allocation( int size ) {
if ( node == NULL )
node = (BookNode *) safe_malloc( size * sizeof( BookNode ) );
else
node = (BookNode *) safe_realloc( node, size * sizeof( BookNode ) );
if ( node == NULL )
fatal_error( "%s %d\n", BOOK_ALLOC_ERROR,
size * sizeof( BookNode ), size );
node_table_size = size;
if ( node_table_size > MAX_HASH_FILL * hash_table_size )
rebuild_hash_table( node_table_size );
}
/*
INCREASE_ALLOCATION
Allocate more memory for the book tree.
*/
static void
increase_allocation( void ) {
set_allocation( node_table_size + 50000 );
}
/*
CREATE_BOOK_NODE
Creates a new book node without any connections whatsoever
to the rest of the tree.
*/
static int
create_BookNode( int val1, int val2, unsigned short flags ) {
int index;
if ( book_node_count == node_table_size )
increase_allocation();
index = book_node_count;
node[index].hash_val1 = val1;
node[index].hash_val2 = val2;
node[index].black_minimax_score = NO_SCORE;
node[index].white_minimax_score = NO_SCORE;
node[index].best_alternative_move = NO_MOVE;
node[index].alternative_score = NO_SCORE;
node[index].flags = flags;
select_hash_slot( index );
book_node_count++;
return index;
}
/*
INIT_BOOK_TREE
Initializes the node tree by creating the root of the tree.
*/
static void
init_book_tree( void ) {
book_node_count = 0;
node = NULL;
}
/*
PREPATE_TREE_TRAVERSAL
Prepares all relevant data structures for a tree search
or traversal.
*/
static void
prepare_tree_traversal( void ) {
int side_to_move;
toggle_experimental( 0 );
game_init( NULL, &side_to_move );
toggle_midgame_hash_usage( TRUE, TRUE );
toggle_abort_check( FALSE );
toggle_midgame_abort_check( FALSE );
}
/*
CLEAR_NODE_DEPTH
Changes the flags of a node so that the search depth
bits are cleared.
*/
static void
clear_node_depth( int index ) {
int depth;
depth = node[index].flags >> DEPTH_SHIFT;
node[index].flags ^= (depth << DEPTH_SHIFT);
}
/*
GET_NODE_DEPTH
*/
static int
get_node_depth( int index ) {
return node[index].flags >> DEPTH_SHIFT;
}
/*
SET_NODE_DEPTH
Marks a node as being searched to a certain depth.
*/
static void
set_node_depth( int index, int depth ) {
node[index].flags |= (depth << DEPTH_SHIFT);
}
/*
ADJUST_SCORE
Tweak a score as to encourage early deviations.
*/
static int
adjust_score( int score, int side_to_move ) {
int adjustment;
int adjust_steps;
adjust_steps = high_deviation_threshold - disks_played;
if ( adjust_steps < 0 )
adjustment = 0;
else {
if ( disks_played < low_deviation_threshold )
adjust_steps = high_deviation_threshold - low_deviation_threshold;
adjustment = floor( adjust_steps * deviation_bonus * 128.0 );
if ( side_to_move == WHITESQ )
adjustment = -adjustment;
}
return (score + adjustment);
}
/*
DO_MINIMAX
Calculates the minimax value of node INDEX.
*/
static void
do_minimax( int index, int *black_score, int *white_score ) {
int i;
int child;
int child_black_score, child_white_score;
int side_to_move;
int this_move, alternative_move;
int alternative_move_found;
int child_count;
int best_black_child_val, best_white_child_val;
int worst_black_child_val, worst_white_child_val;
int slot, val1, val2, orientation;
short best_black_score, best_white_score;
/* If the node has been visited AND it is a midgame node, meaning
that the minimax values are not to be tweaked, return the
stored values. */
if ( !(node[index].flags & NOT_TRAVERSED) ) {
if ( !(node[index].flags & (WLD_SOLVED | FULL_SOLVED)) ) {
*black_score = node[index].black_minimax_score;
*white_score = node[index].white_minimax_score;
return;
}
}
/* Correct WLD solved nodes corresponding to draws to be represented
as full solved and make sure full solved nodes are marked as
WLD solved as well */
if ( (node[index].flags & WLD_SOLVED) &&
(node[index].black_minimax_score == 0) &&
(node[index].white_minimax_score == 0) )
node[index].flags |= FULL_SOLVED;
if ( (node[index].flags & FULL_SOLVED) && !(node[index].flags & WLD_SOLVED) )
node[index].flags |= WLD_SOLVED;
/* Recursively minimax all children of the node */
if ( node[index].flags & BLACK_TO_MOVE )
side_to_move = BLACKSQ;
else
side_to_move = WHITESQ;
best_black_child_val = -99999;
best_white_child_val = -99999;
worst_black_child_val = 99999;
worst_white_child_val = 99999;
if ( node[index].alternative_score != NO_SCORE ) {
best_black_score =
adjust_score( node[index].alternative_score, side_to_move);
best_white_score = best_black_score;
best_black_child_val = worst_black_child_val = best_black_score;
best_white_child_val = worst_white_child_val = best_white_score;
alternative_move_found = FALSE;
alternative_move = node[index].best_alternative_move;
if ( alternative_move > 0 ) {
get_hash( &val1, &val2, &orientation );
alternative_move = inv_symmetry_map[orientation][alternative_move];
}
}
else {
alternative_move_found = TRUE;
alternative_move = 0;
if ( side_to_move == BLACKSQ ) {
best_black_score = -INFINITE_WIN;
best_white_score = -INFINITE_WIN;
}
else {
best_black_score = +INFINITE_WIN;
best_white_score = +INFINITE_WIN;
}
}
generate_all( side_to_move );
child_count = 0;
for ( i = 0; i < move_count[disks_played]; i++ ) {
piece_count[BLACKSQ][disks_played] = disc_count( BLACKSQ );
piece_count[WHITESQ][disks_played] = disc_count( WHITESQ );
this_move = move_list[disks_played][i];
(void) make_move( side_to_move, this_move, TRUE );
get_hash( &val1, &val2, &orientation );
slot = probe_hash_table( val1, val2 );
child = book_hash_table[slot];
if ( child != EMPTY_HASH_SLOT ) {
do_minimax( child, &child_black_score, &child_white_score );
best_black_child_val = MAX( best_black_child_val, child_black_score );
best_white_child_val = MAX( best_white_child_val, child_white_score );
worst_black_child_val = MIN( worst_black_child_val, child_black_score );
worst_white_child_val = MIN( worst_white_child_val, child_white_score );
if ( side_to_move == BLACKSQ ) {
best_black_score = MAX( child_black_score, best_black_score );
best_white_score = MAX( child_white_score, best_white_score );
}
else {
best_black_score = MIN( child_black_score, best_black_score );
best_white_score = MIN( child_white_score, best_white_score );
}
child_count++;
}
else if ( !alternative_move_found && (this_move == alternative_move) )
alternative_move_found = TRUE;
unmake_move( side_to_move, this_move );
}
if ( !alternative_move_found ) {
/* The was-to-be deviation now leads to a position in the database,
hence it can no longer be used. */
node[index].alternative_score = NO_SCORE;
node[index].best_alternative_move = NO_MOVE;
}
/* Try to infer the WLD status from the children */
if ( !(node[index].flags & (FULL_SOLVED | WLD_SOLVED)) &&
(child_count > 0) ) {
if ( side_to_move == BLACKSQ ) {
if ( (best_black_child_val >= CONFIRMED_WIN) &&
(best_white_child_val >= CONFIRMED_WIN) ) { /* Black win */
node[index].black_minimax_score = node[index].white_minimax_score =
MIN( best_black_child_val, best_white_child_val );
node[index].flags |= WLD_SOLVED;
}
else if ( (best_black_child_val <= -CONFIRMED_WIN) &&
(best_white_child_val <= -CONFIRMED_WIN)) { /* Black loss */
node[index].black_minimax_score = node[index].white_minimax_score =
MAX( best_black_child_val, best_white_child_val );
node[index].flags |= WLD_SOLVED;
}
}
else {
if ((worst_black_child_val <= -CONFIRMED_WIN) &&
(worst_white_child_val <= -CONFIRMED_WIN)) { /* White win */
node[index].black_minimax_score = node[index].white_minimax_score =
MAX( worst_black_child_val, worst_white_child_val );
node[index].flags |= WLD_SOLVED;
}
else if ((worst_black_child_val >= CONFIRMED_WIN) &&
(worst_white_child_val >= CONFIRMED_WIN) ) { /* White loss */
node[index].black_minimax_score = node[index].white_minimax_score =
MIN( worst_black_child_val, worst_white_child_val );
node[index].flags |= WLD_SOLVED;
}
}
}
/* Tweak the minimax scores for draws to give the right
draw avoidance behavior */
if ( node[index].flags & (FULL_SOLVED | WLD_SOLVED) ) {
*black_score = node[index].black_minimax_score;
*white_score = node[index].white_minimax_score;
if ( (node[index].black_minimax_score == 0) &&
(node[index].white_minimax_score == 0) )
/* Is it a position in which a draw should be avoided? */
if ( (game_mode == PRIVATE_GAME) || !(node[index].flags & PRIVATE_NODE) )
switch ( draw_mode ) {
case NEUTRAL:
break;
case BLACK_WINS:
*black_score = +UNWANTED_DRAW;
*white_score = +UNWANTED_DRAW;
break;
case WHITE_WINS:
*black_score = -UNWANTED_DRAW;
*white_score = -UNWANTED_DRAW;
break;
case OPPONENT_WINS:
*black_score = -UNWANTED_DRAW;
*white_score = +UNWANTED_DRAW;
break;
}
}
else {
*black_score = node[index].black_minimax_score = best_black_score;
*white_score = node[index].white_minimax_score = best_white_score;
}
node[index].flags ^= NOT_TRAVERSED;
}
/*
MINIMAX_TREE
Calculates the minimax values of all nodes in the tree.
*/
void
minimax_tree( void ) {
int i;
int dummy_black_score, dummy_white_score;
time_t start_time, stop_time;
#ifdef TEXT_BASED
printf( "Calculating minimax value... " );
fflush( stdout );
#endif
prepare_tree_traversal();
time( &start_time );
/* Mark all nodes as not traversed */
for ( i = 0; i < book_node_count; i++ )
node[i].flags |= NOT_TRAVERSED;
do_minimax( ROOT, &dummy_black_score, &dummy_white_score );
time( &stop_time );
#ifdef TEXT_BASED
printf( "done (took %d s)\n", (int) (stop_time - start_time) );
puts("");
#endif
}
#ifdef INCLUDE_BOOKTOOL
/*
EXPORT_POSITION
Output the position and its value according to the database
to file.
*/
static void
export_position( int side_to_move, int score, FILE* target_file ) {
int i, j, pos;
int black_mask, white_mask;
int hi_mask, lo_mask;
for ( i = 1; i <= 8; i++ ) {
black_mask = 0;
white_mask = 0;
for ( j = 0, pos = 10 * i + 1; j < 8; j++, pos++ )
if ( board[pos] == BLACKSQ )
black_mask |= (1 << j);
else if ( board[pos] == WHITESQ )
white_mask |= (1 << j);
hi_mask = black_mask >> 4;
lo_mask = black_mask % 16;
fprintf( target_file, "%c%c", hi_mask + ' ', lo_mask + ' ' );
hi_mask = white_mask >> 4;
lo_mask = white_mask % 16;
fprintf( target_file, "%c%c", hi_mask + ' ', lo_mask + ' ' );
}
fprintf( target_file, " " );
if ( side_to_move == BLACKSQ )
fputc( '*', target_file );
else
fputc( 'O', target_file );
fprintf( target_file, " %2d %+d\n", disks_played, score );
}
/*
DO_RESTRICTED_MINIMAX
Calculates the book-only minimax value of node INDEX,
not caring about deviations from the database.
*/
static void
do_restricted_minimax( int index, int low, int high, FILE *target_file,
int *minimax_values ) {
int i;
int child, corrected_score;
int side_to_move;
int this_move;
int child_count;
int slot, val1, val2, orientation;
short best_score;
if ( !(node[index].flags & NOT_TRAVERSED) )
return;
/* Recursively minimax all children of the node */
if ( node[index].flags & BLACK_TO_MOVE )
side_to_move = BLACKSQ;
else
side_to_move = WHITESQ;
if ( side_to_move == BLACKSQ )
best_score = -INFINITE_WIN;
else
best_score = +INFINITE_WIN;
generate_all( side_to_move );
child_count = 0;
for ( i = 0; i < move_count[disks_played]; i++ ) {
piece_count[BLACKSQ][disks_played] = disc_count( BLACKSQ );
piece_count[WHITESQ][disks_played] = disc_count( WHITESQ );
this_move = move_list[disks_played][i];
(void) make_move( side_to_move, this_move, TRUE );
get_hash( &val1, &val2, &orientation );
slot = probe_hash_table( val1, val2 );
child = book_hash_table[slot];
if ( child != EMPTY_HASH_SLOT ) {
do_restricted_minimax( child, low, high, target_file, minimax_values );
corrected_score = minimax_values[child];
if ( ((side_to_move == BLACKSQ) && (corrected_score > best_score)) ||
((side_to_move == WHITESQ) && (corrected_score < best_score)) )
best_score = corrected_score;
child_count++;
}
unmake_move( side_to_move, this_move );
}
if ( (node[index].flags & FULL_SOLVED) ||
((node[index].flags & WLD_SOLVED) && child_count == 0) )
best_score = node[index].black_minimax_score;
else if ( child_count == 0 ) {
#ifdef TEXT_BASED
printf( "%d disks played\n", disks_played );
printf( "Node #%d has no children and lacks WLD status\n", index );
#endif
exit( EXIT_FAILURE );
}
if ( best_score > CONFIRMED_WIN )
best_score -= CONFIRMED_WIN;
else if ( best_score < -CONFIRMED_WIN )
best_score += CONFIRMED_WIN;
minimax_values[index] = best_score;
node[index].flags ^= NOT_TRAVERSED;
if ( (disks_played >= low) && (disks_played <= high) )
export_position( side_to_move, best_score, target_file );
}
/*
RESTRICTED_MINIMAX_TREE
Calculates the minimax values of all nodes in the tree,
not
*/
void
restricted_minimax_tree( int low, int high, const char *pos_file_name ) {
FILE *pos_file;
int i;
int *minimax_values;
time_t start_time, stop_time;
#ifdef TEXT_BASED
printf( "Calculating restricted minimax value... " );
fflush( stdout );
#endif
prepare_tree_traversal();
time( &start_time );
/* Mark all nodes as not traversed */
for ( i = 0; i < book_node_count; i++ )
node[i].flags |= NOT_TRAVERSED;
minimax_values = (int *) safe_malloc( book_node_count * sizeof( int ) );
pos_file = fopen( pos_file_name, "a" );
do_restricted_minimax( ROOT, low, high, pos_file, minimax_values );
time( &stop_time );
#ifdef TEXT_BASED
printf( "done (took %d s)\n", (int) (stop_time - start_time) );
puts( "" );
#endif
free( minimax_values );
fclose( pos_file );
}
/*
DO_MIDGAME_STATISTICS
Recursively makes sure a subtree is evaluated to the specified depth.
*/
static void
do_midgame_statistics( int index, StatisticsSpec spec ) {
EvaluationType dummy_info;
int i;
int depth;
int child;
int side_to_move;
int this_move;
int slot, val1, val2, orientation;
int eval_list[64];
FILE *out_file;
if ( !(node[index].flags & NOT_TRAVERSED) )
return;
if ( node[index].flags & BLACK_TO_MOVE )
side_to_move = BLACKSQ;
else
side_to_move = WHITESQ;
generate_all( side_to_move );
/* With a certain probability, search the position to a variety
of different depths in order to determine correlations. */
if ( ((my_random() % 1000) < 1000.0 * spec.prob) &&
(abs( node[index].black_minimax_score ) < spec.max_diff) ) {
display_board( stdout, board, BLACKSQ, FALSE, FALSE, FALSE );
setup_hash( FALSE );
determine_hash_values( side_to_move, board );
for ( depth = 1; depth <= spec.max_depth; depth += 2 ) {
(void) middle_game( side_to_move, depth, FALSE, &dummy_info );
eval_list[depth] = root_eval;
#ifdef TEXT_BASED
printf( "%2d: %-5d ", depth, eval_list[depth] );
#endif
}
#ifdef TEXT_BASED
puts( "" );
#endif
setup_hash( FALSE );
determine_hash_values( side_to_move, board );
for ( depth = 2; depth <= spec.max_depth; depth += 2 ) {
(void) middle_game( side_to_move, depth, FALSE, &dummy_info );
eval_list[depth] = root_eval;
#ifdef TEXT_BASED
printf( "%2d: %-5d ", depth, eval_list[depth] );
#endif
}
#ifdef TEXT_BASED
puts( "" );
#endif
/* Store the scores if the last eval is in the range [-20,20] */
out_file = fopen( spec.out_file_name, "a" );
if ( (out_file != NULL) &&
(abs( eval_list[spec.max_depth] ) <= 20 * 128) ) {
get_hash( &val1, &val2, &orientation );
fprintf( out_file, "%08x%08x %2d ", val1, val2, disks_played );
fprintf( out_file, "%2d %2d ", 1, spec.max_depth );
for ( i = 1; i <= spec.max_depth; i++ )
fprintf( out_file, "%5d ", eval_list[i] );
fprintf( out_file, "\n" );
fclose( out_file );
}
}
/* Recursively search the children of the node */
for ( i = 0; i < move_count[disks_played]; i++ ) {
this_move = move_list[disks_played][i];
(void) make_move( side_to_move, this_move, TRUE );
get_hash( &val1, &val2, &orientation );
slot = probe_hash_table( val1, val2 );
child = book_hash_table[slot];
if ( child != EMPTY_HASH_SLOT )
do_midgame_statistics( child, spec );
unmake_move( side_to_move, this_move );
}
node[index].flags ^= NOT_TRAVERSED;
}