-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathABstats.cpp
347 lines (262 loc) · 7.57 KB
/
ABstats.cpp
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
/*
DDS, a bridge double dummy solver.
Copyright (C) 2006-2014 by Bo Haglund /
2014-2018 by Bo Haglund & Soren Hein.
See LICENSE and README.
*/
/*
ABstats is a simple object for AB statistics and return values.
*/
#include <iomanip>
#include "ABstats.h"
ABstats::ABstats()
{
ABstats::Reset();
ABstats::SetNames();
}
void ABstats::Reset()
{
for (int depth = 0; depth < DDS_MAXDEPTH; depth++)
ABnodes.list[depth] = 0;
ABnodes.sum = 0;
ABnodes.sumWeighted = 0;
for (int side = 0; side < 2; side++)
{
for (int depth = 0; depth < DDS_MAXDEPTH; depth++)
ABsides[side].list[depth] = 0;
ABsides[side].sum = 0;
ABsides[side].sumWeighted = 0;
}
for (int place = 0; place < AB_SIZE; place++)
{
for (int depth = 0; depth < DDS_MAXDEPTH; depth++)
ABplaces[place].list[depth] = 0;
ABplaces[place].sum = 0;
ABplaces[place].sumWeighted = 0;
}
}
void ABstats::ResetCum()
{
ABnodesCum.sumCum = 0;
ABnodesCum.sumCumWeighted = 0;
for (int side = 0; side < 2; side++)
{
ABsides[side].sumCum = 0;
ABsides[side].sumCumWeighted = 0;
}
for (int place = 0; place < AB_SIZE; place++)
{
ABplaces[place].sumCum = 0;
ABplaces[place].sumCumWeighted = 0;
}
}
void ABstats::SetNames()
{
name[AB_TARGET_REACHED] = "Target decided";
name[AB_DEPTH_ZERO] = "depth == 0";
name[AB_QUICKTRICKS] = "QuickTricks";
name[AB_QUICKTRICKS_2ND] = "QuickTricks 2nd";
name[AB_LATERTRICKS] = "LaterTricks";
name[AB_MAIN_LOOKUP] = "Main lookup";
name[AB_SIDE_LOOKUP] = "Other lookup";
name[AB_MOVE_LOOP] = "Move trial";
}
void ABstats::IncrPos(
const ABCountType no,
const bool side,
const int depth)
{
if (no < 0 || no >= AB_SIZE)
return;
ABplaces[no].list[depth]++;
ABplaces[no].sum++;
ABplaces[no].sumWeighted += depth;
ABplaces[no].sumCum++;
ABplaces[no].sumCumWeighted += depth;
const int iside = (side ? 1 : 0);
ABsides[iside].list[depth]++;
ABsides[iside].sum++;
ABsides[iside].sumWeighted += depth;
ABsides[iside].sumCum++;
ABsides[iside].sumCumWeighted += depth;
}
void ABstats::IncrNode(const int depth)
{
ABnodes.list[depth]++;
ABnodes.sum++;
ABnodes.sumWeighted += depth;
ABnodesCum.list[depth]++;
ABnodesCum.sumCum++;
ABnodesCum.sumCumWeighted += depth;
}
int ABstats::GetNodes() const
{
return ABnodes.sum;
}
void ABstats::PrintHeaderPosition(ofstream& fout) const
{
fout << "No " <<
setw(20) << left << "Return" <<
setw(9) << right << "Count" <<
setw(6) << "%" <<
setw(6) << "d_avg" <<
setw(9) << "Cumul" <<
setw(6) << "%" <<
setw(6) << "d_avg" << "\n";
fout << string(65, '-') << "\n";
}
void ABstats::PrintStatsPosition(
ofstream& fout,
const int no,
const string& text,
const ABtracker& abt,
const ABtracker& divisor) const
{
if (! abt.sumCum)
return;
fout << setw(2) << (no == -1 ? "" : to_string(no)) << " " <<
setw(20) << left << text <<
setw(9) << right << abt.sum <<
setw(6) << setprecision(1) << fixed <<
100. * abt.sum / static_cast<double>(divisor.sum);
if (abt.sum)
fout << setw(6) << setprecision(1) << fixed <<
abt.sumWeighted / static_cast<double>(abt.sum);
else
fout << setw(6) << "";
fout << setw(9) << abt.sumCum <<
setw(6) << setprecision(1) << fixed <<
100. * abt.sumCum / static_cast<double>(divisor.sumCum) <<
setw(6) << setprecision(1) << fixed <<
abt.sumCumWeighted / static_cast<double>(abt.sumCum) << "\n";
}
void ABstats::PrintHeaderDepth(ofstream& fout) const
{
fout << setw(5) << right << "Depth" <<
setw(7) << "Nodes" <<
setw(7) << "Cumul" <<
setw(6) << "Cum%" <<
setw(6) << "Cumc%" <<
setw(7) << "Branch" << "\n";
fout << string(38, '-') << "\n";
}
void ABstats::PrintStatsDepth(
ofstream& fout,
const int depth,
const int cum) const
{
fout << setw(5) << depth <<
setw(7) << ABnodes.list[depth] <<
setw(7) << ABnodesCum.list[depth] <<
setw(6) << setprecision(1) << fixed <<
100. * ABnodesCum.list[depth] /
static_cast<double>(ABnodesCum.sumCum) <<
setw(6) << setprecision(1) << fixed <<
100. * cum / static_cast<double>(ABnodesCum.sumCum);
// "Branching factor" from end of one trick to end of
// the previous trick.
if ((depth % 4 == 1) &&
(depth < DDS_MAXDEPTH - 4) &&
(ABnodesCum.list[depth + 4] > 0))
fout << setw(6) << setprecision(2) << fixed <<
ABnodesCum.list[depth] /
static_cast<double>(ABnodesCum.list[depth + 4]);
fout << "\n";
}
void ABstats::PrintAverageDepth(
ofstream& fout,
const ABtracker& ABsidesSum) const
{
fout << "\nTotal" <<
setw(7) << right << ABnodes.sum <<
setw(7) << ABnodesCum.sumCum << "\n";
if (! ABnodesCum.sumCum)
return;
fout << setw(5) << left << "Avg" << right;
if (ABnodes.sum)
fout << setw(7) << setprecision(1) << fixed <<
ABnodes.sumWeighted / static_cast<double>(ABnodes.sum);
else
fout << setw(7) << "";
fout << setw(7) << setprecision(1) << fixed <<
ABnodesCum.sumCumWeighted / static_cast<double>(ABnodesCum.sumCum) <<
"\n\n";
fout << setw(5) << left << "Nodes" <<
setw(7) << right << ABnodes.sum <<
setw(7) << ABnodesCum.sumCum << "\n";
fout << setw(5) << left << "Ends" <<
setw(7) << right << ABsidesSum.sum <<
setw(7) << ABsidesSum.sumCum << "\n";
if (ABsidesSum.sum)
fout << setw(5) << left << "Ratio" <<
setw(6) << right << setprecision(0) << fixed <<
100. * ABsidesSum.sum / static_cast<double>(ABnodes.sum) << "%" <<
setw(6) << setprecision(0) << fixed <<
100. * ABsidesSum.sumCum / static_cast<double>(ABnodesCum.sumCum) <<
"%\n\n";
}
void ABstats::PrintHeaderDetail(ofstream& fout) const
{
fout << " d" << setw(7) << "Side1" << setw(7) << "Side0";
for (int p = 0; p < AB_SIZE; p++)
fout << setw(6) << p;
fout << "\n" << string(65, '-') << "\n";
}
void ABstats::PrintStatsDetail(
ofstream& fout,
const int depth) const
{
if (ABsides[1].list[depth] == 0 && ABsides[0].list[depth] == 0)
return;
fout << setw(2) << depth <<
setw(7) << ABsides[1].list[depth] <<
setw(7) << ABsides[0].list[depth];
for (int p = 0; p < AB_SIZE; p++)
fout << setw(6) << ABplaces[p].list[depth];
fout << "\n";
}
void ABstats::PrintSumDetail(ofstream& fout) const
{
fout << string(65, '-') << "\n";
fout << setw(2) << "S" <<
setw(7) << ABsides[1].sum <<
setw(7) << ABsides[0].sum;
for (int p = 0; p < AB_SIZE; p++)
fout << setw(6) << ABplaces[p].sum;
fout << "\n\n";
}
void ABstats::PrintStats(ofstream& fout)
{
ABtracker ABsidesSum;
ABsidesSum.sum = ABsides[1].sum + ABsides[0].sum;
ABsidesSum.sumCum = ABsides[1].sumCum + ABsides[0].sumCum;
if (ABsidesSum.sum)
{
// First table: By side and position.
ABstats::PrintHeaderPosition(fout);
ABstats::PrintStatsPosition(fout, -1, "Side1", ABsides[1], ABsidesSum);
ABstats::PrintStatsPosition(fout, -1, "Side0", ABsides[0], ABsidesSum);
fout << "\n";
for (int p = 0; p < AB_SIZE; p++)
ABstats::PrintStatsPosition(fout, p, name[p], ABplaces[p], ABsidesSum);
fout << "\n";
}
ABstats::PrintHeaderDepth(fout);
// Second table: By depth.
int c = 0;
for (int d = DDS_MAXDEPTH - 1; d >= 0; d--)
{
if (ABnodesCum.list[d] == 0)
continue;
c += ABnodesCum.list[d];
ABstats::PrintStatsDepth(fout, d, c);
}
ABstats::PrintAverageDepth(fout, ABsidesSum);
#ifdef DDS_AB_DETAILS
// Third table: All the detail.
ABstats::PrintHeaderDetail(fout);
for (int d = DDS_MAXDEPTH - 1; d >= 0; d--)
ABstats::PrintStatsDetail(fout, d);
ABstats::PrintSumDetail(fout);
#endif
}