pin.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
eval/ml/pin.cc
Go to the documentation of this file.
1 #include "osl/eval/ml/pin.h"
2 using osl::MultiInt;
3
4 osl::misc::CArray<int, osl::PTYPE_SIZE>
5 osl::eval::ml::SimplePin::table;
6
7 osl::misc::CArray2d<MultiInt, osl::PTYPE_SIZE, 17 * 9>
8 osl::eval::ml::Pin::table;
9
10 void osl::eval::ml::
11 SimplePin::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
20 int osl::eval::ml::
21 SimplePin::eval(const NumEffectState &state,
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
40 void osl::eval::ml::
41 Pin::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
57 MultiInt osl::eval::ml::
58 Pin::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
77 osl::misc::CArray<MultiInt, 80>
78 osl::eval::ml::PinPtypeAll::table;
79 osl::misc::CArray<MultiInt, 48>
80 osl::eval::ml::PinPtypeAll::pawn_table;
81 osl::misc::CArray<MultiInt, 560>
82 osl::eval::ml::PinPtypeAll::distance_table;
83
84 void osl::eval::ml::
85 PinPtype::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
94 void osl::eval::ml::
95 PinPtypeDistance::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
104 void osl::eval::ml::
105 PinPtypePawnAttack::setUp(const Weights &weights)
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
114 template <osl::Player Defense>
115 osl::MultiInt osl::eval::ml::
116 PinPtypeAll::evalOne(const NumEffectState &state)
117 {
118 MultiInt result;
119 const Square king = state.kingSquare<Defense>();
120 osl::container::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
185 osl::MultiInt osl::eval::ml::
186 PinPtypeAll::eval(const NumEffectState &state)
187 {
188 return evalOne<BLACK>(state) - evalOne<WHITE>(state);
189 }
190
191
192 osl::misc::CArray<MultiInt, 80>
193 osl::eval::ml::CheckShadowPtype::table;
194
195 void osl::eval::ml::
196 CheckShadowPtype::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
205 template <osl::Player Defense>
206 osl::MultiInt osl::eval::ml::
207 CheckShadowPtype::evalOne(const NumEffectState &state)
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
247 osl::MultiInt osl::eval::ml::
248 CheckShadowPtype::eval(const NumEffectState &state)
249 {
250 return evalOne<BLACK>(state) - evalOne<WHITE>(state);
251 }
252 // ;;; Local Variables:
253 // ;;; mode:c++
254 // ;;; c-basic-offset:2
255 // ;;; End: