All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
eval/ml/pieceStand.cc
Go to the documentation of this file.
1 #include "osl/pieceStand.h"
3 #include <boost/foreach.hpp>
4 
7 
9 PieceStand::setUp(const Weights &weights,int stage)
10 {
11  for (size_t i = 0; i < weights.dimension(); ++i)
12  {
13  table[i][stage] = weights.value(i);
14  }
15 }
16 
18  const osl::state::NumEffectState &state)
19 {
21  BOOST_FOREACH(Ptype ptype, osl::PieceStand::order)
22  {
23  const int black_count =
24  state.countPiecesOnStand(BLACK, ptype);
25  const int white_count =
26  state.countPiecesOnStand(WHITE, ptype);
27  for (int j = 0; j < black_count; ++j)
28  {
29  result += table[Ptype_Table.getIndexMin(ptype) + j];
30  }
31  for (int j = 0; j < white_count; ++j)
32  {
33  result -= table[Ptype_Table.getIndexMin(ptype) + j];
34  }
35  }
36  return result;
37 }
38 
39 
40 
43 
44 void osl::eval::ml::
45 NonPawnPieceStand::setUp(const Weights &weights,int stage)
46 {
47  for (size_t i = 0; i < weights.dimension(); ++i)
48  {
49  table[i][stage] = weights.value(i);
50  }
51 }
52 
54 NonPawnPieceStand::eval(int black_count, int white_count)
55 {
56  return table[black_count] - table[white_count];
57 }
58 
59 
62 
64 NonPawnPieceStandCombination::sumUp(const CArray<int, 6> &indices,
65  const CArray<MultiInt, 5625> &values)
66 {
68  for (int rook = 0; rook <= indices[0]; ++rook)
69  {
70  for (int bishop = 0; bishop <= indices[1]; ++bishop)
71  {
72  for (int gold = 0; gold <= indices[2]; ++gold)
73  {
74  for (int silver = 0; silver <= indices[3]; ++silver)
75  {
76  for (int knight = 0; knight <= indices[4]; ++knight)
77  {
78  for (int lance = 0; lance <= indices[5]; ++lance)
79  {
80  if (rook + bishop + gold + silver + knight + lance == 0)
81  {
82  continue;
83  }
84  result += values[index(rook, bishop,
85  gold, silver, knight, lance)];
86  }
87  }
88  }
89  }
90  }
91  }
92  return result;
93 }
94 
95 void osl::eval::ml::
97 {
98  CArray<MultiInt, 5625> orig_table;
99  for (size_t i = 0; i < ONE_DIM; ++i)
100  {
101  for (int s=0; s<NStages; ++s)
102  {
103  orig_table[i][s] = weights.value(i + ONE_DIM*s);
104  }
105  }
106  CArray<int, 6> indices;
107  for (indices[0] = 0; indices[0] <= 2; ++indices[0])
108  {
109  for (indices[1] = 0; indices[1] <= 2; ++indices[1])
110  {
111  for (indices[2] = 0; indices[2] <= 4; ++indices[2])
112  {
113  for (indices[3] = 0; indices[3] <= 4; ++indices[3])
114  {
115  for (indices[4] = 0; indices[4] <= 4; ++indices[4])
116  {
117  for (indices[5] = 0; indices[5] <= 4; ++indices[5])
118  {
119  table[index(indices[0],
120  indices[1],
121  indices[2],
122  indices[3],
123  indices[4],
124  indices[5])] = sumUp(indices, orig_table);
125  }
126  }
127  }
128  }
129  }
130  }
131  table[0] = orig_table[0];
132 }
133 
134 void osl::eval::ml::
136 {
137  CArray<MultiInt, 5625> orig_table;
138  for (size_t i = 0; i < ONE_DIM; ++i)
139  {
140  for (int s=0; s<NStages; ++s)
141  {
142  orig_table[i][s] = weights.value(i + ONE_DIM*s);
143  }
144  }
145  CArray<int, 6> indices;
146  for (indices[0] = 0; indices[0] <= 2; ++indices[0])
147  {
148  for (indices[1] = 0; indices[1] <= 2; ++indices[1])
149  {
150  for (indices[2] = 0; indices[2] <= 4; ++indices[2])
151  {
152  for (indices[3] = 0; indices[3] <= 4; ++indices[3])
153  {
154  for (indices[4] = 0; indices[4] <= 4; ++indices[4])
155  {
156  for (indices[5] = 0; indices[5] <= 4; ++indices[5])
157  {
160  indices[1],
161  indices[2],
162  indices[3],
163  indices[4],
164  indices[5])] =
165  NonPawnPieceStandCombination::sumUp(indices, orig_table);
166  }
167  }
168  }
169  }
170  }
171  }
173 }
174 
176 NonPawnPieceStandCombination::eval(const NumEffectState &state,
177  const CArray<bool, 2> &can_check)
178 {
179  const int black_index = index(state.countPiecesOnStand<ROOK>(BLACK),
180  state.countPiecesOnStand<BISHOP>(BLACK),
181  state.countPiecesOnStand<GOLD>(BLACK),
182  state.countPiecesOnStand<SILVER>(BLACK),
183  state.countPiecesOnStand<KNIGHT>(BLACK),
184  state.countPiecesOnStand<LANCE>(BLACK));
185  const int white_index = index(state.countPiecesOnStand<ROOK>(WHITE),
186  state.countPiecesOnStand<BISHOP>(WHITE),
187  state.countPiecesOnStand<GOLD>(WHITE),
188  state.countPiecesOnStand<SILVER>(WHITE),
189  state.countPiecesOnStand<KNIGHT>(WHITE),
190  state.countPiecesOnStand<LANCE>(WHITE));
192  result = table[black_index] - table[white_index];
193  if (can_check[WHITE])
194  {
195  result += check_table[black_index];
196  }
197  if (can_check[BLACK])
198  {
199  result -= check_table[white_index];
200  }
201  return result;
202 }
203 
206  const NumEffectState &state,
207  Move moved,
208  const MultiInt &last_value,
209  const CArray<bool, 2> &could_check,
210  const CArray<bool, 2> &can_check)
211 {
212  if (!moved.isDrop() && ! moved.isCapture() &&
213  could_check[0] == can_check[0] && could_check[1] == can_check[1])
214  {
215  return last_value;
216  }
217  return eval(state, can_check);
218 }
219 
220 
222 
223 void osl::eval::ml::
225 {
226  for (size_t i = 0; i < ONE_DIM; ++i)
227  {
228  for (int s=0; s<NStages; ++s)
229  table[i][s] = weights.value(i + ONE_DIM*s);
230  }
231 }
232 
233 void osl::eval::ml::
234 NonPawnPieceStandTurn::eval(const NumEffectState &state, MultiIntPair& result)
235 {
236  result = MultiIntPair();
237  BOOST_FOREACH(Ptype ptype, osl::PieceStand::order)
238  {
239  if (ptype == PAWN)
240  continue;
241  const int black_count = state.countPiecesOnStand(BLACK, ptype);
242  const int white_count = state.countPiecesOnStand(WHITE, ptype);
243  for (int j = 0; j < black_count; ++j)
244  {
245  const int index_black = index(BLACK, BLACK, ptype, j);
246  const int index_white = index(BLACK, WHITE, ptype, j);
247  result[BLACK] += table[index_black];
248  result[WHITE] += table[index_white];
249  }
250  for (int j = 0; j < white_count; ++j)
251  {
252  const int index_black = index(WHITE, BLACK, ptype, j);
253  const int index_white = index(WHITE, WHITE, ptype, j);
254  result[BLACK] -= table[index_black];
255  result[WHITE] -= table[index_white];
256  }
257  }
258 }
259 
260 template<osl::Player P>
261 void osl::eval::ml::
263  const NumEffectState &state,
264  Move moved, MultiIntPair &result)
265 {
266  assert(P==moved.player());
267  if (!moved.isDrop() && ! moved.isCapture())
268  return;
269 
270  if (moved.isDrop())
271  {
272  const Ptype ptype = moved.ptype();
273  if (ptype == PAWN)
274  return;
275  const int count =
276  state.countPiecesOnStand(P, moved.ptype());
277  const int index_black = index(P, BLACK, moved.ptype(), count);
278  const int index_white = index(P, WHITE, moved.ptype(), count);
279  if(P==BLACK){
280  result[BLACK] -= table[index_black];
281  result[WHITE] -= table[index_white];
282  }
283  else{
284  result[BLACK] += table[index_black];
285  result[WHITE] += table[index_white];
286  }
287  }
288  if (moved.isCapture() &&
289  unpromote(moved.capturePtype()) != PAWN)
290  {
291  Ptype ptype = unpromote(moved.capturePtype());
292  const int count = state.countPiecesOnStand(P, ptype) - 1;
293  const int index_black = index(P, BLACK, ptype, count);
294  const int index_white = index(P, WHITE, ptype, count);
295  if(P==BLACK){
296  result[BLACK] += table[index_black];
297  result[WHITE] += table[index_white];
298  }
299  else{
300  result[BLACK] -= table[index_black];
301  result[WHITE] -= table[index_white];
302  }
303  }
304 }
305 
306 
311 
312 void osl::eval::ml::
314 {
315  for (size_t i = 0; i < ONE_DIM; ++i)
316  {
317  for (int s=0; s<NStages; ++s)
318  {
319  y_attack_table[i][s] = weights.value(i + ONE_DIM * 2 * s);
320  y_defense_table[i][s] = weights.value(i + ONE_DIM * 2 * s + ONE_DIM);
321  }
322  }
323  for (int i=0;i<7;i++){
324  Ptype ptype=osl::PieceStand::order[i];
325  int ptypeSize=Ptype_Table.getIndexLimit(ptype)-Ptype_Table.getIndexMin(ptype);
326  for(int king_y=1;king_y<=9;king_y++){
327  MultiInt attack_sum, defense_sum;
328  for(int count=0;count<=ptypeSize;count++){
329 #if 0
330  int oldIndex=(king_y - 1) * 40 + Ptype_Table.getIndexMin(ptype) + count;
331  int newIndex=(king_y - 1) * 7*19 + i*19 + count;
332 #else
333  int oldIndex=index(ptype,BLACK,Square(5,king_y),count);
334  int newIndex=index(i,BLACK,Square(5,king_y),count);
335 #endif
336  y_attack_table_sum[newIndex]=attack_sum;
337  y_defense_table_sum[newIndex]=defense_sum;
338  if(count==ptypeSize) break;
339  attack_sum += y_attack_table[oldIndex];
340  defense_sum += y_defense_table[oldIndex];
341  }
342  }
343  }
344 }
345 
346 inline
348 {
349  const int black_count = state.countPiecesOnStand(BLACK, ptype);
350  const int white_count = state.countPiecesOnStand(WHITE, ptype);
351  const int attack_index_1 = PieceStandY::index(i, BLACK, kings[WHITE], black_count);
352  const int attack_index_2 = PieceStandY::index(i, WHITE, kings[BLACK], white_count);
353  const int defense_index_1 = PieceStandY::index(i, BLACK, kings[BLACK], black_count);
354  const int defense_index_2 = PieceStandY::index(i, WHITE, kings[WHITE], white_count);
355  result += y_attack_table_sum[attack_index_1] - y_attack_table_sum[attack_index_2] +
356  y_defense_table_sum[defense_index_1] - y_defense_table_sum[defense_index_2];
357 }
358 
360 PieceStandY::eval(const NumEffectState &state)
361 {
363  const CArray<Square,2> kings = {{
364  state.kingSquare(BLACK),
365  state.kingSquare(WHITE),
366  }};
367  updateResult(state,result,0,ROOK,kings);
368  updateResult(state,result,1,BISHOP,kings);
369  updateResult(state,result,2,GOLD,kings);
370  updateResult(state,result,3,SILVER,kings);
371  updateResult(state,result,4,KNIGHT,kings);
372  updateResult(state,result,5,LANCE,kings);
373  updateResult(state,result,6,PAWN,kings);
374  return result;
375 }
376 
377 template<osl::Player P>
380  const NumEffectState &state,
381  Move moved, const MultiInt &last_value)
382 {
383  if (moved.ptype() == KING)
384  return eval(state);
385 
386  MultiInt result(last_value);
387  if (moved.isDrop())
388  {
389  const Ptype ptype = moved.ptype();
390  const int count =
391  state.countPiecesOnStand(P, ptype);
392  const int attack_index = index(ptype, P,
393  state.kingSquare(alt(P)),
394  count);
395  const int defense_index = index(ptype, P,
396  state.kingSquare(P),
397  count);
398  if(P==BLACK)
399  result -= y_attack_table[attack_index] +y_defense_table[defense_index];
400  else
401  result += y_attack_table[attack_index] +y_defense_table[defense_index];
402  }
403  if (moved.isCapture())
404  {
405  Ptype ptype = unpromote(moved.capturePtype());
406  const int count = state.countPiecesOnStand(P, ptype)-1;
407  const int attack_index = index(ptype, P,
408  state.kingSquare(alt(P)),
409  count);
410  const int defense_index = index(ptype, P,
411  state.kingSquare(P),
412  count);
413  if(P==BLACK)
414  result += y_attack_table[attack_index] +y_defense_table[defense_index];
415  else
416  result -= y_attack_table[attack_index] +y_defense_table[defense_index];
417  }
418  return result;
419 }
420 
422 
423 void osl::eval::ml::
425 {
426  for (size_t i = 0; i < ONE_DIM; ++i)
427  {
428  int low = (i & 0x7F);
429  int high = (i >> 7);
430  if (low == high)
431  continue;
432  for (int s = 0; s < NStages; ++s)
433  {
434  table[i][s] = weights.value(i + ONE_DIM*s);
435  if (high > low)
436  {
437  table[(low << 7) | high][s] = -table[i][s];
438  }
439  }
440  }
441 }
442 
444 PieceStandCombinationBoth::eval(const NumEffectState &state)
445 {
446  int black_index = 0;
447  int white_index = 0;
448  black_index |= ((state.hasPieceOnStand<ROOK>(BLACK) ? 1 : 0) << 6);
449  black_index |= ((state.hasPieceOnStand<BISHOP>(BLACK) ? 1 : 0) << 5);
450  black_index |= ((state.hasPieceOnStand<GOLD>(BLACK) ? 1 : 0) << 4);
451  black_index |= ((state.hasPieceOnStand<SILVER>(BLACK) ? 1 : 0) << 3);
452  black_index |= ((state.hasPieceOnStand<KNIGHT>(BLACK) ? 1 : 0) << 2);
453  black_index |= ((state.hasPieceOnStand<LANCE>(BLACK) ? 1 : 0) << 1);
454  black_index |= ((state.hasPieceOnStand<PAWN>(BLACK) ? 1 : 0) << 0);
455  white_index |= ((state.hasPieceOnStand<ROOK>(WHITE) ? 1 : 0) << 6);
456  white_index |= ((state.hasPieceOnStand<BISHOP>(WHITE) ? 1 : 0) << 5);
457  white_index |= ((state.hasPieceOnStand<GOLD>(WHITE) ? 1 : 0) << 4);
458  white_index |= ((state.hasPieceOnStand<SILVER>(WHITE) ? 1 : 0) << 3);
459  white_index |= ((state.hasPieceOnStand<KNIGHT>(WHITE) ? 1 : 0) << 2);
460  white_index |= ((state.hasPieceOnStand<LANCE>(WHITE) ? 1 : 0) << 1);
461  white_index |= ((state.hasPieceOnStand<PAWN>(WHITE) ? 1 : 0) << 0);
462  return table[(black_index << 7) | white_index];
463 }
464 
465 
466 namespace osl
467 {
468  namespace eval
469  {
470  namespace ml
471  {
472  template void NonPawnPieceStandTurn::evalWithUpdateBang<BLACK>(const NumEffectState &, Move, MultiIntPair &);
473  template void NonPawnPieceStandTurn::evalWithUpdateBang<WHITE>(const NumEffectState &, Move, MultiIntPair &);
474  template MultiInt PieceStandY::evalWithUpdate<BLACK>(const NumEffectState &, Move, const MultiInt &);
475  template MultiInt PieceStandY::evalWithUpdate<WHITE>(const NumEffectState &, Move, const MultiInt &);
476  }
477  }
478 }
479 // ;;; Local Variables:
480 // ;;; mode:c++
481 // ;;; c-basic-offset:2
482 // ;;; End: