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