-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevaluate.h
183 lines (159 loc) · 4.5 KB
/
evaluate.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
/*
** Stash, a UCI chess playing engine developed from scratch
** Copyright (C) 2019-2024 Morgan Houppin
**
** Stash 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.
**
** Stash 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 EVALUATE_H
#define EVALUATE_H
#include <string.h>
#include "board.h"
enum {
MIDGAME_COUNT = 24,
ENDGAME_COUNT = 4
};
typedef enum {
IDX_PIECE,
IDX_PSQT = IDX_PIECE + 5,
IDX_INITIATIVE = IDX_PSQT + 48 + 32 * 5,
IDX_KNIGHT_CLOSED_POS,
IDX_KNIGHT_SHIELDED = IDX_KNIGHT_CLOSED_POS + 5,
IDX_KNIGHT_OUTPOST,
IDX_BISHOP_PAWNS_COLOR,
IDX_BISHOP_PAIR = IDX_BISHOP_PAWNS_COLOR + 7,
IDX_BISHOP_SHIELDED,
IDX_BISHOP_OUTPOST,
IDX_BISHOP_LONG_DIAG,
IDX_ROOK_SEMIOPEN,
IDX_ROOK_OPEN,
IDX_ROOK_BLOCKED,
IDX_ROOK_XRAY_QUEEN,
IDX_ROOK_TRAPPED,
IDX_ROOK_BURIED,
IDX_MOBILITY_KNIGHT,
IDX_MOBILITY_BISHOP = IDX_MOBILITY_KNIGHT + 9,
IDX_MOBILITY_ROOK = IDX_MOBILITY_BISHOP + 14,
IDX_MOBILITY_QUEEN = IDX_MOBILITY_ROOK + 15,
IDX_BACKWARD = IDX_MOBILITY_QUEEN + 28,
IDX_STRAGGLER,
IDX_DOUBLED,
IDX_ISOLATED,
IDX_PASSER,
IDX_PHALANX = IDX_PASSER + 6,
IDX_DEFENDER = IDX_PHALANX + 6,
IDX_PASSED_OUR_KING_DIST = IDX_DEFENDER + 5,
IDX_PASSED_THEIR_KING_DIST = IDX_PASSED_OUR_KING_DIST + 24,
IDX_PAWN_ATK_MINOR = IDX_PASSED_THEIR_KING_DIST + 24,
IDX_PAWN_ATK_ROOK,
IDX_PAWN_ATK_QUEEN,
IDX_MINOR_ATK_ROOK,
IDX_MINOR_ATK_QUEEN,
IDX_ROOK_ATK_QUEEN,
IDX_HANGING_PAWN,
IDX_FAR_KNIGHT,
IDX_FAR_BISHOP,
IDX_FAR_ROOK,
IDX_FAR_QUEEN,
// All King Safety terms should go under this enum value. This is done to
// facilitate gradient calculations in the internal tuner.
IDX_KING_SAFETY,
IDX_KS_KNIGHT,
IDX_KS_BISHOP,
IDX_KS_ROOK,
IDX_KS_QUEEN,
IDX_KS_ATTACK,
IDX_KS_WEAK_Z,
IDX_KS_CHECK_N,
IDX_KS_CHECK_B,
IDX_KS_CHECK_R,
IDX_KS_CHECK_Q,
IDX_KS_UNSAFE_CHECK,
IDX_KS_QUEENLESS,
IDX_KS_STORM,
IDX_KS_SHELTER = IDX_KS_STORM + 24,
IDX_KS_OFFSET = IDX_KS_SHELTER + 24,
IDX_COUNT
} TuneIndex;
typedef struct {
i16 phase;
Scorepair tapered_eval;
Scorepair safety_eval[COLOR_NB];
Scalefactor eg_scalefactor;
i8 coeffs[IDX_COUNT][COLOR_NB];
} EvalTrace;
typedef struct {
Bitboard king_zone[COLOR_NB];
Bitboard mobility_zone[COLOR_NB];
Bitboard attacked[COLOR_NB];
Bitboard attacked2[COLOR_NB];
Bitboard attacked_by[COLOR_NB][PIECETYPE_NB];
i32 safety_attackers[COLOR_NB];
i32 safety_attacks[COLOR_NB];
Scorepair safety_value[COLOR_NB];
i32 position_closed;
} EvaluationData;
extern EvalTrace Trace;
#ifdef TUNE
INLINED void trace_init(void) {
memset(&Trace, 0, sizeof(Trace));
}
INLINED void trace_add(u16 index, Color us, i8 coeff) {
Trace.coeffs[index][us] += coeff;
}
INLINED void trace_set_phase(i16 phase) {
Trace.phase = phase;
}
INLINED void trace_set_safety(Color us, Scorepair safety) {
Trace.safety_eval[us] = safety;
}
INLINED void trace_set_eval(Scorepair eval) {
Trace.tapered_eval = eval;
}
INLINED void trace_set_scalefactor(Scalefactor scalefactor) {
Trace.eg_scalefactor = scalefactor;
}
INLINED void trace_clear_safety(Color us) {
Trace.coeffs[IDX_KS_KNIGHT][us] = 0;
Trace.coeffs[IDX_KS_BISHOP][us] = 0;
Trace.coeffs[IDX_KS_ROOK][us] = 0;
Trace.coeffs[IDX_KS_QUEEN][us] = 0;
Trace.coeffs[IDX_KS_ATTACK][us] = 0;
}
#else
INLINED void trace_init(void) {
}
INLINED void trace_add(u16 index, Color us, i8 coeff) {
(void)index;
(void)us;
(void)coeff;
}
INLINED void trace_set_phase(i16 phase) {
(void)phase;
}
INLINED void trace_set_safety(Color us, Scorepair safety) {
(void)us;
(void)safety;
}
INLINED void trace_set_eval(Scorepair eval) {
(void)eval;
}
INLINED void trace_set_scalefactor(Scalefactor scalefactor) {
(void)scalefactor;
}
INLINED void trace_clear_safety(Color us) {
(void)us;
}
#endif
Score evaluate(const Board *board);
#endif