forked from quackle/quackle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.h
458 lines (361 loc) · 11.9 KB
/
sim.h
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
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2019 Jason Katz-Brown, John O'Laughlin, and John Fultz.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef QUACKLE_SIM_H
#define QUACKLE_SIM_H
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <sstream>
#include <thread>
#include <vector>
#include "alphabetparameters.h"
#include "game.h"
namespace Quackle
{
class ComputerDispatch;
struct AveragedValue
{
// new zeroed value
AveragedValue()
: m_valueSum(0), m_squaredValueSum(0), m_incorporatedValues(0)
{
}
void incorporateValue(double newValue);
// zero everything
void clear();
long double valueSum() const;
long double squaredValueSum() const;
long int incorporatedValues() const;
// whether or not incorporatedValues is greater than zero
bool hasValues() const;
// valueSum() / incorporatedValues() or zero
// if there have been no incorporated values
double averagedValue() const;
// sqrt((incorporatedValues() * squaredValueSum() - valueSum()^2) / (incorporatedValues() * (incorporatedValues() - 1)))
double standardDeviation() const;
private:
long double m_valueSum;
long double m_squaredValueSum;
long int m_incorporatedValues;
};
inline void AveragedValue::incorporateValue(double newValue)
{
m_valueSum += newValue;
m_squaredValueSum += newValue * newValue;
++m_incorporatedValues;
}
inline long double AveragedValue::valueSum() const
{
return m_valueSum;
}
inline long double AveragedValue::squaredValueSum() const
{
return m_squaredValueSum;
}
inline double AveragedValue::averagedValue() const
{
return m_incorporatedValues == 0? 0 : (m_valueSum / m_incorporatedValues);
}
inline long int AveragedValue::incorporatedValues() const
{
return m_incorporatedValues;
}
inline bool AveragedValue::hasValues() const
{
return m_incorporatedValues > 0;
}
struct PositionStatistics
{
enum StatisticType { StatisticScore, StatisticBingos };
AveragedValue getStatistic(StatisticType type) const;
AveragedValue score;
AveragedValue bingos;
};
typedef vector<PositionStatistics> PositionStatisticsList;
struct Level
{
// expand the scores list to be at least number long
void setNumberScores(unsigned int number);
PositionStatisticsList statistics;
};
class LevelList : public vector<Level>
{
public:
// expand the levels list to be at least number long
void setNumberLevels(unsigned int number);
};
struct SimmedMove
{
SimmedMove(const Move &_move) :
move(_move),
m_id(++objectIdCounter),
m_includeInSimulation(true) { }
// + our scores - their scores + residual, except if we have no levels,
// in which case returns move.equity
double calculateEquity() const;
// average wins value * 100, except if we have no win percentage data,
// in which case returns move.win
double calculateWinPercentage() const;
// clear all level values
void clear();
bool includeInSimulation() const;
void setIncludeInSimulation(bool includeInSimulation);
long id() const { return m_id; };
Move move;
LevelList levels;
AveragedValue residual;
AveragedValue gameSpread;
AveragedValue wins;
PositionStatistics getPositionStatistics(int level, int playerIndex) const;
private:
long m_id;
bool m_includeInSimulation;
static std::atomic_long objectIdCounter;
};
inline bool SimmedMove::includeInSimulation() const
{
return m_includeInSimulation;
}
inline void SimmedMove::setIncludeInSimulation(bool includeInSimulation)
{
m_includeInSimulation = includeInSimulation;
}
typedef vector<SimmedMove> SimmedMoveList;
class SimmedMoveMessage
{
public:
long id;
Move move;
LevelList levels;
vector<double> score;
vector<double> bingos;
double residual;
double gameSpread;
double wins;
bool bogowin;
std::ostringstream logStream;
UVString xmlIndent;
};
class SimmedMoveConstants
{
public:
Game game;
int startPlayerId;
int playerCount;
int decimalTurns;
int levelCount;
bool ignoreOppos;
bool isLogging;
};
class SimmedMoveMessageQueue
{
public:
SimmedMoveMessageQueue() = default;
SimmedMoveMessageQueue(SimmedMoveMessageQueue&) = delete;
SimmedMoveMessageQueue(SimmedMoveMessageQueue&&) = delete;
void push(SimmedMoveMessage& msg);
SimmedMoveMessage pop();
std::pair<SimmedMoveMessage, bool> pop_or_terminate();
void send_terminate_all();
void send_terminate_one(const std::thread::id& id);
const SimmedMoveConstants& constants() { return m_constants; };
void setConstants(const SimmedMoveConstants& constants) { m_constants = constants; };
private:
SimmedMoveConstants m_constants;
std::queue<SimmedMoveMessage> m_queue;
std::condition_variable m_condition;
std::mutex m_queueMutex;
bool m_terminateAll = false;
std::thread::id m_terminateOne;
};
class Simulator
{
public:
// constructs a new simulator
Simulator();
Simulator(const Simulator&) = delete;
Simulator(Simulator&&) = delete;
~Simulator();
// Simulate moves on this position. Also initializes the
// move list, rack, resets numbers, and closes logfile.
void setPosition(const GamePosition &position);
// get access to the position that starts each playahead of the
// simulation; use to rechange rack or scores etcetera
GamePosition ¤tPosition();
const GamePosition ¤tPosition() const;
const History &history() const;
// If logfile is an empty string, logging is disabled.
// If logfile is the same logfile as currently set, nothing
// happens. If it is different, old logfile is closed if it
// was open. If append is false, this destroys file contents
// already in logfile.
void setLogfile(const string &logfile, bool append = true);
string logfile() const;
// Will honor dispatch->shouldAbort() but won't signal
// any doneness for now.
void setDispatch(ComputerDispatch *dispatch);
ComputerDispatch *dispatch() const;
// append message to logfile if one is open
void logMessage(const UVString &message);
bool isLogging() const;
void closeLogfile();
// Set moves to include in simulation. Moves that
// are in the simmed list now that are not in this given
// list still live in the simmed list but are not iterated thru
// during simulation. Moves that were not in the simmed list are added
// to front of simmed move list and level values zeroed.
void setIncludedMoves(const MoveList &moves);
void makeSureConsideredMovesAreIncluded();
void moveConsideredMovesToBeginning(MoveList &moves) const;
// moves that are immune from pruning
void setConsideredMoves(const MoveList &moves);
const MoveList &consideredMoves() const;
void addConsideredMove(const Move &move);
bool isConsideredMove(const Move &move) const;
// include only currently included moves that are within
// equityThreshold points below the best play and cap at
// maxNumberOfMoves
void pruneTo(double equityThreshold, int maxNumberOfMoves);
// if ignore is true, oppos will always pass in simulation
void setIgnoreOppos(bool ignore);
bool ignoreOppos() const;
static void simThreadFunc(SimmedMoveMessageQueue& incoming, SimmedMoveMessageQueue& outgoing);
void setThreadCount(size_t count);
// set values for all levels of all moves to zero
void resetNumbers();
// Run a chunk of the simulation.
// If plies is negative, simulation runs to end of game.
// Iterations is how many iterations to run before returning;
// more iterations can be computed and incorporated by recalling
// simulate().
void simulate(int plies, int iterations);
// simulate one iteration
void simulate(int plies);
static void simulateOnePosition(SimmedMoveMessage &message, const SimmedMoveConstants &constants);
// Incoporate the results of a single simulation into the
// cumulative results
void incorporateMessage(const SimmedMoveMessage &message);
// Set oppo's rack to some partially-known tiles.
// Set this to an empty rack if no tiles are known, so
// that all tiles are chosen randomly each iteration.
void setPartialOppoRack(const Rack &rack);
const Rack &partialOppoRack() const;
// Set oppo's racks to something random, including
// tiles specified by setPartialOppoRack above.
// Possibly inference-aided randomness.
void randomizeOppoRacks();
// set drawing order for the first refill
void randomizeDrawingOrder();
// returns maximal number of iterations over all moves since
// resetting numbers
int iterations() const;
// returns true if any iterations have been run
bool hasSimulationResults() const;
// full simulation information
const SimmedMoveList &simmedMoves() const;
// Return the moves sorted by simulated equity.
// If prune is true, does not include plays that aren't included
// in simulation anymore.
MoveList moves(bool prune = false, bool byWin = false) const;
const SimmedMove &simmedMoveForMove(const Move &move) const;
int numLevels() const;
int numPlayersAtLevel(int levelIndex) const;
protected:
void writeLogHeader();
void writeLogFooter();
UVOFStream m_logfileStream;
string m_logfile;
bool m_logfileIsOpen;
bool m_hasHeader;
UVString m_xmlIndent;
Rack m_partialOppoRack;
Game m_originalGame;
ComputerDispatch *m_dispatch;
SimmedMoveList m_simmedMoves;
// moves that are immune from pruning
MoveList m_consideredMoves;
int m_iterations;
bool m_ignoreOppos;
// Pair of thread and bool requesting to terminate
std::vector<std::thread> m_threadPool;
SimmedMoveMessageQueue m_sendQueue;
SimmedMoveMessageQueue m_receiveQueue;
};
inline GamePosition &Simulator::currentPosition()
{
return m_originalGame.currentPosition();
}
inline const GamePosition &Simulator::currentPosition() const
{
return m_originalGame.currentPosition();
}
inline const History &Simulator::history() const
{
return m_originalGame.history();
}
inline ComputerDispatch *Simulator::dispatch() const
{
return m_dispatch;
}
inline string Simulator::logfile() const
{
return m_logfile;
}
inline bool Simulator::isLogging() const
{
return m_logfileIsOpen;
}
inline const Rack &Simulator::partialOppoRack() const
{
return m_partialOppoRack;
}
inline void Simulator::setConsideredMoves(const MoveList &moves)
{
m_consideredMoves = moves;
}
inline const MoveList &Simulator::consideredMoves() const
{
return m_consideredMoves;
}
inline void Simulator::setIgnoreOppos(bool ignore)
{
m_ignoreOppos = ignore;
}
inline bool Simulator::ignoreOppos() const
{
return m_ignoreOppos;
}
inline int Simulator::iterations() const
{
return m_iterations;
}
inline bool Simulator::hasSimulationResults() const
{
return m_iterations > 0;
}
inline const SimmedMoveList &Simulator::simmedMoves() const
{
return m_simmedMoves;
}
}
UVOStream& operator<<(UVOStream& o, const Quackle::AveragedValue &value);
UVOStream& operator<<(UVOStream& o, const Quackle::Level &level);
UVOStream& operator<<(UVOStream& o, const Quackle::SimmedMove &move);
UVOStream& operator<<(UVOStream& o, const Quackle::SimmedMoveList &move);
#endif