forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getcoeff.c
3846 lines (3648 loc) · 122 KB
/
getcoeff.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: getcoeff.c
Created: November 19, 1997
Modified: January 3, 2003
Author: Gunnar Andersson ([email protected])
Contents: Unpacks the coefficient file, computes final-stage
pattern values and performs pattern evaluation.
*/
#include "porting.h"
#define __linux__
#if !defined( _WIN32_WCE ) && !defined( __linux__ )
#include <dir.h>
#endif
#ifdef _WIN32_WCE
#include "../zlib113/zlib.h"
#else
#include <assert.h>
#include <zlib.h>
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "constant.h"
#include "error.h"
#include "eval.h"
#include "getcoeff.h"
#include "macros.h"
#include "magic.h"
#include "moves.h"
#include "patterns.h"
#include "safemem.h"
#include "search.h"
#include "texts.h"
/* An upper limit on the number of coefficient blocks in the arena */
#define MAX_BLOCKS 200
/* The file containing the feature values (really shouldn't be #define'd) */
#define PATTERN_FILE "coeffs2.bin"
/* Calculate cycle counts for the eval function? */
#define TIME_EVAL 0
typedef struct {
int permanent;
int loaded;
int prev, next;
int block;
short parity_constant[2];
short parity;
short constant;
short *afile2x, *bfile, *cfile, *dfile;
short *diag8, *diag7, *diag6, *diag5, *diag4;
short *corner33, *corner52;
short *afile2x_last, *bfile_last, *cfile_last, *dfile_last;
short *diag8_last, *diag7_last, *diag6_last, *diag5_last, *diag4_last;
short *corner33_last, *corner52_last;
char alignment_padding[12]; /* In order to achieve 128-byte alignment */
} CoeffSet;
typedef struct {
short afile2x_block[59049];
short bfile_block[6561];
short cfile_block[6561];
short dfile_block[6561];
short diag8_block[6561];
short diag7_block[2187];
short diag6_block[729];
short diag5_block[243];
short diag4_block[81];
short corner33_block[19683];
short corner52_block[59049];
} AllocationBlock;
static int stage_count;
static int block_count;
static int stage[61];
static int block_allocated[MAX_BLOCKS], block_set[MAX_BLOCKS];
static int eval_map[61];
static AllocationBlock *block_list[MAX_BLOCKS];
static CoeffSet set[61];
/*
TERMINAL_PATTERNS
Calculates the patterns associated with a filled board,
only counting discs.
*/
static void
terminal_patterns( void ) {
double result;
double value[8][8];
int i, j, k;
int row[10];
int hit[8][8];
/* Count the number of times each square is counted */
for ( i = 0; i < 8; i++ )
for ( j = 0; j < 8; j++ )
hit[i][j] = 0;
for ( i = 0; i < 8; i++ ) {
hit[0][i]++;
hit[i][0]++;
hit[7][i]++;
hit[i][7]++;
}
for ( i = 0; i < 8; i++ ) {
hit[1][i]++;
hit[i][1]++;
hit[6][i]++;
hit[i][6]++;
}
for ( i = 0; i < 8; i++ ) {
hit[2][i]++;
hit[i][2]++;
hit[5][i]++;
hit[i][5]++;
}
for ( i = 0; i < 8; i++ ) {
hit[3][i]++;
hit[i][3]++;
hit[4][i]++;
hit[i][4]++;
}
for ( i = 0; i < 3; i++ )
for ( j = 0; j < 3; j++ ) {
hit[i][j]++;
hit[i][7 - j]++;
hit[7 - i][j]++;
hit[7 - i][7 - j]++;
}
for ( i = 0; i < 2; i++ )
for ( j = 0; j < 5; j++ ) {
hit[i][j]++;
hit[j][i]++;
hit[i][7 - j]++;
hit[j][7 - i]++;
hit[7 - i][j]++;
hit[7 - j][i]++;
hit[7 - i][7 - j]++;
hit[7 - j][7 - i]++;
}
for ( i = 0; i < 8; i++ ) {
hit[i][i]++;
hit[i][7 - i]++;
}
for ( i = 0; i < 7; i++ ) {
hit[i][i + 1]++;
hit[i + 1][i]++;
hit[i][6 - i]++;
hit[i + 1][7 - i]++;
}
for ( i = 0; i < 6; i++ ) {
hit[i][i + 2]++;
hit[i + 2][i]++;
hit[i][5 - i]++;
hit[i + 2][7 - i]++;
}
for ( i = 0; i < 5; i++ ) {
hit[i][i + 3]++;
hit[i + 3][i]++;
hit[i][4 - i]++;
hit[i + 3][7 - i]++;
}
for ( i = 0; i < 4; i++ ) {
hit[i][i + 4]++;
hit[i + 4][i]++;
hit[i][3 - i]++;
hit[i + 4][7 - i]++;
}
hit[1][1] += 2;
hit[1][6] += 2;
hit[6][1] += 2;
hit[6][6] += 2;
for ( i = 0; i < 8; i++ )
for ( j = 0; j < 8; j++ )
value[i][j] = 1.0 / hit[i][j];
for ( i = 0; i < 10; i++ )
row[i] = 0;
for ( i = 0; i < 59049; i++ ) {
result = 0.0;
for ( j = 0; j < 8; j++ )
if ( row[j] == BLACKSQ )
result += value[0][j];
else if ( row[j] == WHITESQ )
result -= value[0][j];
if ( row[8] == BLACKSQ )
result += value[1][1];
else if ( row[8] == WHITESQ )
result -= value[1][1];
if ( row[9] == BLACKSQ )
result += value[1][6];
else if ( row[9] == WHITESQ )
result -= value[1][6];
set[60].afile2x[i] = floor( result * 128.0 + 0.5 );
result = 0.0;
for ( j = 0; j < 5; j++ )
for ( k = 0; k < 2; k++ )
if ( row[5 * k + j] == BLACKSQ )
result += value[j][k];
else if ( row[5 * k + j] == WHITESQ )
result -= value[j][k];
set[60].corner52[i] = floor( result * 128.0 + 0.5 );
if ( i < 19683 ) {
result = 0.0;
for ( j = 0; j < 3; j++ )
for ( k = 0; k < 3; k++ )
if ( row[3 * j + k] == BLACKSQ )
result += value[j][k];
else if ( row[3 * j + k] == WHITESQ )
result -= value[j][k];
set[60].corner33[i] = floor( result * 128.0 + 0.5 );
}
if ( i < 6561 ) {
result = 0.0;
for ( j = 0; j < 8; j++ )
if ( row[j] == BLACKSQ )
result += value[1][j];
else if ( row[j] == WHITESQ )
result -= value[1][j];
set[60].bfile[i] = floor( result * 128.0 + 0.5 );
result = 0.0;
for ( j = 0; j < 8; j++ )
if ( row[j] == BLACKSQ )
result += value[2][j];
else if ( row[j] == WHITESQ )
result -= value[2][j];
set[60].cfile[i] = floor( result * 128.0 + 0.5 );
result = 0.0;
for ( j = 0; j < 8; j++ )
if ( row[j] == BLACKSQ )
result += value[3][j];
else if ( row[j] == WHITESQ )
result -= value[3][j];
set[60].dfile[i] = floor( result * 128.0 + 0.5 );
result = 0.0;
for ( j = 0; j < 8; j++ )
if ( row[j] == BLACKSQ )
result += value[j][j];
else if ( row[j] == WHITESQ )
result -= value[j][j];
set[60].diag8[i] = floor( result * 128.0 + 0.5 );
}
if ( i < 2187 ) {
result = 0.0;
for ( j = 0; j < 7; j++ )
if ( row[j] == BLACKSQ )
result += value[j][j + 1];
else if ( row[j] == WHITESQ )
result -= value[j][j + 1];
set[60].diag7[i] = floor( result * 128.0 + 0.5 );
}
if ( i < 729 ) {
result = 0.0;
for ( j = 0; j < 6; j++ )
if ( row[j] == BLACKSQ )
result += value[j][j + 2];
else if ( row[j] == WHITESQ )
result -= value[j][j + 2];
set[60].diag6[i] = floor( result * 128.0 + 0.5 );
}
if ( i < 243 ) {
result = 0.0;
for ( j = 0; j < 5; j++ )
if ( row[j] == BLACKSQ )
result += value[j][j + 3];
else if ( row[j] == WHITESQ )
result -= value[j][j + 3];
set[60].diag5[i] = floor( result * 128.0 + 0.5 );
}
if ( i < 81 ) {
result = 0.0;
for ( j = 0; j < 4; j++ )
if ( row[j] == BLACKSQ )
result += value[j][j + 4];
else if ( row[j] == WHITESQ )
result -= value[j][j + 4];
set[60].diag4[i] = floor( result * 128.0 + 0.5 );
}
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 10) );
}
}
/*
GET_WORD
Reads a 16-bit signed integer from a file.
*/
static short
get_word( gzFile stream ) {
union {
short signed_val;
unsigned short unsigned_val;
} val;
#if 0
int received;
unsigned char hi, lo;
received = fread( &hi, sizeof( unsigned char ), 1, stream );
if ( received != 1 )
puts( READ_ERROR_HI );
received = fread( &lo, sizeof( unsigned char ), 1, stream );
if ( received != 1 )
puts( READ_ERROR_LO );
#else
int hi, lo;
hi = gzgetc( stream );
assert( hi != -1 );
lo = gzgetc( stream );
assert( lo != -1 );
#endif
val.unsigned_val = (hi << 8) + lo;
return val.signed_val;
}
/*
UNPACK_BATCH
Reads feature values for one specific pattern
*/
static void
unpack_batch( short *item, int *mirror, int count, gzFile stream ) {
int i;
short *buffer;
buffer = (short *) safe_malloc( count * sizeof( short ) );
/* Unpack the coefficient block where the score is scaled
so that 512 units corresponds to one disk. */
for ( i = 0; i < count; i++ )
if ( (mirror == NULL) || (mirror[i] == i) )
buffer[i] = get_word( stream ) / 4;
else
buffer[i] = buffer[mirror[i]];
for ( i = 0; i < count; i++ )
item[i] = buffer[i];
if ( mirror != NULL )
for ( i = 0; i < count; i++ )
if ( item[i] != item[mirror[i]] ) {
printf( "%s @ %d <--> %d of %d\n", MIRROR_ERROR,
i, mirror[i], count );
printf( "%d <--> %d\n", item[i], item[mirror[i]] );
exit( EXIT_FAILURE );
}
free( buffer );
}
/*
UNPACK_COEFFS
Reads all feature values for a certain stage. To take care of
symmetric patterns, mirror tables are calculated.
*/
static void
unpack_coeffs( gzFile stream ) {
int i, j, k;
int mirror_pattern;
int row[10];
int *map_mirror3;
int *map_mirror4;
int *map_mirror5;
int *map_mirror6;
int *map_mirror7;
int *map_mirror8;
int *map_mirror33;
int *map_mirror8x2;
/* Allocate the memory needed for the temporary mirror maps from the
heap rather than the stack to reduce memory requirements. */
map_mirror3 = (int *) safe_malloc( 27 * sizeof( int ) );
map_mirror4 = (int *) safe_malloc( 81 * sizeof( int ) );
map_mirror5 = (int *) safe_malloc( 243 * sizeof( int ) );
map_mirror6 = (int *) safe_malloc( 729 * sizeof( int ) );
map_mirror7 = (int *) safe_malloc( 2187 * sizeof( int ) );
map_mirror8 = (int *) safe_malloc( 6561 * sizeof( int ) );
map_mirror33 = (int *) safe_malloc( 19683 * sizeof( int ) );
map_mirror8x2 = (int *) safe_malloc( 59049 * sizeof( int ) );
/* Build the pattern tables for 8*1-patterns */
for ( i = 0; i < 8; i++ )
row[i] = 0;
for ( i = 0; i < 6561; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 8; j++ )
mirror_pattern += row[j] * pow3[7 - j];
/* Create the symmetry map */
map_mirror8[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 8) );
}
/* Build the tables for 7*1-patterns */
for ( i = 0; i < 7; i++ )
row[i] = 0;
for ( i = 0; i < 2187; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 7; j++ )
mirror_pattern += row[j] * pow3[6 - j];
map_mirror7[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1]) == 0 && (j < 7) );
}
/* Build the tables for 6*1-patterns */
for ( i = 0; i < 6; i++ )
row[i] = 0;
for ( i = 0; i < 729; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 6; j++ )
mirror_pattern += row[j] * pow3[5 - j];
map_mirror6[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1]) == 0 && (j < 6) );
}
/* Build the tables for 5*1-patterns */
for ( i = 0; i < 5; i++ )
row[i] = 0;
for ( i = 0; i < 243; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 5; j++ )
mirror_pattern += row[j] * pow3[4 - j];
map_mirror5[i] = MIN( mirror_pattern, i );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 5) );
}
/* Build the tables for 4*1-patterns */
for ( i = 0; i < 4; i++ )
row[i] = 0;
for ( i = 0; i < 81; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 4; j++ )
mirror_pattern += row[j] * pow3[3 - j];
map_mirror4[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1]) == 0 && (j < 4) );
}
/* Build the tables for 3*1-patterns */
for ( i = 0; i < 3; i++ )
row[i] = 0;
for ( i = 0; i < 27; i++ ) {
mirror_pattern = 0;
for ( j = 0; j < 3; j++ )
mirror_pattern += row[j] * pow3[2 - j];
map_mirror3[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 3) );
}
/* Build the tables for 5*2-patterns */
/* --- none needed --- */
/* Build the tables for edge2X-patterns */
for ( i = 0; i < 6561; i++ )
for ( j = 0; j < 3; j++ )
for ( k = 0; k < 3; k++ )
map_mirror8x2[i + 6561 * j + 19683 * k] =
MIN( flip8[i] + 6561 * k + 19683 * j, i + 6561 * j + 19683 * k );
/* Build the tables for 3*3-patterns */
for ( i = 0; i < 9; i++ )
row[i] = 0;
for ( i = 0; i < 19683; i++ ) {
mirror_pattern =
row[0] + 3 * row[3] + 9 * row[6] +
27 * row[1] + 81 * row[4] + 243 * row[7] +
729 * row[2] + 2187 * row[5] + 6561 * row[8];
map_mirror33[i] = MIN( i, mirror_pattern );
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 9) );
}
/* Read and unpack - using symmetries - the coefficient tables. */
for ( i = 0; i < stage_count - 1; i++ ) {
set[stage[i]].constant = get_word( stream ) / 4;
set[stage[i]].parity = get_word( stream ) / 4;
set[stage[i]].parity_constant[0] = set[stage[i]].constant;
set[stage[i]].parity_constant[1] =
set[stage[i]].constant + set[stage[i]].parity;
unpack_batch( set[stage[i]].afile2x, map_mirror8x2, 59049, stream );
unpack_batch( set[stage[i]].bfile, map_mirror8, 6561, stream );
unpack_batch( set[stage[i]].cfile, map_mirror8, 6561, stream );
unpack_batch( set[stage[i]].dfile, map_mirror8, 6561, stream );
unpack_batch( set[stage[i]].diag8, map_mirror8, 6561, stream );
unpack_batch( set[stage[i]].diag7, map_mirror7, 2187, stream );
unpack_batch( set[stage[i]].diag6, map_mirror6, 729, stream );
unpack_batch( set[stage[i]].diag5, map_mirror5, 243, stream );
unpack_batch( set[stage[i]].diag4, map_mirror4, 81, stream );
unpack_batch( set[stage[i]].corner33, map_mirror33, 19683, stream );
unpack_batch( set[stage[i]].corner52, NULL, 59049, stream );
}
/* Free the mirror tables - the symmetries are now implicit
in the coefficient tables. */
free( map_mirror3 );
free( map_mirror4 );
free( map_mirror5 );
free( map_mirror6 );
free( map_mirror7 );
free( map_mirror8 );
free( map_mirror33 );
free( map_mirror8x2 );
}
/*
GENERATE_BATCH
Interpolates between two stages.
*/
static void
generate_batch( short *target, int count, short *source1, int weight1,
short *source2, int weight2 ) {
int i;
int total_weight;
total_weight = weight1 + weight2;
for ( i = 0; i < count; i++ )
target[i] = (weight1 * source1[i] + weight2 * source2[i]) /
total_weight;
}
/*
FIND_MEMORY_BLOCK
Maintains an internal memory handler to boost
performance and avoid heap fragmentation.
*/
static int
find_memory_block( short **afile2x, short **bfile, short **cfile,
short **dfile, short **diag8, short **diag7,
short **diag6, short **diag5, short **diag4,
short **corner33, short **corner52, int index ) {
int i;
int found_free, free_block;
found_free = FALSE;
free_block = -1;
for ( i = 0; (i < block_count) && !found_free; i++ )
if (!block_allocated[i] ) {
found_free = TRUE;
free_block = i;
}
if ( !found_free ) {
if ( block_count < MAX_BLOCKS )
block_list[block_count] =
(AllocationBlock *) safe_malloc( sizeof( AllocationBlock ) );
if ( (block_count == MAX_BLOCKS) ||
(block_list[block_count] == NULL) )
fatal_error( "%s @ #%d\n", MEMORY_ERROR, block_count );
free_block = block_count;
block_count++;
}
*afile2x = block_list[free_block]->afile2x_block;
*bfile = block_list[free_block]->bfile_block;
*cfile = block_list[free_block]->cfile_block;
*dfile = block_list[free_block]->dfile_block;
*diag8 = block_list[free_block]->diag8_block;
*diag7 = block_list[free_block]->diag7_block;
*diag6 = block_list[free_block]->diag6_block;
*diag5 = block_list[free_block]->diag5_block;
*diag4 = block_list[free_block]->diag4_block;
*corner33 = block_list[free_block]->corner33_block;
*corner52 = block_list[free_block]->corner52_block;
block_allocated[free_block] = TRUE;
block_set[free_block] = index;
return free_block;
}
/*
FREE_MEMORY_BLOCK
Marks a memory block as no longer in use.
*/
static void
free_memory_block( int block ) {
block_allocated[block] = FALSE;
}
/*
INIT_MEMORY_HANDLER
Mark all blocks in the memory arena as "not used".
*/
static void
init_memory_handler( void ) {
int i;
block_count = 0;
for ( i = 0; i < MAX_BLOCKS; i++ )
block_allocated[i] = FALSE;
}
/*
ALLOCATE_SET
Finds memory for all patterns belonging to a certain stage.
*/
static void
allocate_set( int index ) {
set[index].block =
find_memory_block( &set[index].afile2x, &set[index].bfile,
&set[index].cfile, &set[index].dfile,
&set[index].diag8, &set[index].diag7,
&set[index].diag6, &set[index].diag5, &set[index].diag4,
&set[index].corner33, &set[index].corner52, index );
}
/*
LOAD_SET
Performs linear interpolation between the nearest stages to
obtain the feature values for the stage in question.
Also calculates the offset pointers to the last elements in each block
(used for the inverted patterns when white is to move).
*/
static void
load_set( int index ) {
int prev, next;
int weight1, weight2, total_weight;
if ( !set[index].permanent ) {
prev = set[index].prev;
next = set[index].next;
if ( prev == next ) {
weight1 = 1;
weight2 = 1;
}
else {
weight1 = next - index;
weight2 = index - prev;
}
total_weight = weight1 + weight2;
set[index].constant =
(weight1 * set[prev].constant + weight2 * set[next].constant) /
total_weight;
set[index].parity =
(weight1 * set[prev].parity + weight2 * set[next].parity) /
total_weight;
set[index].parity_constant[0] = set[index].constant;
set[index].parity_constant[1] =
set[index].constant + set[index].parity;
allocate_set( index );
generate_batch( set[index].afile2x, 59049,
set[prev].afile2x, weight1,
set[next].afile2x, weight2 );
generate_batch( set[index].bfile, 6561,
set[prev].bfile, weight1,
set[next].bfile, weight2 );
generate_batch( set[index].cfile, 6561,
set[prev].cfile, weight1,
set[next].cfile, weight2 );
generate_batch( set[index].dfile, 6561,
set[prev].dfile, weight1,
set[next].dfile, weight2 );
generate_batch( set[index].diag8, 6561,
set[prev].diag8, weight1,
set[next].diag8, weight2 );
generate_batch( set[index].diag7, 2187,
set[prev].diag7, weight1,
set[next].diag7, weight2 );
generate_batch( set[index].diag6, 729,
set[prev].diag6, weight1,
set[next].diag6, weight2 );
generate_batch( set[index].diag5, 243,
set[prev].diag5, weight1,
set[next].diag5, weight2 );
generate_batch( set[index].diag4, 81,
set[prev].diag4, weight1,
set[next].diag4, weight2 );
generate_batch( set[index].corner33, 19683,
set[prev].corner33, weight1,
set[next].corner33, weight2 );
generate_batch( set[index].corner52, 59049,
set[prev].corner52, weight1,
set[next].corner52, weight2 );
}
set[index].afile2x_last = set[index].afile2x + 59048;
set[index].bfile_last = set[index].bfile + 6560;
set[index].cfile_last = set[index].cfile + 6560;
set[index].dfile_last = set[index].dfile + 6560;
set[index].diag8_last = set[index].diag8 + 6560;
set[index].diag7_last = set[index].diag7 + 2186;
set[index].diag6_last = set[index].diag6 + 728;
set[index].diag5_last = set[index].diag5 + 242;
set[index].diag4_last = set[index].diag4 + 80;
set[index].corner33_last = set[index].corner33 + 19682;
set[index].corner52_last = set[index].corner52 + 59048;
set[index].loaded = 1;
}
/*
DISC_COUNT_ADJUSTMENT
*/
static void
eval_adjustment( double disc_adjust, double edge_adjust,
double corner_adjust, double x_adjust ) {
int i, j, k;
int adjust;
int row[10];
for ( i = 0; i < stage_count - 1; i++ ) {
/* Bonuses for having more discs */
for ( j = 0; j < 59049; j++ ) {
set[stage[i]].afile2x[j] += set[60].afile2x[j] * disc_adjust;
set[stage[i]].corner52[j] += set[60].corner52[j] * disc_adjust;
}
for ( j = 0; j < 19683; j++ )
set[stage[i]].corner33[j] += set[60].corner33[j] * disc_adjust;
for ( j = 0; j < 6561; j++ ) {
set[stage[i]].bfile[j] += set[60].bfile[j] * disc_adjust;
set[stage[i]].cfile[j] += set[60].cfile[j] * disc_adjust;
set[stage[i]].dfile[j] += set[60].dfile[j] * disc_adjust;
set[stage[i]].diag8[j] += set[60].diag8[j] * disc_adjust;
}
for ( j = 0; j < 2187; j++ )
set[stage[i]].diag7[j] += set[60].diag7[j] * disc_adjust;
for ( j = 0; j < 729; j++ )
set[stage[i]].diag6[j] += set[60].diag6[j] * disc_adjust;
for ( j = 0; j < 243; j++ )
set[stage[i]].diag5[j] += set[60].diag5[j] * disc_adjust;
for ( j = 0; j < 81; j++ )
set[stage[i]].diag4[j] += set[60].diag4[j] * disc_adjust;
for ( j = 0; j < 10; j++ )
row[j] = 0;
for ( j = 0; j < 59049; j++ ) {
adjust = 0;
/* Bonus for having edge discs */
for ( k = 1; k <= 6; k++ )
if ( row[k] == BLACKSQ )
adjust += 128.0 * edge_adjust;
else if ( row[k] == WHITESQ )
adjust -= 128.0 * edge_adjust;
/* Bonus for having corners. The "0.5 *" is because corners are part
of two A-file+2X patterns. */
if ( row[0] == BLACKSQ )
adjust += 0.5 * 128.0 * corner_adjust;
else if ( row[0] == WHITESQ )
adjust -= 0.5 * 128.0 * corner_adjust;
if ( row[7] == BLACKSQ )
adjust += 0.5 * 128.0 * corner_adjust;
else if ( row[7] == WHITESQ )
adjust -= 0.5 * 128.0 * corner_adjust;
/* Bonus for having X-squares when the adjacent corners are empty.
Scaling by 0.5 applies here too. */
if ( (row[8] == BLACKSQ) && (row[0] == EMPTY) )
adjust += 0.5 * 128.0 * x_adjust;
else if ( (row[8] == WHITESQ) && (row[0] == EMPTY) )
adjust -= 0.5 * 128.0 * x_adjust;
if ( (row[9] == BLACKSQ) && (row[7] == EMPTY) )
adjust += 0.5 * 128.0 * x_adjust;
else if ( (row[9] == WHITESQ) && (row[7] == EMPTY) )
adjust -= 0.5 * 128.0 * x_adjust;
set[stage[i]].afile2x[j] += adjust;
/* Next configuration */
k = 0;
do { /* The odometer principle */
row[k]++;
if ( row[k] == 3 )
row[k] = 0;
k++;
} while ( (row[k - 1] == 0) && (k < 10) );
}
}
}
/*
INIT_COEFFS
Manages the initialization of all relevant tables.
*/
void
init_coeffs( void ) {
int i, j;
int word1, word2;
int subsequent_stage;
int curr_stage;
gzFile coeff_stream;
FILE *adjust_stream;
char sPatternFile[260];
init_memory_handler();
#if defined( _WIN32_WCE )
/* Special hack for CE. */
getcwd(sPatternFile, sizeof(sPatternFile));
strcat(sPatternFile, PATTERN_FILE);
#elif defined( __linux__ )
/* Linux don't support current directory. */
strcpy( sPatternFile, PATTERN_FILE );
#else
getcwd(sPatternFile, sizeof(sPatternFile));
strcat(sPatternFile, "/" PATTERN_FILE);
#endif
char* env_coeffs = getenv("COEFFS_PATH");
coeff_stream = gzopen(env_coeffs ? env_coeffs : sPatternFile, "rb" );
if ( coeff_stream == NULL )
fatal_error( "%s '%s'\n", FILE_ERROR, sPatternFile );
/* Check the magic values in the beginning of the file to make sure
the file format is right */
word1 = get_word( coeff_stream );
word2 = get_word( coeff_stream );
if ( (word1 != EVAL_MAGIC1) || (word2 != EVAL_MAGIC2) )
fatal_error( "%s: %s", sPatternFile, CHECKSUM_ERROR );
/* Read the different stages for which the evaluation function
was tuned and mark the other stages with pointers to the previous
and next stages. */
for ( i = 0; i <= 60; i++ ) {
set[i].permanent = 0;
set[i].loaded = 0;
}
stage_count = get_word( coeff_stream );
for ( i = 0; i < stage_count - 1; i++ ) {
stage[i] = get_word( coeff_stream );
curr_stage = stage[i];
if ( i == 0 )
for ( j = 0; j < stage[0]; j++ ) {
set[j].prev = stage[0];
set[j].next = stage[0];
}
else
for ( j = stage[i - 1]; j < stage[i]; j++ ) {
set[j].prev = stage[i - 1];
set[j].next = stage[i];
}
set[curr_stage].permanent = 1;
allocate_set( curr_stage );
}
stage[stage_count - 1] = 60;
for ( j = stage[stage_count - 2]; j < 60; j++ ) {
set[j].prev = stage[stage_count - 2];
set[j].next = 60;
}
set[60].permanent = 1;
allocate_set(60);
/* Read the pattern values */
unpack_coeffs( coeff_stream );
gzclose( coeff_stream );
/* Calculate the patterns which correspond to the board being filled */
terminal_patterns();
set[60].constant = 0;
set[60].parity = 0;
set[60].parity_constant[0] = set[60].constant;
set[60].parity_constant[1] = set[60].constant + set[60].parity;