My Project
eval_pin.cc
Go to the documentation of this file.
1#include "osl/eval/pin.h"
2using osl::MultiInt;
3
6
9
11SimplePin::setUp(const Weights &weights)
12{
13 table.fill(0);
14 for (size_t i = 0; i < weights.dimension(); ++i)
15 {
16 table[i] = weights.value(i);
17 }
18}
19
22 PieceMask black_mask, PieceMask white_mask) const
23{
24 int value = 0;
25 while (black_mask.any())
26 {
27 const osl::Piece piece = state.pieceOf(black_mask.takeOneBit());
28 value += table[piece.ptype()];
29 }
30 while (white_mask.any())
31 {
32 const osl::Piece piece = state.pieceOf(white_mask.takeOneBit());
33 value -= table[piece.ptype()];
34 }
35 return value;
36}
37
38
39
41Pin::setUp(const Weights &weights,int stage)
42{
43 for (int i = PTYPE_PIECE_MIN; i <= PTYPE_MAX; ++i)
44 {
45 for (int y = 0; y <= 16; ++y)
46 {
47 for (int x = 0; x <= 8; ++x)
48 {
49 const int distance = x * 17 + y;
50 table[i][distance][stage] =
51 weights.value((i - PTYPE_PIECE_MIN) * 17 * 9 + distance);
52 }
53 }
54 }
55}
56
58Pin::eval(const NumEffectState &state,
59 PieceMask black_mask, PieceMask white_mask)
60{
61 MultiInt value;
62 const Square black_king = state.kingSquare<BLACK>();
63 const Square white_king = state.kingSquare<WHITE>();
64 while (black_mask.any())
65 {
66 const osl::Piece piece = state.pieceOf(black_mask.takeOneBit());
67 value += table[piece.ptype()][index(black_king, piece)];
68 }
69 while (white_mask.any())
70 {
71 const osl::Piece piece = state.pieceOf(white_mask.takeOneBit());
72 value -= table[piece.ptype()][index(white_king, piece)];
73 }
74 return value;
75}
76
83
85PinPtype::setUp(const Weights &weights)
86{
87 for (size_t i = 0; i < ONE_DIM; ++i)
88 {
89 for (int s=0; s<NStages; ++s)
90 table[i][s] = weights.value(i + ONE_DIM*s);
91 }
92}
93
95PinPtypeDistance::setUp(const Weights &weights)
96{
97 for (size_t i = 0; i < ONE_DIM; ++i)
98 {
99 for (int s=0; s<NStages; ++s)
100 distance_table[i][s] = weights.value(i + ONE_DIM*s);
101 }
102}
103
106{
107 for (size_t i = 0; i < ONE_DIM; ++i)
108 {
109 for (int s=0; s<NStages; ++s)
110 pawn_table[i][s] = weights.value(i + ONE_DIM*s);
111 }
112}
113
114template <osl::Player Defense>
117{
118 MultiInt result;
119 const Square king = state.kingSquare<Defense>();
120 PieceMask pin_mask = state.pin(Defense);
121 while (pin_mask.any())
122 {
123 const Piece piece = state.pieceOf(pin_mask.takeOneBit());
124 if (!state.hasEffectAt<Defense>(piece.square()))
125 continue;
126 if (king.y() == piece.square().y()) // rook h
127 {
128 result +=
129 (distance_table[(piece.ptype() + PTYPE_SIZE * 1) * 7 +
130 std::abs(king.x() - piece.square().x()) - 1] +
131 table[(piece.ptype() + PTYPE_SIZE * 1)]);
132 if (pawnAttack<Defense>(state, piece))
133 {
134 result += pawn_table[(piece.ptype() + PTYPE_SIZE * 0)];
135 }
136 }
137 else if (king.x() == piece.square().x())
138 {
139 if (state.hasEffectByPtypeStrict<LANCE>(alt(Defense),
140 piece.square())) // lance
141 {
142 result +=
143 (distance_table[(piece.ptype() + PTYPE_SIZE * 4) * 7 +
144 std::abs(king.y() - piece.square().y()) - 1] +
145 table[piece.ptype() + PTYPE_SIZE * 4]);
146 }
147 else // rook v
148 {
149 result +=
150 (distance_table[(piece.ptype() + PTYPE_SIZE * 0) * 7 +
151 std::abs(king.y() - piece.square().y()) - 1] +
152 table[piece.ptype() + PTYPE_SIZE * 0]);
153 }
154 }
155 else // bishop
156 {
157 if ((Defense == BLACK && piece.square().y() < king.y()) ||
158 (Defense == WHITE && piece.square().y() > king.y())) // u
159 {
160 result +=
161 (distance_table[(piece.ptype() + PTYPE_SIZE * 2) * 7 +
162 std::abs(king.x() - piece.square().x()) - 1] +
163 table[piece.ptype() + PTYPE_SIZE * 2]);
164 if (pawnAttack<Defense>(state, piece))
165 {
166 result += pawn_table[(piece.ptype() + PTYPE_SIZE * 1)];
167 }
168 }
169 else
170 {
171 result +=
172 (distance_table[(piece.ptype() + PTYPE_SIZE * 3) * 7 +
173 std::abs(king.x() - piece.square().x()) - 1] +
174 table[piece.ptype() + PTYPE_SIZE * 3]);
175 if (pawnAttack<Defense>(state, piece))
176 {
177 result += pawn_table[(piece.ptype() + PTYPE_SIZE * 2)];
178 }
179 }
180 }
181 }
182 return result;
183}
184
187{
188 return evalOne<BLACK>(state) - evalOne<WHITE>(state);
189}
190
191
194
196CheckShadowPtype::setUp(const Weights &weights)
197{
198 for (size_t i = 0; i < ONE_DIM; ++i)
199 {
200 for (int s=0; s<NStages; ++s)
201 table[i][s] = weights.value(i + ONE_DIM*s);
202 }
203}
204
205template <osl::Player Defense>
208{
209 MultiInt result;
210 const Square king = state.kingSquare<Defense>();
211 PieceMask open_mask = state.checkShadow(alt(Defense));
212 while (open_mask.any())
213 {
214 const Piece piece = state.pieceOf(open_mask.takeOneBit());
215 if (king.y() == piece.square().y()) // rook h
216 {
217 result += table[piece.ptype() + PTYPE_SIZE * 1];
218 }
219 else if (king.x() == piece.square().x())
220 {
221 if (state.hasEffectByPtypeStrict<LANCE>(alt(Defense),
222 piece.square())) // lance
223 {
224 result += table[piece.ptype() + PTYPE_SIZE * 4];
225 }
226 else // rook v
227 {
228 result += table[piece.ptype() + PTYPE_SIZE * 0];
229 }
230 }
231 else // bishop
232 {
233 if ((Defense == BLACK && piece.square().y() < king.y()) ||
234 (Defense == WHITE && piece.square().y() > king.y())) // u
235 {
236 result += table[piece.ptype() + PTYPE_SIZE * 2];
237 }
238 else
239 {
240 result += table[piece.ptype() + PTYPE_SIZE * 3];
241 }
242 }
243 }
244 return result;
245}
246
249{
250 return evalOne<BLACK>(state) - evalOne<WHITE>(state);
251}
252// ;;; Local Variables:
253// ;;; mode:c++
254// ;;; c-basic-offset:2
255// ;;; End:
void fill(const T_simple &value=T_simple())
Definition container.h:67
利きを持つ局面
const PieceMask checkShadow(Player attack) const
attack の駒で動くと開き王手になる可能性がある集合
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
const PieceMask pin(Player king) const
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別
駒番号のビットセット.
Definition pieceMask.h:21
bool any() const
Definition pieceMask.h:57
Ptype ptype() const
Definition basic_type.h:821
const Square square() const
Definition basic_type.h:832
const Piece pieceOf(int num) const
Definition simpleState.h:76
Square kingSquare() const
Definition simpleState.h:94
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
static MultiInt evalOne(const NumEffectState &state)
static MultiInt eval(const NumEffectState &state)
Definition eval_pin.cc:248
static void setUp(const Weights &weights)
Definition eval_pin.cc:196
static CArray< MultiInt, ONE_DIM > table
Definition pin.h:102
static CArray< MultiInt, 48 > pawn_table
Definition pin.h:65
static CArray< MultiInt, 560 > distance_table
Definition pin.h:66
static CArray< MultiInt, 80 > table
Definition pin.h:64
static MultiInt eval(const NumEffectState &state)
Definition eval_pin.cc:186
static MultiInt evalOne(const NumEffectState &state)
static void setUp(const Weights &weights)
Definition eval_pin.cc:95
static void setUp(const Weights &weights)
Definition eval_pin.cc:105
static void setUp(const Weights &weights)
Definition eval_pin.cc:85
static CArray2d< MultiInt, PTYPE_SIZE, 17 *9 > table
Definition pin.h:36
static MultiInt eval(const NumEffectState &state, PieceMask black_mask, PieceMask white_mask)
Definition eval_pin.cc:58
static void setUp(const Weights &weights, int stage)
Definition eval_pin.cc:41
static CArray< int, PTYPE_SIZE > table
Definition pin.h:19
int eval(const NumEffectState &state, PieceMask black_mask, PieceMask white_mask) const
Definition eval_pin.cc:21
static void setUp(const Weights &weights)
Definition eval_pin.cc:11
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
@ PTYPE_MAX
Definition basic_type.h:105
@ LANCE
Definition basic_type.h:96
const int PTYPE_SIZE
Definition basic_type.h:107
const int NStages
Definition midgame.h:11
@ WHITE
Definition basic_type.h:10
@ BLACK
Definition basic_type.h:9
constexpr Player alt(Player player)
Definition basic_type.h:13
size_t dimension() const
Definition weights.h:29
int value(size_t index) const
Definition weights.h:27