kingTable.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kingTable.h
Go to the documentation of this file.
1 /* kingTable.h
2 */
3
4 #ifndef EVAL_ML_KINGTABLE_H
5 #define EVAL_ML_KINGTABLE_H
6
7 #include "osl/eval/ml/weights.h"
8 #include "osl/eval/ml/midgame.h"
9 #include "osl/ptype.h"
10 #include "osl/misc/carray.h"
11 #include "osl/misc/carray3d.h"
12 #include "osl/state/numEffectState.h"
13 #include "osl/container/tripleInt.h"
14
15 namespace osl
16 {
17 namespace eval
18 {
19 namespace ml
20 {
21 struct KingPieceRelative
22 {
23 typedef CArray2d<MultiInt, PTYPE_SIZE, 17 * 9> table_t;
24 static table_t attack_table, defense_table;
25 static int index(const NumEffectState &,
26 Player owner,
27 const Square position,
28 Square king)
29 {
30 return std::abs(position.x() - king.x()) * 17 +
31 (owner == BLACK ? (king.y() - position.y()) :
32 (position.y() - king.y())) + 8;
33 }
34 static int index(const NumEffectState &state,
35 Player owner,
36 const Square position,
37 bool same_king)
38 {
39 const Square king = state.kingSquare(
40 same_king ? owner : alt(owner));
41 return index(state, owner, position, king);
42 }
43 static int index(const NumEffectState &state,
44 PtypeO ptypeo,
45 const Square position,
46 bool same_king)
47 {
48 return index(state, getOwner(ptypeo), position, same_king);
49 }
50 static int index(const NumEffectState &state,
51 const Piece piece,
52 bool same_king)
53 {
54 return index(state, piece.owner(), piece.square(), same_king);
55 }
56 enum { DIM = ((osl::PTYPE_MAX - osl::PTYPE_PIECE_MIN + 1) *
57 (17 * 9)) * 2};
58 enum { TABLE_DIM = DIM / 2};
59 static void setUp(const Weights &weights, int stage);
60 static MultiInt eval(const NumEffectState &state);
61 template<Player P>
62 static MultiInt evalWithUpdate(const NumEffectState &state, Move moved,
63 const MultiInt& last_values);
64
65 };
66
67 class KingPieceRelativeNoSupport
68 {
69 public:
70 enum { ONE_DIM = 4284, DIM = ONE_DIM * EvalStages };
71 static MultiInt eval(const NumEffectState &state);
72 static MultiInt evalWithUpdate(
73 const NumEffectState &state,
74 Move moved,
75 const CArray<PieceMask, 2> &effected_mask,
76 const MultiInt &last_values);
77 static void setUp(const Weights &weights);
78 template <int Sign>
79 static void adjust(int attack, int defense, MultiInt& out);
80 private:
81 static int index(const Player player, const Square king,
82 const Ptype ptype, const Square pos)
83 {
84 const int x = std::abs(pos.x() - king.x());
85 const int y = (king.y() - pos.y()) *
86 (player == osl::BLACK ? 1 : -1) + 8;
87 return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
88 }
89 static int index(const Player player, const Square king,
90 const Piece piece)
91 {
92 return index(player, king, piece.ptype(), piece.square());
93 }
94 static CArray<MultiInt, ONE_DIM> table;
95 };
96
97 struct PtypeYY
98 {
99 enum { ONE_DIM = 2592, DIM = ONE_DIM * EvalStages};
100 static void setUp(const Weights &weights);
101 static MultiInt eval(const NumEffectState &state);
102 static MultiInt evalWithUpdate(
103 const NumEffectState& state,
104 Move moved,
105 const MultiInt &last_values);
106 static CArray<MultiInt, 2592> table;
107 template <int Sign>
108 static void adjust(int black, int white, MultiInt &out);
109 private:
110 template <Player KingPlayer>
111 static int index(const Piece p, const Square king)
112 {
113 return index<KingPlayer>(p.ptypeO(), p.square(), king);
114 }
115
116 template <Player KingPlayer>
117 static int index(const PtypeO ptypeO, const Square position,
118 const Square king)
119 {
120 const int king_y = (KingPlayer == BLACK ? king.y() : 10 - king.y());
121 const int piece_y = (KingPlayer == BLACK ? position.y() :
122 10 - position.y());
123 return (king_y - 1) * 9 * 32 + (piece_y - 1) * 32 +
124 (KingPlayer == BLACK ? ptypeO : alt(ptypeO)) - PTYPEO_MIN;
125 }
126 };
127
128 class King25Effect
129 {
130 private:
131 static void countEffectAndPieces(const osl::state::NumEffectState &state,
132 const osl::Player attack,
133 int &effect,
134 int &piece);
135 static int index(int effect, int piece_count);
136 public:
137 enum { DIM = 17 * 128 };
138 static CArray<int, DIM> table;
139 static void setUp(const Weights &weights);
140 static int eval(const NumEffectState &state);
141 };
142
143 class King25EffectBoth
144 {
145 private:
146 static int index(int effect, int piece_count)
147 {
148 return effect + 128 * piece_count;
149 }
150 public:
151 template <Player Attack>
152 static void countEffectAndPiecesBoth(
153 const osl::state::NumEffectState &state,
154 PieceMask& effect25,
155 PieceMask& effect_supported,
156 int &attack_effect,
157 int &attack_piece,
158 int &defense_effect,
159 int &defence_piece,
160 int &attack_piece_supported,
161 CArray<int, 5> &effect_vertical,
162 CArray<int, 5> &king_vertical);
163 enum { DIM = 17 * 128 * 2};
164 static CArray<int, DIM/2> attack_table;
165 static CArray<int, DIM/2> defense_table;
166 static void setUp(const Weights &weights);
167 static int eval(const NumEffectState &,
168 int black_attack_effect, int black_attack_piece,
169 int white_attack_effect, int white_attack_piece,
170 int black_defense_effect, int black_defense_piece,
171 int white_defense_effect, int white_defense_piece)
172 {
173 return attack_table[index(black_attack_effect, black_attack_piece)] -
174 attack_table[index(white_attack_effect, white_attack_piece)] +
175 defense_table[index(black_defense_effect, black_defense_piece)] -
176 defense_table[index(white_defense_effect, white_defense_piece)];
177 }
178 };
179
180 class King25EffectAttack
181 {
182 private:
183 static int index(int effect, int piece_count)
184 {
185 return effect + 128 * piece_count;
186 }
187 public:
188 enum { DIM = 17 * 128};
189 static CArray<int, 17 * 128> table;
190 static void setUp(const Weights &weights)
191 {
192 for (size_t i = 0; i < weights.dimension(); ++i)
193 {
194 table[i] = weights.value(i);
195 }
196 }
197 static int eval(const NumEffectState &,
198 int black_effect, int black_piece,
199 int white_effect, int white_piece)
200 {
201 return table[index(black_effect, black_piece)] -
202 table[index(white_effect, white_piece)];
203 }
204 };
205
206 class King25EffectDefense
207 {
208 private:
209 static int index(int effect, int piece_count)
210 {
211 return effect + 128 * piece_count;
212 }
213 public:
214 enum { DIM = 17 * 128};
215 static CArray<MultiInt, 17 * 128> table;
216 static void setUp(const Weights &weights,int stage)
217 {
218 for (size_t i = 0; i < weights.dimension(); ++i)
219 {
220 table[i][stage] = weights.value(i);
221 }
222 }
223 static MultiInt eval(const NumEffectState &,
224 int black_effect, int black_piece,
225 int white_effect, int white_piece)
226 {
227 return table[index(black_effect, black_piece)] -
228 table[index(white_effect, white_piece)];
229 }
230 };
231
232
233 class King25EffectYAttack
234 {
235 private:
236 static int index(int king_y, int effect, int piece_count)
237 {
238 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
239 }
240 public:
241 enum { DIM = 17 * 128 * 9};
242 static CArray<int, 17 * 128 * 9> table;
243 static void setUp(const Weights &weights)
244 {
245 for (size_t i = 0; i < weights.dimension(); ++i)
246 {
247 table[i] = weights.value(i);
248 }
249 }
250 static int eval(const NumEffectState &state,
251 int black_effect, int black_piece,
252 int white_effect, int white_piece)
253 {
254 // ugly hack. -1 is attack. 0 >= is defense
255 return table[index(10 - state.kingSquare<WHITE>().y(),
256 black_effect, black_piece)] -
257 table[index(state.kingSquare<BLACK>().y(),
258 white_effect, white_piece)];
259 }
260 };
261
262 class King25EffectYDefense
263 {
264 private:
265 static int index(int king_y, int effect, int piece_count)
266 {
267 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
268 }
269 public:
270 enum { DIM = 17 * 128 * 9};
271 static CArray<MultiInt, 17 * 128 * 9> table;
272 static void setUp(const Weights &weights,int stage)
273 {
274 for (size_t i = 0; i < weights.dimension(); ++i)
275 {
276 table[i][stage] = weights.value(i);
277 }
278 }
279 static MultiInt eval(const NumEffectState &state,
280 int black_effect, int black_piece,
281 int white_effect, int white_piece)
282 {
283 return table[index(state.kingSquare<BLACK>().y(),
284 black_effect, black_piece)] -
285 table[index(10 - state.kingSquare<WHITE>().y(),
286 white_effect, white_piece)];
287 }
288 };
289
290 class King25EffectY
291 {
292 public:
293 enum { DIM = 17 * 128 * 2 * 9};
294 private:
295 static int index(int king_y, int effect, int piece_count)
296 {
297 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
298 }
299 static CArray<int, DIM/2> attack_table;
300 static CArray<int, DIM/2> defense_table;
301 public:
302 static void setUp(const Weights &weights);
303 static int eval(const NumEffectState &state,
304 int black_attack_effect, int black_attack_piece,
305 int white_attack_effect, int white_attack_piece,
306 int black_defense_effect, int black_defense_piece,
307 int white_defense_effect, int white_defense_piece)
308 {
309 return attack_table[index(10 - state.kingSquare<WHITE>().y(),
310 black_attack_effect, black_attack_piece)] -
311 attack_table[index(state.kingSquare<BLACK>().y(),
312 white_attack_effect, white_attack_piece)] +
313 defense_table[index(state.kingSquare<BLACK>().y(),
314 black_defense_effect, black_defense_piece)] -
315 defense_table[index(10 - state.kingSquare<WHITE>().y(),
316 white_defense_effect, white_defense_piece)];
317 }
318 };
319
320 class King25Effect2
321 {
322 public:
323 enum { ONE_DIM = 17 * 13 * 64, DIM = ONE_DIM * EvalStages };
324 private:
325 static int index(int effect, int piece_count,
326 int stand_count)
327 {
328 return (effect + 64 * piece_count) * 13 + stand_count;
329 }
330 static CArray<MultiInt, ONE_DIM> table;
331 public:
332 static void setUp(const Weights &weights);
333 static MultiInt eval(
334 const NumEffectState &,
335 int black_attack_effect, int black_attack_piece,
336 int white_attack_effect, int white_attack_piece,
337 int black_stand_count, int white_stand_count)
338 {
339 const int black_index = index(black_attack_effect,
340 black_attack_piece,
341 black_stand_count);
342 const int white_index = index(white_attack_effect,
343 white_attack_piece,
344 white_stand_count);
345 return table[black_index] - table[white_index];
346 }
347 };
348 class King25EffectY2
349 {
350 public:
351 enum { ONE_DIM = 17 * 13 * 64 * 9, DIM = ONE_DIM * EvalStages };
352 private:
353 static int index(int king_y, int effect, int piece_count,
354 int stand_count)
355 {
356 return ((effect + 64 * piece_count) * 13 + stand_count) * 9 +
357 (king_y - 1);
358 }
359 static CArray<MultiInt, ONE_DIM> table;
360 public:
361 static void setUp(const Weights &weights);
362 static MultiInt eval(
363 const NumEffectState &state,
364 int black_attack_effect, int black_attack_piece,
365 int white_attack_effect, int white_attack_piece,
366 int black_stand_count, int white_stand_count)
367 {
368 const int black_index = index(10 - state.kingSquare<WHITE>().y(),
369 black_attack_effect, black_attack_piece,
370 black_stand_count);
371 const int white_index = index(state.kingSquare<BLACK>().y(),
372 white_attack_effect, white_attack_piece,
373 white_stand_count);
374 return table[black_index] - table[white_index];
375 }
376 };
377 class King25EffectSupported
378 {
379 public:
380 enum { ONE_DIM = 17 * 17, DIM = ONE_DIM * EvalStages };
381 private:
382 static int index(int piece_count, int supported)
383 {
384 return supported * 17 + piece_count;
385 }
386 static CArray<MultiInt, ONE_DIM> table;
387 public:
388 static void setUp(const Weights &weights);
389 static MultiInt eval(
390 int black_attack_piece,
391 int white_attack_piece,
392 int black_attack_supported_piece, int white_attack_supported_piece)
393 {
394 const int black_index = index(black_attack_piece,
395 black_attack_supported_piece);
396 const int white_index = index(white_attack_piece,
397 white_attack_supported_piece);
398 return table[black_index] - table[white_index];
399 }
400 };
401 class King25EffectSupportedY
402 {
403 public:
404 enum { ONE_DIM = 17 * 17 * 9, DIM = ONE_DIM * EvalStages };
405 private:
406 static int index(int piece_count, int supported, int y)
407 {
408 return (supported * 17 + piece_count) * 9 + y - 1;
409 }
410 static CArray<MultiInt, ONE_DIM> table;
411 public:
412 static void setUp(const Weights &weights);
413 static MultiInt eval(
414 int black_attack_piece,
415 int white_attack_piece,
416 int black_attack_supported_piece, int white_attack_supported_piece,
417 int black_king_y, int white_king_y)
418 {
419 const int black_index = index(black_attack_piece,
420 black_attack_supported_piece,
421 10 - white_king_y);
422 const int white_index = index(white_attack_piece,
423 white_attack_supported_piece,
424 black_king_y);
425 return table[black_index] - table[white_index];
426 }
427 };
428 struct King25EmptySquareNoEffect
429 {
430 enum { DIM = 3 * 5 };
431 template <Player defense>
432 static int evalOne(const NumEffectState &state, const CArray<int, 15>& table);
433 template <Player defense>
434 static std::pair<int,int> evalOne(const NumEffectState &state, const CArray<int, 15>& opening, const CArray<int, 15>& ending);
435 static std::pair<CArray<int,2>, CArray<int,2> >
436 eval(const NumEffectState &state, const CArray<int, 15>& opening, const CArray<int, 15>& ending);
437 static std::pair<CArray<int,2>, CArray<int,2> >
438 evalWithUpdate(const NumEffectState &state, Move last_move,
439 const CArray<int, 15>& opening, const CArray<int, 15>& ending,
440 const CArray<int,2>& last_opening_value, const CArray<int,2>& last_ending_value);
441 static int index(int rel_x, int rel_y)
442 {
443 return (rel_y + 2) * 3 + std::abs(rel_x);
444 }
445 static void setUpBase(const Weights &weigths, CArray<int, 15>& table);
446 };
447
448 class King25EmptySquareNoEffectOpening
449 : public King25EmptySquareNoEffect
450 {
451 static CArray<int, 15> table;
452 public:
453 static void setUp(const Weights &weigths) { setUpBase(weigths, table); }
454 static const CArray<int,2> eval(const NumEffectState &state);
455 static const CArray<int, 15>& weights() { return table; }
456 };
457
458 class King25EmptySquareNoEffectEnding
459 : public King25EmptySquareNoEffect
460 {
461 static CArray<int, 15> table;
462 public:
463 static void setUp(const Weights &weigths) { setUpBase(weigths, table); }
464 static const CArray<int,2> eval(const NumEffectState &state);
465 static const CArray<int, 15>& weights() { return table; }
466 };
467
468 template <bool Opening>
469 class King25EmptyAbs
470 {
471 public:
472 enum { DIM = 5 * 5 * 5 * 9 };
473 private:
474 static CArray<int, 1125> table;
475 template <Player player>
476 static int index(Square king,
477 Square target);
478 static int index(Square king,
479 Square target, Player player) {
480 if (player == BLACK)
481 return index<BLACK>(king, target);
482 else
483 return index<WHITE>(king, target);
484 }
485 template <Player Defense>
486 static int evalOne(const osl::state::NumEffectState &state);
487 public:
488 static int evalWithUpdate(
489 const NumEffectState &state, osl::Move moved,
490 int last_value);
491 static void setUp(const Weights &weigths);
492 static int eval(const osl::state::NumEffectState &state);
493 };
494
495 class King25EmptyAbsOpening : public King25EmptyAbs<true>
496 {
497 };
498 class King25EmptyAbsEnding : public King25EmptyAbs<false>
499 {
500 };
501
502 enum EffectState
503 {
504 NO_ATTACK_DEFENSE_0,
505 NO_ATTACK_DEFENSE_1,
506 NO_ATTACK_DEFENSE_2,
507 ATTACK_DIFF_N2,
508 ATTACK_DIFF_N1,
509 ATTACK_DIFF_0,
510 ATTACK_DIFF_1,
511 ATTACK_DIFF_2,
512 STATE_MAX, // 8
513 };
514 template <int Stage>
515 class King25EffectEach
516 {
517 public:
518 enum { DIM = 5 * 3 * STATE_MAX * 3 };
519 private:
520 static CArray<int, 5 * 3 * 8 * 3> table;
521 template <Player Defense>
522 static EffectState effectState(const NumEffectState &state,
523 Square target);
524 template <Player Defense>
525 static int index(const NumEffectState &state, Square king,
526 Square target);
527 template <osl::Player Defense>
528 static int evalOne(const NumEffectState &state);
529 public:
530 static void setUp(const Weights &weigths);
531 static int eval(const NumEffectState &state);
532 };
533
534 class King25EffectEachOpening : public King25EffectEach<0>
535 {
536 };
537 class King25EffectEachMidgame : public King25EffectEach<1>
538 {
539 };
540 class King25EffectEachEnding : public King25EffectEach<2>
541 {
542 };
543
544 class King25EffectEachBothOpening
545 {
546 public:
547 enum { DIM = 5 * 3 * STATE_MAX * 3 };
548 static void setUp(const Weights &weigths);
549 };
550 class King25EffectEachBothMidgame
551 {
552 public:
553 enum { DIM = 5 * 3 * STATE_MAX * 3 };
554 static void setUp(const Weights &weigths);
555 };
556 class King25EffectEachBothMidgame2
557 {
558 public:
559 enum { DIM = 5 * 3 * STATE_MAX * 3 };
560 static void setUp(const Weights &weigths);
561 };
562 class King25EffectEachBothEnding
563 {
564 public:
565 enum { DIM = 5 * 3 * STATE_MAX * 3 };
566 static void setUp(const Weights &weigths);
567 };
568
569 class King25EffectEachBoth
570 {
571 enum EffectState
572 {
573 NO_ATTACK_DEFENSE_0,
574 NO_ATTACK_DEFENSE_1,
575 NO_ATTACK_DEFENSE_2,
576 ATTACK_DIFF_N2,
577 ATTACK_DIFF_N1,
578 ATTACK_DIFF_0,
579 ATTACK_DIFF_1,
580 ATTACK_DIFF_2,
581 STATE_MAX, // 8
582 };
583 friend class King25EffectEachBothOpening;
584 friend class King25EffectEachBothMidgame;
585 friend class King25EffectEachBothMidgame2;
586 friend class King25EffectEachBothEnding;
587 friend class King25EffectEachXY;
588 friend class King25EffectEachKXY;
589 private:
590 static CArray<MultiInt, 5 * 3 * 8 * 3> table;
591 static CArray<MultiInt, 3000> x_table;
592 static CArray<MultiInt, 3240> y_table;
593 static CArray<MultiInt, 27000> xy_table;
594 static CArray<int, 256> effect_state_table;
595 template <Player Defense>
596 static int effectStateIndex3(const NumEffectState &state,
597 Square target);
598 template <Player Defense>
599 static void index(const NumEffectState &state,
600 Square target,
601 int &index_xy,
602 int rel_y, int king_x, int king_y, int x_diff
603 );
604 template <osl::Player Defense>
605 static void evalOne(const NumEffectState &state,
606 MultiInt& out);
607 public:
608 static void eval(const NumEffectState &state,
609 MultiIntPair &out);
610 static void
611 evalWithUpdate(const NumEffectState &state, Move last_move,
612 MultiIntPair & values);
613 };
614
615 class King25EffectEachXY
616 {
617 public:
618 enum { X_DIM = 3000, Y_DIM = 3240, DIM = (X_DIM + Y_DIM) * EvalStages};
619 static void setUp(const Weights &weigths);
620 };
621
622 class King25EffectEachKXY
623 {
624 public:
625 enum { ONE_DIM = 27000, DIM = ONE_DIM * EvalStages };
626 static void setUp(const Weights &weigths);
627 };
628
629 class King3Pieces
630 {
631 friend class King3PiecesXY;
632 public:
633 enum { ONE_DIM = 3072, DIM = ONE_DIM * EvalStages };
634 static void setUp(const Weights &weights);
635 static MultiInt eval(const NumEffectState &state);
636 static MultiInt evalWithUpdate(const NumEffectState &state,
637 Move last_move,
638 MultiInt &last_value);
639 private:
640 enum Direction
641 {
642 HORIZONTAL = 0,
643 VERTICAL,
644 DIAGONAL,
645 };
646 template <Player King, Direction Dir>
647 static int index(PtypeO p1, PtypeO p2)
648 {
649 if (King == WHITE)
650 {
651 p1 = altIfPiece(p1);
652 p2 = altIfPiece(p2);
653 }
654 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir;
655 }
656 template <Player King, Direction Dir>
657 static int indexY(const Square king_position,
658 PtypeO p1, PtypeO p2)
659 {
660 if (King == WHITE)
661 {
662 p1 = altIfPiece(p1);
663 p2 = altIfPiece(p2);
664 }
665 const int king_y = (King == BLACK ? king_position.y() :
666 10 - king_position.y());
667 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir
668 + (king_y - 1) * 32 * 32 * 3;
669 }
670 template <Player King, Direction Dir>
671 static int indexX(const Square king_position,
672 PtypeO p1, PtypeO p2)
673 {
674 if (King == WHITE)
675 {
676 p1 = altIfPiece(p1);
677 p2 = altIfPiece(p2);
678 }
679 const int king_x = (king_position.x() > 5 ? 10 - king_position.x() :
680 king_position.x());
681 if (Dir == HORIZONTAL &&
682 ((King == BLACK && king_position.x() >= 6) ||
683 (King == WHITE && king_position.x() <= 4)))
684 {
685 PtypeO tmp = p1;
686 p1 = p2; p2 = tmp;
687 }
688 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir
689 + (king_x - 1) * 32 * 32 * 3;
690 }
691 static MultiInt value(int vertical_index, int horizontal_index,
692 int diagonal_index1, int diagonal_index2,
693 int vertical_index_x, int horizontal_index_x,
694 int diagonal_index1_x, int diagonal_index2_x,
695 int vertical_index_y , int horizontal_index_y,
696 int diagonal_index1_y, int diagonal_index2_y)
697 {
698 return table[vertical_index] + table[horizontal_index] +
699 table[diagonal_index1] + table[diagonal_index2] +
700 x_table[vertical_index_x] + x_table[horizontal_index_x] +
701 x_table[diagonal_index1_x] + x_table[diagonal_index2_x] +
702 y_table[vertical_index_y] + y_table[horizontal_index_y] +
703 y_table[diagonal_index1_y] + y_table[diagonal_index2_y];
704 }
705
706 template <Player King>
707 static void evalOne(const NumEffectState &state,
708 MultiInt &result);
709 static CArray<MultiInt, 3072> table;
710 static CArray<MultiInt, 15360> x_table;
711 static CArray<MultiInt, 27648> y_table;
712 };
713
714 class King3PiecesXY
715 {
716 public:
717 enum
718 {
719 X_DIM = 32 * 32 * 3 * 5,
720 Y_DIM = 32 * 32 * 3 * 9,
721 ONE_DIM = X_DIM + Y_DIM,
722 DIM = ONE_DIM * EvalStages,
723 };
724 static void setUp(const Weights &weights);
725 };
726
727 class KingMobility
728 {
729 friend class KingMobilityWithRook;
730 friend class KingMobilityWithBishop;
731 public:
732 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
733 static void setUp(const Weights &weights);
734 static MultiInt eval(const NumEffectState &state);
735 private:
736 template <Player P>
737 static MultiInt evalOne(const NumEffectState &state);
738 template<Direction Dir>
739 static int mobilityDir(Square king,Square target)
740 {
741 if(Dir==L) return king.x()-target.x()-1;
742 else if(Dir==R) return target.x()-king.x()-1;
743 else if(Dir==UL || Dir==U || Dir==UR) return target.y()-king.y()-1;
744 else return king.y()-target.y()-1;
745 }
746 static CArray<MultiInt, 3240> table;
747 static CArray<MultiInt, 3240> rook_table;
748 static CArray<MultiInt, 3240> bishop_table;
749 static CArray<MultiInt, 3240> rook_bishop_table;
750 };
751
752 class KingMobilityWithRook
753 {
754 public:
755 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
756 static void setUp(const Weights &weights);
757 };
758 class KingMobilityWithBishop
759 {
760 public:
761 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
762 static void setUp(const Weights &weights);
763 };
764
765 class KingMobilitySum
766 {
767 public:
768 enum { ONE_DIM = 2925, DIM = ONE_DIM * EvalStages };
769 static void setUp(const Weights &weights);
770 static MultiInt eval(const NumEffectState &state);
771 private:
772 template <Player P>
773 static MultiInt evalOne(const NumEffectState &state);
774 static CArray<MultiInt, 45*33> table;
775 };
776
777 class King25BothSide
778 {
779 friend class King25BothSideX;
780 friend class King25BothSideY;
781 public:
782 enum { ONE_DIM = 8192, DIM = ONE_DIM * EvalStages };
783 static void setUp(const Weights &weights);
784 template<Player P>
785 static MultiInt evalOne(const NumEffectState &state,
786 const CArray<int, 5> &effects);
787 static MultiInt eval(const NumEffectState &state,
788 const CArray<int, 5> &black,
789 const CArray<int, 5> &white);
790 private:
791 static int index(int effect1, int effect2, int i)
792 {
793 assert(0 <= effect1 && effect1 < 32);
794 assert(0 <= effect2 && effect2 < 32);
795 return effect1 + 32 * (effect2 + 32 * i);
796 }
797 template <Player P>
798 static int indexX(Square king, int effect1, int effect2,
799 int i, int j)
800 {
801 const int king_x = (king.x() >= 6 ? 10 - king.x() : king.x());
802 if ((P == BLACK && king.x() > 5) ||
803 (P == WHITE && king.x() < 5))
804 {
805 const int tmp = effect1;
806 effect1 = effect2;
807 effect2 = tmp;
808 const int tmp2 = i;
809 i = 4 - j;
810 j = 4 - tmp2;
811 }
812 if (i == 2)
813 --j;
814 const int combination = (i * 3 + j - 2);
815 assert(0 <= effect1 && effect1 < 32);
816 assert(0 <= effect2 && effect2 < 32);
817 return king_x - 1 + 5 * (effect1 + 32 *
818 (effect2 + 32 * combination));
819 }
820 static int indexX(int king_x,int effect1,int effect2, int i){
821 return king_x - 1 + 5 * (effect1 + 32 *
822 (effect2 + 32 * i));
823 }
824 template <Player P>
825 static int indexY(Square king, int effect1, int effect2, int i)
826 {
827 const int king_y = (P == BLACK ? king.y() : 10 - king.y());
828 assert(0 <= effect1 && effect1 < 32);
829 assert(0 <= effect2 && effect2 < 32);
830 return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
831 }
832 static int indexY(int king_y,int effect1,int effect2, int i){
833 return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
834 }
835 static CArray<MultiInt, 8192> table;
836 static CArray<MultiInt, 40960> x_table;
837 static CArray<MultiInt, 73728> y_table;
838 };
839 class King25BothSideX
840 {
841 public:
842 enum { ONE_DIM = 40960, DIM = ONE_DIM * EvalStages };
843 static void setUp(const Weights &weights);
844 };
845 class King25BothSideY
846 {
847 public:
848 enum { ONE_DIM = 73728, DIM = ONE_DIM * EvalStages };
849 static void setUp(const Weights &weights);
850 };
851
852 class King25Mobility
853 {
854 friend class King25MobilityX;
855 friend class King25MobilityY;
856 public:
857 enum { ONE_DIM = 4096, DIM = ONE_DIM * EvalStages };
858 static void setUp(const Weights &weights);
859 static MultiInt eval(const NumEffectState &state,
860 const CArray<int, 5> &black,
861 const CArray<int, 5> &white);
862 private:
863 static int index(int effect1, int effect2, int i)
864 {
865 assert(0 <= effect1 && effect1 < 32);
866 assert(0 <= effect2 && effect2 < 32);
867 return effect1 + 32 * (effect2 + 32 * i);
868 }
869 template <Player Defense>
870 static int indexX(Square king, int effect1, int effect2, int i)
871 {
872 const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
873 if ((Defense == BLACK && king.x() > 5) ||
874 (Defense == WHITE && king.x() < 5))
875 {
876 const int tmp = effect1;
877 effect1 = effect2;
878 effect2 = tmp;
879 i = 3 - i;
880 }
881 assert(0 <= effect1 && effect1 < 32);
882 assert(0 <= effect2 && effect2 < 32);
883 return king_x - 1 + 5 * (effect1 + 32 * (effect2 + 32 * i));
884 }
885 template <Player Defense>
886 static int indexY(Square king, int effect1, int effect2, int i)
887 {
888 const int king_y = (Defense == BLACK ? king.y() : 10 - king.y());
889 assert(0 <= effect1 && effect1 < 32);
890 assert(0 <= effect2 && effect2 < 32);
891 return king_y - 1 + 9 * (effect1 + 32 * (effect2 + 32 * i));
892 }
893 static CArray<MultiInt, 4096> table;
894 static CArray<MultiInt, 20480> x_table;
895 static CArray<MultiInt, 36864> y_table;
896 };
897 class King25MobilityX
898 {
899 public:
900 enum { ONE_DIM = 20480, DIM = ONE_DIM * EvalStages };
901 static void setUp(const Weights &weights);
902 };
903 class King25MobilityY
904 {
905 public:
906 enum { ONE_DIM = 36864, DIM = ONE_DIM * EvalStages };
907 static void setUp(const Weights &weights);
908 };
909
910 class King25Effect3
911 {
912 friend class King25Effect3Y;
913 public:
914 enum { ONE_DIM = 2400, DIM = ONE_DIM * EvalStages };
915 static void setUp(const Weights &weights);
916 static MultiInt eval(const NumEffectState &state,
917 const CArray<PieceMask, 2> &king25_mask);
918 private:
919 static int index(int piece_count, bool with_knight,
920 int stand_count, bool with_knight_on_stand,
921 int attacked_count)
922 {
923 assert(piece_count >= 0 && piece_count <= 9);
924 assert(stand_count >= 0 && stand_count <= 9);
925 assert(attacked_count >= 0 && attacked_count <= 5);
926 return (piece_count + 10 *
927 ((with_knight ? 1 : 0) + 2 *
928 (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
929 2 * attacked_count))));
930 }
931 static int indexY(int piece_count, bool with_knight,
932 int stand_count, bool with_knight_on_stand,
933 int attacked_count, int king_y)
934 {
935 assert(piece_count >= 0 && piece_count <= 9);
936 assert(stand_count >= 0 && stand_count <= 9);
937 assert(attacked_count >= 0 && attacked_count <= 5);
938 return ((piece_count + 10 *
939 ((with_knight ? 1 : 0) + 2 *
940 (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
941 2 * attacked_count))))) * 9 +
942 king_y - 1;
943 }
944 template <osl::Player Attack>
945 static MultiInt evalOne(const NumEffectState &state,
946 PieceMask king25);
947 static CArray<MultiInt, 2400> table;
948 static CArray<MultiInt, 21600> y_table;
949 };
950 class King25Effect3Y
951 {
952 public:
953 enum { ONE_DIM = 21600, DIM = ONE_DIM * EvalStages };
954 static void setUp(const Weights &weights);
955 };
956
957 class King25EffectCountCombination
958 {
959 friend class King25EffectCountCombinationY;
960 public:
961 enum { ONE_DIM = 100, DIM = ONE_DIM * EvalStages };
962 static void setUp(const Weights &weights);
963 static MultiInt eval(const NumEffectState &state,
964 const CArray<PieceMask, 2> &king25);
965 private:
966 template <osl::Player Attack>
967 static MultiInt evalOne(const NumEffectState &state,
968 PieceMask king25);
969 static CArray<MultiInt, 100> table;
970 static CArray<MultiInt, 900> y_table;
971 };
972 class King25EffectCountCombinationY
973 {
974 public:
975 enum { ONE_DIM = 900, DIM = ONE_DIM * EvalStages };
976 static void setUp(const Weights &weights);
977 };
978
979 class BishopExchangeSilverKing
980 {
981 static int indexKing(Square king)
982 {
983 const int y = king.y();
984 if (y >= 3)
985 return -1;
986 return (y-1)*9 + king.x()-1;
987 }
988 static int indexRook(Square rook)
989 {
990 assert(rook.isOnBoard());
991 const int y = rook.y();
992 if (y >= 6)
993 return -1;
994 return (y-1)*9 + rook.x()-1;
995 }
996 static int indexSilver(Square silver)
997 {
998 return (silver.y()-1)*9 + silver.x()-1;
999 }
1000 public:
1001 enum { BISHOP_ONE_DIM = 18 * 81 * (45*2), DIM = BISHOP_ONE_DIM*3 };
1002 static void setUp(const Weights &weights);
1003 static int eval(const NumEffectState& state);
1004 private:
1005 template <Player KingOwner>
1006 static int evalOne(const NumEffectState &state, int offset);
1007 static CArray<int, DIM> table;
1008 };
1009
1010 class EnterKingDefense
1011 {
1012 public:
1013 enum { DIM = (8+8+8+8)*3 };
1014 static void setUp(const Weights &weights);
1015 static int eval(const NumEffectState &state);
1016 private:
1017 template <Player KingOwner>
1018 static int evalOne(const NumEffectState &state);
1019 static CArray<int, DIM> table;
1020 };
1021 }
1022 }
1023 }
1024 #endif // EVAL_ML_KINGTABLE_H
1025 // ;;; Local Variables:
1026 // ;;; mode:c++
1027 // ;;; c-basic-offset:2
1028 // ;;; End: