majorPiece.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
majorPiece.h
Go to the documentation of this file.
1 /* majorPiece.h
2 */
3
4 #ifndef EVAL_ML_MAJORPIECE_H
5 #define EVAL_ML_MAJORPIECE_H
6
7 #include "osl/eval/ml/weights.h"
8 #include "osl/eval/ml/midgame.h"
9 #include "osl/misc/carray.h"
10 #include "osl/misc/carray2d.h"
11 #include "osl/state/numEffectState.h"
12 #include "osl/container/tripleInt.h"
13
14 namespace osl
15 {
16 namespace eval
17 {
18 namespace ml
19 {
20 template <bool Opening, Ptype MajorBasic>
21 class MajorY
22 {
23 private:
24 static CArray<int, 18> table;
25 static int index(Piece piece)
26 {
27 return ((piece.owner() == BLACK) ? (piece.square().y() - 1) :
28 (9 - piece.square().y())) + (piece.isPromoted() ? 9 : 0);
29 }
30 public:
31 enum { DIM = 18 };
32 static void setUp(const Weights &weights);
33 static int eval(const NumEffectState &state)
34 {
35 int value = 0;
36 for (int i = PtypeTraits<MajorBasic>::indexMin;
37 i < PtypeTraits<MajorBasic>::indexLimit;
38 ++i)
39 {
40 const Piece piece = state.pieceOf(i);
41 if (piece.isOnBoard())
42 {
43 if (piece.owner() == BLACK)
44 value += table[index(piece)];
45 else
46 value -= table[index(piece)];
47 }
48 }
49 return value;
50 }
51 };
52
53 class RookYOpening : public MajorY<true, ROOK>
54 {
55 };
56 class RookYEnding : public MajorY<false, ROOK>
57 {
58 };
59 class BishopYOpening : public MajorY<true, BISHOP>
60 {
61 };
62 class BishopYEnding : public MajorY<false, BISHOP>
63 {
64 };
65
66 template <bool Opening>
67 class RookPawn
68 {
69 public:
70 enum { DIM = 1 };
71 static void setUp(const Weights &weights);
72 static int eval(const NumEffectState &state);
73 private:
74 static int weight;
75 };
76 class RookPawnOpening : public RookPawn<true>
77 {
78 };
79 class RookPawnEnding : public RookPawn<false>
80 {
81 };
82
83 class RookPawnY
84 {
85 friend class RookPawnYX;
86 public:
87 enum { ONE_DIM = 180, DIM = ONE_DIM * EvalStages };
88 static void setUp(const Weights &weights);
89 static MultiInt eval(const NumEffectState &state,
90 const CArray2d<int, 2, 9> &pawns);
91 private:
92 static int index(const Piece rook, const int pawn_y)
93 {
94 const int rook_y =
95 (rook.owner() == BLACK ? rook.square().y() : 10 - rook.square().y());
96 return (rook_y - 1) * 10 + pawn_y + (rook.isPromoted() ? 90 : 0);
97 }
98 static int indexY(const Square king,
99 const Piece rook, int pawn_y)
100 {
101 const int x_diff = std::abs(rook.square().x() - king.x());
102 const int rook_y =
103 (rook.owner() == BLACK ? rook.square().y() : 10 - rook.square().y());
104 return x_diff * 10 * 9 + (rook_y - 1) * 10 + pawn_y + (rook.isPromoted() ? 810 : 0);
105 }
106 static CArray<MultiInt, 180> table;
107 static CArray<MultiInt, 1620> y_attack_table;
108 static CArray<MultiInt, 1620> y_defense_table;
109 };
110
111 class RookPawnYX
112 {
113 public:
114 enum { ONE_DIM = 1620, DIM = ONE_DIM * 2*EvalStages };
115 static void setUp(const Weights &weights);
116 };
117
118 class AllMajor
119 {
120 public:
121 enum { DIM = 1 };
122 static void setUp(const Weights &weights,int stage);
123 static MultiInt eval(int black_major_count)
124 {
125 if (black_major_count == 4)
126 return weight;
127 else if (black_major_count == 0)
128 return -weight;
129
130 return MultiInt();
131 }
132 private:
133 static MultiInt weight;
134 };
135
136 template <bool Opening>
137 class MajorGoldSilverAttacked
138 {
139 public:
140 enum { DIM = 32 };
141 static void setUp(const Weights &weights);
142 static int eval(const NumEffectState &state);
143 static int index(const NumEffectState &state, Piece piece);
144 template <Ptype PTYPE>
145 static int evalOne(const NumEffectState &state);
146 private:
147 static CArray<int, 32> table;
148 };
149
150 class MajorGoldSilverAttackedOpening : public MajorGoldSilverAttacked<true>
151 {
152 };
153 class MajorGoldSilverAttackedEnding : public MajorGoldSilverAttacked<false>
154 {
155 };
156
157 class RookEffectBase
158 {
159 friend class RookEffectPiece;
160 public:
161 enum { ONE_DIM = 612, DIM = ONE_DIM * 2 };
162 static MultiInt eval(const NumEffectState &state);
163 protected:
164 template<Player P>
165 static MultiInt evalOne(const NumEffectState& state,
166 Square rook,
167 Square myKing,
168 Square opKing,
169 Square up,
170 Square dp,
171 Square rp,
172 Square lp,
173 bool isP);
179 static int index(int abs_x_diff, int y_diff, bool horizontal, bool is_promoted)
180 {
181 return y_diff + 8 + abs_x_diff * 17 + (horizontal ? 153 : 0) +
182 (is_promoted ? 306 : 0);
183 }
192 static int index0(int abs_x_diff,int y_diff,
193 PtypeO ptypeO,
194 bool horizontal, bool promoted){
195 return y_diff+8+abs_x_diff*17+(ptypeO - PTYPEO_MIN) * 17 * 9 +
196 (horizontal ? 4896 : 0) + (promoted ? 9792 : 0);
197 }
207 static int index1(Square king,Square from,PtypeO ptypeO,bool isP)
208 {
209 int y_diff=from.y()-king.y();
210 int x_diff=from.x()-king.x();
211 return index1(x_diff,y_diff,ptypeO,isP);
212 }
221 static int index1(int x_diff,int y_diff,PtypeO ptypeO,bool isP){
222 assert(-9 <= y_diff && y_diff <= 9);
223 assert(-9 <= x_diff && x_diff <= 9);
224 assert(getPtype((PtypeO)ptypeO)!=PTYPE_EMPTY);
225 int index=(ptypeO-PTYPEO_MIN)+32*((y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0)));
226 assert(0<=index && index<32*19*19*2);
227 return index;
228 }
236 static int index2(Square king,Square from,bool isP)
237 {
238 int y_diff=from.y()-king.y();
239 int x_diff=from.x()-king.x();
240 return index2(x_diff,y_diff,isP);
241 }
248 static int index2(int x_diff,int y_diff,bool isP){
249 assert(-9 <= y_diff && y_diff <= 9);
250 assert(-9 <= x_diff && x_diff <= 9);
251 int index=(y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0));
252 assert(0<=index && index<19*19*2);
253 return index;
254 }
255 static CArray<MultiInt, 612> attack_table;
256 static CArray<MultiInt, 612> defense_table;
257 static CArray<MultiInt, 32> piece_table;
258 static CArray<MultiInt, 23104> attack_u;
259 static CArray<MultiInt, 23104> attack_d;
260 static CArray<MultiInt, 23104> attack_l;
261 static CArray<MultiInt, 23104> attack_r;
262 static CArray<MultiInt, 23104> defense_u;
263 static CArray<MultiInt, 23104> defense_d;
264 static CArray<MultiInt, 23104> defense_l;
265 static CArray<MultiInt, 23104> defense_r;
266 static CArray<MultiInt, 722> attack_nospace;
267 static CArray<MultiInt, 722> defense_nospace;
268 };
269 class RookEffect : public RookEffectBase
270 {
271 public:
272 static void setUp(const Weights &weights,int stage);
273 };
274
275 class RookEffectPiece
276 {
277 public:
278 enum { DIM = 32 * EvalStages };
279 static void setUp(const Weights &weights);
280 };
281 class RookEffectPieceKingRelative : RookEffectBase
282 {
283 public:
284 enum { ONE_DIM = 19584, DIM = ONE_DIM * 2*EvalStages };
285 static void setUp(const Weights & weights);
286 };
287
288 class RookPromoteDefense : public RookEffectBase
289 {
290 friend class RookPromoteDefenseRookH;
291 public:
292 enum { ONE_DIM = 256, DIM = ONE_DIM * EvalStages };
293 static void setUp(const Weights &weights);
294 static MultiInt eval(const NumEffectState &state);
295 private:
296 static CArray<MultiInt, 256> promote_defense_table;
297 static CArray<MultiInt, 144> promote_defense_rook_table;
298 };
299
300 class RookPromoteDefenseRookH : public RookEffectBase
301 {
302 public:
303 enum { ONE_DIM = 144, DIM = ONE_DIM * EvalStages };
304 static void setUp(const Weights &weights);
305 private:
306 };
307
308 class BishopEffectBase
309 {
310 friend class BishopEffectPiece;
311 public:
312 enum { ONE_DIM = 612, DIM = ONE_DIM * 2 };
313 static MultiInt eval(const NumEffectState &state);
314 protected:
315 template<Player P>
316 static MultiInt evalOne(const NumEffectState& state,
317 Square bishop,
318 Square myKing,
319 Square opKing,
320 Square ulp,
321 Square urp,
322 Square dlp,
323 Square drp,
324 bool isP);
325 static int index(int x_diff, int y_diff, bool ur, bool promoted)
326 {
327 if (x_diff<0)
328 ur = !ur;
329 return y_diff + 8 + std::abs(x_diff) * 17 + (ur ? 153 : 0) + (promoted ? 306 : 0);
330 }
331 static int index0(int x_diff, int y_diff,PtypeO ptypeO,bool ur, bool promoted)
332 {
333 if (x_diff>0)
334 ur = !ur;
335 return -y_diff + 8 + std::abs(x_diff) * 17 + (ptypeO - PTYPEO_MIN) * 17 * 9 +
336 (ur ? 4896 : 0) + (promoted ? 9792 : 0);
337 }
347 static int index1(Square king,Square from,PtypeO ptypeO,bool isP)
348 {
349 int y_diff=from.y()-king.y();
350 int x_diff=from.x()-king.x();
351 return index1(x_diff,y_diff,ptypeO,isP);
352 }
361 static int index1(int x_diff,int y_diff,PtypeO ptypeO,bool isP){
362 assert(-9 <= y_diff && y_diff <= 9);
363 assert(-9 <= x_diff && x_diff <= 9);
364 assert(getPtype((PtypeO)ptypeO)!=PTYPE_EMPTY);
365 int index=(ptypeO-PTYPEO_MIN)+32*((y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0)));
366 assert(0<=index && index<32*19*19*2);
367 return index;
368 }
376 static int index2(Square king,Square from,bool isP)
377 {
378 int y_diff=from.y()-king.y();
379 int x_diff=from.x()-king.x();
380 return index2(x_diff,y_diff,isP);
381 }
388 static int index2(int x_diff,int y_diff,bool isP){
389 assert(-9 <= y_diff && y_diff <= 9);
390 assert(-9 <= x_diff && x_diff <= 9);
391 int index=(y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0));
392 assert(0<=index && index<19*19*2);
393 return index;
394 }
395 static CArray<MultiInt, 612> attack_table;
396 static CArray<MultiInt, 612> defense_table;
397 static CArray<MultiInt, 32> piece_table;
398 static CArray<MultiInt, 23104> attack_ur;
399 static CArray<MultiInt, 23104> attack_ul;
400 static CArray<MultiInt, 23104> attack_dr;
401 static CArray<MultiInt, 23104> attack_dl;
402 static CArray<MultiInt, 23104> defense_ur;
403 static CArray<MultiInt, 23104> defense_ul;
404 static CArray<MultiInt, 23104> defense_dr;
405 static CArray<MultiInt, 23104> defense_dl;
406 static CArray<MultiInt, 722> attack_nospace;
407 static CArray<MultiInt, 722> defense_nospace;
408 };
409 class BishopEffect : public BishopEffectBase
410 {
411 public:
412 static void setUp(const Weights &weights,int stage);
413 };
414 class BishopEffectPiece
415 {
416 public:
417 enum { DIM = 32*EvalStages };
418 static void setUp(const Weights &weights);
419 };
420
421 class BishopEffectPieceKingRelative : BishopEffectBase
422 {
423 public:
424 enum { ONE_DIM = 19584, DIM = ONE_DIM * 2*EvalStages };
425 static void setUp(const Weights & weights);
426 };
427
428 class BishopHead
429 {
430 friend class BishopHeadKingRelative;
431 friend class BishopHeadX;
432 public:
433 enum { ONE_DIM = 32, DIM = ONE_DIM * EvalStages };
434 static void setUp(const Weights &weights);
435 static MultiInt eval(const NumEffectState &state);
436 private:
437 static int indexK(Player player, PtypeO ptypeO, int x_diff, int y_diff)
438 {
439 if (player == WHITE)
440 {
441 ptypeO=(PtypeO)(static_cast<int>(ptypeO)^(~15));
442 }
443 if (player == WHITE)
444 {
445 y_diff = -y_diff;
446 }
447 return (ptypeOIndex(ptypeO) * 9 + x_diff) * 17 + y_diff + 8;
448 }
449 template <Player P>
450 static int indexX(PtypeO ptypeO, int x)
451 {
452 if (x > 5)
453 {
454 x = 10 - x;
455 }
456 if (P == WHITE)
457 {
458 ptypeO = altIfPiece(ptypeO);
459 }
460 return x - 1 + 5 * ptypeOIndex(ptypeO);
461 }
462 static CArray<MultiInt, 32> table;
463 static CArray<MultiInt, 4896> king_table;
464 static CArray<MultiInt, 160> x_table;
465 };
466
467 class BishopHeadKingRelative
468 {
469 public:
470 enum { ONE_DIM = 4896, DIM = ONE_DIM * EvalStages };
471 static void setUp(const Weights &weights);
472 };
473 class BishopHeadX
474 {
475 public:
476 enum { ONE_DIM = 160, DIM = ONE_DIM * EvalStages };
477 static void setUp(const Weights &weights);
478 };
479 class KingRookBishop
480 {
481 public:
482 enum { ONE_DIM = 374544, DIM = ONE_DIM * EvalStages };
483 static void setUp(const Weights &weights);
484 template<Player King>
485 static MultiInt evalOne(const NumEffectState &state);
486 static MultiInt eval(const NumEffectState &state);
487 private:
488 static CArray<MultiInt, 374544> table;
489 template <Player King>
490 static int index(const Square king, const Piece rook, const Piece bishop)
491 {
492 const int rook_x = std::abs(king.x() - rook.square().x());
493 const int bishop_x = std::abs(king.x() - bishop.square().x());
494 const int rook_y = (King == BLACK ? rook.square().y() - king.y() : king.y() - rook.square().y());
495 const int bishop_y = (King == BLACK ? bishop.square().y() - king.y() : king.y() - bishop.square().y());
496 return bishop_y + 8 + 17 * (bishop_x + 9 * (rook_y + 8 + 17 * (rook_x + 9 * ((bishop.owner() == King ? 1 : 0) + 2 * ((rook.owner() == King ? 1 : 0) + 2 * (2 * (bishop.isPromoted() ? 1 : 0) + (rook.isPromoted() ? 1 : 0)))))));
497 }
498 };
499
500 class NumPiecesBetweenBishopAndKing
501 {
502 friend class NumPiecesBetweenBishopAndKingSelf;
503 friend class NumPiecesBetweenBishopAndKingOpp;
504 friend class NumPiecesBetweenBishopAndKingAll;
505 public:
506 static MultiInt eval(const NumEffectState &state);
507 private:
508 static void countBetween(const NumEffectState &state,
509 Square king, Piece bishop,
510 int &self_count, int &opp_count,
511 int &total_count);
512 static CArray<MultiInt, 9> self_table;
513 static CArray<MultiInt, 9> opp_table;
514 static CArray<MultiInt, 9> all_table;
515 };
516 class NumPiecesBetweenBishopAndKingSelf
517 {
518 public:
519 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
520 static void setUp(const Weights &weights);
521 };
522 class NumPiecesBetweenBishopAndKingOpp
523 {
524 public:
525 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
526 static void setUp(const Weights &weights);
527 };
528 class NumPiecesBetweenBishopAndKingAll
529 {
530 public:
531 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
532 static void setUp(const Weights &weights);
533 };
534 class BishopBishopPiece
535 {
536 public:
537 enum { ONE_DIM = 64, DIM = ONE_DIM * EvalStages };
538 static void setUp(const Weights &weights);
539 static MultiInt eval(const NumEffectState &state);
540 private:
541 static int index(Ptype ptype, bool self_with_support,
542 bool opp_with_support)
543 {
544 return ptype + PTYPE_SIZE * ((self_with_support ? 1 : 0) +
545 2 * (opp_with_support ? 1 : 0));
546 }
547 static CArray<MultiInt, 64> table;
548 };
549
550 class RookRook
551 {
552 public:
553 enum { ONE_DIM = 800, DIM = ONE_DIM * EvalStages };
554 static void setUp(const Weights &weights);
555 static MultiInt eval(const NumEffectState &state);
556 private:
557 template <bool SamePlayer, Player P>
558 static int index(Piece rook1, Piece rook2)
559 {
560 const int y1 = (rook1.isOnBoard() ? rook1.square().y() : 0);
561 const int y2 = (rook2.isOnBoard() ? rook2.square().y() : 0);
562 if (SamePlayer)
563 {
564 if (P == BLACK)
565 {
566 return y1 + 10 *
567 (y2 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
568 ((rook2.isPromoted() ? 1 : 0) + 2 *
569 (SamePlayer ? 1 : 0))));
570 }
571 else
572 {
573 if (y1 == 0 || y2 == 0 || y1 == y2)
574 {
575 return (10 - y1) % 10 + 10 *
576 ((10 - y2) % 10 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
577 ((rook2.isPromoted() ? 1 : 0) + 2 *
578 (SamePlayer ? 1 : 0))));
579 }
580 else
581 {
582 return (10 - y2) % 10 + 10 *
583 ((10 - y1) % 10 + 10 * ((rook2.isPromoted() ? 1 : 0) + 2 *
584 ((rook1.isPromoted() ? 1 : 0) + 2 *
585 (SamePlayer ? 1 : 0))));
586 }
587 }
588 }
589 else
590 {
591 return y1 + 10 *
592 (y2 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
593 ((rook2.isPromoted() ? 1 : 0) + 2 *
594 (SamePlayer ? 1 : 0))));
595 }
596 }
597 static int index(bool same_player, bool promoted1,
598 bool promoted2, int y1, int y2)
599 {
600 return y1 + 10 *
601 (y2 + 10 * ((promoted1 ? 1 : 0) + 2 *
602 ((promoted2 ? 1 : 0) + 2 *
603 (same_player ? 1 : 0))));
604 }
605 static CArray<MultiInt, 800> table;
606 };
607
608 class RookRookPiece
609 {
610 public:
611 enum { ONE_DIM = 128, DIM = ONE_DIM * EvalStages };
612 static void setUp(const Weights &weights);
613 static MultiInt eval(const NumEffectState &state);
614 private:
615 static int index(Ptype ptype, bool self_with_support,
616 bool opp_with_support, bool vertical)
617 {
618 return ptype + PTYPE_SIZE * ((self_with_support ? 1 : 0) +
619 2 * (opp_with_support ? 1 : 0)) +
620 (vertical ? PTYPE_SIZE * 2 * 2 : 0);
621 }
622 static CArray<MultiInt, 128> table;
623 };
624
625 class BishopStandFile5
626 {
627 public:
628 enum { ONE_DIM = 32, DIM = ONE_DIM * EvalStages };
629 static void setUp(const Weights &weights);
630 static MultiInt eval(const NumEffectState &state);
631 private:
632 static CArray<MultiInt, 32> table;
633 };
634
635 class MajorCheckWithCapture
636 {
637 public:
638 enum {
639 ONE_DIM = PTYPE_SIZE * 2/*bishop or rook*/ * 2 /*promotable*/,
640 DIM = ONE_DIM * EvalStages
641 };
642 static void setUp(const Weights &weights);
643 static MultiInt eval(const NumEffectState &state);
644 private:
645 static CArray<MultiInt, ONE_DIM> table;
646 template <Player Owner>
647 static MultiInt addOne(const NumEffectState &state);
648 static size_t index(Ptype ptype, bool is_rook, bool can_promote)
649 {
650 return ptype * 4 + is_rook * 2 + can_promote;
651 }
652 };
653
654 class RookSilverKnight
655 {
656 public:
657 enum {
658 ONE_DIM = 5 * 9 * 9 * 9 * 9 * 9,
659 DIM = ONE_DIM * EvalStages
660 };
661 static void setUp(const Weights &weights);
662 static MultiInt eval(const NumEffectState &state);
663 private:
664 static CArray<MultiInt, ONE_DIM> table;
665 static size_t index(int rook_x, int rook_y, int silver_x, int silver_y,
666 int knight_x, int knight_y)
667 {
668 return knight_y + 9 * (knight_x + 9 * (silver_y + 9 * (silver_x + 9 * (rook_y + 9 * rook_x))));
669 }
670 };
671
672 class BishopSilverKnight
673 {
674 public:
675 enum {
676 ONE_DIM = 5 * 9 * 9 * 9 * 9 * 9,
677 DIM = ONE_DIM * EvalStages
678 };
679 static void setUp(const Weights &weights);
680 static MultiInt eval(const NumEffectState &state);
681 private:
682 static CArray<MultiInt, ONE_DIM> table;
683 static size_t index(int bishop_x, int bishop_y, int silver_x, int silver_y,
684 int knight_x, int knight_y)
685 {
686 return knight_y + 9 * (knight_x + 9 * (silver_y + 9 * (silver_x + 9 * (bishop_y + 9 * bishop_x))));
687 }
688 };
689
690 class AttackMajorsInBase
691 {
692 public:
693 enum {
694 ONE_DIM = PTYPE_SIZE * PTYPE_SIZE * 2 * 2 * 2,
695 DIM = ONE_DIM * EvalStages
696 };
697 static void setUp(const Weights &weights);
698 static MultiInt eval(const NumEffectState &state);
699 private:
700 static CArray<MultiInt, ONE_DIM> table;
701 size_t maxActive() const { return 4; }
702 static int index(Ptype support, Ptype attack, bool has_gold,
703 bool rook_support, bool bishop_support)
704 {
705 return (unpromoteSafe(support)*16 + unpromoteSafe(attack))*8+has_gold*4
706 +rook_support*2+bishop_support;
707 }
708 template <Player Owner>
709 static void addOne(const NumEffectState &state, Piece rook, MultiInt&);
710 };
711 }
712 }
713 }
714
715 #endif // EVAL_ML_MAJORPIECE_H
716 // ;;; Local Variables:
717 // ;;; mode:c++
718 // ;;; c-basic-offset:2
719 // ;;; End: