00001
00002
00003
00004 #ifndef EVAL_ML_MAJORPIECE_H
00005 #define EVAL_ML_MAJORPIECE_H
00006
00007 #include "osl/eval/ml/weights.h"
00008 #include "osl/eval/ml/midgame.h"
00009 #include "osl/misc/carray.h"
00010 #include "osl/misc/carray2d.h"
00011 #include "osl/state/numEffectState.h"
00012 #include "osl/container/tripleInt.h"
00013
00014 namespace osl
00015 {
00016 namespace eval
00017 {
00018 namespace ml
00019 {
00020 template <bool Opening, Ptype MajorBasic>
00021 class MajorY
00022 {
00023 private:
00024 static CArray<int, 18> table;
00025 static int index(Piece piece)
00026 {
00027 return ((piece.owner() == BLACK) ? (piece.position().y() - 1) :
00028 (9 - piece.position().y())) + (piece.isPromoted() ? 9 : 0);
00029 }
00030 public:
00031 enum { DIM = 18 };
00032 static void setUp(const Weights &weights);
00033 static int eval(const NumEffectState &state)
00034 {
00035 int value = 0;
00036 for (int i = PtypeTraits<MajorBasic>::indexMin;
00037 i < PtypeTraits<MajorBasic>::indexLimit;
00038 ++i)
00039 {
00040 const Piece piece = state.getPieceOf(i);
00041 if (piece.isOnBoard())
00042 {
00043 if (piece.owner() == BLACK)
00044 value += table[index(piece)];
00045 else
00046 value -= table[index(piece)];
00047 }
00048 }
00049 return value;
00050 }
00051 };
00052
00053 class RookYOpening : public MajorY<true, ROOK>
00054 {
00055 };
00056 class RookYEnding : public MajorY<false, ROOK>
00057 {
00058 };
00059 class BishopYOpening : public MajorY<true, BISHOP>
00060 {
00061 };
00062 class BishopYEnding : public MajorY<false, BISHOP>
00063 {
00064 };
00065
00066 template <bool Opening>
00067 class RookPawn
00068 {
00069 public:
00070 enum { DIM = 1 };
00071 static void setUp(const Weights &weights);
00072 static int eval(const NumEffectState &state);
00073 private:
00074 static int weight;
00075 };
00076 class RookPawnOpening : public RookPawn<true>
00077 {
00078 };
00079 class RookPawnEnding : public RookPawn<false>
00080 {
00081 };
00082
00083 class RookPawnY
00084 {
00085 friend class RookPawnYX;
00086 public:
00087 enum { ONE_DIM = 180, DIM = ONE_DIM * EvalStages };
00088 static void setUp(const Weights &weights);
00089 static MultiInt eval(const NumEffectState &state,
00090 const CArray2d<int, 2, 9> &pawns);
00091 private:
00092 static int index(const Piece rook, const int pawn_y)
00093 {
00094 const int rook_y =
00095 (rook.owner() == BLACK ? rook.position().y() : 10 - rook.position().y());
00096 return (rook_y - 1) * 10 + pawn_y + (rook.isPromoted() ? 90 : 0);
00097 }
00098 static int indexY(const Position king,
00099 const Piece rook, int pawn_y)
00100 {
00101 const int x_diff = std::abs(rook.position().x() - king.x());
00102 const int rook_y =
00103 (rook.owner() == BLACK ? rook.position().y() : 10 - rook.position().y());
00104 return x_diff * 10 * 9 + (rook_y - 1) * 10 + pawn_y + (rook.isPromoted() ? 810 : 0);
00105 }
00106 static CArray<MultiInt, 180> table;
00107 static CArray<MultiInt, 1620> y_attack_table;
00108 static CArray<MultiInt, 1620> y_defense_table;
00109 };
00110
00111 class RookPawnYX
00112 {
00113 public:
00114 enum { ONE_DIM = 1620, DIM = ONE_DIM * 2*EvalStages };
00115 static void setUp(const Weights &weights);
00116 };
00117
00118 class AllMajor
00119 {
00120 public:
00121 enum { DIM = 1 };
00122 static void setUp(const Weights &weights,int stage);
00123 static MultiInt eval(int black_major_count)
00124 {
00125 if (black_major_count == 4)
00126 return weight;
00127 else if (black_major_count == 0)
00128 return -weight;
00129
00130 return MultiInt();
00131 }
00132 private:
00133 static MultiInt weight;
00134 };
00135
00136 template <bool Opening>
00137 class MajorGoldSilverAttacked
00138 {
00139 public:
00140 enum { DIM = 32 };
00141 static void setUp(const Weights &weights);
00142 static int eval(const NumEffectState &state);
00143 static int index(const NumEffectState &state, Piece piece);
00144 template <Ptype PTYPE>
00145 static int evalOne(const NumEffectState &state);
00146 private:
00147 static CArray<int, 32> table;
00148 };
00149
00150 class MajorGoldSilverAttackedOpening : public MajorGoldSilverAttacked<true>
00151 {
00152 };
00153 class MajorGoldSilverAttackedEnding : public MajorGoldSilverAttacked<false>
00154 {
00155 };
00156
00157 class RookEffectBase
00158 {
00159 friend class RookEffectPiece;
00160 public:
00161 enum { ONE_DIM = 612, DIM = ONE_DIM * 2 };
00162 static MultiInt eval(const NumEffectState &state);
00163 protected:
00164 template<Player P>
00165 static MultiInt evalOne(const NumEffectState& state,
00166 Position rook,
00167 Position myKing,
00168 Position opKing,
00169 Position up,
00170 Position dp,
00171 Position rp,
00172 Position lp,
00173 bool isP);
00179 static int index(int abs_x_diff, int y_diff, bool horizontal, bool is_promoted)
00180 {
00181 return y_diff + 8 + abs_x_diff * 17 + (horizontal ? 153 : 0) +
00182 (is_promoted ? 306 : 0);
00183 }
00192 static int index0(int abs_x_diff,int y_diff,
00193 PtypeO ptypeO,
00194 bool horizontal, bool promoted){
00195 return y_diff+8+abs_x_diff*17+(ptypeO - PTYPEO_MIN) * 17 * 9 +
00196 (horizontal ? 4896 : 0) + (promoted ? 9792 : 0);
00197 }
00207 static int index1(Position king,Position from,PtypeO ptypeO,bool isP)
00208 {
00209 int y_diff=from.y()-king.y();
00210 int x_diff=from.x()-king.x();
00211 return index1(x_diff,y_diff,ptypeO,isP);
00212 }
00221 static int index1(int x_diff,int y_diff,PtypeO ptypeO,bool isP){
00222 assert(-9 <= y_diff && y_diff <= 9);
00223 assert(-9 <= x_diff && x_diff <= 9);
00224 assert(getPtype((PtypeO)ptypeO)!=PTYPE_EMPTY);
00225 int index=(ptypeO-PTYPEO_MIN)+32*((y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0)));
00226 assert(0<=index && index<32*19*19*2);
00227 return index;
00228 }
00236 static int index2(Position king,Position from,bool isP)
00237 {
00238 int y_diff=from.y()-king.y();
00239 int x_diff=from.x()-king.x();
00240 return index2(x_diff,y_diff,isP);
00241 }
00248 static int index2(int x_diff,int y_diff,bool isP){
00249 assert(-9 <= y_diff && y_diff <= 9);
00250 assert(-9 <= x_diff && x_diff <= 9);
00251 int index=(y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0));
00252 assert(0<=index && index<19*19*2);
00253 return index;
00254 }
00255 static CArray<MultiInt, 612> attack_table;
00256 static CArray<MultiInt, 612> defense_table;
00257 static CArray<MultiInt, 32> piece_table;
00258 static CArray<MultiInt, 23104> attack_u;
00259 static CArray<MultiInt, 23104> attack_d;
00260 static CArray<MultiInt, 23104> attack_l;
00261 static CArray<MultiInt, 23104> attack_r;
00262 static CArray<MultiInt, 23104> defense_u;
00263 static CArray<MultiInt, 23104> defense_d;
00264 static CArray<MultiInt, 23104> defense_l;
00265 static CArray<MultiInt, 23104> defense_r;
00266 static CArray<MultiInt, 722> attack_nospace;
00267 static CArray<MultiInt, 722> defense_nospace;
00268 };
00269 class RookEffect : public RookEffectBase
00270 {
00271 public:
00272 static void setUp(const Weights &weights,int stage);
00273 };
00274
00275 class RookEffectPiece
00276 {
00277 public:
00278 enum { DIM = 32 * EvalStages };
00279 static void setUp(const Weights &weights);
00280 };
00281 class RookEffectPieceKingRelative : RookEffectBase
00282 {
00283 public:
00284 enum { ONE_DIM = 19584, DIM = ONE_DIM * 2*EvalStages };
00285 static void setUp(const Weights & weights);
00286 };
00287
00288 class RookPromoteDefense : public RookEffectBase
00289 {
00290 friend class RookPromoteDefenseRookH;
00291 public:
00292 enum { ONE_DIM = 256, DIM = ONE_DIM * EvalStages };
00293 static void setUp(const Weights &weights);
00294 static MultiInt eval(const NumEffectState &state);
00295 private:
00296 static CArray<MultiInt, 256> promote_defense_table;
00297 static CArray<MultiInt, 144> promote_defense_rook_table;
00298 };
00299
00300 class RookPromoteDefenseRookH : public RookEffectBase
00301 {
00302 public:
00303 enum { ONE_DIM = 144, DIM = ONE_DIM * EvalStages };
00304 static void setUp(const Weights &weights);
00305 private:
00306 };
00307
00308 class BishopEffectBase
00309 {
00310 friend class BishopEffectPiece;
00311 public:
00312 enum { ONE_DIM = 612, DIM = ONE_DIM * 2 };
00313 static MultiInt eval(const NumEffectState &state);
00314 protected:
00315 template<Player P>
00316 static MultiInt evalOne(const NumEffectState& state,
00317 Position bishop,
00318 Position myKing,
00319 Position opKing,
00320 Position ulp,
00321 Position urp,
00322 Position dlp,
00323 Position drp,
00324 bool isP);
00325 static int index(int x_diff, int y_diff, bool ur, bool promoted)
00326 {
00327 if (x_diff<0)
00328 ur = !ur;
00329 return y_diff + 8 + std::abs(x_diff) * 17 + (ur ? 153 : 0) + (promoted ? 306 : 0);
00330 }
00331 static int index0(int x_diff, int y_diff,PtypeO ptypeO,bool ur, bool promoted)
00332 {
00333 if (x_diff>0)
00334 ur = !ur;
00335 return -y_diff + 8 + std::abs(x_diff) * 17 + (ptypeO - PTYPEO_MIN) * 17 * 9 +
00336 (ur ? 4896 : 0) + (promoted ? 9792 : 0);
00337 }
00347 static int index1(Position king,Position from,PtypeO ptypeO,bool isP)
00348 {
00349 int y_diff=from.y()-king.y();
00350 int x_diff=from.x()-king.x();
00351 return index1(x_diff,y_diff,ptypeO,isP);
00352 }
00361 static int index1(int x_diff,int y_diff,PtypeO ptypeO,bool isP){
00362 assert(-9 <= y_diff && y_diff <= 9);
00363 assert(-9 <= x_diff && x_diff <= 9);
00364 assert(getPtype((PtypeO)ptypeO)!=PTYPE_EMPTY);
00365 int index=(ptypeO-PTYPEO_MIN)+32*((y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0)));
00366 assert(0<=index && index<32*19*19*2);
00367 return index;
00368 }
00376 static int index2(Position king,Position from,bool isP)
00377 {
00378 int y_diff=from.y()-king.y();
00379 int x_diff=from.x()-king.x();
00380 return index2(x_diff,y_diff,isP);
00381 }
00388 static int index2(int x_diff,int y_diff,bool isP){
00389 assert(-9 <= y_diff && y_diff <= 9);
00390 assert(-9 <= x_diff && x_diff <= 9);
00391 int index=(y_diff+9)+19*(x_diff+9+19*(isP ? 1 : 0));
00392 assert(0<=index && index<19*19*2);
00393 return index;
00394 }
00395 static CArray<MultiInt, 612> attack_table;
00396 static CArray<MultiInt, 612> defense_table;
00397 static CArray<MultiInt, 32> piece_table;
00398 static CArray<MultiInt, 23104> attack_ur;
00399 static CArray<MultiInt, 23104> attack_ul;
00400 static CArray<MultiInt, 23104> attack_dr;
00401 static CArray<MultiInt, 23104> attack_dl;
00402 static CArray<MultiInt, 23104> defense_ur;
00403 static CArray<MultiInt, 23104> defense_ul;
00404 static CArray<MultiInt, 23104> defense_dr;
00405 static CArray<MultiInt, 23104> defense_dl;
00406 static CArray<MultiInt, 722> attack_nospace;
00407 static CArray<MultiInt, 722> defense_nospace;
00408 };
00409 class BishopEffect : public BishopEffectBase
00410 {
00411 public:
00412 static void setUp(const Weights &weights,int stage);
00413 };
00414 class BishopEffectPiece
00415 {
00416 public:
00417 enum { DIM = 32*EvalStages };
00418 static void setUp(const Weights &weights);
00419 };
00420
00421 class BishopEffectPieceKingRelative : BishopEffectBase
00422 {
00423 public:
00424 enum { ONE_DIM = 19584, DIM = ONE_DIM * 2*EvalStages };
00425 static void setUp(const Weights & weights);
00426 };
00427
00428 class BishopHead
00429 {
00430 friend class BishopHeadKingRelative;
00431 friend class BishopHeadX;
00432 public:
00433 enum { ONE_DIM = 32, DIM = ONE_DIM * EvalStages };
00434 static void setUp(const Weights &weights);
00435 static MultiInt eval(const NumEffectState &state);
00436 private:
00437 static int indexK(Player player, PtypeO ptypeO, int x_diff, int y_diff)
00438 {
00439 if (player == WHITE)
00440 {
00441 ptypeO=(PtypeO)(static_cast<int>(ptypeO)^(~15));
00442 }
00443 if (player == WHITE)
00444 {
00445 y_diff = -y_diff;
00446 }
00447 return (ptypeOIndex(ptypeO) * 9 + x_diff) * 17 + y_diff + 8;
00448 }
00449 template <Player P>
00450 static int indexX(PtypeO ptypeO, int x)
00451 {
00452 if (x > 5)
00453 {
00454 x = 10 - x;
00455 }
00456 if (P == WHITE)
00457 {
00458 ptypeO = altIfPiece(ptypeO);
00459 }
00460 return x - 1 + 5 * ptypeOIndex(ptypeO);
00461 }
00462 static CArray<MultiInt, 32> table;
00463 static CArray<MultiInt, 4896> king_table;
00464 static CArray<MultiInt, 160> x_table;
00465 };
00466
00467 class BishopHeadKingRelative
00468 {
00469 public:
00470 enum { ONE_DIM = 4896, DIM = ONE_DIM * EvalStages };
00471 static void setUp(const Weights &weights);
00472 };
00473 class BishopHeadX
00474 {
00475 public:
00476 enum { ONE_DIM = 160, DIM = ONE_DIM * EvalStages };
00477 static void setUp(const Weights &weights);
00478 };
00479 class KingRookBishop
00480 {
00481 public:
00482 enum { ONE_DIM = 374544, DIM = ONE_DIM * EvalStages };
00483 static void setUp(const Weights &weights);
00484 template<Player King>
00485 static MultiInt evalOne(const NumEffectState &state);
00486 static MultiInt eval(const NumEffectState &state);
00487 private:
00488 static CArray<MultiInt, 374544> table;
00489 template <Player King>
00490 static int index(const Position king, const Piece rook, const Piece bishop)
00491 {
00492 const int rook_x = std::abs(king.x() - rook.position().x());
00493 const int bishop_x = std::abs(king.x() - bishop.position().x());
00494 const int rook_y = (King == BLACK ? rook.position().y() - king.y() : king.y() - rook.position().y());
00495 const int bishop_y = (King == BLACK ? bishop.position().y() - king.y() : king.y() - bishop.position().y());
00496 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)))))));
00497 }
00498 };
00499
00500 class NumPiecesBetweenBishopAndKing
00501 {
00502 friend class NumPiecesBetweenBishopAndKingSelf;
00503 friend class NumPiecesBetweenBishopAndKingOpp;
00504 friend class NumPiecesBetweenBishopAndKingAll;
00505 public:
00506 static MultiInt eval(const NumEffectState &state);
00507 private:
00508 static void countBetween(const NumEffectState &state,
00509 Position king, Piece bishop,
00510 int &self_count, int &opp_count,
00511 int &total_count);
00512 static CArray<MultiInt, 9> self_table;
00513 static CArray<MultiInt, 9> opp_table;
00514 static CArray<MultiInt, 9> all_table;
00515 };
00516 class NumPiecesBetweenBishopAndKingSelf
00517 {
00518 public:
00519 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
00520 static void setUp(const Weights &weights);
00521 };
00522 class NumPiecesBetweenBishopAndKingOpp
00523 {
00524 public:
00525 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
00526 static void setUp(const Weights &weights);
00527 };
00528 class NumPiecesBetweenBishopAndKingAll
00529 {
00530 public:
00531 enum { ONE_DIM = 9, DIM = ONE_DIM * EvalStages };
00532 static void setUp(const Weights &weights);
00533 };
00534 class BishopBishopPiece
00535 {
00536 public:
00537 enum { ONE_DIM = 64, DIM = ONE_DIM * EvalStages };
00538 static void setUp(const Weights &weights);
00539 static MultiInt eval(const NumEffectState &state);
00540 private:
00541 static int index(Ptype ptype, bool self_with_support,
00542 bool opp_with_support)
00543 {
00544 return ptype + PTYPE_SIZE * ((self_with_support ? 1 : 0) +
00545 2 * (opp_with_support ? 1 : 0));
00546 }
00547 static CArray<MultiInt, 64> table;
00548 };
00549
00550 class RookRook
00551 {
00552 public:
00553 enum { ONE_DIM = 800, DIM = ONE_DIM * EvalStages };
00554 static void setUp(const Weights &weights);
00555 static MultiInt eval(const NumEffectState &state);
00556 private:
00557 template <bool SamePlayer, Player P>
00558 static int index(Piece rook1, Piece rook2)
00559 {
00560 const int y1 = (rook1.isOnBoard() ? rook1.position().y() : 0);
00561 const int y2 = (rook2.isOnBoard() ? rook2.position().y() : 0);
00562 if (SamePlayer)
00563 {
00564 if (P == BLACK)
00565 {
00566 return y1 + 10 *
00567 (y2 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
00568 ((rook2.isPromoted() ? 1 : 0) + 2 *
00569 (SamePlayer ? 1 : 0))));
00570 }
00571 else
00572 {
00573 if (y1 == 0 || y2 == 0 || y1 == y2)
00574 {
00575 return (10 - y1) % 10 + 10 *
00576 ((10 - y2) % 10 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
00577 ((rook2.isPromoted() ? 1 : 0) + 2 *
00578 (SamePlayer ? 1 : 0))));
00579 }
00580 else
00581 {
00582 return (10 - y2) % 10 + 10 *
00583 ((10 - y1) % 10 + 10 * ((rook2.isPromoted() ? 1 : 0) + 2 *
00584 ((rook1.isPromoted() ? 1 : 0) + 2 *
00585 (SamePlayer ? 1 : 0))));
00586 }
00587 }
00588 }
00589 else
00590 {
00591 return y1 + 10 *
00592 (y2 + 10 * ((rook1.isPromoted() ? 1 : 0) + 2 *
00593 ((rook2.isPromoted() ? 1 : 0) + 2 *
00594 (SamePlayer ? 1 : 0))));
00595 }
00596 }
00597 static int index(bool same_player, bool promoted1,
00598 bool promoted2, int y1, int y2)
00599 {
00600 return y1 + 10 *
00601 (y2 + 10 * ((promoted1 ? 1 : 0) + 2 *
00602 ((promoted2 ? 1 : 0) + 2 *
00603 (same_player ? 1 : 0))));
00604 }
00605 static CArray<MultiInt, 800> table;
00606 };
00607
00608 class RookRookPiece
00609 {
00610 public:
00611 enum { ONE_DIM = 128, DIM = ONE_DIM * EvalStages };
00612 static void setUp(const Weights &weights);
00613 static MultiInt eval(const NumEffectState &state);
00614 private:
00615 static int index(Ptype ptype, bool self_with_support,
00616 bool opp_with_support, bool vertical)
00617 {
00618 return ptype + PTYPE_SIZE * ((self_with_support ? 1 : 0) +
00619 2 * (opp_with_support ? 1 : 0)) +
00620 (vertical ? PTYPE_SIZE * 2 * 2 : 0);
00621 }
00622 static CArray<MultiInt, 128> table;
00623 };
00624
00625 class BishopStandFile5
00626 {
00627 public:
00628 enum { ONE_DIM = 32, DIM = ONE_DIM * EvalStages };
00629 static void setUp(const Weights &weights);
00630 static MultiInt eval(const NumEffectState &state);
00631 private:
00632 static CArray<MultiInt, 32> table;
00633 };
00634 }
00635 }
00636 }
00637
00638 #endif // EVAL_ML_MAJORPIECE_H
00639
00640
00641
00642