My Project
eval_pieceStand.cc
Go to the documentation of this file.
1 #include "osl/eval/pieceStand.h"
2 #include "osl/bits/pieceStand.h"
3 
6 
8 PieceStand::setUp(const Weights &weights,int stage)
9 {
10  for (size_t i = 0; i < weights.dimension(); ++i)
11  {
12  table[i][stage] = weights.value(i);
13  }
14 }
15 
17  const NumEffectState &state)
18 {
19  MultiInt result;
20  for (Ptype ptype: osl::PieceStand::order)
21  {
22  const int black_count =
23  state.countPiecesOnStand(BLACK, ptype);
24  const int white_count =
25  state.countPiecesOnStand(WHITE, ptype);
26  for (int j = 0; j < black_count; ++j)
27  {
28  result += table[Ptype_Table.getIndexMin(ptype) + j];
29  }
30  for (int j = 0; j < white_count; ++j)
31  {
32  result -= table[Ptype_Table.getIndexMin(ptype) + j];
33  }
34  }
35  return result;
36 }
37 
38 ␌
39 
42 
44 NonPawnPieceStand::setUp(const Weights &weights,int stage)
45 {
46  for (size_t i = 0; i < weights.dimension(); ++i)
47  {
48  table[i][stage] = weights.value(i);
49  }
50 }
51 
53 NonPawnPieceStand::eval(int black_count, int white_count)
54 {
55  return table[black_count] - table[white_count];
56 }
57 
58 ␌
61 
64  const CArray<MultiInt, 5625> &values)
65 {
66  osl::MultiInt result;
67  for (int rook = 0; rook <= indices[0]; ++rook)
68  {
69  for (int bishop = 0; bishop <= indices[1]; ++bishop)
70  {
71  for (int gold = 0; gold <= indices[2]; ++gold)
72  {
73  for (int silver = 0; silver <= indices[3]; ++silver)
74  {
75  for (int knight = 0; knight <= indices[4]; ++knight)
76  {
77  for (int lance = 0; lance <= indices[5]; ++lance)
78  {
79  if (rook + bishop + gold + silver + knight + lance == 0)
80  {
81  continue;
82  }
83  result += values[index(rook, bishop,
84  gold, silver, knight, lance)];
85  }
86  }
87  }
88  }
89  }
90  }
91  return result;
92 }
93 
96 {
97  CArray<MultiInt, 5625> orig_table;
98  for (size_t i = 0; i < ONE_DIM; ++i)
99  {
100  for (int s=0; s<NStages; ++s)
101  {
102  orig_table[i][s] = weights.value(i + ONE_DIM*s);
103  }
104  }
105  CArray<int, 6> indices;
106  for (indices[0] = 0; indices[0] <= 2; ++indices[0])
107  {
108  for (indices[1] = 0; indices[1] <= 2; ++indices[1])
109  {
110  for (indices[2] = 0; indices[2] <= 4; ++indices[2])
111  {
112  for (indices[3] = 0; indices[3] <= 4; ++indices[3])
113  {
114  for (indices[4] = 0; indices[4] <= 4; ++indices[4])
115  {
116  for (indices[5] = 0; indices[5] <= 4; ++indices[5])
117  {
118  table[index(indices[0],
119  indices[1],
120  indices[2],
121  indices[3],
122  indices[4],
123  indices[5])] = sumUp(indices, orig_table);
124  }
125  }
126  }
127  }
128  }
129  }
130  table[0] = orig_table[0];
131 }
132 
135 {
136  CArray<MultiInt, 5625> orig_table;
137  for (size_t i = 0; i < ONE_DIM; ++i)
138  {
139  for (int s=0; s<NStages; ++s)
140  {
141  orig_table[i][s] = weights.value(i + ONE_DIM*s);
142  }
143  }
144  CArray<int, 6> indices;
145  for (indices[0] = 0; indices[0] <= 2; ++indices[0])
146  {
147  for (indices[1] = 0; indices[1] <= 2; ++indices[1])
148  {
149  for (indices[2] = 0; indices[2] <= 4; ++indices[2])
150  {
151  for (indices[3] = 0; indices[3] <= 4; ++indices[3])
152  {
153  for (indices[4] = 0; indices[4] <= 4; ++indices[4])
154  {
155  for (indices[5] = 0; indices[5] <= 4; ++indices[5])
156  {
159  indices[1],
160  indices[2],
161  indices[3],
162  indices[4],
163  indices[5])] =
164  NonPawnPieceStandCombination::sumUp(indices, orig_table);
165  }
166  }
167  }
168  }
169  }
170  }
172 }
173 
176  const CArray<bool, 2> &can_check)
177 {
178  const int black_index = index(state.countPiecesOnStand<ROOK>(BLACK),
180  state.countPiecesOnStand<GOLD>(BLACK),
183  state.countPiecesOnStand<LANCE>(BLACK));
184  const int white_index = index(state.countPiecesOnStand<ROOK>(WHITE),
186  state.countPiecesOnStand<GOLD>(WHITE),
189  state.countPiecesOnStand<LANCE>(WHITE));
190  MultiInt result;
191  result = table[black_index] - table[white_index];
192  if (can_check[WHITE])
193  {
194  result += check_table[black_index];
195  }
196  if (can_check[BLACK])
197  {
198  result -= check_table[white_index];
199  }
200  return result;
201 }
202 
205  const NumEffectState &state,
206  Move moved,
207  const MultiInt &last_value,
208  const CArray<bool, 2> &could_check,
209  const CArray<bool, 2> &can_check)
210 {
211  if (!moved.isDrop() && ! moved.isCapture() &&
212  could_check[0] == can_check[0] && could_check[1] == can_check[1])
213  {
214  return last_value;
215  }
216  return eval(state, can_check);
217 }
218 
219 ␌
221 
224 {
225  for (size_t i = 0; i < ONE_DIM; ++i)
226  {
227  for (int s=0; s<NStages; ++s)
228  table[i][s] = weights.value(i + ONE_DIM*s);
229  }
230 }
231 
234 {
235  result = MultiIntPair();
236  for (Ptype ptype: osl::PieceStand::order)
237  {
238  if (ptype == PAWN)
239  continue;
240  const int black_count = state.countPiecesOnStand(BLACK, ptype);
241  const int white_count = state.countPiecesOnStand(WHITE, ptype);
242  for (int j = 0; j < black_count; ++j)
243  {
244  const int index_black = index(BLACK, BLACK, ptype, j);
245  const int index_white = index(BLACK, WHITE, ptype, j);
246  result[BLACK] += table[index_black];
247  result[WHITE] += table[index_white];
248  }
249  for (int j = 0; j < white_count; ++j)
250  {
251  const int index_black = index(WHITE, BLACK, ptype, j);
252  const int index_white = index(WHITE, WHITE, ptype, j);
253  result[BLACK] -= table[index_black];
254  result[WHITE] -= table[index_white];
255  }
256  }
257 }
258 
259 template<osl::Player P>
262  const NumEffectState &state,
263  Move moved, MultiIntPair &result)
264 {
265  assert(P==moved.player());
266  if (!moved.isDrop() && ! moved.isCapture())
267  return;
268 
269  if (moved.isDrop())
270  {
271  const Ptype ptype = moved.ptype();
272  if (ptype == PAWN)
273  return;
274  const int count =
275  state.countPiecesOnStand(P, moved.ptype());
276  const int index_black = index(P, BLACK, moved.ptype(), count);
277  const int index_white = index(P, WHITE, moved.ptype(), count);
278  if(P==BLACK){
279  result[BLACK] -= table[index_black];
280  result[WHITE] -= table[index_white];
281  }
282  else{
283  result[BLACK] += table[index_black];
284  result[WHITE] += table[index_white];
285  }
286  }
287  if (moved.isCapture() &&
288  unpromote(moved.capturePtype()) != PAWN)
289  {
290  Ptype ptype = unpromote(moved.capturePtype());
291  const int count = state.countPiecesOnStand(P, ptype) - 1;
292  const int index_black = index(P, BLACK, ptype, count);
293  const int index_white = index(P, WHITE, ptype, count);
294  if(P==BLACK){
295  result[BLACK] += table[index_black];
296  result[WHITE] += table[index_white];
297  }
298  else{
299  result[BLACK] -= table[index_black];
300  result[WHITE] -= table[index_white];
301  }
302  }
303 }
304 
305 ␌
310 
312 PieceStandY::setUp(const Weights &weights)
313 {
314  for (size_t i = 0; i < ONE_DIM; ++i)
315  {
316  for (int s=0; s<NStages; ++s)
317  {
318  y_attack_table[i][s] = weights.value(i + ONE_DIM * 2 * s);
319  y_defense_table[i][s] = weights.value(i + ONE_DIM * 2 * s + ONE_DIM);
320  }
321  }
322  for (int i=0;i<7;i++){
323  Ptype ptype=osl::PieceStand::order[i];
324  int ptypeSize=Ptype_Table.getIndexLimit(ptype)-Ptype_Table.getIndexMin(ptype);
325  for(int king_y=1;king_y<=9;king_y++){
326  MultiInt attack_sum, defense_sum;
327  for(int count=0;count<=ptypeSize;count++){
328 #if 0
329  int oldIndex=(king_y - 1) * 40 + Ptype_Table.getIndexMin(ptype) + count;
330  int newIndex=(king_y - 1) * 7*19 + i*19 + count;
331 #else
332  int oldIndex=index(ptype,BLACK,Square(5,king_y),count);
333  int newIndex=index(i,BLACK,Square(5,king_y),count);
334 #endif
335  y_attack_table_sum[newIndex]=attack_sum;
336  y_defense_table_sum[newIndex]=defense_sum;
337  if(count==ptypeSize) break;
338  attack_sum += y_attack_table[oldIndex];
339  defense_sum += y_defense_table[oldIndex];
340  }
341  }
342  }
343 }
344 
345 inline
347 {
348  const int black_count = state.countPiecesOnStand(BLACK, ptype);
349  const int white_count = state.countPiecesOnStand(WHITE, ptype);
350  const int attack_index_1 = PieceStandY::index(i, BLACK, kings[WHITE], black_count);
351  const int attack_index_2 = PieceStandY::index(i, WHITE, kings[BLACK], white_count);
352  const int defense_index_1 = PieceStandY::index(i, BLACK, kings[BLACK], black_count);
353  const int defense_index_2 = PieceStandY::index(i, WHITE, kings[WHITE], white_count);
354  result += y_attack_table_sum[attack_index_1] - y_attack_table_sum[attack_index_2] +
355  y_defense_table_sum[defense_index_1] - y_defense_table_sum[defense_index_2];
356 }
357 
359 PieceStandY::eval(const NumEffectState &state)
360 {
361  MultiInt result;
362  const CArray<Square,2> kings = {{
363  state.kingSquare(BLACK),
364  state.kingSquare(WHITE),
365  }};
366  updateResult(state,result,0,ROOK,kings);
367  updateResult(state,result,1,BISHOP,kings);
368  updateResult(state,result,2,GOLD,kings);
369  updateResult(state,result,3,SILVER,kings);
370  updateResult(state,result,4,KNIGHT,kings);
371  updateResult(state,result,5,LANCE,kings);
372  updateResult(state,result,6,PAWN,kings);
373  return result;
374 }
375 
376 template<osl::Player P>
379  const NumEffectState &state,
380  Move moved, const MultiInt &last_value)
381 {
382  if (moved.ptype() == KING)
383  return eval(state);
384 
385  MultiInt result(last_value);
386  if (moved.isDrop())
387  {
388  const Ptype ptype = moved.ptype();
389  const int count =
390  state.countPiecesOnStand(P, ptype);
391  const int attack_index = index(ptype, P,
392  state.kingSquare(alt(P)),
393  count);
394  const int defense_index = index(ptype, P,
395  state.kingSquare(P),
396  count);
397  if(P==BLACK)
398  result -= y_attack_table[attack_index] +y_defense_table[defense_index];
399  else
400  result += y_attack_table[attack_index] +y_defense_table[defense_index];
401  }
402  if (moved.isCapture())
403  {
404  Ptype ptype = unpromote(moved.capturePtype());
405  const int count = state.countPiecesOnStand(P, ptype)-1;
406  const int attack_index = index(ptype, P,
407  state.kingSquare(alt(P)),
408  count);
409  const int defense_index = index(ptype, P,
410  state.kingSquare(P),
411  count);
412  if(P==BLACK)
413  result += y_attack_table[attack_index] +y_defense_table[defense_index];
414  else
415  result -= y_attack_table[attack_index] +y_defense_table[defense_index];
416  }
417  return result;
418 }
419 ␌
421 
424 {
425  for (size_t i = 0; i < ONE_DIM; ++i)
426  {
427  int low = (i & 0x7F);
428  int high = (i >> 7);
429  if (low == high)
430  continue;
431  for (int s = 0; s < NStages; ++s)
432  {
433  table[i][s] = weights.value(i + ONE_DIM*s);
434  if (high > low)
435  {
436  table[(low << 7) | high][s] = -table[i][s];
437  }
438  }
439  }
440 }
441 
444 {
445  int black_index = 0;
446  int white_index = 0;
447  black_index |= ((state.hasPieceOnStand<ROOK>(BLACK) ? 1 : 0) << 6);
448  black_index |= ((state.hasPieceOnStand<BISHOP>(BLACK) ? 1 : 0) << 5);
449  black_index |= ((state.hasPieceOnStand<GOLD>(BLACK) ? 1 : 0) << 4);
450  black_index |= ((state.hasPieceOnStand<SILVER>(BLACK) ? 1 : 0) << 3);
451  black_index |= ((state.hasPieceOnStand<KNIGHT>(BLACK) ? 1 : 0) << 2);
452  black_index |= ((state.hasPieceOnStand<LANCE>(BLACK) ? 1 : 0) << 1);
453  black_index |= ((state.hasPieceOnStand<PAWN>(BLACK) ? 1 : 0) << 0);
454  white_index |= ((state.hasPieceOnStand<ROOK>(WHITE) ? 1 : 0) << 6);
455  white_index |= ((state.hasPieceOnStand<BISHOP>(WHITE) ? 1 : 0) << 5);
456  white_index |= ((state.hasPieceOnStand<GOLD>(WHITE) ? 1 : 0) << 4);
457  white_index |= ((state.hasPieceOnStand<SILVER>(WHITE) ? 1 : 0) << 3);
458  white_index |= ((state.hasPieceOnStand<KNIGHT>(WHITE) ? 1 : 0) << 2);
459  white_index |= ((state.hasPieceOnStand<LANCE>(WHITE) ? 1 : 0) << 1);
460  white_index |= ((state.hasPieceOnStand<PAWN>(WHITE) ? 1 : 0) << 0);
461  return table[(black_index << 7) | white_index];
462 }
463 ␌
464 
465 namespace osl
466 {
467  namespace eval
468  {
469  namespace ml
470  {
471  template void NonPawnPieceStandTurn::evalWithUpdateBang<BLACK>(const NumEffectState &, Move, MultiIntPair &);
472  template void NonPawnPieceStandTurn::evalWithUpdateBang<WHITE>(const NumEffectState &, Move, MultiIntPair &);
473  template MultiInt PieceStandY::evalWithUpdate<BLACK>(const NumEffectState &, Move, const MultiInt &);
474  template MultiInt PieceStandY::evalWithUpdate<WHITE>(const NumEffectState &, Move, const MultiInt &);
475  }
476  }
477 }
478 // ;;; Local Variables:
479 // ;;; mode:c++
480 // ;;; c-basic-offset:2
481 // ;;; End:
圧縮していない moveの表現 .
Definition: basic_type.h:1052
Ptype ptype() const
Definition: basic_type.h:1155
Player player() const
Definition: basic_type.h:1195
bool isDrop() const
Definition: basic_type.h:1150
Ptype capturePtype() const
Definition: basic_type.h:1180
bool isCapture() const
Definition: basic_type.h:1148
利きを持つ局面
static const CArray< Ptype, 7 > order
持駒の表示で良く使われる順番.
int getIndexLimit(Ptype ptype) const
Definition: ptypeTable.h:93
int getIndexMin(Ptype ptype) const
Definition: ptypeTable.h:88
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition: simpleState.h:191
Square kingSquare() const
Definition: simpleState.h:94
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
Definition: simpleState.h:182
static void setUp(const Weights &weights)
static MultiInt sumUp(const CArray< int, 6 > &indices, const CArray< MultiInt, 5625 > &values)
static CArray< MultiInt, 5625 > check_table
static MultiInt eval(const NumEffectState &state, const CArray< bool, 2 > &can_check)
static CArray< MultiInt, 5625 > table
static MultiInt evalWithUpdate(const NumEffectState &state, Move moved, const MultiInt &last_value, const CArray< bool, 2 > &could_check, const CArray< bool, 2 > &can_check)
static int index(int rook, int bishop, int gold, int silver, int knight, int lance)
static void setUp(const Weights &weights)
static CArray< MultiInt, 44 > table
static void evalWithUpdateBang(const NumEffectState &state, Move moved, MultiIntPair &last_value_and_out)
static void eval(const NumEffectState &state, MultiIntPair &out)
static void setUp(const Weights &weights, int stage)
static MultiInt eval(int black_count, int white_count)
static CArray< MultiInt, 21 > table
static MultiInt eval(const NumEffectState &state)
static void setUp(const Weights &weights)
static CArray< MultiInt, 16384 > table
static CArray< MultiInt, 360 > y_attack_table
static int index(Ptype ptype, Player player, Square king, int count)
static CArray< MultiInt, 9 *7 *19 > y_attack_table_sum
static void updateResult(NumEffectState const &state, MultiInt &result, int i, Ptype ptype, CArray< Square, 2 > const &kings)
static MultiInt evalWithUpdate(const NumEffectState &state, Move moved, const MultiInt &last_value)
static MultiInt eval(const NumEffectState &state)
static CArray< MultiInt, 9 *7 *19 > y_defense_table_sum
static CArray< MultiInt, 360 > y_defense_table
static void setUp(const Weights &weights)
static CArray< MultiInt, osl::Piece::SIZE > table
static MultiInt eval(const NumEffectState &state)
static void setUp(const Weights &weights, int stage)
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ ROOK
Definition: basic_type.h:100
@ BISHOP
Definition: basic_type.h:99
@ PAWN
Definition: basic_type.h:95
@ KING
Definition: basic_type.h:93
@ KNIGHT
Definition: basic_type.h:97
@ SILVER
Definition: basic_type.h:98
@ GOLD
Definition: basic_type.h:94
@ LANCE
Definition: basic_type.h:96
const PtypeTable Ptype_Table
Definition: tables.cc:97
QuadIntPair MultiIntPair
Definition: midgame.h:14
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
Definition: basic_type.h:157
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