4 #ifndef EVAL_ML_KINGTABLE_H 
    5 #define EVAL_ML_KINGTABLE_H 
   23         typedef CArray2d<MultiInt, PTYPE_SIZE, 17 * 9> 
table_t;
 
   25         static int index(
const NumEffectState &,
 
   30           return std::abs(position.
x() - king.
x()) * 17 +
 
   31             (owner == 
BLACK ? (king.
y() - position.
y()) :
 
   32              (position.
y() - king.
y())) + 8;
 
   34         static int index(
const NumEffectState &state,
 
   39           const Square king = state.kingSquare(
 
   40             same_king ? owner : 
alt(owner));
 
   41           return index(state, owner, position, king);
 
   43         static int index(
const NumEffectState &state,
 
   50         static int index(
const NumEffectState &state,
 
   73           const NumEffectState &state,
 
   75           const CArray<PieceMask, 2> &effected_mask,
 
   84           const int x = std::abs(pos.
x() - king.
x());
 
   85           const int y = (king.
y() - pos.
y()) *
 
   94         static CArray<MultiInt, ONE_DIM> 
table;
 
  103           const NumEffectState& state,
 
  106         static CArray<MultiInt, 2592> 
table;
 
  110         template <Player KingPlayer>
 
  116         template <Player KingPlayer>
 
  120           const int king_y = (KingPlayer == 
BLACK ? king.
y() : 10 - king.
y());
 
  121           const int piece_y = (KingPlayer == 
BLACK ? position.
y() :
 
  123           return (king_y - 1) * 9 * 32 + (piece_y - 1) * 32 +
 
  135         static int index(
int effect, 
int piece_count);
 
  140         static int eval(
const NumEffectState &state);
 
  146         static int index(
int effect, 
int piece_count)
 
  148           return effect + 128 * piece_count;
 
  151         template <Player Attack>
 
  152         static void countEffectAndPiecesBoth(
 
  155           PieceMask& effect_supported,
 
  160           int &attack_piece_supported,
 
  161           CArray<int, 5> &effect_vertical,
 
  162           CArray<int, 5> &king_vertical);
 
  163         enum { 
DIM = 17 * 128 * 2};
 
  167         static int eval(
const NumEffectState &,
 
  168                         int black_attack_effect, 
int black_attack_piece,
 
  169                         int white_attack_effect, 
int white_attack_piece,
 
  170                         int black_defense_effect, 
int black_defense_piece,
 
  171                         int white_defense_effect, 
int white_defense_piece)
 
  183         static int index(
int effect, 
int piece_count)
 
  185           return effect + 128 * piece_count;
 
  192           for (
size_t i = 0; i < weights.
dimension(); ++i)
 
  194             table[i] = weights.
value(i);
 
  197         static int eval(
const NumEffectState &,
 
  198                         int black_effect, 
int black_piece,
 
  199                         int white_effect, 
int white_piece)
 
  201           return table[
index(black_effect, black_piece)] -
 
  202             table[
index(white_effect, white_piece)];
 
  209         static int index(
int effect, 
int piece_count)
 
  211           return effect + 128 * piece_count;
 
  215         static CArray<MultiInt, 17 * 128> 
table;
 
  218           for (
size_t i = 0; i < weights.
dimension(); ++i)
 
  220             table[i][stage] = weights.
value(i);
 
  224                         int black_effect, 
int black_piece,
 
  225                         int white_effect, 
int white_piece)
 
  227           return table[
index(black_effect, black_piece)] -
 
  228             table[
index(white_effect, white_piece)];
 
  236         static int index(
int king_y, 
int effect, 
int piece_count)
 
  238           return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
 
  241         enum { 
DIM = 17 * 128 * 9};
 
  242         static CArray<int, 17 * 128 * 9> 
table;
 
  245           for (
size_t i = 0; i < weights.
dimension(); ++i)
 
  247             table[i] = weights.
value(i);
 
  250         static int eval(
const NumEffectState &state,
 
  251                         int black_effect, 
int black_piece,
 
  252                         int white_effect, 
int white_piece)
 
  255             return table[
index(10 - state.kingSquare<
WHITE>().y(),
 
  256                                black_effect, black_piece)] -
 
  258                           white_effect, white_piece)];
 
  265         static int index(
int king_y, 
int effect, 
int piece_count)
 
  267           return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
 
  270         enum { 
DIM = 17 * 128 * 9};
 
  271         static CArray<MultiInt, 17 * 128 * 9> 
table;
 
  274           for (
size_t i = 0; i < weights.
dimension(); ++i)
 
  276             table[i][stage] = weights.
value(i);
 
  280                         int black_effect, 
int black_piece,
 
  281                         int white_effect, 
int white_piece)
 
  283           return table[
index(state.kingSquare<
BLACK>().y(),
 
  284                              black_effect, black_piece)] -
 
  285             table[
index(10 - state.kingSquare<
WHITE>().y(),
 
  286                         white_effect, white_piece)];
 
  293         enum { 
DIM = 17 * 128 * 2 * 9};
 
  295         static int index(
int king_y, 
int effect, 
int piece_count)
 
  297           return effect + 128 * piece_count + (king_y - 1) * 128 * 17;
 
  303         static int eval(
const NumEffectState &state,
 
  304                         int black_attack_effect, 
int black_attack_piece,
 
  305                         int white_attack_effect, 
int white_attack_piece,
 
  306                         int black_defense_effect, 
int black_defense_piece,
 
  307                         int white_defense_effect, 
int white_defense_piece)
 
  310                                     black_attack_effect, black_attack_piece)] -
 
  312                                white_attack_effect, white_attack_piece)] +
 
  314                                 black_defense_effect, black_defense_piece)] -
 
  316                                 white_defense_effect, white_defense_piece)];
 
  325         static int index(
int effect, 
int piece_count,
 
  328           return (effect + 64 * piece_count) * 13 + stand_count;
 
  330         static CArray<MultiInt, ONE_DIM> 
table;
 
  334           const NumEffectState &,
 
  335           int black_attack_effect, 
int black_attack_piece,
 
  336           int white_attack_effect, 
int white_attack_piece,
 
  337           int black_stand_count, 
int white_stand_count)
 
  339           const int black_index = 
