-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerComputer.cpp
323 lines (263 loc) · 8.38 KB
/
PlayerComputer.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
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include "core/MPCStats.h"
#include "core/Cache.h"
#include "core/options.h"
#include "core/CalcParams.h"
#include "Pos2.h"
#include "SearchParams.h"
#include "Search.h"
#include "Evaluator.h"
#include "PlayerComputer.h"
using namespace std;
////////////////////////////////////////
// CComputerDefaults
////////////////////////////////////////
CComputerDefaults::CComputerDefaults() : sCalcParams("s12"), cEval('J'), cCoeffSet('A')
, iPruneEndgame(5), iPruneMidgame(4), iEdmund(1) {
vContempts[0]=0;
vContempts[1]=0;
nRandShifts[0]=nRandShifts[1]=0;
fsPrint=5;
fsPrintOpponent=1;
}
int CComputerDefaults::MinutesOrDepth() const {
if (sCalcParams.empty())
return 0;
else
return atol(sCalcParams.c_str()+1);
}
CValue CComputerDefaults::FloatToContempt(double fContempt) {
CValue v=int(fContempt*kStoneValue);
if (v>kMaxBonus)
v=kMaxBonus;
else if (v<-kMaxBonus)
v=-kMaxBonus;
return -v;
}
////////////////////////////////////////
// CPlayerComputer
////////////////////////////////////////
CPlayerComputer::CPlayerComputer(const CComputerDefaults& acd) {
cd=acd;
pcp=CCalcParams::NewFromString(cd.sCalcParams);
caches[0]=caches[1]=NULL;
fHasCachedPos[0]=fHasCachedPos[1]=false;
eval=CEvaluator::FindEvaluator(cd.cEval, cd.cCoeffSet);
mpcs=CMPCStats::GetMPCStats(cd.cEval, cd.cCoeffSet, std::max(cd.iPruneMidgame, cd.iPruneEndgame));
fAnalyzingDeferred=false;
// get saved-game file name
std::ostringstream os;
os << fnBaseDir << "results/" << cd.cEval << cd.cCoeffSet << '_' << *pcp << ".ggf";
m_fnSaveGame=os.str();
toot=false;
// calculate computer's name
std::ostringstream osName;
pcp->Name(osName);
m_sName=osName.str();
if (!mpcs) cd.iPruneMidgame=cd.iPruneEndgame=0;
}
CPlayerComputer::~CPlayerComputer() {
int i;
for (i=0; i<2; i++)
if (caches[i])
delete caches[i];
delete mpcs;
delete pcp;
}
u4 CPlayerComputer::LogCacheSize(CCalcParams* pcp, int aPrune) {
return 21;
/*
u2 lgCacheSize;
// basic suggestion from the calc params
lgCacheSize=pcp->LogCacheSize(aPrune);
// constrain lgCacheSize to be reasonable, due in part to Pentium handling of
// shift operators with right operand >= 32
if (lgCacheSize<2)
lgCacheSize=2;
else if (lgCacheSize>30)
lgCacheSize=30;
// constrain lgCacheSize to fit within available RAM (from params.txt)
if (maxCacheMem>sizeof(CCacheData)) {
while (sizeof(CCacheData) > maxCacheMem>>lgCacheSize)
lgCacheSize--;
}
// lgCacheSize must be at least 2, due to the way cache operates
if (lgCacheSize<2)
lgCacheSize=2;
return lgCacheSize; */
}
//! Set the contempt such that draws always go to black or white, depending on fDrawsToBlack
void CPlayerComputer::SetContempt(bool fDrawsToBlack) {
int vContempt = cd.FloatToContempt(100);
if (fDrawsToBlack) {
vContempt = -vContempt;
}
cd.vContempts[0]=vContempt; cd.vContempts[1]=-vContempt;
}
CCache* CPlayerComputer::GetCache(int iCache) {
if (caches[iCache]==NULL)
caches[iCache]=new CCache(1<<LogCacheSize(pcp, cd.iPruneMidgame && cd.iPruneEndgame));
if (caches[iCache]==NULL) {
std::cerr << "out of memory allocating cache " << iCache << " for computer " << Name() << "\n";
assert(0);
exit(-1);
}
return caches[iCache];
}
//! Get my move (if it's my move) or my recommended move (if it's the opponent move) and the time taken
//!
//! \return see CPlayer::TCheatcode for a description of cheat codes.
//!
//! \param[in] flags - combination of CPlayer::kMyMove and CPlayer::Game2
//! \param[in] game - game up to the position to evaluate
//! \param[out] mli - move eval and elapsed time are filled in.
CPlayer::TCheatcode CPlayerComputer::GetMoveAndTime(COsGame& game, int flags, COsMoveListItem& mli) {
CPlayer::TCheatcode result;
clock_t t=clock();
result=GetMove(game, flags, mli);
mli.tElapsed=double(clock()-t)/CLOCKS_PER_SEC;
return result;
}
//! Get my move (if it's my move) or my recommended move (if it's the opponent move)
//!
//! \return see CPlayer::TCheatcode for a description of cheat codes.
//!
//! \param[in] flags - combination of CPlayer::kMyMove and CPlayer::Game2
//! \param[in] game - game up to the position to evaluate
//! \param[out] mli - move and eval are filled in; elapsed time calculated by the calling routine.
CPlayer::TCheatcode CPlayerComputer::GetMove(COsGame& game, int flags, COsMoveListItem& mli) {
CMVK chosen;
double tRemaining;
COsPosition pos = game.GetPos();
tRemaining=pos.cks[!pos.board.IsBlackMove()].tCurrent;
const bool fMyMove=(flags&kMyMove)!=0;
const bool fAnalyze=(flags&kAnalyze)!=0;
CQPosition qpos(game.GetPos().board);
u4 fNeeds=CSearchInfo::kNeedMove;
if (game.mt.fRand)
fNeeds|=CSearchInfo::kNeedRandSearch;
if (fAnalyze)
fNeeds|=CSearchInfo::kNeedValue;
CSearchInfo si=DefaultSearchInfo(game.GetPos().board.IsBlackMove(), fNeeds, tRemaining, (flags&kGame2)?1:0);
if (fMyMove | fAnalyze) {
if (!fMyMove)
si.SetPrintLevel(si.GetPrintLevel()|CSearchInfo::kPrintPondering);
else
si.SetPrintLevel(si.GetPrintLevel()&~CSearchInfo::kPrintPondering);
GetChosen(si, qpos, chosen);
extern bool fInTournament;
if (chosen.fKnown && !fInTournament)
mli.dEval=chosen.value/(double)kStoneValue;
else
mli.dEval=0;
mli.mv=(std::string)chosen.move;
}
else {
si.SetPrintLevel(cd.fsPrintOpponent);
}
return kCheatNone;
}
//! Display evals for the top nBest moves from a position.
void CPlayerComputer::Hint(const CQPosition& pos, int nBest) {
CMoves moves;
bool fHasMove = pos.CalcMoves(moves);
if (nBest>0) {
const u4 fNeeds=
CSearchInfo::kNeedMove
|CSearchInfo::kNeedValue
|CSearchInfo::kPrintRound
|CSearchInfo::kPrintPondering;
CSearchInfo si=DefaultSearchInfo(pos.BlackMove(), fNeeds, 1e6, 0);
CMVK mvk;
Pos2 pos2;
pos2.Initialize(pos.BitBoard(),pos.BlackMove());
IterativeValue(pos2, moves, *pcp, si, mvk, false, nBest);
}
}
//! Return the computer's default search information
CSearchInfo CPlayerComputer::DefaultSearchInfo(bool fBlackMove, u4 fNeeds, double tRemaining, int iCache) const {
int rs=DefaultRandomness() + cd.nRandShifts[!fBlackMove];
return CSearchInfo(cd.iPruneMidgame, cd.iPruneEndgame, rs, cd.vContempts[fBlackMove], fNeeds, tRemaining, iCache, cd.fsPrint);
}
void CPlayerComputer::GetChosen(const CSearchInfo& si, const CQPosition& pos, CMVK& mvk) {
SetParameters(pos, si.iCache);
assert((mpcs==NULL) || cd.iPruneMidgame<=::mpcs->NPrunes());
Pos2 pos2;
pos2.Initialize(pos.BitBoard(),pos.BlackMove());
TimedMVK(pos2, *pcp, si, mvk, false);
// Print the move
if (si.PrintMove()) {
cout << (si.PrintPondering()?"Predict: ":"=== ")
<< mvk
<< setw(3) << pos.NEmpty() << "e: "
<< (si.PrintPondering()?"":" ===") << "\n";
}
}
void CPlayerComputer::Clear() {
int i;
for (i=0; i<2; i++)
if (caches[i])
caches[i]->SetStale();
solved=false;
}
void CPlayerComputer::StartMatch(const COsMatch& match) {
cerr << "s";
}
void CPlayerComputer::EndGame(const COsGame& game) {
extern bool fInTournament;
// save the game to the saved game file
if (!m_fnSaveGame.empty()) {
ofstream os(m_fnSaveGame.c_str(),ios::app);
os << game << "\n";
}
if (!game.mt.fRand) {
// defer analysis of the game if we're in a tournament
if (fInTournament) {
ofstream os((string(Name())+"_deferred.ggf").c_str(),ios::app);
os << game << "\n";
}
}
}
static void prepareCache() {
tSetStale=::cache->NBuckets()*1E-7/dGHz;
}
void CPlayerComputer::SetParameters(const CQPosition& pos, int iCache) {
// Set up parameters
::cache=GetCache(iCache);
// we may want to copy the other cache, if the position is a predecessor
posCached[iCache]=pos;
fHasCachedPos[iCache]=true;
if (caches[!iCache] && fHasCachedPos[!iCache] && posCached[!iCache].IsSuccessor(pos)) {
caches[iCache]->CopyData(*(caches[!iCache]));
cout << "Cache copy!\n";
}
prepareCache();
//::iPruneMidgame = iPruneMidgame;
//::iPruneEndgame = iPruneEndgame;
::mpcs = mpcs;
::evaluator = eval;
}
void CPlayerComputer::SetParameters(int iCache) {
// Set up parameters
::cache=GetCache(iCache);
prepareCache();
//::iPruneMidgame = iPruneMidgame;
//::iPruneEndgame = iPruneEndgame;
::mpcs = mpcs;
::evaluator = eval;
}
bool CPlayerComputer::IsHuman() const {
return false;
}
// default randomness for computer, dependent upon stone value
int CPlayerComputer::DefaultRandomness() {
int result, sv;
for (result=-4, sv=kStoneValue; sv; result++, sv>>=1);
return result;
}