[go: up one dir, main page]

Menu

[r74]: / trunk / rlgo / RlEvaluator.cpp  Maximize  Restore  History

Download this file

326 lines (278 with data), 8.8 kB

  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
//----------------------------------------------------------------------------
/** @file RlEvaluator.cpp
*/
//----------------------------------------------------------------------------
#include "SgSystem.h"
#include "RlEvaluator.h"
#include "GoBoard.h"
#include "RlBinaryFeatures.h"
#include "RlMoveFilter.h"
#include "RlUtils.h"
#include "RlState.h"
#include "RlWeightSet.h"
using namespace boost;
using namespace std;
using namespace RlShapeUtil;
IMPLEMENT_OBJECT(RlEvaluator);
RlEvaluator::RlEvaluator(GoBoard& board,
RlBinaryFeatures* featureset,
RlWeightSet* weightset,
RlMoveFilter* filter)
: RlAutoObject(board),
m_featureSet(featureset),
m_weightSet(weightset),
m_moveFilter(filter),
m_differences(false),
m_supportUndo(true)
{
}
void RlEvaluator::LoadSettings(istream& settings)
{
int version;
settings >> RlVersion(version, 6, 6);
settings >> RlSetting<RlBinaryFeatures*>("FeatureSet", m_featureSet);
settings >> RlSetting<RlWeightSet*>("WeightSet", m_weightSet);
settings >> RlSetting<RlMoveFilter*>("MoveFilter", m_moveFilter);
settings >> RlSetting<bool>("Differences", m_differences);
settings >> RlSetting<bool>("SupportUndo", m_supportUndo);
}
void RlEvaluator::Initialise()
{
m_featureSet->EnsureInitialised();
m_weightSet->EnsureInitialised();
m_moveFilter->EnsureInitialised();
// Tracker map ensures that each feature creates just one tracker
map<RlBinaryFeatures*, RlTracker*> trackermap;
m_tracker = m_featureSet->CreateTracker(trackermap);
m_tracker->Initialise();
m_active.Resize(m_tracker->GetActiveSize());
}
void RlEvaluator::Reset()
{
// Resets agent's internal state, but not the board
// Called at the start of a new search, before simulation
m_eval = 0;
// Reset move filter first (as it may be used by trackers during reset)
if (m_moveFilter)
m_moveFilter->Reset();
if (m_differences)
m_dirty.MarkAll(m_board);
m_tracker->Reset();
m_active.Clear();
AddWeightsUpdateActive(m_tracker->ChangeList(), m_eval);
}
void RlEvaluator::Execute(SgMove move, SgBlackWhite colour, bool real)
{
// When a real move is executed, fully update internal representation
if (real)
{
if (m_tracker->MarkSet())
throw SgException("Can't set mark for real execution");
Reset();
}
// When a simulated move is executed, update incrementally
else
{
m_tracker->Execute(move, colour, true, m_supportUndo);
AddWeightsUpdateActive(m_tracker->ChangeList(), m_eval);
if (m_differences)
m_tracker->UpdateDirty(move, colour, m_dirty);
if (m_moveFilter)
m_moveFilter->Execute(move, colour);
}
}
void RlEvaluator::PlayExecute(SgMove move, SgBlackWhite colour, bool real)
{
m_board.Play(move, colour);
Execute(move, colour, real);
}
void RlEvaluator::Undo(bool real)
{
// When a real move is undone, fully update internal representation
if (real)
Reset();
// When a simulated move is undone, update incrementally
else
{
if (!m_supportUndo)
throw SgException("This evaluator does not support undo");
m_tracker->Undo();
AddWeightsUpdateActive(m_tracker->ChangeList(), m_eval);
if (m_differences)
m_dirty.MarkAll(m_board); // @todo: could do something smarter here
if (m_moveFilter)
m_moveFilter->Undo();
}
}
void RlEvaluator::TakeBackUndo(bool real)
{
m_board.Undo();
Undo(real);
}
inline void RlEvaluator::AddWeights(const RlChangeList& changes, RlFloat& eval)
{
for (RlChangeList::Iterator i_changes(changes); i_changes; ++i_changes)
{
RlWeight& weight = m_weightSet->Get(i_changes->m_featureIndex);
eval += weight.Weight() * i_changes->m_occurrences;
}
}
inline void RlEvaluator::AddWeightsUpdateActive(
const RlChangeList& changes, RlFloat& eval)
{
for (RlChangeList::Iterator i_changes(changes); i_changes; ++i_changes)
{
RlWeight& weight = m_weightSet->Get(i_changes->m_featureIndex);
eval += weight.Weight() * i_changes->m_occurrences;
m_active.Change(*i_changes);
}
}
RlFloat RlEvaluator::EvaluateMove(SgMove move, SgBlackWhite colour)
{
if (m_differences)
return EvalMoveDiffs(move, colour);
else
return EvalMoveSimple(move, colour);
}
RlFloat RlEvaluator::EvalMoveSimple(SgMove move, SgBlackWhite colour)
{
RlFloat weightchange = 0;
m_board.Play(move, colour);
m_tracker->Execute(move, colour, false, false);
AddWeights(m_tracker->ChangeList(), weightchange);
m_board.Undo();
return m_eval + weightchange;
}
RlFloat RlEvaluator::EvalMoveDiffs(SgMove move, SgBlackWhite colour)
{
// Add differences to current evaluation (recalculate where necessary)
RlFloat weightchange = 0;
if (m_dirty.IsDirty(move, colour))
{
m_board.Play(move, colour);
m_tracker->Execute(move, colour, false, false);
AddWeights(m_tracker->ChangeList(), weightchange);
m_diffs[BWIndex(colour)][move] = weightchange;
m_dirty.Clear(move, colour);
m_dirty.IncPruned(false);
m_board.Undo();
}
else
{
weightchange = m_diffs[BWIndex(colour)][move];
m_dirty.IncPruned(true);
}
return m_eval + weightchange;
}
void RlEvaluator::FindBest(RlState& state)
{
static SgMove ties[SG_MAX_MOVES];
int numties = 0;
SgBlackWhite colour = state.m_colour;
state.m_bestMove = SG_PASS;
state.m_bestEval = colour == SG_BLACK ? -RlInfinity : RlInfinity;
for (RlMoveFilter::Iterator i_filter(*m_moveFilter, colour);
i_filter; ++i_filter)
{
SgMove move = *i_filter;
RlFloat eval = EvaluateMove(move, colour);
if ((colour == SG_BLACK && eval >= state.m_bestEval)
|| (colour == SG_WHITE && eval <= state.m_bestEval))
{
if (eval != state.m_bestEval)
numties = 0;
state.m_bestMove = move;
state.m_bestEval = eval;
ties[numties++] = move;
}
}
// Random tie-breaking
if (numties > 1)
{
int index = SgRandom::Global().Int(numties);
state.m_bestMove = ties[index];
}
}
void RlEvaluator::SetMark()
{
m_tracker->SetMark();
}
void RlEvaluator::ClearMark()
{
m_tracker->ClearMark();
}
void RlEvaluator::RefreshValue(RlState& state)
{
RlFloat eval = 0;
for (RlActiveSet::Iterator i_active(state.Active());
i_active; ++i_active)
{
RlWeight& weight = m_weightSet->Get(i_active->m_featureIndex);
eval += weight.Weight() * i_active->m_occurrences;
}
state.SetEval(eval);
}
int RlEvaluator::GetActiveSize() const
{
SG_ASSERT(IsInitialised());
return m_tracker->GetActiveSize();
}
void RlEvaluator::PrintDirty(ostream& ostr)
{
for (int c = SG_BLACK; c <= SG_WHITE; ++c)
{
ostr << "Dirty points for " << SgBW(c) << ":\n";
for (int j = m_board.Size(); j >= 1; --j)
{
for (int i = 1; i <= m_board.Size(); ++i)
ostr << (IsDirty(SgPointUtil::Pt(i, j), c) ? "*" : " ");
ostr << "\n";
}
}
}
void RlEvaluator::EnsureSimple()
{
if (m_differences)
throw SgException("Differences must be disabled for simple operation");
}
void RlEvaluator::EnsureSupportUndo()
{
if (!m_supportUndo)
throw SgException("Undo not supported");
}
//----------------------------------------------------------------------------
void RlMoveSorter::Evaluate(RlEvaluator* evaluator, SgBlackWhite toplay)
{
m_evals.clear();
// Evaluate all legal moves
for (RlMoveFilter::Iterator i_moves(*evaluator->GetMoveFilter(), toplay);
i_moves; ++i_moves)
{
SgMove move = *i_moves;
RlFloat eval = evaluator->EvaluateMove(move, toplay);
m_evals.push_back(EvalPair(move, eval));
}
}
void RlMoveSorter::SortMoves(SgBlackWhite toplay)
{
// Sort moves so that best moves are first
if (toplay == SG_BLACK)
sort(m_evals.begin(), m_evals.end(), greater<EvalPair>());
else
sort(m_evals.begin(), m_evals.end(), less<EvalPair>());
}
int RlMoveSorter::GetRank(SgMove move) const
{
for (int i = 0; i < ssize(m_evals); ++i)
if (m_evals[i].m_move == move)
return i;
return -1;
}
RlFloat RlMoveSorter::GetProportionalRank(SgMove move) const
{
if (GetNumMoves() > 0)
return (RlFloat) GetRank(move) / (RlFloat) GetNumMoves();
else
return 0.0;
}
//----------------------------------------------------------------------------