-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathevaluator.h
165 lines (131 loc) · 4.65 KB
/
evaluator.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
/*
Copyright (C) 2015 Matthew Lai
Giraffe 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.
Giraffe 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 EVALUATOR_H
#define EVALUATOR_H
#include "types.h"
#include "board.h"
#include "see.h"
#include <limits>
// add small offsets to prevent overflow/underflow on adding/subtracting 1 (eg. for PV search)
const static Score SCORE_MAX = std::numeric_limits<Score>::max() - 1000;
const static Score SCORE_MIN = std::numeric_limits<Score>::lowest() + 1000;
class EvaluatorIface
{
public:
constexpr static float EvalFullScale = 10000.0f;
// return score for side to move
virtual Score EvaluateForSTM(Board &b, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
if (b.GetSideToMove() == WHITE)
{
return EvaluateForWhiteImpl(b, lowerBound, upperBound);
}
else
{
return -EvaluateForWhiteImpl(b, -upperBound, -lowerBound);
}
}
virtual Score EvaluateForWhite(Board &b, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
return EvaluateForWhiteImpl(b, lowerBound, upperBound);
}
virtual Score EvaluateForSTMGEE(Board &board, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
if (board.GetSideToMove() == WHITE)
{
return EvaluateForWhiteGEEImpl(board, lowerBound, upperBound);
}
else
{
return -EvaluateForWhiteGEEImpl(board, -upperBound, -lowerBound);
}
}
virtual Score EvaluateForWhiteGEE(Board &board, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
return EvaluateForWhiteGEEImpl(board, lowerBound, upperBound);
}
virtual void BatchEvaluateForSTMGEE(std::vector<Board> &positions, std::vector<Score> &results, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
// check that they all have the same stm
Color stm = positions[0].GetSideToMove();
for (size_t i = 1; i < positions.size(); ++i)
{
assert(positions[i].GetSideToMove() == stm);
}
if (stm == WHITE)
{
BatchEvaluateForWhiteGEEImpl(positions, results, lowerBound, upperBound);
}
else
{
BatchEvaluateForWhiteGEEImpl(positions, results, -upperBound, -lowerBound);
for (auto &x : results)
{
x *= -1;
}
}
}
virtual void BatchEvaluateForWhiteGEE(std::vector<Board> &positions, std::vector<Score> &results, Score lowerBound = SCORE_MIN, Score upperBound = SCORE_MAX)
{
BatchEvaluateForWhiteGEEImpl(positions, results, lowerBound, upperBound);
}
virtual float UnScale(float x)
{
float ret = x / EvalFullScale;
ret = std::max(ret, -1.0f);
ret = std::min(ret, 1.0f);
return ret;
}
// this is the only function evaluators need to implement
virtual Score EvaluateForWhiteImpl(Board &b, Score lowerBound, Score upperBound) = 0;
// this allows evaluators to evaluate multiple positions at once
// default implementation does it one at a time
virtual void BatchEvaluateForWhiteImpl(std::vector<Board> &positions, std::vector<Score> &results, Score lowerBound, Score upperBound)
{
results.resize(positions.size());
for (size_t i = 0; i < positions.size(); ++i)
{
results[i] = EvaluateForWhiteImpl(positions[i], lowerBound, upperBound);
}
}
// evaluates the board from the perspective of the moving side by running eval on the leaf of a GEE
// this is a generic implementation that can be overridden
virtual Score EvaluateForWhiteGEEImpl(Board &board, Score lowerBound, Score upperBound)
{
Score result = 0;
auto staticEvalCallback = [this, &result, lowerBound, upperBound](Board &board)
{
result = EvaluateForWhiteImpl(board, lowerBound, upperBound);
};
SEE::GEERunFunc(board, staticEvalCallback);
return result;
}
virtual void BatchEvaluateForWhiteGEEImpl(std::vector<Board> &positions, std::vector<Score> &results, Score lowerBound, Score upperBound)
{
std::vector<Board> leafPositions;
leafPositions.reserve(positions.size());
auto vectorInsertCallback = [this, &leafPositions](Board &board)
{
leafPositions.push_back(board);
};
for (size_t i = 0; i < positions.size(); ++i)
{
SEE::GEERunFunc(positions[i], vectorInsertCallback);
}
BatchEvaluateForWhiteImpl(leafPositions, results, lowerBound, upperBound);
}
// this is optional
virtual void PrintDiag(Board &/*board*/) {}
};
#endif // EVALUATOR_H