00001 #include "osl/eval/pieceStand.h"
00002 #include "osl/bits/pieceStand.h"
00003
00004 osl::CArray<osl::MultiInt, osl::Piece::SIZE>
00005 osl::eval::ml::PieceStand::table;
00006
00007 void osl::eval::ml::
00008 PieceStand::setUp(const Weights &weights,int stage)
00009 {
00010 for (size_t i = 0; i < weights.dimension(); ++i)
00011 {
00012 table[i][stage] = weights.value(i);
00013 }
00014 }
00015
00016 osl::MultiInt osl::eval::ml::PieceStand::eval(
00017 const NumEffectState &state)
00018 {
00019 MultiInt result;
00020 for (Ptype ptype: osl::PieceStand::order)
00021 {
00022 const int black_count =
00023 state.countPiecesOnStand(BLACK, ptype);
00024 const int white_count =
00025 state.countPiecesOnStand(WHITE, ptype);
00026 for (int j = 0; j < black_count; ++j)
00027 {
00028 result += table[Ptype_Table.getIndexMin(ptype) + j];
00029 }
00030 for (int j = 0; j < white_count; ++j)
00031 {
00032 result -= table[Ptype_Table.getIndexMin(ptype) + j];
00033 }
00034 }
00035 return result;
00036 }
00037
00038
00039
00040 osl::CArray<osl::MultiInt, 21>
00041 osl::eval::ml::NonPawnPieceStand::table;
00042
00043 void osl::eval::ml::
00044 NonPawnPieceStand::setUp(const Weights &weights,int stage)
00045 {
00046 for (size_t i = 0; i < weights.dimension(); ++i)
00047 {
00048 table[i][stage] = weights.value(i);
00049 }
00050 }
00051
00052 osl::MultiInt osl::eval::ml::
00053 NonPawnPieceStand::eval(int black_count, int white_count)
00054 {
00055 return table[black_count] - table[white_count];
00056 }
00057
00058
00059 osl::CArray<osl::MultiInt, 5625> osl::eval::ml::NonPawnPieceStandCombination::table;
00060 osl::CArray<osl::MultiInt, 5625> osl::eval::ml::NonPawnPieceStandCombination::check_table;
00061
00062 osl::MultiInt osl::eval::ml::
00063 NonPawnPieceStandCombination::sumUp(const CArray<int, 6> &indices,
00064 const CArray<MultiInt, 5625> &values)
00065 {
00066 osl::MultiInt result;
00067 for (int rook = 0; rook <= indices[0]; ++rook)
00068 {
00069 for (int bishop = 0; bishop <= indices[1]; ++bishop)
00070 {
00071 for (int gold = 0; gold <= indices[2]; ++gold)
00072 {
00073 for (int silver = 0; silver <= indices[3]; ++silver)
00074 {
00075 for (int knight = 0; knight <= indices[4]; ++knight)
00076 {
00077 for (int lance = 0; lance <= indices[5]; ++lance)
00078 {
00079 if (rook + bishop + gold + silver + knight + lance == 0)
00080 {
00081 continue;
00082 }
00083 result += values[index(rook, bishop,
00084 gold, silver, knight, lance)];
00085 }
00086 }
00087 }
00088 }
00089 }
00090 }
00091 return result;
00092 }
00093
00094 void osl::eval::ml::
00095 NonPawnPieceStandCombination::setUp(const Weights &weights)
00096 {
00097 CArray<MultiInt, 5625> orig_table;
00098 for (size_t i = 0; i < ONE_DIM; ++i)
00099 {
00100 for (int s=0; s<NStages; ++s)
00101 {
00102 orig_table[i][s] = weights.value(i + ONE_DIM*s);
00103 }
00104 }
00105 CArray<int, 6> indices;
00106 for (indices[0] = 0; indices[0] <= 2; ++indices[0])
00107 {
00108 for (indices[1] = 0; indices[1] <= 2; ++indices[1])
00109 {
00110 for (indices[2] = 0; indices[2] <= 4; ++indices[2])
00111 {
00112 for (indices[3] = 0; indices[3] <= 4; ++indices[3])
00113 {
00114 for (indices[4] = 0; indices[4] <= 4; ++indices[4])
00115 {
00116 for (indices[5] = 0; indices[5] <= 4; ++indices[5])
00117 {
00118 table[index(indices[0],
00119 indices[1],
00120 indices[2],
00121 indices[3],
00122 indices[4],
00123 indices[5])] = sumUp(indices, orig_table);
00124 }
00125 }
00126 }
00127 }
00128 }
00129 }
00130 table[0] = orig_table[0];
00131 }
00132
00133 void osl::eval::ml::
00134 CanCheckNonPawnPieceStandCombination::setUp(const Weights &weights)
00135 {
00136 CArray<MultiInt, 5625> orig_table;
00137 for (size_t i = 0; i < ONE_DIM; ++i)
00138 {
00139 for (int s=0; s<NStages; ++s)
00140 {
00141 orig_table[i][s] = weights.value(i + ONE_DIM*s);
00142 }
00143 }
00144 CArray<int, 6> indices;
00145 for (indices[0] = 0; indices[0] <= 2; ++indices[0])
00146 {
00147 for (indices[1] = 0; indices[1] <= 2; ++indices[1])
00148 {
00149 for (indices[2] = 0; indices[2] <= 4; ++indices[2])
00150 {
00151 for (indices[3] = 0; indices[3] <= 4; ++indices[3])
00152 {
00153 for (indices[4] = 0; indices[4] <= 4; ++indices[4])
00154 {
00155 for (indices[5] = 0; indices[5] <= 4; ++indices[5])
00156 {
00157 NonPawnPieceStandCombination::check_table[
00158 NonPawnPieceStandCombination::index(indices[0],
00159 indices[1],
00160 indices[2],
00161 indices[3],
00162 indices[4],
00163 indices[5])] =
00164 NonPawnPieceStandCombination::sumUp(indices, orig_table);
00165 }
00166 }
00167 }
00168 }
00169 }
00170 }
00171 NonPawnPieceStandCombination::check_table[0] = orig_table[0];
00172 }
00173
00174 osl::MultiInt osl::eval::ml::
00175 NonPawnPieceStandCombination::eval(const NumEffectState &state,
00176 const CArray<bool, 2> &can_check)
00177 {
00178 const int black_index = index(state.countPiecesOnStand<ROOK>(BLACK),
00179 state.countPiecesOnStand<BISHOP>(BLACK),
00180 state.countPiecesOnStand<GOLD>(BLACK),
00181 state.countPiecesOnStand<SILVER>(BLACK),
00182 state.countPiecesOnStand<KNIGHT>(BLACK),
00183 state.countPiecesOnStand<LANCE>(BLACK));
00184 const int white_index = index(state.countPiecesOnStand<ROOK>(WHITE),
00185 state.countPiecesOnStand<BISHOP>(WHITE),
00186 state.countPiecesOnStand<GOLD>(WHITE),
00187 state.countPiecesOnStand<SILVER>(WHITE),
00188 state.countPiecesOnStand<KNIGHT>(WHITE),
00189 state.countPiecesOnStand<LANCE>(WHITE));
00190 MultiInt result;
00191 result = table[black_index] - table[white_index];
00192 if (can_check[WHITE])
00193 {
00194 result += check_table[black_index];
00195 }
00196 if (can_check[BLACK])
00197 {
00198 result -= check_table[white_index];
00199 }
00200 return result;
00201 }
00202
00203 osl::MultiInt osl::eval::ml::
00204 NonPawnPieceStandCombination::evalWithUpdate(
00205 const NumEffectState &state,
00206 Move moved,
00207 const MultiInt &last_value,
00208 const CArray<bool, 2> &could_check,
00209 const CArray<bool, 2> &can_check)
00210 {
00211 if (!moved.isDrop() && ! moved.isCapture() &&
00212 could_check[0] == can_check[0] && could_check[1] == can_check[1])
00213 {
00214 return last_value;
00215 }
00216 return eval(state, can_check);
00217 }
00218
00219
00220 osl::CArray<osl::MultiInt, 44> osl::eval::ml::NonPawnPieceStandTurn::table;
00221
00222 void osl::eval::ml::
00223 NonPawnPieceStandTurn::setUp(const Weights &weights)
00224 {
00225 for (size_t i = 0; i < ONE_DIM; ++i)
00226 {
00227 for (int s=0; s<NStages; ++s)
00228 table[i][s] = weights.value(i + ONE_DIM*s);
00229 }
00230 }
00231
00232 void osl::eval::ml::
00233 NonPawnPieceStandTurn::eval(const NumEffectState &state, MultiIntPair& result)
00234 {
00235 result = MultiIntPair();
00236 for (Ptype ptype: osl::PieceStand::order)
00237 {
00238 if (ptype == PAWN)
00239 continue;
00240 const int black_count = state.countPiecesOnStand(BLACK, ptype);
00241 const int white_count = state.countPiecesOnStand(WHITE, ptype);
00242 for (int j = 0; j < black_count; ++j)
00243 {
00244 const int index_black = index(BLACK, BLACK, ptype, j);
00245 const int index_white = index(BLACK, WHITE, ptype, j);
00246 result[BLACK] += table[index_black];
00247 result[WHITE] += table[index_white];
00248 }
00249 for (int j = 0; j < white_count; ++j)
00250 {
00251 const int index_black = index(WHITE, BLACK, ptype, j);
00252 const int index_white = index(WHITE, WHITE, ptype, j);
00253 result[BLACK] -= table[index_black];
00254 result[WHITE] -= table[index_white];
00255 }
00256 }
00257 }
00258
00259 template<osl::Player P>
00260 void osl::eval::ml::
00261 NonPawnPieceStandTurn::evalWithUpdateBang(
00262 const NumEffectState &state,
00263 Move moved, MultiIntPair &result)
00264 {
00265 assert(P==moved.player());
00266 if (!moved.isDrop() && ! moved.isCapture())
00267 return;
00268
00269 if (moved.isDrop())
00270 {
00271 const Ptype ptype = moved.ptype();
00272 if (ptype == PAWN)
00273 return;
00274 const int count =
00275 state.countPiecesOnStand(P, moved.ptype());
00276 const int index_black = index(P, BLACK, moved.ptype(), count);
00277 const int index_white = index(P, WHITE, moved.ptype(), count);
00278 if(P==BLACK){
00279 result[BLACK] -= table[index_black];
00280 result[WHITE] -= table[index_white];
00281 }
00282 else{
00283 result[BLACK] += table[index_black];
00284 result[WHITE] += table[index_white];
00285 }
00286 }
00287 if (moved.isCapture() &&
00288 unpromote(moved.capturePtype()) != PAWN)
00289 {
00290 Ptype ptype = unpromote(moved.capturePtype());
00291 const int count = state.countPiecesOnStand(P, ptype) - 1;
00292 const int index_black = index(P, BLACK, ptype, count);
00293 const int index_white = index(P, WHITE, ptype, count);
00294 if(P==BLACK){
00295 result[BLACK] += table[index_black];
00296 result[WHITE] += table[index_white];
00297 }
00298 else{
00299 result[BLACK] -= table[index_black];
00300 result[WHITE] -= table[index_white];
00301 }
00302 }
00303 }
00304
00305
00306 osl::CArray<osl::MultiInt, 360> osl::eval::ml::PieceStandY::y_attack_table;
00307 osl::CArray<osl::MultiInt, 360> osl::eval::ml::PieceStandY::y_defense_table;;
00308 osl::CArray<osl::MultiInt, 9*7*19> osl::eval::ml::PieceStandY::y_attack_table_sum;
00309 osl::CArray<osl::MultiInt, 9*7*19> osl::eval::ml::PieceStandY::y_defense_table_sum;
00310
00311 void osl::eval::ml::
00312 PieceStandY::setUp(const Weights &weights)
00313 {
00314 for (size_t i = 0; i < ONE_DIM; ++i)
00315 {
00316 for (int s=0; s<NStages; ++s)
00317 {
00318 y_attack_table[i][s] = weights.value(i + ONE_DIM * 2 * s);
00319 y_defense_table[i][s] = weights.value(i + ONE_DIM * 2 * s + ONE_DIM);
00320 }
00321 }
00322 for (int i=0;i<7;i++){
00323 Ptype ptype=osl::PieceStand::order[i];
00324 int ptypeSize=Ptype_Table.getIndexLimit(ptype)-Ptype_Table.getIndexMin(ptype);
00325 for(int king_y=1;king_y<=9;king_y++){
00326 MultiInt attack_sum, defense_sum;
00327 for(int count=0;count<=ptypeSize;count++){
00328 #if 0
00329 int oldIndex=(king_y - 1) * 40 + Ptype_Table.getIndexMin(ptype) + count;
00330 int newIndex=(king_y - 1) * 7*19 + i*19 + count;
00331 #else
00332 int oldIndex=index(ptype,BLACK,Square(5,king_y),count);
00333 int newIndex=index(i,BLACK,Square(5,king_y),count);
00334 #endif
00335 y_attack_table_sum[newIndex]=attack_sum;
00336 y_defense_table_sum[newIndex]=defense_sum;
00337 if(count==ptypeSize) break;
00338 attack_sum += y_attack_table[oldIndex];
00339 defense_sum += y_defense_table[oldIndex];
00340 }
00341 }
00342 }
00343 }
00344
00345 inline
00346 void osl::eval::ml::PieceStandY::updateResult(NumEffectState const& state,osl::MultiInt &result,int i, osl::Ptype ptype, osl::CArray<osl::Square,2> const&kings)
00347 {
00348 const int black_count = state.countPiecesOnStand(BLACK, ptype);
00349 const int white_count = state.countPiecesOnStand(WHITE, ptype);
00350 const int attack_index_1 = PieceStandY::index(i, BLACK, kings[WHITE], black_count);
00351 const int attack_index_2 = PieceStandY::index(i, WHITE, kings[BLACK], white_count);
00352 const int defense_index_1 = PieceStandY::index(i, BLACK, kings[BLACK], black_count);
00353 const int defense_index_2 = PieceStandY::index(i, WHITE, kings[WHITE], white_count);
00354 result += y_attack_table_sum[attack_index_1] - y_attack_table_sum[attack_index_2] +
00355 y_defense_table_sum[defense_index_1] - y_defense_table_sum[defense_index_2];
00356 }
00357
00358 osl::MultiInt osl::eval::ml::
00359 PieceStandY::eval(const NumEffectState &state)
00360 {
00361 MultiInt result;
00362 const CArray<Square,2> kings = {{
00363 state.kingSquare(BLACK),
00364 state.kingSquare(WHITE),
00365 }};
00366 updateResult(state,result,0,ROOK,kings);
00367 updateResult(state,result,1,BISHOP,kings);
00368 updateResult(state,result,2,GOLD,kings);
00369 updateResult(state,result,3,SILVER,kings);
00370 updateResult(state,result,4,KNIGHT,kings);
00371 updateResult(state,result,5,LANCE,kings);
00372 updateResult(state,result,6,PAWN,kings);
00373 return result;
00374 }
00375
00376 template<osl::Player P>
00377 osl::MultiInt osl::eval::ml::
00378 PieceStandY::evalWithUpdate(
00379 const NumEffectState &state,
00380 Move moved, const MultiInt &last_value)
00381 {
00382 if (moved.ptype() == KING)
00383 return eval(state);
00384
00385 MultiInt result(last_value);
00386 if (moved.isDrop())
00387 {
00388 const Ptype ptype = moved.ptype();
00389 const int count =
00390 state.countPiecesOnStand(P, ptype);
00391 const int attack_index = index(ptype, P,
00392 state.kingSquare(alt(P)),
00393 count);
00394 const int defense_index = index(ptype, P,
00395 state.kingSquare(P),
00396 count);
00397 if(P==BLACK)
00398 result -= y_attack_table[attack_index] +y_defense_table[defense_index];
00399 else
00400 result += y_attack_table[attack_index] +y_defense_table[defense_index];
00401 }
00402 if (moved.isCapture())
00403 {
00404 Ptype ptype = unpromote(moved.capturePtype());
00405 const int count = state.countPiecesOnStand(P, ptype)-1;
00406 const int attack_index = index(ptype, P,
00407 state.kingSquare(alt(P)),
00408 count);
00409 const int defense_index = index(ptype, P,
00410 state.kingSquare(P),
00411 count);
00412 if(P==BLACK)
00413 result += y_attack_table[attack_index] +y_defense_table[defense_index];
00414 else
00415 result -= y_attack_table[attack_index] +y_defense_table[defense_index];
00416 }
00417 return result;
00418 }
00419
00420 osl::CArray<osl::MultiInt, 16384> osl::eval::ml::PieceStandCombinationBoth::table;
00421
00422 void osl::eval::ml::
00423 PieceStandCombinationBoth::setUp(const Weights &weights)
00424 {
00425 for (size_t i = 0; i < ONE_DIM; ++i)
00426 {
00427 int low = (i & 0x7F);
00428 int high = (i >> 7);
00429 if (low == high)
00430 continue;
00431 for (int s = 0; s < NStages; ++s)
00432 {
00433 table[i][s] = weights.value(i + ONE_DIM*s);
00434 if (high > low)
00435 {
00436 table[(low << 7) | high][s] = -table[i][s];
00437 }
00438 }
00439 }
00440 }
00441
00442 osl::MultiInt osl::eval::ml::
00443 PieceStandCombinationBoth::eval(const NumEffectState &state)
00444 {
00445 int black_index = 0;
00446 int white_index = 0;
00447 black_index |= ((state.hasPieceOnStand<ROOK>(BLACK) ? 1 : 0) << 6);
00448 black_index |= ((state.hasPieceOnStand<BISHOP>(BLACK) ? 1 : 0) << 5);
00449 black_index |= ((state.hasPieceOnStand<GOLD>(BLACK) ? 1 : 0) << 4);
00450 black_index |= ((state.hasPieceOnStand<SILVER>(BLACK) ? 1 : 0) << 3);
00451 black_index |= ((state.hasPieceOnStand<KNIGHT>(BLACK) ? 1 : 0) << 2);
00452 black_index |= ((state.hasPieceOnStand<LANCE>(BLACK) ? 1 : 0) << 1);
00453 black_index |= ((state.hasPieceOnStand<PAWN>(BLACK) ? 1 : 0) << 0);
00454 white_index |= ((state.hasPieceOnStand<ROOK>(WHITE) ? 1 : 0) << 6);
00455 white_index |= ((state.hasPieceOnStand<BISHOP>(WHITE) ? 1 : 0) << 5);
00456 white_index |= ((state.hasPieceOnStand<GOLD>(WHITE) ? 1 : 0) << 4);
00457 white_index |= ((state.hasPieceOnStand<SILVER>(WHITE) ? 1 : 0) << 3);
00458 white_index |= ((state.hasPieceOnStand<KNIGHT>(WHITE) ? 1 : 0) << 2);
00459 white_index |= ((state.hasPieceOnStand<LANCE>(WHITE) ? 1 : 0) << 1);
00460 white_index |= ((state.hasPieceOnStand<PAWN>(WHITE) ? 1 : 0) << 0);
00461 return table[(black_index << 7) | white_index];
00462 }
00463
00464
00465 namespace osl
00466 {
00467 namespace eval
00468 {
00469 namespace ml
00470 {
00471 template void NonPawnPieceStandTurn::evalWithUpdateBang<BLACK>(const NumEffectState &, Move, MultiIntPair &);
00472 template void NonPawnPieceStandTurn::evalWithUpdateBang<WHITE>(const NumEffectState &, Move, MultiIntPair &);
00473 template MultiInt PieceStandY::evalWithUpdate<BLACK>(const NumEffectState &, Move, const MultiInt &);
00474 template MultiInt PieceStandY::evalWithUpdate<WHITE>(const NumEffectState &, Move, const MultiInt &);
00475 }
00476 }
00477 }
00478
00479
00480
00481