00001
00002
00003
00004 #ifndef EVAL_ML_KINGTABLE_H
00005 #define EVAL_ML_KINGTABLE_H
00006
00007 #include "osl/eval/weights.h"
00008 #include "osl/eval/midgame.h"
00009 #include "osl/numEffectState.h"
00010 #include <cstdlib>
00011 namespace osl
00012 {
00013 namespace eval
00014 {
00015 namespace ml
00016 {
00017 struct KingPieceRelative
00018 {
00019 typedef CArray2d<MultiInt, PTYPE_SIZE, 17 * 9> table_t;
00020 static table_t attack_table, defense_table;
00021 static int index(const NumEffectState &,
00022 Player owner,
00023 const Square position,
00024 Square king)
00025 {
00026 return std::abs(position.x() - king.x()) * 17 +
00027 (owner == BLACK ? (king.y() - position.y()) :
00028 (position.y() - king.y())) + 8;
00029 }
00030 static int index(const NumEffectState &state,
00031 Player owner,
00032 const Square position,
00033 bool same_king)
00034 {
00035 const Square king = state.kingSquare(
00036 same_king ? owner : alt(owner));
00037 return index(state, owner, position, king);
00038 }
00039 static int index(const NumEffectState &state,
00040 PtypeO ptypeo,
00041 const Square position,
00042 bool same_king)
00043 {
00044 return index(state, getOwner(ptypeo), position, same_king);
00045 }
00046 static int index(const NumEffectState &state,
00047 const Piece piece,
00048 bool same_king)
00049 {
00050 return index(state, piece.owner(), piece.square(), same_king);
00051 }
00052 enum { DIM = ((osl::PTYPE_MAX - osl::PTYPE_PIECE_MIN + 1) *
00053 (17 * 9)) * 2};
00054 enum { TABLE_DIM = DIM / 2};
00055 static void setUp(const Weights &weights, int stage);
00056 static MultiInt eval(const NumEffectState &state);
00057 template<Player P>
00058 static MultiInt evalWithUpdate(const NumEffectState &state, Move moved,
00059 const MultiInt& last_values);
00060
00061 };
00062
00063 class KingPieceRelativeNoSupport
00064 {
00065 public:
00066 enum { ONE_DIM = 4284, DIM = ONE_DIM * EvalStages };
00067 static MultiInt eval(const NumEffectState &state);
00068 static MultiInt evalWithUpdate(
00069 const NumEffectState &state,
00070 Move moved,
00071 const CArray<PieceMask, 2> &effected_mask,
00072 const MultiInt &last_values);
00073 static void setUp(const Weights &weights);
00074 template <int Sign>
00075 static void adjust(int attack, int defense, MultiInt& out);
00076 private:
00077 static int index(const Player player, const Square king,
00078 const Ptype ptype, const Square pos)
00079 {
00080 const int x = std::abs(pos.x() - king.x());
00081 const int y = (king.y() - pos.y()) *
00082 (player == osl::BLACK ? 1 : -1) + 8;
00083 return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
00084 }
00085 static int index(const Player player, const Square king,
00086 const Piece piece)
00087 {
00088 return index(player, king, piece.ptype(), piece.square());
00089 }
00090 static CArray<MultiInt, ONE_DIM> table;
00091 };
00092
00093 struct PtypeYY
00094 {
00095 enum { ONE_DIM = 2592, DIM = ONE_DIM * EvalStages};
00096 static void setUp(const Weights &weights);
00097 static MultiInt eval(const NumEffectState &state);
00098 static MultiInt evalWithUpdate(
00099 const NumEffectState& state,
00100 Move moved,
00101 const MultiInt &last_values);
00102 static CArray<MultiInt, 2592> table;
00103 template <int Sign>
00104 static void adjust(int black, int white, MultiInt &out);
00105 private:
00106 template <Player KingPlayer>
00107 static int index(const Piece p, const Square king)
00108 {
00109 return index<KingPlayer>(p.ptypeO(), p.square(), king);
00110 }
00111
00112 template <Player KingPlayer>
00113 static int index(const PtypeO ptypeO, const Square position,
00114 const Square king)
00115 {
00116 const int king_y = (KingPlayer == BLACK ? king.y() : 10 - king.y());
00117 const int piece_y = (KingPlayer == BLACK ? position.y() :
00118 10 - position.y());
00119 return (king_y - 1) * 9 * 32 + (piece_y - 1) * 32 +
00120 (KingPlayer == BLACK ? ptypeO : alt(ptypeO)) - PTYPEO_MIN;
00121 }
00122 };
00123
00124 class King25Effect
00125 {
00126 private:
00127 static void countEffectAndPieces(const NumEffectState &state,
00128 const Player attack,
00129 int &effect,
00130 int &piece);
00131 static int index(int effect, int piece_count);
00132 public:
00133 enum { DIM = 17 * 128 };
00134 static CArray<int, DIM> table;
00135 static void setUp(const Weights &weights);
00136 static int eval(const NumEffectState &state);
00137 };
00138
00139 class King25EffectBoth
00140 {
00141 private:
00142 static int index(int effect, int piece_count)
00143 {
00144 return effect + 128 * piece_count;
00145 }
00146 public:
00147 template <Player Attack>
00148 static void countEffectAndPiecesBoth(
00149 const NumEffectState &state,
00150 PieceMask& effect25,
00151 PieceMask& effect_supported,
00152 int &attack_effect,
00153 int &attack_piece,
00154 int &defense_effect,
00155 int &defence_piece,
00156 int &attack_piece_supported,
00157 CArray<int, 5> &effect_vertical,
00158 CArray<int, 5> &king_vertical);
00159 enum { DIM = 17 * 128 * 2};
00160 static CArray<int, DIM/2> attack_table;
00161 static CArray<int, DIM/2> defense_table;
00162 static void setUp(const Weights &weights);
00163 static int eval(const NumEffectState &,
00164 int black_attack_effect, int black_attack_piece,
00165 int white_attack_effect, int white_attack_piece,
00166 int black_defense_effect, int black_defense_piece,
00167 int white_defense_effect, int white_defense_piece)
00168 {
00169 return attack_table[index(black_attack_effect, black_attack_piece)] -
00170 attack_table[index(white_attack_effect, white_attack_piece)] +
00171 defense_table[index(black_defense_effect, black_defense_piece)] -
00172 defense_table[index(white_defense_effect, white_defense_piece)];
00173 }
00174 };
00175
00176 class King25EffectAttack
00177 {
00178 private:
00179 static int index(int effect, int piece_count)
00180 {
00181 return effect + 128 * piece_count;
00182 }
00183 public:
00184 enum { DIM = 17 * 128};
00185 static CArray<int, 17 * 128> table;
00186 static void setUp(const Weights &weights)
00187 {
00188 for (size_t i = 0; i < weights.dimension(); ++i)
00189 {
00190 table[i] = weights.value(i);
00191 }
00192 }
00193 static int eval(const NumEffectState &,
00194 int black_effect, int black_piece,
00195 int white_effect, int white_piece)
00196 {
00197 return table[index(black_effect, black_piece)] -
00198 table[index(white_effect, white_piece)];
00199 }
00200 };
00201
00202 class King25EffectDefense
00203 {
00204 private:
00205 static int index(int effect, int piece_count)
00206 {
00207 return effect + 128 * piece_count;
00208 }
00209 public:
00210 enum { DIM = 17 * 128};
00211 static CArray<MultiInt, 17 * 128> table;
00212 static void setUp(const Weights &weights,int stage)
00213 {
00214 for (size_t i = 0; i < weights.dimension(); ++i)
00215 {
00216 table[i][stage] = weights.value(i);
00217 }
00218 }
00219 static MultiInt eval(const NumEffectState &,
00220 int black_effect, int black_piece,
00221 int white_effect, int white_piece)
00222 {
00223 return table[index(black_effect, black_piece)] -
00224 table[index(white_effect, white_piece)];
00225 }
00226 };
00227
00228
00229 class King25EffectYAttack
00230 {
00231 private:
00232 static int index(int king_y, int effect, int piece_count)
00233 {
00234 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
00235 }
00236 public:
00237 enum { DIM = 17 * 128 * 9};
00238 static CArray<int, 17 * 128 * 9> table;
00239 static void setUp(const Weights &weights)
00240 {
00241 for (size_t i = 0; i < weights.dimension(); ++i)
00242 {
00243 table[i] = weights.value(i);
00244 }
00245 }
00246 static int eval(const NumEffectState &state,
00247 int black_effect, int black_piece,
00248 int white_effect, int white_piece)
00249 {
00250
00251 return table[index(10 - state.kingSquare<WHITE>().y(),
00252 black_effect, black_piece)] -
00253 table[index(state.kingSquare<BLACK>().y(),
00254 white_effect, white_piece)];
00255 }
00256 };
00257
00258 class King25EffectYDefense
00259 {
00260 private:
00261 static int index(int king_y, int effect, int piece_count)
00262 {
00263 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
00264 }
00265 public:
00266 enum { DIM = 17 * 128 * 9};
00267 static CArray<MultiInt, 17 * 128 * 9> table;
00268 static void setUp(const Weights &weights,int stage)
00269 {
00270 for (size_t i = 0; i < weights.dimension(); ++i)
00271 {
00272 table[i][stage] = weights.value(i);
00273 }
00274 }
00275 static MultiInt eval(const NumEffectState &state,
00276 int black_effect, int black_piece,
00277 int white_effect, int white_piece)
00278 {
00279 return table[index(state.kingSquare<BLACK>().y(),
00280 black_effect, black_piece)] -
00281 table[index(10 - state.kingSquare<WHITE>().y(),
00282 white_effect, white_piece)];
00283 }
00284 };
00285
00286 class King25EffectY
00287 {
00288 public:
00289 enum { DIM = 17 * 128 * 2 * 9};
00290 private:
00291 static int index(int king_y, int effect, int piece_count)
00292 {
00293 return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
00294 }
00295 static CArray<int, DIM/2> attack_table;
00296 static CArray<int, DIM/2> defense_table;
00297 public:
00298 static void setUp(const Weights &weights);
00299 static int eval(const NumEffectState &state,
00300 int black_attack_effect, int black_attack_piece,
00301 int white_attack_effect, int white_attack_piece,
00302 int black_defense_effect, int black_defense_piece,
00303 int white_defense_effect, int white_defense_piece)
00304 {
00305 return attack_table[index(10 - state.kingSquare<WHITE>().y(),
00306 black_attack_effect, black_attack_piece)] -
00307 attack_table[index(state.kingSquare<BLACK>().y(),
00308 white_attack_effect, white_attack_piece)] +
00309 defense_table[index(state.kingSquare<BLACK>().y(),
00310 black_defense_effect, black_defense_piece)] -
00311 defense_table[index(10 - state.kingSquare<WHITE>().y(),
00312 white_defense_effect, white_defense_piece)];
00313 }
00314 };
00315
00316 class King25Effect2
00317 {
00318 public:
00319 enum { ONE_DIM = 17 * 13 * 64, DIM = ONE_DIM * EvalStages };
00320 private:
00321 static int index(int effect, int piece_count,
00322 int stand_count)
00323 {
00324 return (effect + 64 * piece_count) * 13 + stand_count;
00325 }
00326 static CArray<MultiInt, ONE_DIM> table;
00327 public:
00328 static void setUp(const Weights &weights);
00329 static MultiInt eval(
00330 const NumEffectState &,
00331 int black_attack_effect, int black_attack_piece,
00332 int white_attack_effect, int white_attack_piece,
00333 int black_stand_count, int white_stand_count)
00334 {
00335 const int black_index = index(black_attack_effect,
00336 black_attack_piece,
00337 black_stand_count);
00338 const int white_index = index(white_attack_effect,
00339 white_attack_piece,
00340 white_stand_count);
00341 return table[black_index] - table[white_index];
00342 }
00343 };
00344 class King25EffectY2
00345 {
00346 public:
00347 enum { ONE_DIM = 17 * 13 * 64 * 9, DIM = ONE_DIM * EvalStages };
00348 private:
00349 static int index(int king_y, int effect, int piece_count,
00350 int stand_count)
00351 {
00352 return ((effect + 64 * piece_count) * 13 + stand_count) * 9 +
00353 (king_y - 1);
00354 }
00355 static CArray<MultiInt, ONE_DIM> table;
00356 public:
00357 static void setUp(const Weights &weights);
00358 static MultiInt eval(
00359 const NumEffectState &state,
00360 int black_attack_effect, int black_attack_piece,
00361 int white_attack_effect, int white_attack_piece,
00362 int black_stand_count, int white_stand_count)
00363 {
00364 const int black_index = index(10 - state.kingSquare<WHITE>().y(),
00365 black_attack_effect, black_attack_piece,
00366 black_stand_count);
00367 const int white_index = index(state.kingSquare<BLACK>().y(),
00368 white_attack_effect, white_attack_piece,
00369 white_stand_count);
00370 return table[black_index] - table[white_index];
00371 }
00372 };
00373 class King25EffectSupported
00374 {
00375 public:
00376 enum { ONE_DIM = 17 * 17, DIM = ONE_DIM * EvalStages };
00377 private:
00378 static int index(int piece_count, int supported)
00379 {
00380 return supported * 17 + piece_count;
00381 }
00382 static CArray<MultiInt, ONE_DIM> table;
00383 public:
00384 static void setUp(const Weights &weights);
00385 static MultiInt eval(
00386 int black_attack_piece,
00387 int white_attack_piece,
00388 int black_attack_supported_piece, int white_attack_supported_piece)
00389 {
00390 const int black_index = index(black_attack_piece,
00391 black_attack_supported_piece);
00392 const int white_index = index(white_attack_piece,
00393 white_attack_supported_piece);
00394 return table[black_index] - table[white_index];
00395 }
00396 };
00397 class King25EffectSupportedY
00398 {
00399 public:
00400 enum { ONE_DIM = 17 * 17 * 9, DIM = ONE_DIM * EvalStages };
00401 private:
00402 static int index(int piece_count, int supported, int y)
00403 {
00404 return (supported * 17 + piece_count) * 9 + y - 1;
00405 }
00406 static CArray<MultiInt, ONE_DIM> table;
00407 public:
00408 static void setUp(const Weights &weights);
00409 static MultiInt eval(
00410 int black_attack_piece,
00411 int white_attack_piece,
00412 int black_attack_supported_piece, int white_attack_supported_piece,
00413 int black_king_y, int white_king_y)
00414 {
00415 const int black_index = index(black_attack_piece,
00416 black_attack_supported_piece,
00417 10 - white_king_y);
00418 const int white_index = index(white_attack_piece,
00419 white_attack_supported_piece,
00420 black_king_y);
00421 return table[black_index] - table[white_index];
00422 }
00423 };
00424 struct King25EmptySquareNoEffect
00425 {
00426 enum { DIM = 3 * 5 };
00427 template <Player defense>
00428 static int evalOne(const NumEffectState &state, const CArray<int, 15>& table);
00429 template <Player defense>
00430 static std::pair<int,int> evalOne(const NumEffectState &state, const CArray<int, 15>& opening, const CArray<int, 15>& ending);
00431 static std::pair<CArray<int,2>, CArray<int,2> >
00432 eval(const NumEffectState &state, const CArray<int, 15>& opening, const CArray<int, 15>& ending);
00433 static std::pair<CArray<int,2>, CArray<int,2> >
00434 evalWithUpdate(const NumEffectState &state, Move last_move,
00435 const CArray<int, 15>& opening, const CArray<int, 15>& ending,
00436 const CArray<int,2>& last_opening_value, const CArray<int,2>& last_ending_value);
00437 static int index(int rel_x, int rel_y)
00438 {
00439 return (rel_y + 2) * 3 + std::abs(rel_x);
00440 }
00441 static void setUpBase(const Weights &weigths, CArray<int, 15>& table);
00442 };
00443
00444 class King25EmptySquareNoEffectOpening
00445 : public King25EmptySquareNoEffect
00446 {
00447 static CArray<int, 15> table;
00448 public:
00449 static void setUp(const Weights &weigths) { setUpBase(weigths, table); }
00450 static const CArray<int,2> eval(const NumEffectState &state);
00451 static const CArray<int, 15>& weights() { return table; }
00452 };
00453
00454 class King25EmptySquareNoEffectEnding
00455 : public King25EmptySquareNoEffect
00456 {
00457 static CArray<int, 15> table;
00458 public:
00459 static void setUp(const Weights &weigths) { setUpBase(weigths, table); }
00460 static const CArray<int,2> eval(const NumEffectState &state);
00461 static const CArray<int, 15>& weights() { return table; }
00462 };
00463
00464 template <bool Opening>
00465 class King25EmptyAbs
00466 {
00467 public:
00468 enum { DIM = 5 * 5 * 5 * 9 };
00469 private:
00470 static CArray<int, 1125> table;
00471 template <Player player>
00472 static int index(Square king,
00473 Square target);
00474 static int index(Square king,
00475 Square target, Player player) {
00476 if (player == BLACK)
00477 return index<BLACK>(king, target);
00478 else
00479 return index<WHITE>(king, target);
00480 }
00481 template <Player Defense>
00482 static int evalOne(const NumEffectState &state);
00483 public:
00484 static int evalWithUpdate(
00485 const NumEffectState &state, osl::Move moved,
00486 int last_value);
00487 static void setUp(const Weights &weigths);
00488 static int eval(const NumEffectState &state);
00489 };
00490
00491 class King25EmptyAbsOpening : public King25EmptyAbs<true>
00492 {
00493 };
00494 class King25EmptyAbsEnding : public King25EmptyAbs<false>
00495 {
00496 };
00497
00498 enum EffectState
00499 {
00500 NO_ATTACK_DEFENSE_0,
00501 NO_ATTACK_DEFENSE_1,
00502 NO_ATTACK_DEFENSE_2,
00503 ATTACK_DIFF_N2,
00504 ATTACK_DIFF_N1,
00505 ATTACK_DIFF_0,
00506 ATTACK_DIFF_1,
00507 ATTACK_DIFF_2,
00508 STATE_MAX,
00509 };
00510 template <int Stage>
00511 class King25EffectEach
00512 {
00513 public:
00514 enum { DIM = 5 * 3 * STATE_MAX * 3 };
00515 private:
00516 static CArray<int, 5 * 3 * 8 * 3> table;
00517 template <Player Defense>
00518 static EffectState effectState(const NumEffectState &state,
00519 Square target);
00520 template <Player Defense>
00521 static int index(const NumEffectState &state, Square king,
00522 Square target);
00523 template <osl::Player Defense>
00524 static int evalOne(const NumEffectState &state);
00525 public:
00526 static void setUp(const Weights &weigths);
00527 static int eval(const NumEffectState &state);
00528 };
00529
00530 class King25EffectEachOpening : public King25EffectEach<0>
00531 {
00532 };
00533 class King25EffectEachMidgame : public King25EffectEach<1>
00534 {
00535 };
00536 class King25EffectEachEnding : public King25EffectEach<2>
00537 {
00538 };
00539
00540 class King25EffectEachBothOpening
00541 {
00542 public:
00543 enum { DIM = 5 * 3 * STATE_MAX * 3 };
00544 static void setUp(const Weights &weigths);
00545 };
00546 class King25EffectEachBothMidgame
00547 {
00548 public:
00549 enum { DIM = 5 * 3 * STATE_MAX * 3 };
00550 static void setUp(const Weights &weigths);
00551 };
00552 class King25EffectEachBothMidgame2
00553 {
00554 public:
00555 enum { DIM = 5 * 3 * STATE_MAX * 3 };
00556 static void setUp(const Weights &weigths);
00557 };
00558 class King25EffectEachBothEnding
00559 {
00560 public:
00561 enum { DIM = 5 * 3 * STATE_MAX * 3 };
00562 static void setUp(const Weights &weigths);
00563 };
00564
00565 class King25EffectEachBoth
00566 {
00567 enum EffectState
00568 {
00569 NO_ATTACK_DEFENSE_0,
00570 NO_ATTACK_DEFENSE_1,
00571 NO_ATTACK_DEFENSE_2,
00572 ATTACK_DIFF_N2,
00573 ATTACK_DIFF_N1,
00574 ATTACK_DIFF_0,
00575 ATTACK_DIFF_1,
00576 ATTACK_DIFF_2,
00577 STATE_MAX,
00578 };
00579 friend class King25EffectEachBothOpening;
00580 friend class King25EffectEachBothMidgame;
00581 friend class King25EffectEachBothMidgame2;
00582 friend class King25EffectEachBothEnding;
00583 friend class King25EffectEachXY;
00584 friend class King25EffectEachKXY;
00585 private:
00586 static CArray<MultiInt, 5 * 3 * 8 * 3> table;
00587 static CArray<MultiInt, 3000> x_table;
00588 static CArray<MultiInt, 3240> y_table;
00589 static CArray<MultiInt, 27000> xy_table;
00590 static CArray<int, 256> effect_state_table;
00591 template <Player Defense>
00592 static int effectStateIndex3(const NumEffectState &state,
00593 Square target);
00594 template <Player Defense>
00595 static void index(const NumEffectState &state,
00596 Square target,
00597 int &index_xy,
00598 int rel_y, int king_x, int king_y, int x_diff
00599 );
00600 template <osl::Player Defense>
00601 static void evalOne(const NumEffectState &state,
00602 MultiInt& out);
00603 public:
00604 static void eval(const NumEffectState &state,
00605 MultiIntPair &out);
00606 static void
00607 evalWithUpdate(const NumEffectState &state, Move last_move,
00608 MultiIntPair & values);
00609 };
00610
00611 class King25EffectEachXY
00612 {
00613 public:
00614 enum { X_DIM = 3000, Y_DIM = 3240, DIM = (X_DIM + Y_DIM) * EvalStages};
00615 static void setUp(const Weights &weigths);
00616 };
00617
00618 class King25EffectEachKXY
00619 {
00620 public:
00621 enum { ONE_DIM = 27000, DIM = ONE_DIM * EvalStages };
00622 static void setUp(const Weights &weigths);
00623 };
00624
00625 class King3Pieces
00626 {
00627 friend class King3PiecesXY;
00628 public:
00629 enum { ONE_DIM = 3072, DIM = ONE_DIM * EvalStages };
00630 static void setUp(const Weights &weights);
00631 static MultiInt eval(const NumEffectState &state);
00632 static MultiInt evalWithUpdate(const NumEffectState &state,
00633 Move last_move,
00634 MultiInt &last_value);
00635 private:
00636 enum Direction
00637 {
00638 HORIZONTAL = 0,
00639 VERTICAL,
00640 DIAGONAL,
00641 };
00642 template <Player King, Direction Dir>
00643 static int index(PtypeO p1, PtypeO p2)
00644 {
00645 if (King == WHITE)
00646 {
00647 p1 = altIfPiece(p1);
00648 p2 = altIfPiece(p2);
00649 }
00650 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir;
00651 }
00652 template <Player King, Direction Dir>
00653 static int indexY(const Square king_position,
00654 PtypeO p1, PtypeO p2)
00655 {
00656 if (King == WHITE)
00657 {
00658 p1 = altIfPiece(p1);
00659 p2 = altIfPiece(p2);
00660 }
00661 const int king_y = (King == BLACK ? king_position.y() :
00662 10 - king_position.y());
00663 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir
00664 + (king_y - 1) * 32 * 32 * 3;
00665 }
00666 template <Player King, Direction Dir>
00667 static int indexX(const Square king_position,
00668 PtypeO p1, PtypeO p2)
00669 {
00670 if (King == WHITE)
00671 {
00672 p1 = altIfPiece(p1);
00673 p2 = altIfPiece(p2);
00674 }
00675 const int king_x = (king_position.x() > 5 ? 10 - king_position.x() :
00676 king_position.x());
00677 if (Dir == HORIZONTAL &&
00678 ((King == BLACK && king_position.x() >= 6) ||
00679 (King == WHITE && king_position.x() <= 4)))
00680 {
00681 PtypeO tmp = p1;
00682 p1 = p2; p2 = tmp;
00683 }
00684 return ptypeOIndex(p1) * 32 + ptypeOIndex(p2) + 1024 * Dir
00685 + (king_x - 1) * 32 * 32 * 3;
00686 }
00687 static MultiInt value(int vertical_index, int horizontal_index,
00688 int diagonal_index1, int diagonal_index2,
00689 int vertical_index_x, int horizontal_index_x,
00690 int diagonal_index1_x, int diagonal_index2_x,
00691 int vertical_index_y , int horizontal_index_y,
00692 int diagonal_index1_y, int diagonal_index2_y)
00693 {
00694 return table[vertical_index] + table[horizontal_index] +
00695 table[diagonal_index1] + table[diagonal_index2] +
00696 x_table[vertical_index_x] + x_table[horizontal_index_x] +
00697 x_table[diagonal_index1_x] + x_table[diagonal_index2_x] +
00698 y_table[vertical_index_y] + y_table[horizontal_index_y] +
00699 y_table[diagonal_index1_y] + y_table[diagonal_index2_y];
00700 }
00701
00702 template <Player King>
00703 static void evalOne(const NumEffectState &state,
00704 MultiInt &result);
00705 static CArray<MultiInt, 3072> table;
00706 static CArray<MultiInt, 15360> x_table;
00707 static CArray<MultiInt, 27648> y_table;
00708 };
00709
00710 class King3PiecesXY
00711 {
00712 public:
00713 enum
00714 {
00715 X_DIM = 32 * 32 * 3 * 5,
00716 Y_DIM = 32 * 32 * 3 * 9,
00717 ONE_DIM = X_DIM + Y_DIM,
00718 DIM = ONE_DIM * EvalStages,
00719 };
00720 static void setUp(const Weights &weights);
00721 };
00722
00723 class KingMobility
00724 {
00725 friend class KingMobilityWithRook;
00726 friend class KingMobilityWithBishop;
00727 public:
00728 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
00729 static void setUp(const Weights &weights);
00730 static MultiInt eval(const NumEffectState &state);
00731 private:
00732 template <Player P>
00733 static MultiInt evalOne(const NumEffectState &state);
00734 template<Direction Dir>
00735 static int mobilityDir(Square king,Square target)
00736 {
00737 if(Dir==L) return king.x()-target.x()-1;
00738 else if(Dir==R) return target.x()-king.x()-1;
00739 else if(Dir==UL || Dir==U || Dir==UR) return target.y()-king.y()-1;
00740 else return king.y()-target.y()-1;
00741 }
00742 static CArray<MultiInt, 3240> table;
00743 static CArray<MultiInt, 3240> rook_table;
00744 static CArray<MultiInt, 3240> bishop_table;
00745 static CArray<MultiInt, 3240> rook_bishop_table;
00746 };
00747
00748 class KingMobilityWithRook
00749 {
00750 public:
00751 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
00752 static void setUp(const Weights &weights);
00753 };
00754 class KingMobilityWithBishop
00755 {
00756 public:
00757 enum { ONE_DIM = 3240, DIM = ONE_DIM * EvalStages };
00758 static void setUp(const Weights &weights);
00759 };
00760
00761 class KingMobilitySum
00762 {
00763 public:
00764 enum { ONE_DIM = 2925, DIM = ONE_DIM * EvalStages };
00765 static void setUp(const Weights &weights);
00766 static MultiInt eval(const NumEffectState &state);
00767 private:
00768 template <Player P>
00769 static MultiInt evalOne(const NumEffectState &state);
00770 static CArray<MultiInt, 45*33> table;
00771 };
00772
00773 class King25BothSide
00774 {
00775 friend class King25BothSideX;
00776 friend class King25BothSideY;
00777 public:
00778 enum { ONE_DIM = 8192, DIM = ONE_DIM * EvalStages };
00779 static void setUp(const Weights &weights);
00780 template<Player P>
00781 static MultiInt evalOne(const NumEffectState &state,
00782 const CArray<int, 5> &effects);
00783 static MultiInt eval(const NumEffectState &state,
00784 const CArray<int, 5> &black,
00785 const CArray<int, 5> &white);
00786 private:
00787 static int index(int effect1, int effect2, int i)
00788 {
00789 assert(0 <= effect1 && effect1 < 32);
00790 assert(0 <= effect2 && effect2 < 32);
00791 return effect1 + 32 * (effect2 + 32 * i);
00792 }
00793 template <Player P>
00794 static int indexX(Square king, int effect1, int effect2,
00795 int i, int j)
00796 {
00797 const int king_x = (king.x() >= 6 ? 10 - king.x() : king.x());
00798 if ((P == BLACK && king.x() > 5) ||
00799 (P == WHITE && king.x() < 5))
00800 {
00801 const int tmp = effect1;
00802 effect1 = effect2;
00803 effect2 = tmp;
00804 const int tmp2 = i;
00805 i = 4 - j;
00806 j = 4 - tmp2;
00807 }
00808 if (i == 2)
00809 --j;
00810 const int combination = (i * 3 + j - 2);
00811 assert(0 <= effect1 && effect1 < 32);
00812 assert(0 <= effect2 && effect2 < 32);
00813 return king_x - 1 + 5 * (effect1 + 32 *
00814 (effect2 + 32 * combination));
00815 }
00816 static int indexX(int king_x,int effect1,int effect2, int i){
00817 return king_x - 1 + 5 * (effect1 + 32 *
00818 (effect2 + 32 * i));
00819 }
00820 template <Player P>
00821 static int indexY(Square king, int effect1, int effect2, int i)
00822 {
00823 const int king_y = (P == BLACK ? king.y() : 10 - king.y());
00824 assert(0 <= effect1 && effect1 < 32);
00825 assert(0 <= effect2 && effect2 < 32);
00826 return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
00827 }
00828 static int indexY(int king_y,int effect1,int effect2, int i){
00829 return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
00830 }
00831 static CArray<MultiInt, 8192> table;
00832 static CArray<MultiInt, 40960> x_table;
00833 static CArray<MultiInt, 73728> y_table;
00834 };
00835 class King25BothSideX
00836 {
00837 public:
00838 enum { ONE_DIM = 40960, DIM = ONE_DIM * EvalStages };
00839 static void setUp(const Weights &weights);
00840 };
00841 class King25BothSideY
00842 {
00843 public:
00844 enum { ONE_DIM = 73728, DIM = ONE_DIM * EvalStages };
00845 static void setUp(const Weights &weights);
00846 };
00847
00848 class King25Mobility
00849 {
00850 friend class King25MobilityX;
00851 friend class King25MobilityY;
00852 public:
00853 enum { ONE_DIM = 4096, DIM = ONE_DIM * EvalStages };
00854 static void setUp(const Weights &weights);
00855 static MultiInt eval(const NumEffectState &state,
00856 const CArray<int, 5> &black,
00857 const CArray<int, 5> &white);
00858 private:
00859 static int index(int effect1, int effect2, int i)
00860 {
00861 assert(0 <= effect1 && effect1 < 32);
00862 assert(0 <= effect2 && effect2 < 32);
00863 return effect1 + 32 * (effect2 + 32 * i);
00864 }
00865 template <Player Defense>
00866 static int indexX(Square king, int effect1, int effect2, int i)
00867 {
00868 const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
00869 if ((Defense == BLACK && king.x() > 5) ||
00870 (Defense == WHITE && king.x() < 5))
00871 {
00872 const int tmp = effect1;
00873 effect1 = effect2;
00874 effect2 = tmp;
00875 i = 3 - i;
00876 }
00877 assert(0 <= effect1 && effect1 < 32);
00878 assert(0 <= effect2 && effect2 < 32);
00879 return king_x - 1 + 5 * (effect1 + 32 * (effect2 + 32 * i));
00880 }
00881 template <Player Defense>
00882 static int indexY(Square king, int effect1, int effect2, int i)
00883 {
00884 const int king_y = (Defense == BLACK ? king.y() : 10 - king.y());
00885 assert(0 <= effect1 && effect1 < 32);
00886 assert(0 <= effect2 && effect2 < 32);
00887 return king_y - 1 + 9 * (effect1 + 32 * (effect2 + 32 * i));
00888 }
00889 static CArray<MultiInt, 4096> table;
00890 static CArray<MultiInt, 20480> x_table;
00891 static CArray<MultiInt, 36864> y_table;
00892 };
00893 class King25MobilityX
00894 {
00895 public:
00896 enum { ONE_DIM = 20480, DIM = ONE_DIM * EvalStages };
00897 static void setUp(const Weights &weights);
00898 };
00899 class King25MobilityY
00900 {
00901 public:
00902 enum { ONE_DIM = 36864, DIM = ONE_DIM * EvalStages };
00903 static void setUp(const Weights &weights);
00904 };
00905
00906 class King25Effect3
00907 {
00908 friend class King25Effect3Y;
00909 public:
00910 enum { ONE_DIM = 2400, DIM = ONE_DIM * EvalStages };
00911 static void setUp(const Weights &weights);
00912 static MultiInt eval(const NumEffectState &state,
00913 const CArray<PieceMask, 2> &king25_mask);
00914 private:
00915 static int index(int piece_count, bool with_knight,
00916 int stand_count, bool with_knight_on_stand,
00917 int attacked_count)
00918 {
00919 assert(piece_count >= 0 && piece_count <= 9);
00920 assert(stand_count >= 0 && stand_count <= 9);
00921 assert(attacked_count >= 0 && attacked_count <= 5);
00922 return (piece_count + 10 *
00923 ((with_knight ? 1 : 0) + 2 *
00924 (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
00925 2 * attacked_count))));
00926 }
00927 static int indexY(int piece_count, bool with_knight,
00928 int stand_count, bool with_knight_on_stand,
00929 int attacked_count, int king_y)
00930 {
00931 assert(piece_count >= 0 && piece_count <= 9);
00932 assert(stand_count >= 0 && stand_count <= 9);
00933 assert(attacked_count >= 0 && attacked_count <= 5);
00934 return ((piece_count + 10 *
00935 ((with_knight ? 1 : 0) + 2 *
00936 (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
00937 2 * attacked_count))))) * 9 +
00938 king_y - 1;
00939 }
00940 template <osl::Player Attack>
00941 static MultiInt evalOne(const NumEffectState &state,
00942 PieceMask king25);
00943 static CArray<MultiInt, 2400> table;
00944 static CArray<MultiInt, 21600> y_table;
00945 };
00946 class King25Effect3Y
00947 {
00948 public:
00949 enum { ONE_DIM = 21600, DIM = ONE_DIM * EvalStages };
00950 static void setUp(const Weights &weights);
00951 };
00952
00953 class King25EffectCountCombination
00954 {
00955 friend class King25EffectCountCombinationY;
00956 public:
00957 enum { ONE_DIM = 100, DIM = ONE_DIM * EvalStages };
00958 static void setUp(const Weights &weights);
00959 static MultiInt eval(const NumEffectState &state,
00960 const CArray<PieceMask, 2> &king25);
00961 private:
00962 template <osl::Player Attack>
00963 static MultiInt evalOne(const NumEffectState &state,
00964 PieceMask king25);
00965 static CArray<MultiInt, 100> table;
00966 static CArray<MultiInt, 900> y_table;
00967 };
00968 class King25EffectCountCombinationY
00969 {
00970 public:
00971 enum { ONE_DIM = 900, DIM = ONE_DIM * EvalStages };
00972 static void setUp(const Weights &weights);
00973 };
00974
00975 class BishopExchangeSilverKing
00976 {
00977 static int indexKing(Square king)
00978 {
00979 const int y = king.y();
00980 if (y >= 3)
00981 return -1;
00982 return (y-1)*9 + king.x()-1;
00983 }
00984 static int indexRook(Square rook)
00985 {
00986 assert(rook.isOnBoard());
00987 const int y = rook.y();
00988 if (y >= 6)
00989 return -1;
00990 return (y-1)*9 + rook.x()-1;
00991 }
00992 static int indexSilver(Square silver)
00993 {
00994 return (silver.y()-1)*9 + silver.x()-1;
00995 }
00996 public:
00997 enum { BISHOP_ONE_DIM = 18 * 81 * (45*2), DIM = BISHOP_ONE_DIM*3 };
00998 static void setUp(const Weights &weights);
00999 static int eval(const NumEffectState& state);
01000 private:
01001 template <Player KingOwner>
01002 static int evalOne(const NumEffectState &state, int offset);
01003 static CArray<int, DIM> table;
01004 };
01005
01006 class EnterKingDefense
01007 {
01008 public:
01009 enum { DIM = (8+8+8+8)*3 };
01010 static void setUp(const Weights &weights);
01011 static int eval(const NumEffectState &state);
01012 private:
01013 template <Player KingOwner>
01014 static int evalOne(const NumEffectState &state);
01015 static CArray<int, DIM> table;
01016 };
01017 }
01018 }
01019 }
01020 #endif // EVAL_ML_KINGTABLE_H
01021
01022
01023
01024