forked from sangmichaelxie/giraffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_move_evaluator.h
163 lines (136 loc) · 3.79 KB
/
static_move_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
/*
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 STATIC_MOVE_EVALUATOR_H
#define STATIC_MOVE_EVALUATOR_H
#include <iostream>
#include <random>
#include <vector>
#include <string>
#include "move_evaluator.h"
#include "see.h"
#include "random_device.h"
// in sampling mode, we are collecting internal nodes for training
//#define SAMPLING
class StaticMoveEvaluator : public MoveEvaluatorIface
{
public:
std::vector<std::string> samples;
virtual void EvaluateMoves(Board &board, SearchInfo &si, MoveInfoList &list, MoveList &/*ml*/) override
{
#ifdef SAMPLING
static std::uniform_real_distribution<float> dist;
static auto mt = gRd.MakeMT();
if (dist(mt) < 0.002f)
{
std::string fen = board.GetFen();
#pragma omp critical(sampleInsert)
{
samples.push_back(std::move(fen));
}
}
#endif // SAMPLING
KillerMoveList killerMoves;
if (si.killer)
{
si.killer->GetKillers(killerMoves, si.ply);
}
Move counterMove = 0;
if (si.counter)
{
counterMove = si.counter->GetCounterMove(board);
}
for (auto &mi : list)
{
Move mv = mi.move;
PieceType promoType = GetPromoType(mv);
bool isViolent = board.IsViolent(mv);
bool isPromo = IsPromotion(mv);
bool isQueenPromo = (promoType == WQ || promoType == BQ);
bool isUnderPromo = (isPromo && !isQueenPromo);
mi.seeScore = SEE::StaticExchangeEvaluation(board, mv);
mi.nmSeeScore = SEE::NMStaticExchangeEvaluation(board, mv);
if (mv == si.hashMove)
{
// hash move
mi.nodeAllocation = 3.0009f;
}
else if (isQueenPromo && mi.seeScore >= 0)
{
// queen promos that aren't losing
mi.nodeAllocation = 2.0008f;
}
else if (isViolent && mi.seeScore >= 0 && !isUnderPromo)
{
// winning captures (excluding underpromoting captures)
mi.nodeAllocation = 2.0007f;
}
else if (si.isQS)
{
// the above categories are the only ones we want to look at for QS
mi.nodeAllocation = 0.0f;
}
else if (killerMoves.Exists(mv) && !isViolent)
{
bool found = false;
// killer
for (size_t slot = 0; slot < killerMoves.GetSize(); ++slot)
{
if (killerMoves[slot] == mv)
{
// for killer moves, score is based on which slot we are in (lower = better)
mi.nodeAllocation = 1.100f - 0.0001f * slot;
found = true;
break;
}
}
assert(found);
}
else if (mv == counterMove)
{
mi.nodeAllocation = 1.05f;
}
else if (mi.seeScore >= 0 && !isUnderPromo)
{
// other non-losing moves (excluding underpomotions)
mi.nodeAllocation = 1.0000f + si.history->GetHistoryScore(mv) * 0.01f;
}
else if (isViolent && !isUnderPromo)
{
// losing captures
mi.nodeAllocation = 0.1f;
}
else
{
// losing quiet moves and underpromos
mi.nodeAllocation = 0.01f;
}
}
std::stable_sort(list.begin(), list.end(), [](const MoveInfo &a, const MoveInfo &b)
{
if (a.nodeAllocation != b.nodeAllocation)
{
return a.nodeAllocation > b.nodeAllocation;
}
else
{
// sort based on SEE (or another source of score)
return a.seeScore > b.seeScore;
}
}
);
NormalizeMoveInfoList(list);
}
};
extern StaticMoveEvaluator gStaticMoveEvaluator;
#endif // STATIC_MOVE_EVALUATOR_H