index(black_attack_effect,
 
  342           const int white_index = 
index(white_attack_effect,
 
  345           return table[black_index] - table[white_index];
 
  353         static int index(
int king_y, 
int effect, 
int piece_count,
 
  356           return ((effect + 64 * piece_count) * 13 + stand_count) * 9 +
 
  359         static CArray<MultiInt, ONE_DIM> 
table;
 
  363           const NumEffectState &state,
 
  364           int black_attack_effect, 
int black_attack_piece,
 
  365           int white_attack_effect, 
int white_attack_piece,
 
  366           int black_stand_count, 
int white_stand_count)
 
  368           const int black_index = 
index(10 - state.kingSquare<
WHITE>().y(),
 
  369                                         black_attack_effect, black_attack_piece,
 
  371           const int white_index = 
index(state.kingSquare<
BLACK>().y(),
 
  372                                         white_attack_effect, white_attack_piece,
 
  374           return table[black_index] - table[white_index];
 
  382         static int index(
int piece_count, 
int supported)
 
  384           return supported * 17 + piece_count;
 
  386         static CArray<MultiInt, ONE_DIM> 
table;
 
  390           int black_attack_piece,
 
  391           int white_attack_piece,
 
  392           int black_attack_supported_piece, 
int white_attack_supported_piece)
 
  394           const int black_index = 
index(black_attack_piece,
 
  395                                         black_attack_supported_piece);
 
  396           const int white_index = 
index(white_attack_piece,
 
  397                                         white_attack_supported_piece);
 
  398           return table[black_index] - table[white_index];
 
  406         static int index(
int piece_count, 
int supported, 
int y)
 
  408           return (supported * 17 + piece_count) * 9 + y - 1;
 
  410         static CArray<MultiInt, ONE_DIM> 
table;
 
  414           int black_attack_piece,
 
  415           int white_attack_piece,
 
  416           int black_attack_supported_piece, 
int white_attack_supported_piece,
 
  417           int black_king_y, 
int white_king_y)
 
  419           const int black_index = 
index(black_attack_piece,
 
  420                                         black_attack_supported_piece,
 
  422           const int white_index = 
index(white_attack_piece,
 
  423                                         white_attack_supported_piece,
 
  425           return table[black_index] - table[white_index];
 
  431         template <Player defense>
 
  432         static int evalOne(
const NumEffectState &state, 
const CArray<int, 15>& table);
 
  433         template <Player defense>
 
  434         static std::pair<int,int> evalOne(
const NumEffectState &state, 
const CArray<int, 15>& opening, 
const CArray<int, 15>& ending);
 
  435         static std::pair<CArray<int,2>, CArray<int,2> >
 
  436         eval(
const NumEffectState &state, 
const CArray<int, 15>& opening, 
const CArray<int, 15>& ending);
 
  437         static std::pair<CArray<int,2>, CArray<int,2> >
 
  439                        const CArray<int, 15>& opening, 
const CArray<int, 15>& ending,
 
  440                        const CArray<int,2>& last_opening_value, 
const CArray<int,2>& last_ending_value);
 
  441         static int index(
int rel_x, 
int rel_y)
 
  443           return (rel_y + 2) * 3 + std::abs(rel_x);
 
  445         static void setUpBase(
const Weights &weigths, CArray<int, 15>& table);
 
  453         static void setUp(
const Weights &weigths) { setUpBase(weigths, table); }
 
  454         static const CArray<int,2> 
eval(
const NumEffectState &state);
 
  455         static const CArray<int, 15>& 
weights() { 
return table; }
 
  463         static void setUp(
const Weights &weigths) { setUpBase(weigths, table); }
 
  464         static const CArray<int,2> 
eval(
const NumEffectState &state);
 
  465         static const CArray<int, 15>& 
weights() { 
return table; }
 
  468       template <
bool Opening>
 
  472         enum { 
DIM = 5 * 5 * 5 * 9 };
 
  475         template <Player player>
 
  481             return index<BLACK>(king, 
target);
 
  483             return index<WHITE>(king, 
target);
 
  485         template <Player Defense>
 
  489           const NumEffectState &state, 
osl::Move moved,
 
  520         static CArray<int, 5 * 3 * 8 * 3> 
table;
 
  521         template <Player Defense>
 
  522         static EffectState effectState(
const NumEffectState &state,
 
  524         template <Player Defense>
 
  525         static int index(
const NumEffectState &state, 
Square king,
 
  527         template <osl::Player Defense>
 
  528         static int evalOne(
const NumEffectState &state);
 
  531         static int eval(
const NumEffectState &state);
 
  590         static CArray<MultiInt, 5 * 3 * 8 * 3> 
table;
 
  595         template <Player Defense>
 
  596         static int effectStateIndex3(
const NumEffectState &state,
 
  598         template <Player Defense>
 
  599         static void index(
const NumEffectState &state, 
 
  602                           int rel_y, 
int king_x, 
int king_y, 
int x_diff
 
  604         template <osl::Player Defense>
 
  605         static void evalOne(
const NumEffectState &state,
 
  608         static void eval(
const NumEffectState &state,
 
  646         template <Player King, Direction Dir>
 
  656         template <Player King, Direction Dir>
 
  665           const int king_y = (King == 
BLACK ? king_position.
y() :
 
  666                               10 - king_position.
y());
 
  668             + (king_y - 1) * 32 * 32 * 3;
 
  670         template <Player King, Direction Dir>
 
  679           const int king_x = (king_position.
x() > 5 ? 10 - king_position.
x() :
 
  681           if (Dir == HORIZONTAL &&
 
  682               ((King == 
BLACK && king_position.
x() >= 6) ||
 
  683                (King == 
WHITE && king_position.
x() <= 4)))
 
  689             + (king_x - 1) * 32 * 32 * 3;
 
  692                                int diagonal_index1, 
int diagonal_index2,
 
  693                                int vertical_index_x,  
int horizontal_index_x,
 
  694                                int diagonal_index1_x, 
int diagonal_index2_x,
 
  695                                int vertical_index_y , 
int horizontal_index_y,
 
  696                                int diagonal_index1_y, 
int diagonal_index2_y) 
 
  698           return table[vertical_index] + table[horizontal_index] +
 
  699             table[diagonal_index1] + table[diagonal_index2] +
 
  706         template <Player King>
 
  707         static void evalOne(
const NumEffectState &state,
 
  709         static CArray<MultiInt, 3072> 
table;
 
  719           X_DIM = 32 * 32 * 3 * 5,
 
  720           Y_DIM = 32 * 32 * 3 * 9,
 
  721           ONE_DIM = X_DIM + Y_DIM,
 
  737         static MultiInt evalOne(
const NumEffectState &state);
 
  738         template<Direction Dir>
 
  741           if(Dir==
L) 
return king.
x()-target.
x()-1;
 
  742           else if(Dir==
R) 
return target.
x()-king.
x()-1;
 
  743           else if(Dir==
UL || Dir==
U || Dir==
UR) 
return target.
y()-king.
y()-1;
 
  744           else return king.
y()-target.
y()-1;
 
  746         static CArray<MultiInt, 3240> 
table;
 
  773         static MultiInt evalOne(
const NumEffectState &state);
 
  774         static CArray<MultiInt, 45*33> 
table;
 
  785         static MultiInt evalOne(
const NumEffectState &state,
 
  786                                 const CArray<int, 5> &effects);
 
  788                              const CArray<int, 5> &black,
 
  789                              const CArray<int, 5> &white);
 
  791         static int index(
int effect1, 
int effect2, 
int i)
 
  793           assert(0 <= effect1 && effect1 < 32);
 
  794           assert(0 <= effect2 && effect2 < 32);
 
  795           return effect1 + 32 * (effect2 + 32 * i);
 
  801           const int king_x = (king.
x() >= 6 ? 10 - king.
x() : king.
x());
 
  802           if ((P == 
BLACK && king.
x() > 5) ||
 
  803               (P == 
WHITE && king.
x() < 5))
 
  805             const int tmp = effect1;
 
  814           const int combination = (i * 3 + j - 2);
 
  815           assert(0 <= effect1 && effect1 < 32);
 
  816           assert(0 <= effect2 && effect2 < 32);
 
  817           return king_x - 1 + 5 * (effect1 + 32 *
 
  818                                    (effect2 + 32 * combination));
 
  820         static int indexX(
int king_x,
int effect1,
int effect2, 
int i){
 
  821           return king_x - 1 + 5 * (effect1 + 32 *
 
  827           const int king_y = (P == 
BLACK ? king.
y() : 10 - king.
y());
 
  828           assert(0 <= effect1 && effect1 < 32);
 
  829           assert(0 <= effect2 && effect2 < 32);
 
  830           return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
 
  832         static int indexY(
int king_y,
int effect1,
int effect2, 
int i){
 
  833           return king_y - 1 + 9 *(effect1 + 32 * (effect2 + 32 * i));
 
  835         static CArray<MultiInt, 8192> 
table;
 
  860                              const CArray<int, 5> &black,
 
  861                              const CArray<int, 5> &white);
 
  863         static int index(
int effect1, 
int effect2, 
int i)
 
  865           assert(0 <= effect1 && effect1 < 32);
 
  866           assert(0 <= effect2 && effect2 < 32);
 
  867           return effect1 + 32 * (effect2 + 32 * i);
 
  869         template <Player Defense>
 
  872           const int king_x = (king.
x() > 5 ? 10 - king.
x() : king.
x());
 
  873           if ((Defense == 
BLACK && king.
x() > 5) ||
 
  874               (Defense == 
WHITE && king.
x() < 5))
 
  876             const int tmp = effect1;
 
  881           assert(0 <= effect1 && effect1 < 32);
 
  882           assert(0 <= effect2 && effect2 < 32);
 
  883           return king_x - 1 + 5 * (effect1 + 32 * (effect2 + 32 * i));
 
  885         template <Player Defense>
 
  888           const int king_y = (Defense == 
BLACK ? king.
y() : 10 - king.
y());
 
  889           assert(0 <= effect1 && effect1 < 32);
 
  890           assert(0 <= effect2 && effect2 < 32);
 
  891           return king_y - 1 + 9 * (effect1 + 32 * (effect2 + 32 * i));
 
  893         static CArray<MultiInt, 4096> 
table;
 
  917                              const CArray<PieceMask, 2> &king25_mask);
 
  919         static int index(
int piece_count, 
bool with_knight,
 
  920                          int stand_count, 
bool with_knight_on_stand,
 
  923           assert(piece_count >= 0 && piece_count <= 9);
 
  924           assert(stand_count >= 0 && stand_count <= 9);
 
  925           assert(attacked_count >= 0 && attacked_count <= 5);
 
  926           return (piece_count + 10 *
 
  927                   ((with_knight ? 1 : 0) + 2 *
 
  928                    (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
 
  929                                         2 * attacked_count))));
 
  931         static int indexY(
int piece_count, 
bool with_knight,
 
  932                           int stand_count, 
bool with_knight_on_stand,
 
  933                           int attacked_count, 
int king_y)
 
  935           assert(piece_count >= 0 && piece_count <= 9);
 
  936           assert(stand_count >= 0 && stand_count <= 9);
 
  937           assert(attacked_count >= 0 && attacked_count <= 5);
 
  938           return ((piece_count + 10 *
 
  939                    ((with_knight ? 1 : 0) + 2 *
 
  940                     (stand_count + 10 * ((with_knight_on_stand ? 1 : 0) +
 
  941                                          2 * attacked_count))))) * 9 +
 
  944         template <osl::Player Attack>
 
  945         static MultiInt evalOne(
const NumEffectState &state,
 
  947         static CArray<MultiInt, 2400> 
table;
 
  964                              const CArray<PieceMask, 2> &king25);
 
  966         template <osl::Player Attack>
 
  967         static MultiInt evalOne(
const NumEffectState &state,
 
  983           const int y = king.
y();
 
  986           return (y-1)*9 + king.
x()-1;
 
  991           const int y = rook.
y();
 
  994           return (y-1)*9 + rook.
x()-1;
 
  998           return (silver.
y()-1)*9 + silver.
x()-1;
 
 1001         enum { BISHOP_ONE_DIM = 18 * 81 * (45*2), 
DIM = BISHOP_ONE_DIM*3 };
 
 1003         static int eval(
const NumEffectState& state);
 
 1005         template <Player KingOwner>
 
 1006         static int evalOne(
const NumEffectState &state, 
int offset);
 
 1015         static int eval(
const NumEffectState &state);
 
 1017         template <Player KingOwner>
 
 1018         static int evalOne(
const NumEffectState &state);
 
 1024 #endif // EVAL_ML_KINGTABLE_H