1 /*
2  * rollout.h
3  *
4  * by Gary Wong <gary@cs.arizona.edu>, 1999.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of version 3 or later of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * $Id: rollout.h,v 1.42 2018/05/20 16:32:58 plm Exp $
20  */
21 
22 #ifndef ROLLOUT_H
23 #define ROLLOUT_H
24 
25 #define MAXHIT 50               /* for statistics */
26 #define STAT_MAXCUBE 10
27 
28 typedef struct _rolloutstat {
29 
30     /* Regular win statistics (dimension is cube turns) */
31 
32     int acWin[STAT_MAXCUBE];
33     int acWinGammon[STAT_MAXCUBE];
34     int acWinBackgammon[STAT_MAXCUBE];
35 
36     /* Cube statistics (dimension is cube turns) */
37 
38     int acDoubleDrop[STAT_MAXCUBE];     /* # of Double, drop */
39     int acDoubleTake[STAT_MAXCUBE];     /* # of Double, takes */
40 
41     /* Chequer hit statistics (dimension is move number) */
42 
43     /* Opponent closed out */
44 
45     int nOpponentHit;
46     int rOpponentHitMove;
47 
48     /* Average loss of pips in bear-off */
49 
50     int nBearoffMoves;          /* number of moves with bearoff */
51     int nBearoffPipsLost;       /* number of pips lost in these moves */
52 
53     /* Opponent closed out */
54 
55     int nOpponentClosedOut;
56     int rOpponentClosedOutMove;
57 
58     /* FIXME: add more stuff */
59 
60 } rolloutstat;
61 
62 typedef void
63  (rolloutprogressfunc) (float arOutput[][NUM_ROLLOUT_OUTPUTS],
64                         float arStdDev[][NUM_ROLLOUT_OUTPUTS],
65                         const rolloutcontext * prc,
66                         const cubeinfo aci[],
67                         unsigned int initial_game_count,
68                         const int iGame,
69                         const int iAlternative,
70                         const int nRank,
71                         const float rJsd, const int fStopped, const int fShowRanks, int fCubeRollout, void *pUserData);
72 
73 extern int
74 RolloutGeneral(ConstTanBoard * apBoard,
75                float (*apOutput[])[NUM_ROLLOUT_OUTPUTS],
76                float (*apStdDev[])[NUM_ROLLOUT_OUTPUTS],
77                rolloutstat apStatistics[][2],
78                evalsetup(*apes[]),
79                const cubeinfo(*apci[]),
80                int (*apCubeDecTop[]), int alternatives,
81                int fInvert, int fCubeRollout, rolloutprogressfunc * pfRolloutProgress, void *pUserData);
82 
83 extern int
84 GeneralEvaluation(float arOutput[NUM_ROLLOUT_OUTPUTS],
85                   float arStdDev[NUM_ROLLOUT_OUTPUTS],
86                   rolloutstat arsStatistics[2],
87                   TanBoard anBoard,
88                   cubeinfo * const pci, const evalsetup * pes,
89                   rolloutprogressfunc * pfRolloutProgress, void *pUserData);
90 
91 extern int
92 GeneralEvaluationR(float arOutput[NUM_ROLLOUT_OUTPUTS],
93                    float arStdDev[NUM_ROLLOUT_OUTPUTS],
94                    rolloutstat arsStatistics[2],
95                    const TanBoard anBoard,
96                    const cubeinfo * pci, const rolloutcontext * prc,
97                    rolloutprogressfunc * pfRolloutProgress, void *pUserData);
98 
99 extern int
100 GeneralCubeDecision(float aarOutput[2][NUM_ROLLOUT_OUTPUTS],
101                     float aarStdDev[2][NUM_ROLLOUT_OUTPUTS],
102                     rolloutstat aarsStatistics[2][2],
103                     const TanBoard anBoard,
104                     cubeinfo * pci, evalsetup * pes, rolloutprogressfunc * pfRolloutProgress, void *pUserData);
105 
106 
107 extern int
108 GeneralCubeDecisionR(float aarOutput[2][NUM_ROLLOUT_OUTPUTS],
109                      float aarStdDev[2][NUM_ROLLOUT_OUTPUTS],
110                      rolloutstat aarsStatistics[2][2],
111                      const TanBoard anBoard,
112                      cubeinfo * pci, rolloutcontext * prc, evalsetup * pes,
113                      rolloutprogressfunc * pfRolloutProgress, void *pUserData);
114 
115 /* operations on rolloutstat */
116 
117 /* Resignations */
118 
119 extern int
120 getResignation(float arResign[NUM_ROLLOUT_OUTPUTS],
121                TanBoard anBoard, cubeinfo * const pci, const evalsetup * pesResign);
122 
123 extern void
124  getResignEquities(float arResign[NUM_ROLLOUT_OUTPUTS], cubeinfo * pci, int nResigned, float *prBefore, float *prAfter);
125 
126 extern int
127 ScoreMoveRollout(move ** ppm, cubeinfo ** ppci, int cMoves,
128                  rolloutprogressfunc * pfRolloutProgress, void *pUserData);
129 
130 extern void RolloutLoopMT(void *unused);
131 
132 /* Quasi-random permutation array: the first index is the "generation" of the
133  * permutation (0 permutes each set of 36 rolls, 1 permutes those sets of 36
134  * into 1296, etc.); the second is the roll within the game (limited to QRLEN,
135  * so we use pseudo-random dice after that); the last is the permutation
136  * itself.  6 generations are enough for 36^6 > 2^31 trials. */
137 #define QRLEN 128
138 typedef struct _perArray {
139     unsigned char aaanPermutation[6][QRLEN][36];
140     int nPermutationSeed;
141 } perArray;
142 
143 EXP_LOCK_FUN(int, BasicCubefulRollout, unsigned int aanBoard[][2][25], float aarOutput[][NUM_ROLLOUT_OUTPUTS],
144              int iTurn, int iGame, const cubeinfo aci[], int afCubeDecTop[], unsigned int cci, rolloutcontext * prc,
145              rolloutstat aarsStatistics[][2], int nBasisCube, perArray * dicePerms, rngcontext * rngctxRollout,
146              FILE * logfp);
147 
148 
149 extern void log_cube(FILE * logfp, const char *action, int side);
150 extern void log_move(FILE * logfp, const int *anMove, int side, int die0, int die1);
151 extern int RolloutDice(int iTurn, int iGame, int fInitial, unsigned int anDice[2], rng * rngx, void *rngctx,
152                        const int fRotate, const perArray * dicePerms);
153 extern void ClosedBoard(int afClosedBoard[2], const TanBoard anBoard);
154 #endif
155