-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.c
6510 lines (4852 loc) · 157 KB
/
eval.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
/*
* eval.c
*
* by Gary Wong <[email protected]>, 1998, 1999, 2000, 2001, 2002.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 3 or later of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: eval.c,v 1.407 2012/05/14 05:58:55 gflohr Exp $
*/
#include "config.h"
#include "backgammon.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <locale.h>
#include <string.h>
#include <errno.h>
#include <cache.h>
#include <fcntl.h>
#include "isaac.h"
#include <md5.h>
#include "bearoffgammon.h"
#include "positionid.h"
#include "matchid.h"
#include "matchequity.h"
#include "format.h"
#include "sse.h"
#include "multithread.h"
#include "util.h"
#ifdef WIN32
#define BINARY O_BINARY
#else
#define BINARY 0
#endif
typedef int ( *classevalfunc )( const TanBoard anBoard, float arOutput[],
const bgvariation bgv, NNState *nnStates );
typedef void ( *classstatusfunc )( char *szOutput );
typedef int ( *cfunc )( const void *, const void * );
#if !LOCKING_VERSION
f_FindnSaveBestMoves FindnSaveBestMoves = FindnSaveBestMovesNoLocking;
f_FindBestMove FindBestMove = FindBestMoveNoLocking;
f_EvaluatePosition EvaluatePosition = EvaluatePositionNoLocking;
f_ScoreMove ScoreMove = ScoreMoveNoLocking;
f_GeneralCubeDecisionE GeneralCubeDecisionE = GeneralCubeDecisionENoLocking;
f_GeneralEvaluationE GeneralEvaluationE = GeneralEvaluationENoLocking;
#define FindnSaveBestMoves FindnSaveBestMovesNoLocking
#define FindBestMove FindBestMoveNoLocking
#define EvaluatePosition EvaluatePositionNoLocking
#define ScoreMove ScoreMoveNoLocking
#define GeneralCubeDecisionE GeneralCubeDecisionENoLocking
#define GeneralEvaluationE GeneralEvaluationENoLocking
#define EvaluatePositionCache EvaluatePositionCacheNoLocking
#define FindBestMovePlied FindBestMovePliedNoLocking
#define GeneralEvaluationEPlied GeneralEvaluationEPliedNoLocking
#define EvaluatePositionCubeful3 EvaluatePositionCubeful3NoLocking
#define ScoreMoves ScoreMovesNoLocking
#define FindBestMoveInEval FindBestMoveInEvalNoLocking
#define GeneralEvaluationEPliedCubeful GeneralEvaluationEPliedCubefulNoLocking
#define EvaluatePositionCubeful4 EvaluatePositionCubeful4NoLocking
#define CacheAdd CacheAddNoLocking
#define CacheLookup CacheLookupNoLocking
static int EvaluatePositionCache( NNState *nnStates, const TanBoard anBoard, float arOutput[],
const cubeinfo* pci, const evalcontext* pecx,
int nPlies, positionclass pc );
static int FindBestMovePlied( int anMove[ 8 ], int nDice0, int nDice1,
TanBoard anBoard, const cubeinfo* pci,
const evalcontext* pec, int nPlies,
movefilter aamf[ MAX_FILTER_PLIES ][ MAX_FILTER_PLIES ] );
/* Race inputs */
enum {
/* In a race position, bar and the 24 points are always empty, so only */
/* 23*4 (92) are needed */
/* (0 <= k < 14), RI_OFF + k = */
/* 1 if exactly k+1 checkers are off, 0 otherwise */
RI_OFF = 92,
/* Number of cross-overs by outside checkers */
RI_NCROSS = 92 + 14,
HALF_RACE_INPUTS
};
/* Contact inputs -- see Berliner for most of these */
enum {
/* n - number of checkers off
off1 - 1 n >= 5
n/5 otherwise
off2 - 1 n >= 10
(n-5)/5 n < 5 < 10
0 otherwise
off3 - (n-10)/5 n > 10
0 otherwise
*/
I_OFF1, I_OFF2, I_OFF3,
/* Minimum number of pips required to break contact.
For each checker x, N(x) is checker location,
C(x) is max({forall o : N(x) - N(o)}, 0)
Break Contact : (sum over x of C(x)) / 152
152 is dgree of contact of start position.
*/
I_BREAK_CONTACT,
/* Location of back checker (Normalized to [01])
*/
I_BACK_CHEQUER,
/* Location of most backward anchor. (Normalized to [01])
*/
I_BACK_ANCHOR,
/* Forward anchor in opponents home.
Normalized in the following way: If there is an anchor in opponents
home at point k (1 <= k <= 6), value is k/6. Otherwise, if there is an
anchor in points (7 <= k <= 12), take k/6 as well. Otherwise set to 2.
This is an attempt for some continuity, since a 0 would be the "same" as
a forward anchor at the bar.
*/
I_FORWARD_ANCHOR,
/* Average number of pips opponent loses from hits.
Some heuristics are required to estimate it, since we have no idea what
the best move actually is.
1. If board is weak (less than 3 anchors), don't consider hitting on
points 22 and 23.
2. Dont break anchors inside home to hit.
*/
I_PIPLOSS,
/* Number of rolls that hit at least one checker.
*/
I_P1,
/* Number of rolls that hit at least two checkers.
*/
I_P2,
/* How many rolls permit the back checker to escape (Normalized to [01])
*/
I_BACKESCAPES,
/* Maximum containment of opponent checkers, from our points 9 to op back
checker.
Value is (1 - n/36), where n is number of rolls to escape.
*/
I_ACONTAIN,
/* Above squared */
I_ACONTAIN2,
/* Maximum containment, from our point 9 to home.
Value is (1 - n/36), where n is number of rolls to escape.
*/
I_CONTAIN,
/* Above squared */
I_CONTAIN2,
/* For all checkers out of home,
sum (Number of rolls that let x escape * distance from home)
Normalized by dividing by 3600.
*/
I_MOBILITY,
/* One sided moment.
Let A be the point of weighted average:
A = sum of N(x) for all x) / nCheckers.
Then for all x : A < N(x), M = (average (N(X) - A)^2)
Diveded by 400 to normalize.
*/
I_MOMENT2,
/* Average number of pips lost when on the bar.
Normalized to [01]
*/
I_ENTER,
/* Probablity of one checker not entering from bar.
1 - (1 - n/6)^2, where n is number of closed points in op home.
*/
I_ENTER2,
I_TIMING,
I_BACKBONE,
I_BACKG,
I_BACKG1,
I_FREEPIP,
I_BACKRESCAPES,
MORE_INPUTS };
#define MINPPERPOINT 4
#define NUM_INPUTS ((25 * MINPPERPOINT + MORE_INPUTS) * 2)
#define NUM_RACE_INPUTS ( HALF_RACE_INPUTS * 2 )
#define NUM_PRUNING_INPUTS (25 * MINPPERPOINT * 2)
#define DATABASE1_SIZE ( 54264 * 32 * 2 )
#define DATABASE2_SIZE ( 924 * 924 * 2 )
#define DATABASE_SIZE ( DATABASE1_SIZE + DATABASE2_SIZE )
static int anEscapes[ 0x1000 ];
static int anEscapes1[ 0x1000 ];
static int anPoint[16] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
neuralnet nnContact, nnRace, nnCrashed;
neuralnet nnpContact, nnpRace, nnpCrashed;
bearoffcontext *pbcOS = NULL;
bearoffcontext *pbcTS = NULL;
bearoffcontext *pbc1 = NULL;
bearoffcontext *pbc2 = NULL;
bearoffcontext *apbcHyper[ 3 ] = { NULL, NULL, NULL };
evalCache cEval;
evalCache cpEval;
unsigned int cCache;
int fInterrupt = FALSE;
int fMatchCancelled = FALSE;
/* variation of backgammon used by gnubg */
bgvariation bgvDefault = VARIATION_STANDARD;
/* the number of chequers for the variations */
int anChequers[ NUM_VARIATIONS ] = { 15, 15, 1, 2, 3 };
const char *aszVariations[ NUM_VARIATIONS ] = {
N_("Standard backgammon"),
N_("Nackgammon"),
N_("1-chequer hypergammon"),
N_("2-chequer hypergammon"),
N_("3-chequer hypergammon")
};
const char *aszVariationCommands[ NUM_VARIATIONS ] = {
"standard",
"nackgammon",
"1-chequer-hypergammon",
"2-chequer-hypergammon",
"3-chequer-hypergammon"
};
cubeinfo ciCubeless = { 1, 0, 0, 0, { 0, 0 }, FALSE, FALSE, FALSE,
{ 1.0, 1.0, 1.0, 1.0 }, VARIATION_STANDARD };
const char *aszEvalType[] =
{
N_ ("No evaluation"),
N_ ("Neural net evaluation"),
N_ ("Rollout")
};
evalcontext ecBasic = { FALSE, 0, FALSE, TRUE, 0.0 };
/* defaults for the filters - 0 ply uses no filters */
movefilter
defaultFilters[MAX_FILTER_PLIES][MAX_FILTER_PLIES] = {
{ { 0, 8, 0.16f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 2, 0.04f }, { 0, 0, 0 } },
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 2, 0.04f }, { -1, 0, 0 } },
};
/* Random context, for generating non-deterministic noisy evaluations. */
static randctx rc;
/*
* predefined settings
*/
const char *aszSettings[ NUM_SETTINGS ] = {
N_ ("setting|beginner"),
N_ ("setting|casual play"),
N_ ("setting|intermediate"),
N_ ("setting|advanced"),
N_ ("setting|expert"),
N_ ("setting|world class"),
N_ ("setting|supremo"),
N_ ("setting|grandmaster"),
N_ ("setting|4ply") };
/* which evaluation context does the predefined settings use */
evalcontext aecSettings[ NUM_SETTINGS ] = {
{ TRUE, 0, FALSE, TRUE, 0.060f }, /* beginner */
{ TRUE, 0, FALSE, TRUE, 0.050f }, /* casual player */
{ TRUE, 0, FALSE, TRUE, 0.040f }, /* intermediate */
{ TRUE, 0, FALSE, TRUE, 0.015f }, /* advanced */
{ TRUE, 0, FALSE, TRUE, 0.0f }, /* expert */
{ TRUE, 2, TRUE, TRUE, 0.0f }, /* world class */
{ TRUE, 2, TRUE, TRUE, 0.0f }, /* supremo */
{ TRUE, 3, TRUE, TRUE, 0.0f }, /* grand master */
{ TRUE, 4, TRUE, TRUE, 0.0f }, /* 4ply */
};
/* which move filter does the predefined settings use */
int aiSettingsMoveFilter[ NUM_SETTINGS ] = {
-1, /* beginner: n/a */
-1, /* casual play: n/a */
-1, /* intermediate: n/a */
-1, /* advanced: n/a */
-1, /* expoert: n/a */
2, /* wc: normal */
3, /* supremo: large */
3, /* grandmaster: large */
3, /* 4ply: large */
};
/* the predefined move filters */
const char *aszMoveFilterSettings[ NUM_MOVEFILTER_SETTINGS ] = {
N_("Tiny"),
N_("Narrow"),
N_("Normal"),
N_("Large"),
N_("Huge")
};
movefilter aaamfMoveFilterSettings[ NUM_MOVEFILTER_SETTINGS ][ MAX_FILTER_PLIES ][ MAX_FILTER_PLIES ] = {
/* tiny */
{ { { 0, 5, 0.08f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 5, 0.08f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 5, 0.08f }, { -1, 0, 0 }, { 0, 2, 0.02f }, { 0, 0, 0 } },
{ { 0, 5, 0.08f }, { -1, 0, 0 }, { 0, 2, 0.02f }, { -1 , 0, 0 } } },
/* narrow */
{ { { 0, 8, 0.12f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.12f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.12f }, { -1, 0, 0 }, { 0, 2, 0.03f }, { 0, 0, 0 } },
{ { 0, 8, 0.12f }, { -1, 0, 0 }, { 0, 2, 0.03f }, { -1, 0, 0 } } },
/* normal */
{ { { 0, 8, 0.16f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 2, 0.04f }, { 0, 0, 0 } },
{ { 0, 8, 0.16f }, { -1, 0, 0 }, { 0, 2, 0.04f }, { -1, 0, 0 } } },
/* large */
{ { { 0, 16, 0.32f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 16, 0.32f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 16, 0.32f }, { -1, 0, 0 }, { 0, 4, 0.08f }, { 0, 0, 0 } },
{ { 0, 16, 0.32f }, { -1, 0, 0 }, { 0, 4, 0.08f }, { -1, 0, 0.0f } } },
/* huge */
{ { { 0, 20, 0.44f }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 20, 0.44f }, { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } ,
{ { 0, 20, 0.44f }, { -1, 0, 0 }, { 0, 6, 0.11f }, { 0, 0, 0 } },
{ { 0, 20, 0.44f }, { -1, 0, 0 }, { 0, 6, 0.11f }, { -1, 0, 0.0 } } }
};
const char *aszDoubleTypes[ NUM_DOUBLE_TYPES ] = {
N_("doubletype|Double"),
N_("doubletype|Beaver"),
N_("doubletype|Raccoon")
};
/* parameters for EvalEfficiency */
float rTSCubeX = 0.6f; /* for match play only */
float rOSCubeX = 0.6f;
float rRaceFactorX = 0.00125f;
float rRaceCoefficientX = 0.55f;
float rRaceMax = 0.7f;
float rRaceMin = 0.6f;
float rCrashedX = 0.68f;
float rContactX = 0.68f;
static void ComputeTable0( void )
{
int i, c, n0, n1;
for( i = 0; i < 0x1000; i++ ) {
c = 0;
for( n0 = 0; n0 <= 5; n0++ )
for( n1 = 0; n1 <= n0; n1++ )
if( !( i & ( 1 << ( n0 + n1 + 1 ) ) ) &&
!( ( i & ( 1 << n0 ) ) && ( i & ( 1 << n1 ) ) ) )
c += ( n0 == n1 ) ? 1 : 2;
anEscapes[ i ] = c;
}
}
static int Escapes( const unsigned int anBoard[ 25 ], int n ) {
int i, af = 0, m;
m = (n < 12) ? n : 12;
for( i = 0; i < m; i++ )
af |= ( anPoint[anBoard[ 24 + i - n ]] << i );
return anEscapes[ af ];
}
static void ComputeTable1( void )
{
int i, c, n0, n1, low;
anEscapes1[ 0 ] = 0;
for( i = 1; i < 0x1000; i++ ) {
c = 0;
low = 0;
while( ! (i & (1 << low)) ) {
++low;
}
for( n0 = 0; n0 <= 5; n0++ )
for( n1 = 0; n1 <= n0; n1++ ) {
if( (n0 + n1 + 1 > low) &&
!( i & ( 1 << ( n0 + n1 + 1 ) ) ) &&
!( ( i & ( 1 << n0 ) ) && ( i & ( 1 << n1 ) ) ) ) {
c += ( n0 == n1 ) ? 1 : 2;
}
}
anEscapes1[ i ] = c;
}
}
static int Escapes1( const unsigned int anBoard[ 25 ], int n ) {
int i, af = 0, m;
m = (n < 12) ? n : 12;
for( i = 0; i < m; i++ )
af |= ( anPoint[anBoard[ 24 + i - n ]] << i );
return anEscapes1[ af ];
}
static void ComputeTable( void )
{
ComputeTable0();
ComputeTable1();
}
NNState nnStatesStorage[MAX_NUMTHREADS][3];
static void
DestroyWeights( void )
{
NeuralNetDestroy( &nnContact );
NeuralNetDestroy( &nnCrashed );
NeuralNetDestroy( &nnRace );
NeuralNetDestroy( &nnpContact );
NeuralNetDestroy( &nnpCrashed );
NeuralNetDestroy( &nnpRace );
}
extern int
EvalShutdown ( void ) {
int i;
int j;
for (j = 0; j < MAX_NUMTHREADS; j++)
for (i = 0; i < 3; i++)
{
free(nnStatesStorage[j][i].savedBase);
free(nnStatesStorage[j][i].savedIBase);
}
/* close bearoff databases */
BearoffClose( pbc1 );
BearoffClose( pbc2 );
BearoffClose( pbcOS );
BearoffClose( pbcTS );
for ( i = 0; i < 3; ++i )
BearoffClose( apbcHyper[ i ] );
/* destroy neural nets */
DestroyWeights();
/* destroy cache */
CacheDestroy( &cEval );
CacheDestroy( &cpEval );
return 0;
}
static int binary_weights_failed(char * filename, FILE * weights)
{
float r;
if (!weights)
{
g_print("%s", filename);
return -1;
}
if (fread(&r, sizeof r, 1, weights) < 1) {
g_print("%s", filename);
return -2;
}
if (r != WEIGHTS_MAGIC_BINARY) {
g_print(_("%s is not a weights file"), filename);
g_print("\n");
return -3;
}
if (fread(&r, sizeof r, 1, weights) < 1) {
g_print("%s", filename);
return -4;
}
if (r != WEIGHTS_VERSION_BINARY)
{
char buf[20];
sprintf(buf, "%.2f", r);
g_print(_("weights file %s, has incorrect version (%s), expected (%s)"),
filename, buf, WEIGHTS_VERSION);
g_print("\n");
return -5;
}
return 0;
}
static int weights_failed(char * filename, FILE * weights)
{
char file_version[16];
if (!weights)
{
g_print("%s", filename);
return -1;
}
if( fscanf( weights, "GNU Backgammon %15s\n",
file_version ) != 1 )
{
g_print(_("%s is not a weights file"), filename);
g_print("\n");
return -2;
}
if( strcmp( file_version, WEIGHTS_VERSION ) )
{
g_print(_("weights file %s, has incorrect version (%s), expected (%s)"),
filename, file_version, WEIGHTS_VERSION);
g_print("\n");
return -3;
}
return 0;
}
extern void EvalInitialise(char *szWeights, char *szWeightsBinary,
int fNoBearoff, void (*pfProgress) (unsigned int))
{
FILE *pfWeights = NULL;
int i, fReadWeights = FALSE;
static int fInitialised = FALSE;
char *gnubg_bearoff;
char *gnubg_bearoff_os;
if (!fInitialised) {
#if USE_SSE_VECTORIZE
if (!SSE_Supported())
g_critical(_("This version of GNU Backgammon is compiled with SSE support but this machine does not support SSE"));
#endif
cCache = 0x1 << CACHE_SIZE_DEFAULT;
if( CacheCreate( &cEval, cCache ) )
{
PrintError( "CacheCreate" );
return;
}
if( CacheCreate( &cpEval, 0x1 << 16) )
{
PrintError( "CacheCreate" );
return;
}
ComputeTable();
rc.randrsl[ 0 ] = (ub4)time( NULL );
for( i = 0; i < RANDSIZ; i++ )
rc.randrsl[ i ] = rc.randrsl[ 0 ];
irandinit( &rc, TRUE );
fInitialised = TRUE;
}
if( ! fNoBearoff )
{
#ifdef USE_BUILTIN_BEAROFF
/* read one-sided db from gnubg.bd */
pbc1 = BearoffInitBuiltin();
#endif
gnubg_bearoff_os = BuildFilename("gnubg_os0.bd");
if( !pbc1 )
pbc1 = BearoffInit( gnubg_bearoff_os, (int)BO_IN_MEMORY, NULL );
g_free(gnubg_bearoff_os);
if( !pbc1 )
pbc1 = BearoffInit ( NULL, BO_HEURISTIC, pfProgress );
/* read two-sided db from gnubg.bd */
gnubg_bearoff = BuildFilename("gnubg_ts0.bd");
pbc2 = BearoffInit ( gnubg_bearoff, BO_IN_MEMORY | BO_MUST_BE_TWO_SIDED, NULL );
g_free(gnubg_bearoff);
if ( ! pbc2 )
fprintf ( stderr,
"\n***WARNING***\n\n"
"Note that GNU Backgammon does not use the gnubg.bd file.\n"
"You should obtain the file gnubg_ts0.bd or generate\n"
"it yourself using the program 'makebearoff'.\n"
"You can generate the file with the command:\n"
"makebearoff -t 6x6 -f gnubg_ts0.bd\n"
"You can also generate other bearoff databases; see\n"
"README for more details\n\n" );
gnubg_bearoff_os = BuildFilename("gnubg_os.bd");
/* init one-sided db */
pbcOS = BearoffInit ( gnubg_bearoff_os, BO_IN_MEMORY, NULL );
g_free(gnubg_bearoff_os);
gnubg_bearoff = BuildFilename("gnubg_ts.bd");
/* init two-sided db */
pbcTS = BearoffInit ( gnubg_bearoff, BO_IN_MEMORY, NULL );
g_free(gnubg_bearoff);
/* hyper-gammon databases */
for (i = 0; i < 3; ++i) {
char *fn;
char sz[10];
sprintf(sz, "hyper%1d.bd", i + 1);
fn = BuildFilename(sz);
apbcHyper[i] = BearoffInit(fn, BO_NONE, NULL);
g_free(fn);
}
}
if( szWeightsBinary)
{
pfWeights = g_fopen(szWeightsBinary, "rb");
if (!binary_weights_failed(szWeightsBinary, pfWeights))
{
if( !fReadWeights && !( fReadWeights =
!NeuralNetLoadBinary(&nnContact, pfWeights ) &&
!NeuralNetLoadBinary(&nnRace, pfWeights ) &&
!NeuralNetLoadBinary(&nnCrashed, pfWeights ) &&
!NeuralNetLoadBinary(&nnpContact, pfWeights ) &&
!NeuralNetLoadBinary(&nnpCrashed, pfWeights ) &&
!NeuralNetLoadBinary(&nnpRace, pfWeights ) ) ) {
perror( szWeightsBinary );
}
}
if (pfWeights)
fclose( pfWeights );
pfWeights = NULL;
}
if( !fReadWeights && szWeights )
{
pfWeights = g_fopen(szWeights, "r");
if (!weights_failed(szWeights, pfWeights))
{
setlocale (LC_ALL, "C");
if( !( fReadWeights =
!NeuralNetLoad( &nnContact, pfWeights ) &&
!NeuralNetLoad( &nnRace, pfWeights ) &&
!NeuralNetLoad( &nnCrashed, pfWeights ) &&
!NeuralNetLoad( &nnpContact, pfWeights ) &&
!NeuralNetLoad( &nnpCrashed, pfWeights ) &&
!NeuralNetLoad( &nnpRace, pfWeights )
) )
perror( szWeights );
setlocale (LC_ALL, "");
}
if (pfWeights)
fclose( pfWeights );
pfWeights = NULL;
}
g_assert(fReadWeights);
g_assert(nnContact.cInput == NUM_INPUTS
&& nnContact.cOutput == NUM_OUTPUTS);
g_assert(nnCrashed.cInput == NUM_INPUTS
&& nnCrashed.cOutput == NUM_OUTPUTS);
g_assert(nnRace.cInput == NUM_RACE_INPUTS
&& nnRace.cOutput == NUM_OUTPUTS);
g_assert(nnpContact.cInput == NUM_PRUNING_INPUTS
&& nnpContact.cOutput == NUM_OUTPUTS);
g_assert(nnpCrashed.cInput == NUM_PRUNING_INPUTS
&& nnpCrashed.cOutput == NUM_OUTPUTS);
g_assert(nnpRace.cInput == NUM_PRUNING_INPUTS
&& nnpRace.cOutput == NUM_OUTPUTS);
{
int j;
for (j = 0; j < MAX_NUMTHREADS; j++)
{
nnStatesStorage[j][CLASS_RACE - CLASS_RACE].savedBase = malloc( nnRace.cHidden * sizeof( float ) );
nnStatesStorage[j][CLASS_RACE - CLASS_RACE].savedIBase = malloc( nnRace.cInput * sizeof( float ) );
nnStatesStorage[j][CLASS_CRASHED - CLASS_RACE].savedBase = malloc( nnCrashed.cHidden * sizeof( float ) );
nnStatesStorage[j][CLASS_CRASHED - CLASS_RACE].savedIBase = malloc( nnCrashed.cInput * sizeof( float ) );
nnStatesStorage[j][CLASS_CONTACT - CLASS_RACE].savedBase = malloc( nnContact.cHidden * sizeof( float ) );
nnStatesStorage[j][CLASS_CONTACT - CLASS_RACE].savedIBase = malloc( nnContact.cInput * sizeof( float ) );
}
}
}
/* Calculates inputs for any contact position, for one player only. */
static void
CalculateHalfInputs( const unsigned int anBoard[ 25 ], const unsigned int anBoardOpp[ 25 ], float afInput[] )
{
int i, j, k, l, nOppBack, n, aHit[ 39 ], nBoard;
/* aanCombination[n] -
How many ways to hit from a distance of n pips.
Each number is an index into aIntermediate below.
*/
static const int aanCombination[ 24 ][ 5 ] = {
{ 0, -1, -1, -1, -1 }, /* 1 */
{ 1, 2, -1, -1, -1 }, /* 2 */
{ 3, 4, 5, -1, -1 }, /* 3 */
{ 6, 7, 8, 9, -1 }, /* 4 */
{ 10, 11, 12, -1, -1 }, /* 5 */
{ 13, 14, 15, 16, 17 }, /* 6 */
{ 18, 19, 20, -1, -1 }, /* 7 */
{ 21, 22, 23, 24, -1 }, /* 8 */
{ 25, 26, 27, -1, -1 }, /* 9 */
{ 28, 29, -1, -1, -1 }, /* 10 */
{ 30, -1, -1, -1, -1 }, /* 11 */
{ 31, 32, 33, -1, -1 }, /* 12 */
{ -1, -1, -1, -1, -1 }, /* 13 */
{ -1, -1, -1, -1, -1 }, /* 14 */
{ 34, -1, -1, -1, -1 }, /* 15 */
{ 35, -1, -1, -1, -1 }, /* 16 */
{ -1, -1, -1, -1, -1 }, /* 17 */
{ 36, -1, -1, -1, -1 }, /* 18 */
{ -1, -1, -1, -1, -1 }, /* 19 */
{ 37, -1, -1, -1, -1 }, /* 20 */
{ -1, -1, -1, -1, -1 }, /* 21 */
{ -1, -1, -1, -1, -1 }, /* 22 */
{ -1, -1, -1, -1, -1 }, /* 23 */
{ 38, -1, -1, -1, -1 } /* 24 */
};
/* One way to hit */
typedef struct _Inter {
/* if true, all intermediate points (if any) are required;
if false, one of two intermediate points are required.
Set to true for a direct hit, but that can be checked with
nFaces == 1,
*/
int fAll;
/* Intermediate points required */
int anIntermediate[ 3 ];
/* Number of faces used in hit (1 to 4) */
int nFaces;
/* Number of pips used to hit */
int nPips;
} Inter;
const Inter *pi;
/* All ways to hit */
static const Inter aIntermediate[ 39 ] = {
{ 1, { 0, 0, 0 }, 1, 1 }, /* 0: 1x hits 1 */
{ 1, { 0, 0, 0 }, 1, 2 }, /* 1: 2x hits 2 */
{ 1, { 1, 0, 0 }, 2, 2 }, /* 2: 11 hits 2 */
{ 1, { 0, 0, 0 }, 1, 3 }, /* 3: 3x hits 3 */
{ 0, { 1, 2, 0 }, 2, 3 }, /* 4: 21 hits 3 */
{ 1, { 1, 2, 0 }, 3, 3 }, /* 5: 11 hits 3 */
{ 1, { 0, 0, 0 }, 1, 4 }, /* 6: 4x hits 4 */
{ 0, { 1, 3, 0 }, 2, 4 }, /* 7: 31 hits 4 */
{ 1, { 2, 0, 0 }, 2, 4 }, /* 8: 22 hits 4 */
{ 1, { 1, 2, 3 }, 4, 4 }, /* 9: 11 hits 4 */
{ 1, { 0, 0, 0 }, 1, 5 }, /* 10: 5x hits 5 */
{ 0, { 1, 4, 0 }, 2, 5 }, /* 11: 41 hits 5 */
{ 0, { 2, 3, 0 }, 2, 5 }, /* 12: 32 hits 5 */
{ 1, { 0, 0, 0 }, 1, 6 }, /* 13: 6x hits 6 */
{ 0, { 1, 5, 0 }, 2, 6 }, /* 14: 51 hits 6 */
{ 0, { 2, 4, 0 }, 2, 6 }, /* 15: 42 hits 6 */
{ 1, { 3, 0, 0 }, 2, 6 }, /* 16: 33 hits 6 */
{ 1, { 2, 4, 0 }, 3, 6 }, /* 17: 22 hits 6 */
{ 0, { 1, 6, 0 }, 2, 7 }, /* 18: 61 hits 7 */
{ 0, { 2, 5, 0 }, 2, 7 }, /* 19: 52 hits 7 */
{ 0, { 3, 4, 0 }, 2, 7 }, /* 20: 43 hits 7 */
{ 0, { 2, 6, 0 }, 2, 8 }, /* 21: 62 hits 8 */
{ 0, { 3, 5, 0 }, 2, 8 }, /* 22: 53 hits 8 */
{ 1, { 4, 0, 0 }, 2, 8 }, /* 23: 44 hits 8 */
{ 1, { 2, 4, 6 }, 4, 8 }, /* 24: 22 hits 8 */
{ 0, { 3, 6, 0 }, 2, 9 }, /* 25: 63 hits 9 */
{ 0, { 4, 5, 0 }, 2, 9 }, /* 26: 54 hits 9 */
{ 1, { 3, 6, 0 }, 3, 9 }, /* 27: 33 hits 9 */
{ 0, { 4, 6, 0 }, 2, 10 }, /* 28: 64 hits 10 */
{ 1, { 5, 0, 0 }, 2, 10 }, /* 29: 55 hits 10 */
{ 0, { 5, 6, 0 }, 2, 11 }, /* 30: 65 hits 11 */
{ 1, { 6, 0, 0 }, 2, 12 }, /* 31: 66 hits 12 */
{ 1, { 4, 8, 0 }, 3, 12 }, /* 32: 44 hits 12 */
{ 1, { 3, 6, 9 }, 4, 12 }, /* 33: 33 hits 12 */
{ 1, { 5, 10, 0 }, 3, 15 }, /* 34: 55 hits 15 */
{ 1, { 4, 8, 12 }, 4, 16 }, /* 35: 44 hits 16 */
{ 1, { 6, 12, 0 }, 3, 18 }, /* 36: 66 hits 18 */
{ 1, { 5, 10, 15 }, 4, 20 }, /* 37: 55 hits 20 */
{ 1, { 6, 12, 18 }, 4, 24 } /* 38: 66 hits 24 */
};
/* aaRoll[n] - All ways to hit with the n'th roll
Each entry is an index into aIntermediate above.
*/
static const int aaRoll[ 21 ][ 4 ] = {
{ 0, 2, 5, 9 }, /* 11 */
{ 0, 1, 4, -1 }, /* 21 */
{ 1, 8, 17, 24 }, /* 22 */
{ 0, 3, 7, -1 }, /* 31 */
{ 1, 3, 12, -1 }, /* 32 */
{ 3, 16, 27, 33 }, /* 33 */
{ 0, 6, 11, -1 }, /* 41 */
{ 1, 6, 15, -1 }, /* 42 */
{ 3, 6, 20, -1 }, /* 43 */
{ 6, 23, 32, 35 }, /* 44 */
{ 0, 10, 14, -1 }, /* 51 */
{ 1, 10, 19, -1 }, /* 52 */
{ 3, 10, 22, -1 }, /* 53 */
{ 6, 10, 26, -1 }, /* 54 */
{ 10, 29, 34, 37 }, /* 55 */
{ 0, 13, 18, -1 }, /* 61 */
{ 1, 13, 21, -1 }, /* 62 */
{ 3, 13, 25, -1 }, /* 63 */
{ 6, 13, 28, -1 }, /* 64 */
{ 10, 13, 30, -1 }, /* 65 */
{ 13, 31, 36, 38 } /* 66 */
};
/* One roll stat */
struct {
/* count of pips this roll hits */
int nPips;
/* number of chequers this roll hits */
int nChequers;
} aRoll[ 21 ];
for(nOppBack = 24; nOppBack >= 0; --nOppBack) {
if( anBoardOpp[nOppBack] ) {
break;
}
}
nOppBack = 23 - nOppBack;
n = 0;
for( i = nOppBack + 1; i < 25; i++ )
if( anBoard[ i ] )
n += ( i + 1 - nOppBack ) * anBoard[ i ];
g_assert( n );
afInput[ I_BREAK_CONTACT ] = n / (15 + 152.0f);
{
unsigned int p = 0;
for( i = 0; i < nOppBack; i++ ) {
if( anBoard[i] )
p += (i+1) * anBoard[i];
}
afInput[I_FREEPIP] = p / 100.0f;
}
{
int t = 0;
int no = 0;
t += 24 * anBoard[24];
no += anBoard[24];
for( i = 23; i >= 12 && i > nOppBack; --i ) {
if( anBoard[i] && anBoard[i] != 2 ) {
int n = ((anBoard[i] > 2) ? (anBoard[i] - 2) : 1);
no += n;
t += i * n;
}
}
for( ; i >= 6; --i ) {
if( anBoard[i] ) {
int n = anBoard[i];
no += n;
t += i * n;
}
}
for( i = 5; i >= 0; --i ) {
if( anBoard[i] > 2 ) {
t += i * (anBoard[i] - 2);
no += (anBoard[i] - 2);
} else if( anBoard[i] < 2 ) {
int n = (2 - anBoard[i]);
if( no >= n ) {
t -= i * n;
no -= n;
}
}
}
if( t < 0 ) {
t = 0;
}
afInput[ I_TIMING ] = t / 100.0f;
}
/* Back chequer */
{
int nBack;
for( nBack = 24; nBack >= 0; --nBack ) {
if( anBoard[nBack] ) {
break;
}
}
afInput[ I_BACK_CHEQUER ] = nBack / 24.0f;
/* Back anchor */
for( i = nBack == 24 ? 23 : nBack; i >= 0; --i ) {
if( anBoard[i] >= 2 ) {
break;
}
}