00001 #include "osl/eval/majorPiece.h"
00002 #include "osl/mobility/rookMobility.h"
00003 #include <algorithm>
00004 using osl::MultiInt;
00005
00006 template <bool Opening, osl::Ptype MajorBasic>
00007 osl::CArray<int, 18>
00008 osl::eval::ml::MajorY<Opening, MajorBasic>::table;
00009
00010 template <bool Opening, osl::Ptype MajorBasic>
00011 void osl::eval::ml::
00012 MajorY<Opening, MajorBasic>::setUp(const Weights &weights)
00013 {
00014 for (size_t i = 0; i < weights.dimension(); ++i)
00015 {
00016 table[i] = weights.value(i);
00017 }
00018 }
00019
00020
00021 template <bool Opening>
00022 int osl::eval::ml::RookPawn<Opening>::weight;
00023
00024 template <bool Opening>
00025 void osl::eval::ml::
00026 RookPawn<Opening>::setUp(const Weights &weights)
00027 {
00028 weight = weights.value(0);
00029 }
00030
00031 template <bool Opening>
00032 int osl::eval::ml::RookPawn<Opening>::eval(const NumEffectState &state)
00033 {
00034 int result = 0;
00035 for (int i = PtypeTraits<ROOK>::indexMin;
00036 i < PtypeTraits<ROOK>::indexLimit;
00037 ++i)
00038 {
00039 const Piece piece = state.pieceOf(i);
00040 if (piece.isOnBoard() && !piece.square().canPromote(piece.owner()) &&
00041 !state.isPawnMaskSet(piece.owner(), piece.square().x()))
00042 {
00043 if (piece.owner() == BLACK)
00044 result += weight;
00045 else
00046 result -= weight;
00047 }
00048 }
00049 return result;
00050 }
00051
00052
00053 osl::CArray<MultiInt, 180> osl::eval::ml::RookPawnY::table;
00054 osl::CArray<MultiInt, 1620> osl::eval::ml::
00055 RookPawnY::y_attack_table;
00056 osl::CArray<MultiInt, 1620> osl::eval::ml::
00057 RookPawnY::y_defense_table;
00058
00059 void osl::eval::ml::
00060 RookPawnYX::setUp(const Weights &weights)
00061 {
00062 for (int i = 0; i < ONE_DIM; ++i)
00063 {
00064 for (int s=0; s<NStages; ++s)
00065 {
00066 RookPawnY::y_attack_table[i][s] = weights.value(i + ONE_DIM * 2 * s);
00067 RookPawnY::y_defense_table[i][s] = weights.value(i + ONE_DIM * 2 * s + ONE_DIM);
00068 }
00069 }
00070 }
00071
00072
00073 void osl::eval::ml::
00074 RookPawnY::setUp(const Weights &weights)
00075 {
00076 for (int i = 0; i < ONE_DIM; ++i)
00077 {
00078 for (int s=0; s<NStages; ++s)
00079 table[i][s] = weights.value(i + ONE_DIM*s);
00080 }
00081 }
00082
00083 MultiInt osl::eval::ml::
00084 RookPawnY::eval(const NumEffectState &state,
00085 const CArray2d<int, 2, 9> &pawns)
00086 {
00087 MultiInt result;
00088 const CArray<Square,2> kings = {
00089 state.kingSquare<BLACK>(),
00090 state.kingSquare<WHITE>(),
00091 };
00092 for (int i = PtypeTraits<ROOK>::indexMin;
00093 i < PtypeTraits<ROOK>::indexLimit;
00094 ++i)
00095 {
00096 const Piece piece = state.pieceOf(i);
00097 if (piece.isOnBoard())
00098 {
00099 if (piece.owner() == BLACK)
00100 {
00101 const int pawn_y =
00102 pawns[BLACK][piece.square().x() - 1];
00103 result +=
00104 table[index(piece, pawn_y)] +
00105 y_attack_table[indexY(kings[WHITE], piece, pawn_y)] +
00106 y_defense_table[indexY(kings[BLACK], piece, pawn_y)];
00107 }
00108 else
00109 {
00110 int y = pawns[WHITE][piece.square().x() - 1];
00111 if (y != 0)
00112 y = 10 - y;
00113 result -=
00114 table[index(piece, y)] +
00115 y_attack_table[indexY(kings[BLACK], piece, y)] +
00116 y_defense_table[indexY(kings[WHITE], piece, y)];
00117 }
00118 }
00119 }
00120 return result;
00121 }
00122
00123
00124 MultiInt osl::eval::ml::AllMajor::weight;
00125
00126 void osl::eval::ml::
00127 AllMajor::setUp(const Weights &weights,int stage)
00128 {
00129 weight[stage] = weights.value(0);
00130 }
00131
00132
00133 template <bool Opening>
00134 osl::CArray<int, 32> osl::eval::ml::MajorGoldSilverAttacked<Opening>::table;
00135
00136 template <bool Opening>
00137 void osl::eval::ml::
00138 MajorGoldSilverAttacked<Opening>::setUp(const Weights &weights)
00139 {
00140 for (size_t i = 0; i < weights.dimension(); ++i)
00141 {
00142 table[i] = weights.value(i);
00143 }
00144 }
00145
00146 template <bool Opening>
00147 int osl::eval::ml::
00148 MajorGoldSilverAttacked<Opening>::index(
00149 const NumEffectState &state, Piece piece)
00150 {
00151 return piece.ptype() + (state.turn() == piece.owner() ? 0 : PTYPE_SIZE);
00152 }
00153
00154 template <bool Opening>
00155 template <osl::Ptype PTYPE>
00156 int osl::eval::ml::
00157 MajorGoldSilverAttacked<Opening>::evalOne(const NumEffectState &state)
00158 {
00159 int result = 0;
00160 for (int i = PtypeTraits<PTYPE>::indexMin;
00161 i < PtypeTraits<PTYPE>::indexLimit;
00162 ++i)
00163 {
00164 const Piece piece = state.pieceOf(i);
00165 if (piece.isOnBoard() &&
00166 state.hasEffectAt(alt(piece.owner()), piece.square()))
00167 {
00168 const int weight = table[index(state, piece)];
00169 if (piece.owner() == BLACK)
00170 result += weight;
00171 else
00172 result -= weight;
00173 }
00174 }
00175 return result;
00176 }
00177
00178 template <bool Opening>
00179 int osl::eval::ml::
00180 MajorGoldSilverAttacked<Opening>::eval(const NumEffectState &state)
00181 {
00182 int result = 0;
00183 result += evalOne<ROOK>(state);
00184 result += evalOne<BISHOP>(state);
00185 result += evalOne<GOLD>(state);
00186 result += evalOne<SILVER>(state);
00187
00188 return result;
00189 }
00190
00191 osl::CArray<MultiInt, 612> osl::eval::ml::RookEffectBase::attack_table;
00192 osl::CArray<MultiInt, 612> osl::eval::ml::RookEffectBase::defense_table;
00193 osl::CArray<MultiInt, 32> osl::eval::ml::RookEffectBase::piece_table;
00194
00195 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::attack_u;
00196 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::attack_d;
00197 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::attack_r;
00198 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::attack_l;
00199 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::defense_u;
00200 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::defense_d;
00201 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::defense_r;
00202 osl::CArray<MultiInt, 23104> osl::eval::ml::RookEffectBase::defense_l;
00203 osl::CArray<MultiInt, 722> osl::eval::ml::RookEffectBase::attack_nospace;
00204 osl::CArray<MultiInt, 722> osl::eval::ml::RookEffectBase::defense_nospace;
00205
00206
00207 template<osl::Player P>
00208 inline
00209 MultiInt osl::eval::ml::RookEffectBase::evalOne(
00210 const NumEffectState& state,
00211 Square rook,
00212 Square myKing,
00213 Square opKing,
00214 Square up,
00215 Square dp,
00216 Square rp,
00217 Square lp,
00218 bool isP)
00219 {
00220 MultiInt result;
00221 PtypeO uPtypeO=state.pieceAt(up).ptypeO();
00222 PtypeO dPtypeO=state.pieceAt(dp).ptypeO();
00223 PtypeO rPtypeO=state.pieceAt(rp).ptypeO();
00224 PtypeO lPtypeO=state.pieceAt(lp).ptypeO();
00225 if(P==WHITE){
00226 uPtypeO=(PtypeO)(static_cast<int>(uPtypeO)^(~15));
00227 dPtypeO=(PtypeO)(static_cast<int>(dPtypeO)^(~15));
00228 rPtypeO=(PtypeO)(static_cast<int>(rPtypeO)^(~15));
00229 lPtypeO=(PtypeO)(static_cast<int>(lPtypeO)^(~15));
00230 up=up.rotate180EdgeOK();
00231 dp=dp.rotate180EdgeOK();
00232 rp=rp.rotate180EdgeOK();
00233 lp=lp.rotate180EdgeOK();
00234 rook=rook.rotate180();
00235 myKing=myKing.rotate180();
00236 opKing=opKing.rotate180();
00237 }
00238 assert((myKing.y()-dp.y())<(myKing.y()-up.y()));
00239 assert((myKing.x()-lp.x())<(myKing.x()-rp.x()));
00240 result+=attack_u[index1(opKing,up,uPtypeO,isP)]+
00241 attack_d[index1(opKing,dp,dPtypeO,isP)]+
00242 attack_l[index1(opKing,lp,lPtypeO,isP)]+
00243 attack_r[index1(opKing,rp,rPtypeO,isP)]+
00244 defense_u[index1(myKing,up,uPtypeO,isP)]+
00245 defense_d[index1(myKing,dp,dPtypeO,isP)]+
00246 defense_l[index1(myKing,lp,lPtypeO,isP)]+
00247 defense_r[index1(myKing,rp,rPtypeO,isP)]+
00248 attack_nospace[index2(opKing,rook,isP)]+
00249 defense_nospace[index2(myKing,rook,isP)];
00250 return result;
00251 }
00252
00253 MultiInt osl::eval::ml::
00254 RookEffectBase::eval(const NumEffectState &state)
00255 {
00256 const CArray<Square,2> kings = {
00257 state.kingSquare(BLACK),
00258 state.kingSquare(WHITE),
00259 };
00260 MultiInt result;
00261 for (int i = PtypeTraits<ROOK>::indexMin; i < PtypeTraits<ROOK>::indexLimit;
00262 ++i)
00263 {
00264 const Piece p = state.pieceOf(i);
00265 if (! p.isOnBoard()) continue;
00266 const Square pos=p.square();
00267 Square up=state.mobilityOf(U,i);
00268 Square dp=state.mobilityOf(D,i);
00269 Square lp=state.mobilityOf(L,i);
00270 Square rp=state.mobilityOf(R,i);
00271 const bool isP=p.isPromoted();
00272 if(p.owner()==BLACK)
00273 result+=evalOne<BLACK>(state,pos,kings[0],kings[1],up,dp,rp,lp,isP);
00274 else
00275 result-=evalOne<WHITE>(state,pos,kings[1],kings[0],dp,up,lp,rp,isP);
00276 }
00277 return result;
00278 }
00279
00280 void osl::eval::ml::RookEffect::setUp(const Weights &weights,int stage)
00281 {
00282 for (size_t i = 0; i < ONE_DIM; ++i)
00283 {
00284 attack_table[i][stage] = weights.value(i);
00285 defense_table[i][stage] = weights.value(i + ONE_DIM);
00286 }
00287 }
00288
00289 void osl::eval::ml::RookEffectPiece::setUp(const Weights &weights)
00290 {
00291 for (size_t i = 0; i < 32; ++i)
00292 {
00293 for (int s=0; s<NStages; ++s)
00294 RookEffectBase::piece_table[i][s] = weights.value(i + 32*s);
00295 }
00296 }
00297
00298 void osl::eval::ml::
00299 RookEffectPieceKingRelative::setUp(const Weights &weights)
00300 {
00301 CArray<MultiInt, 19584> piece_attack_table;
00302 CArray<MultiInt, 19584> piece_defense_table;
00303 for (size_t i = 0; i < ONE_DIM; ++i)
00304 {
00305 for (int s=0; s<NStages; ++s)
00306 {
00307 piece_attack_table[i][s] = weights.value(i + ONE_DIM*2*s);
00308 piece_defense_table[i][s] = weights.value(i + ONE_DIM*2*s + ONE_DIM);
00309 }
00310 }
00311 for(int isP=0;isP<2;isP++)
00312 for(int y_diff=-9;y_diff<=9;y_diff++)
00313 for(int x_diff= -9;x_diff<=9;x_diff++){
00314 int i2=index2(x_diff,y_diff,isP);
00315 if(abs(x_diff)<9 && abs(y_diff)<9){
00316 attack_nospace[i2]=
00317 -(attack_table[index(abs(x_diff),y_diff,true,isP)]+
00318 attack_table[index(abs(x_diff),y_diff,false,isP)]);
00319 defense_nospace[i2]=
00320 -(defense_table[index(abs(x_diff),y_diff,true,isP)]+
00321 defense_table[index(abs(x_diff),y_diff,false,isP)]);
00322 }
00323 for(int ptypeo= PTYPEO_MIN;ptypeo<=PTYPEO_MAX;ptypeo++){
00324 if(getPtype((PtypeO)ptypeo)==(int)PTYPE_EMPTY) continue;
00325 int i1=index1(x_diff,y_diff,(PtypeO)ptypeo,isP);
00326 int indexPieceH,indexPieceV;
00327 int table_ptypeo=ptypeo;
00328 if(getPtype((PtypeO)ptypeo)==PTYPE_EDGE) table_ptypeo=PTYPEO_EDGE;
00329 if(getPtype((PtypeO)ptypeo)==PTYPE_EDGE || abs(x_diff)==9 || abs(y_diff)==9){
00330 indexPieceH= 0+0+(PTYPEO_EDGE-PTYPEO_MIN)*17*9+4896+isP*9792;
00331 indexPieceV= 0+0+(PTYPEO_EDGE-PTYPEO_MIN)*17*9+ isP*9792;
00332 }
00333 else{
00334 indexPieceH= index0(abs(x_diff),-y_diff,(PtypeO)ptypeo,true,isP);
00335 indexPieceV= index0(abs(x_diff),-y_diff,(PtypeO)ptypeo,false,isP);
00336 }
00337 attack_u[i1]=piece_attack_table[indexPieceV]+piece_table[table_ptypeo-PTYPEO_MIN];
00338 defense_u[i1]=piece_defense_table[indexPieceV];
00339 attack_d[i1]=piece_attack_table[indexPieceV]+piece_table[table_ptypeo-PTYPEO_MIN];
00340 defense_d[i1]=piece_defense_table[indexPieceV];
00341 if(abs(x_diff)<=8){
00342 for(int y_diff_1=y_diff+1;y_diff_1<=8;y_diff_1++){
00343 int i=index(abs(x_diff),y_diff_1,false,isP);
00344 attack_u[i1]+=attack_table[i];
00345 defense_u[i1]+=defense_table[i];
00346 }
00347 for(int y_diff_1=std::max(-8,y_diff);y_diff_1<=8;y_diff_1++){
00348 int i=index(abs(x_diff),y_diff_1,false,isP);
00349 attack_d[i1]-=attack_table[i];
00350 defense_d[i1]-=defense_table[i];
00351 }
00352 }
00353 attack_l[i1]=piece_attack_table[indexPieceH]+piece_table[table_ptypeo-PTYPEO_MIN];
00354 defense_l[i1]=piece_defense_table[indexPieceH];
00355 attack_r[i1]=piece_attack_table[indexPieceH]+piece_table[table_ptypeo-PTYPEO_MIN];
00356 defense_r[i1]=piece_defense_table[indexPieceH];
00357 if(abs(y_diff)<=8){
00358 for(int x_diff_1=x_diff+1;x_diff_1<=8;x_diff_1++){
00359 int i=index(abs(x_diff_1),y_diff,true,isP);
00360 attack_r[i1]+=attack_table[i];
00361 defense_r[i1]+=defense_table[i];
00362 }
00363 for(int x_diff_1=std::max(-8,x_diff);x_diff_1<=8;x_diff_1++){
00364 int i=index(abs(x_diff_1),y_diff,true,isP);
00365 attack_l[i1]-=attack_table[i];
00366 defense_l[i1]-=defense_table[i];
00367 }
00368 }
00369 }
00370 }
00371 }
00372
00373
00374
00375 osl::CArray<MultiInt, 256> osl::eval::ml::RookPromoteDefense::promote_defense_table;
00376 osl::CArray<MultiInt, 144> osl::eval::ml::RookPromoteDefense::promote_defense_rook_table;
00377
00378 void osl::eval::ml::RookPromoteDefense::setUp(const Weights &weights)
00379 {
00380 for (size_t i = 0; i < ONE_DIM; ++i)
00381 {
00382 for (int s=0; s<NStages; ++s)
00383 promote_defense_table[i][s] = weights.value(i + ONE_DIM*s);
00384 }
00385 }
00386
00387 void osl::eval::ml::RookPromoteDefenseRookH::setUp(const Weights &weights)
00388 {
00389 for (size_t i = 0; i < ONE_DIM; ++i)
00390 {
00391 for (int s=0; s<NStages; ++s)
00392 RookPromoteDefense::promote_defense_rook_table[i][s] =
00393 weights.value(i + ONE_DIM*s);
00394 }
00395 }
00396
00397 MultiInt osl::eval::ml::
00398 RookPromoteDefense::eval(const NumEffectState &state)
00399 {
00400 MultiInt result;
00401 for (int i = PtypeTraits<ROOK>::indexMin;
00402 i < PtypeTraits<ROOK>::indexLimit;
00403 ++i){
00404 const Piece rook = state.pieceOf(i);
00405 if(rook.isOnBoardNotPromoted()){
00406 if(rook.owner()==BLACK){
00407 Square rookPos=rook.square();
00408 if(rookPos.y()>=4){
00409 Square pos=state.mobilityOf(U,i);
00410 const Piece attacked = state.pieceAt(pos);
00411 if (attacked.canMoveOn<BLACK>()){
00412 const NumBitmapEffect effect = state.effectSetAt(pos);
00413 if (effect.countEffect(WHITE) == 1){
00414 PieceMask mask = effect & state.piecesOnBoard(WHITE);
00415 const Piece effect_piece = state.pieceOf(mask.takeOneBit());
00416 const int index = attacked.ptype() * 16 + effect_piece.ptype();
00417 result += promote_defense_table[index];
00418 if (effect_piece.ptype() == ROOK &&
00419 effect_piece.square().x() != rookPos.x())
00420 {
00421 result +=
00422 promote_defense_rook_table[
00423 attacked.ptype() * 9 +
00424 mobility::RookMobility::countHorizontalAll<BLACK>(state,
00425 rook)];
00426 }
00427 }
00428 }
00429 }
00430 }
00431 else{
00432 Square rookPos=rook.square();
00433 if(rookPos.y()<=6){
00434 Square pos=state.mobilityOf(D,i);
00435 const Piece attacked = state.pieceAt(pos);
00436 if (attacked.canMoveOn<WHITE>()){
00437 const NumBitmapEffect effect = state.effectSetAt(pos);
00438 if (effect.countEffect(BLACK) == 1){
00439 PieceMask mask = effect & state.piecesOnBoard(BLACK);
00440 const Piece effect_piece = state.pieceOf(mask.takeOneBit());
00441 const int index = attacked.ptype() * 16 + effect_piece.ptype();
00442 result -= promote_defense_table[index];
00443 if (effect_piece.ptype() == ROOK &&
00444 effect_piece.square().x() != rookPos.x())
00445 {
00446 result -=
00447 promote_defense_rook_table[
00448 attacked.ptype() * 9 +
00449 mobility::RookMobility::countHorizontalAll<WHITE>(state,
00450 rook)];
00451 }
00452 }
00453 }
00454 }
00455 }
00456 }
00457 }
00458 return result;
00459 }
00460
00461
00462
00463 osl::CArray<MultiInt, 612> osl::eval::ml::BishopEffectBase::attack_table;
00464 osl::CArray<MultiInt, 612> osl::eval::ml::BishopEffectBase::defense_table;
00465 osl::CArray<MultiInt, 32> osl::eval::ml::BishopEffectBase::piece_table;
00466 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::attack_ul;
00467 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::attack_ur;
00468 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::attack_dl;
00469 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::attack_dr;
00470 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::defense_ul;
00471 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::defense_ur;
00472 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::defense_dl;
00473 osl::CArray<MultiInt, 23104> osl::eval::ml::BishopEffectBase::defense_dr;
00474 osl::CArray<MultiInt, 722> osl::eval::ml::BishopEffectBase::attack_nospace;
00475 osl::CArray<MultiInt, 722> osl::eval::ml::BishopEffectBase::defense_nospace;
00476
00477
00478 template<osl::Player P>
00479 inline
00480 MultiInt osl::eval::ml::BishopEffectBase::evalOne(
00481 const NumEffectState& state,
00482 Square bishop,
00483 Square myKing,
00484 Square opKing,
00485 Square ulp,
00486 Square urp,
00487 Square dlp,
00488 Square drp,
00489 bool isP)
00490 {
00491 MultiInt result;
00492 PtypeO ulPtypeO=state.pieceAt(ulp).ptypeO();
00493 PtypeO urPtypeO=state.pieceAt(urp).ptypeO();
00494 PtypeO dlPtypeO=state.pieceAt(dlp).ptypeO();
00495 PtypeO drPtypeO=state.pieceAt(drp).ptypeO();
00496 if(P==WHITE){
00497 ulPtypeO=(PtypeO)(static_cast<int>(ulPtypeO)^(~15));
00498 urPtypeO=(PtypeO)(static_cast<int>(urPtypeO)^(~15));
00499 dlPtypeO=(PtypeO)(static_cast<int>(dlPtypeO)^(~15));
00500 drPtypeO=(PtypeO)(static_cast<int>(drPtypeO)^(~15));
00501 ulp=ulp.rotate180EdgeOK();
00502 urp=urp.rotate180EdgeOK();
00503 dlp=dlp.rotate180EdgeOK();
00504 drp=drp.rotate180EdgeOK();
00505 bishop=bishop.rotate180();
00506 myKing=myKing.rotate180();
00507 opKing=opKing.rotate180();
00508 }
00509 result+=attack_ul[index1(opKing,ulp,ulPtypeO,isP)]+
00510 attack_ur[index1(opKing,urp,urPtypeO,isP)]+
00511 attack_dl[index1(opKing,dlp,dlPtypeO,isP)]+
00512 attack_dr[index1(opKing,drp,drPtypeO,isP)]+
00513 defense_ul[index1(myKing,ulp,ulPtypeO,isP)]+
00514 defense_ur[index1(myKing,urp,urPtypeO,isP)]+
00515 defense_dl[index1(myKing,dlp,dlPtypeO,isP)]+
00516 defense_dr[index1(myKing,drp,drPtypeO,isP)]+
00517 attack_nospace[index2(opKing,bishop,isP)]+
00518 defense_nospace[index2(myKing,bishop,isP)];
00519 return result;
00520 }
00521
00522 MultiInt osl::eval::ml::
00523 BishopEffectBase::eval(const NumEffectState &state)
00524 {
00525 const CArray<Square,2> kings = {{
00526 state.kingSquare(BLACK),
00527 state.kingSquare(WHITE),
00528 }};
00529
00530 MultiInt result;
00531 for (int i = PtypeTraits<BISHOP>::indexMin; i < PtypeTraits<BISHOP>::indexLimit;
00532 ++i)
00533 {
00534 const Piece p = state.pieceOf(i);
00535 if (! p.isOnBoard()) continue;
00536 const Square pos=p.square();
00537 Square ulp=state.mobilityOf(UL,i);
00538 Square urp=state.mobilityOf(UR,i);
00539 Square dlp=state.mobilityOf(DL,i);
00540 Square drp=state.mobilityOf(DR,i);
00541 const bool isP=p.isPromoted();
00542 if(p.owner()==BLACK)
00543 result+=evalOne<BLACK>(state,pos,kings[0],kings[1],ulp,urp,dlp,drp,isP);
00544 else
00545 result-=evalOne<WHITE>(state,pos,kings[1],kings[0],drp,dlp,urp,ulp,isP);
00546 }
00547 return result;
00548 }
00549
00550 void osl::eval::ml::BishopEffect::setUp(const Weights &weights,int stage)
00551 {
00552 for (size_t i = 0; i < ONE_DIM; ++i)
00553 {
00554 attack_table[i][stage] = weights.value(i);
00555 defense_table[i][stage] = weights.value(i + ONE_DIM);
00556 }
00557 }
00558
00559 void osl::eval::ml::BishopEffectPiece::setUp(const Weights &weights)
00560 {
00561 for (size_t i = 0; i < 32; ++i)
00562 {
00563 for (int s=0; s<NStages; ++s)
00564 BishopEffectBase::piece_table[i][s] = weights.value(i + 32*s);
00565 }
00566 }
00567
00568
00569 void osl::eval::ml::
00570 BishopEffectPieceKingRelative::setUp(const Weights &weights)
00571 {
00572 CArray<MultiInt, 19584> piece_attack_table;
00573 CArray<MultiInt, 19584> piece_defense_table;
00574 for (size_t i = 0; i < ONE_DIM; ++i)
00575 {
00576 for (int s=0; s<NStages; ++s)
00577 {
00578 piece_attack_table[i][s] = weights.value(i + ONE_DIM * 2 * s);
00579 piece_defense_table[i][s] = weights.value(i + ONE_DIM * 2 * s + ONE_DIM);
00580 }
00581 }
00582 for(int isP=0;isP<2;isP++)
00583 for(int y_diff=-9;y_diff<=9;y_diff++)
00584 for(int x_diff= -9;x_diff<=9;x_diff++){
00585 int i2=index2(x_diff,y_diff,isP);
00586 if(abs(x_diff)<9 && abs(y_diff)<9){
00587 attack_nospace[i2]=
00588 -(attack_table[index(x_diff,y_diff,true,isP)]+
00589 attack_table[index(x_diff,y_diff,false,isP)]);
00590 defense_nospace[i2]=
00591 -(defense_table[index(x_diff,y_diff,true,isP)]+
00592 defense_table[index(x_diff,y_diff,false,isP)]);
00593 }
00594 for(int ptypeo= PTYPEO_MIN;ptypeo<=PTYPEO_MAX;ptypeo++){
00595 if(getPtype((PtypeO)ptypeo)==(int)PTYPE_EMPTY) continue;
00596 int i1=index1(x_diff,y_diff,(PtypeO)ptypeo,isP);
00597 int indexPieceUR,indexPieceUL;
00598 int table_ptypeo=ptypeo;
00599 if(getPtype((PtypeO)ptypeo)==PTYPE_EDGE) table_ptypeo=PTYPEO_EDGE;
00600 if(getPtype((PtypeO)ptypeo)==PTYPE_EDGE || abs(x_diff)==9 || abs(y_diff)==9){
00601 indexPieceUR= 0+0+(PTYPEO_EDGE-PTYPEO_MIN)*17*9+4896+isP*9792;
00602 indexPieceUL= 0+0+(PTYPEO_EDGE-PTYPEO_MIN)*17*9+ isP*9792;
00603 }
00604 else{
00605 indexPieceUR= index0(x_diff,y_diff,(PtypeO)ptypeo,true,isP);
00606 indexPieceUL= index0(x_diff,y_diff,(PtypeO)ptypeo,false,isP);
00607 }
00608 attack_ul[i1]=piece_attack_table[indexPieceUL]+piece_table[table_ptypeo-PTYPEO_MIN];
00609 defense_ul[i1]=piece_defense_table[indexPieceUL];
00610 attack_dr[i1]=piece_attack_table[indexPieceUL]+piece_table[table_ptypeo-PTYPEO_MIN];
00611 defense_dr[i1]=piece_defense_table[indexPieceUL];
00612 {
00613 int y_diff_1=y_diff+1, x_diff_1=x_diff-1;
00614 for(;y_diff_1<=8 && x_diff_1>=-8;y_diff_1++,x_diff_1--){
00615 if(std::abs(x_diff_1)<=8 && std::abs(y_diff_1)<=8){
00616 int i=index(x_diff_1,y_diff_1,false,isP);
00617 attack_ul[i1]+=attack_table[i];
00618 defense_ul[i1]+=defense_table[i];
00619 }
00620 }
00621 }
00622 {
00623 int y_diff_1=y_diff, x_diff_1=x_diff;
00624 for(;y_diff_1<=8 && x_diff_1>=-8;y_diff_1++,x_diff_1--){
00625 if(std::abs(x_diff_1)<=8 && std::abs(y_diff_1)<=8){
00626 int i=index(x_diff_1,y_diff_1,false,isP);
00627 attack_dr[i1]-=attack_table[i];
00628 defense_dr[i1]-=defense_table[i];
00629 }
00630 }
00631 }
00632 attack_ur[i1]=piece_attack_table[indexPieceUR]+piece_table[table_ptypeo-PTYPEO_MIN];
00633 defense_ur[i1]=piece_defense_table[indexPieceUR];
00634 attack_dl[i1]=piece_attack_table[indexPieceUR]+piece_table[table_ptypeo-PTYPEO_MIN];
00635 defense_dl[i1]=piece_defense_table[indexPieceUR];
00636 {
00637 int y_diff_1=y_diff+1, x_diff_1=x_diff+1;
00638 for(;y_diff_1<=8 && x_diff_1<=8;y_diff_1++,x_diff_1++){
00639 if(std::abs(x_diff_1)<=8 && std::abs(y_diff_1)<=8){
00640 int i=index(x_diff_1,y_diff_1,true,isP);
00641 attack_ur[i1]+=attack_table[i];
00642 defense_ur[i1]+=defense_table[i];
00643 }
00644 }
00645 }
00646 {
00647 int y_diff_1=y_diff, x_diff_1=x_diff;
00648 for(;y_diff_1<=8 && x_diff_1<=8;y_diff_1++,x_diff_1++){
00649 if(std::abs(x_diff_1)<=8 && std::abs(y_diff_1)<=8){
00650 int i=index(x_diff_1,y_diff_1,true,isP);
00651 attack_dl[i1]-=attack_table[i];
00652 defense_dl[i1]-=defense_table[i];
00653 }
00654 }
00655 }
00656 }
00657 }
00658 }
00659
00660 osl::CArray<MultiInt, 32> osl::eval::ml::BishopHead::table;
00661 osl::CArray<MultiInt, 4896> osl::eval::ml::BishopHead::king_table;
00662 osl::CArray<MultiInt, 160> osl::eval::ml::BishopHead::x_table;
00663
00664 void osl::eval::ml::BishopHead::setUp(const Weights &weights)
00665 {
00666 for (size_t i = 0; i < ONE_DIM; ++i)
00667 {
00668 for (int s=0; s<NStages; ++s)
00669 table[i][s] = weights.value(i + ONE_DIM*s);
00670 }
00671 }
00672
00673 void osl::eval::ml::
00674 BishopHeadKingRelative::setUp(const Weights &weights)
00675 {
00676 for (size_t i = 0; i < ONE_DIM; ++i)
00677 {
00678 for (int s=0; s<NStages; ++s)
00679 BishopHead::king_table[i][s] = weights.value(i + ONE_DIM*s);
00680 }
00681 for(int x_diff=0;x_diff<=8;x_diff++)
00682 for(int y_diff=-8;y_diff<=8;y_diff++){
00683 for(int i=0;i<32;i++)
00684 BishopHead::king_table[(i*9+x_diff)*17+y_diff+8]+=BishopHead::table[i];
00685 }
00686 const PtypeO PTYPEO_EMPTY_R=newPtypeO(WHITE,PTYPE_EMPTY);
00687 const PtypeO PTYPEO_EDGE_R=newPtypeO(BLACK,PTYPE_EDGE);
00688 for(int x_diff=0;x_diff<=8;x_diff++)
00689 for(int y_diff=-8;y_diff<=8;y_diff++){
00690 BishopHead::king_table[(ptypeOIndex(PTYPEO_EMPTY_R)*9+x_diff)*17+y_diff+8]=
00691 BishopHead::king_table[(ptypeOIndex(PTYPEO_EMPTY)*9+x_diff)*17+y_diff+8];
00692 BishopHead::king_table[(ptypeOIndex(PTYPEO_EDGE_R)*9+x_diff)*17+y_diff+8]=
00693 BishopHead::king_table[(ptypeOIndex(PTYPEO_EDGE)*9+x_diff)*17+y_diff+8];
00694 }
00695 }
00696
00697 void osl::eval::ml::BishopHeadX::setUp(const Weights &weights)
00698 {
00699 for (size_t i = 0; i < ONE_DIM; ++i)
00700 {
00701 for (int s=0; s<NStages; ++s)
00702 BishopHead::x_table[i][s] = weights.value(i + ONE_DIM*s);
00703 }
00704 }
00705
00706 MultiInt osl::eval::ml::
00707 BishopHead::eval(const NumEffectState &state)
00708 {
00709 MultiInt result;
00710 for (int i = PtypeTraits<BISHOP>::indexMin;
00711 i < PtypeTraits<BISHOP>::indexLimit;
00712 ++i){
00713 const Piece p = state.pieceOf(i);
00714 if (p.isOnBoardNotPromoted()){
00715 const Square pos=p.square();
00716 if (p.owner() == BLACK){
00717 if(pos.y()>=2){
00718 const Square up = pos+DirectionPlayerTraits<U,BLACK>::offset();
00719 if (!state.hasEffectAt(BLACK, up)){
00720 const Square king = state.kingSquare(BLACK);
00721 const PtypeO ptypeo = state.pieceAt(up).ptypeO();
00722 const int index_k = indexK(BLACK, ptypeo,
00723 std::abs(pos.x() - king.x()),
00724 pos.y() - king.y());
00725 result += king_table[index_k];
00726 result += x_table[indexX<BLACK>(ptypeo, pos.x())];
00727 }
00728 }
00729 }
00730 else if(pos.y()<=8) {
00731 const Square up = pos+DirectionPlayerTraits<U,WHITE>::offset();
00732 if (!state.hasEffectAt(WHITE, up)){
00733 const Square king = state.kingSquare(WHITE);
00734 const PtypeO ptypeo = state.pieceAt(up).ptypeO();
00735 const int index_k = indexK(WHITE, ptypeo,
00736 std::abs(pos.x() - king.x()),
00737 pos.y() - king.y());
00738 result -= king_table[index_k];
00739 result -= x_table[indexX<WHITE>(ptypeo, pos.x())];
00740 }
00741 }
00742 }
00743 }
00744 return result;
00745 }
00746
00747
00748 osl::CArray<MultiInt, 374544> osl::eval::ml::KingRookBishop::table;
00749
00750 void osl::eval::ml::KingRookBishop::setUp(const Weights &weights)
00751 {
00752 for (size_t i = 0; i < ONE_DIM; ++i)
00753 {
00754 for (int s=0; s<NStages; ++s)
00755 table[i][s] = weights.value(i + ONE_DIM*s);
00756 }
00757 }
00758
00759 template<osl::Player P>
00760 MultiInt osl::eval::ml::
00761 KingRookBishop::evalOne(const NumEffectState &state)
00762 {
00763 const Square king=state.kingSquare(P);
00764 MultiInt result;
00765 for (int i = PtypeTraits<ROOK>::indexMin;
00766 i < PtypeTraits<ROOK>::indexLimit;
00767 ++i)
00768 {
00769 const Piece rook = state.pieceOf(i);
00770 if (!rook.isOnBoard())
00771 {
00772 continue;
00773 }
00774 for (int j = PtypeTraits<BISHOP>::indexMin;
00775 j < PtypeTraits<BISHOP>::indexLimit;
00776 ++j)
00777 {
00778 const Piece bishop = state.pieceOf(j);
00779 if (!bishop.isOnBoard())
00780 {
00781 continue;
00782 }
00783 result += table[index<P>(king, rook, bishop)];
00784 }
00785 }
00786 return result;
00787 }
00788
00789 MultiInt osl::eval::ml::
00790 KingRookBishop::eval(const NumEffectState &state)
00791 {
00792 return evalOne<BLACK>(state)-evalOne<WHITE>(state);
00793 }
00794
00795
00796 osl::CArray<MultiInt, 9> osl::eval::ml::NumPiecesBetweenBishopAndKing::self_table;
00797 osl::CArray<MultiInt, 9> osl::eval::ml::NumPiecesBetweenBishopAndKing::opp_table;
00798 osl::CArray<MultiInt, 9> osl::eval::ml::NumPiecesBetweenBishopAndKing::all_table;
00799
00800 void osl::eval::ml::
00801 NumPiecesBetweenBishopAndKingSelf::setUp(const Weights &weights)
00802 {
00803 for (size_t i = 0; i < ONE_DIM; ++i)
00804 {
00805 for (int s=0; s<NStages; ++s)
00806 NumPiecesBetweenBishopAndKing::self_table[i][s] =
00807 weights.value(i + ONE_DIM*s);
00808 }
00809 }
00810
00811 void osl::eval::ml::
00812 NumPiecesBetweenBishopAndKingOpp::setUp(const Weights &weights)
00813 {
00814 for (size_t i = 0; i < ONE_DIM; ++i)
00815 {
00816 for (int s=0; s<NStages; ++s)
00817 NumPiecesBetweenBishopAndKing::opp_table[i][s] =
00818 weights.value(i + ONE_DIM*s);
00819 }
00820 }
00821
00822 void osl::eval::ml::
00823 NumPiecesBetweenBishopAndKingAll::setUp(const Weights &weights)
00824 {
00825 for (size_t i = 0; i < ONE_DIM; ++i)
00826 {
00827 for (int s=0; s<NStages; ++s)
00828 NumPiecesBetweenBishopAndKing::all_table[i][s] =
00829 weights.value(i + ONE_DIM*s);
00830 }
00831 }
00832
00833 osl::MultiInt osl::eval::ml::
00834 NumPiecesBetweenBishopAndKing::eval(const NumEffectState &state)
00835 {
00836 MultiInt result;
00837 for (int i = PtypeTraits<BISHOP>::indexMin;
00838 i < PtypeTraits<BISHOP>::indexLimit;
00839 ++i)
00840 {
00841 const Piece bishop = state.pieceOf(i);
00842 if (!bishop.isOnBoard())
00843 {
00844 continue;
00845 }
00846 int self, opp, all;
00847 countBetween(state,
00848 state.kingSquare(alt(bishop.owner())),
00849 bishop, self, opp, all);
00850 if (bishop.owner() == BLACK)
00851 {
00852 result += (self_table[self] + opp_table[opp] + all_table[all]);
00853 }
00854 else
00855 {
00856 result -= (self_table[self] + opp_table[opp] + all_table[all]);
00857 }
00858 }
00859 return result;
00860 }
00861
00862 void osl::eval::ml::
00863 NumPiecesBetweenBishopAndKing::countBetween(
00864 const NumEffectState &state, Square king, Piece bishop,
00865 int &self_count, int &opp_count, int &total_count)
00866 {
00867 assert(bishop.isOnBoard());
00868 if ((king.x() + king.y() != bishop.square().x() + bishop.square().y()) &&
00869 (king.x() - king.y() != bishop.square().x() - bishop.square().y()))
00870 {
00871 self_count = opp_count = total_count = 8;
00872 return;
00873 }
00874 Direction dir;
00875 assert(king.x() != bishop.square().x());
00876 assert(king.y() != bishop.square().y());
00877 if (king.x() < bishop.square().x())
00878 {
00879 if (king.y() < bishop.square().y())
00880 {
00881 dir = UR;
00882 }
00883 else
00884 {
00885 dir = DR;
00886 }
00887 }
00888 else
00889 {
00890 if (king.y() < bishop.square().y())
00891 {
00892 dir = UL;
00893 }
00894 else
00895 {
00896 dir = DL;
00897 }
00898 }
00899 const Player player = bishop.owner();
00900 const Direction move_dir = (player == BLACK ? dir : inverse(dir));
00901 self_count = opp_count = total_count = 0;
00902 for (Square pos = state.mobilityOf(dir, bishop.number());
00903 pos != king; pos = Board_Table.nextSquare(player, pos, move_dir))
00904 {
00905 assert(pos.isOnBoard());
00906 const Piece piece = state.pieceAt(pos);
00907 if (!piece.isEmpty())
00908 {
00909 ++total_count;
00910 if (piece.owner() == player)
00911 ++self_count;
00912 else
00913 ++opp_count;
00914 }
00915 }
00916 }
00917
00918
00919 osl::CArray<MultiInt, 64>
00920 osl::eval::ml::BishopBishopPiece::table;
00921
00922 void osl::eval::ml::
00923 BishopBishopPiece::setUp(const Weights &weights)
00924 {
00925 for (size_t i = 0; i < ONE_DIM; ++i)
00926 {
00927 for (int s=0; s<NStages; ++s)
00928 table[i][s] = weights.value(i + ONE_DIM*s);
00929 }
00930 }
00931
00932 osl::MultiInt osl::eval::ml::
00933 BishopBishopPiece::eval(const NumEffectState &state)
00934 {
00935 MultiInt result;
00936 const Piece bishop1 = state.pieceOf(PtypeTraits<BISHOP>::indexMin);
00937 const Piece bishop2 = state.pieceOf(PtypeTraits<BISHOP>::indexMin + 1);
00938 if (!bishop1.isOnBoard() || !bishop2.isOnBoard() ||
00939 bishop1.owner() == bishop2.owner())
00940 return result;
00941 if (bishop1.square().x() + bishop1.square().y() !=
00942 bishop2.square().x() + bishop2.square().y() &&
00943 bishop1.square().x() - bishop1.square().y() !=
00944 bishop2.square().x() - bishop2.square().y())
00945 return result;
00946
00947 if (state.hasEffectByPtype<BISHOP>(bishop2.owner(), bishop1.square()))
00948 return result;
00949
00950 Direction dir;
00951 if (bishop1.square().x() < bishop2.square().x())
00952 {
00953 if (bishop1.square().y() < bishop2.square().y())
00954 {
00955 dir = UR;
00956 }
00957 else
00958 {
00959 dir = DR;
00960 }
00961 }
00962 else
00963 {
00964 if (bishop1.square().y() < bishop2.square().y())
00965 {
00966 dir = UL;
00967 }
00968 else
00969 {
00970 dir = DL;
00971 }
00972 }
00973 Square p1 = state.mobilityOf(inverse(dir), bishop1.number());
00974 Square p2 = state.mobilityOf(dir, bishop2.number());
00975 if (p1 == p2)
00976 {
00977 const Piece p = state.pieceAt(p1);
00978 const bool black_with_support =
00979 state.hasEffectAt<BLACK>(bishop1.owner() == BLACK ?
00980 bishop1.square() : bishop2.square());
00981 const bool white_with_support =
00982 state.hasEffectAt<WHITE>(bishop1.owner() == WHITE ?
00983 bishop1.square() : bishop2.square());
00984 if (p.owner() == BLACK)
00985 {
00986 result += table[index(p.ptype(), black_with_support,
00987 white_with_support)];
00988 }
00989 else
00990 {
00991 result -= table[index(p.ptype(), white_with_support,
00992 black_with_support)];
00993 }
00994 }
00995 return result;
00996 }
00997
00998 osl::CArray<MultiInt, 800>
00999 osl::eval::ml::RookRook::table;
01000
01001 void osl::eval::ml::
01002 RookRook::setUp(const Weights &weights)
01003 {
01004 CArray<MultiInt, 800> orig_table;
01005 for (size_t i = 0; i < ONE_DIM; ++i)
01006 {
01007 for (int s=0; s<NStages; ++s)
01008 orig_table[i][s] = weights.value(i + ONE_DIM*s);
01009 }
01010 for (int owner = 0; owner < 2; ++owner)
01011 {
01012 const bool same_player = (owner == 0);
01013 for (int y1 = 0; y1 < 10; ++y1)
01014 {
01015 for (int y2 = 0; y2 < 10; ++y2)
01016 {
01017 for (int promoted1 = 0; promoted1 < 2; ++promoted1)
01018 {
01019 for (int promoted2 = 0; promoted2 < 2; ++promoted2)
01020 {
01021 if (same_player)
01022 {
01023 int y1p = y1;
01024 int y2p = y2;
01025 int promoted1p = promoted1;
01026 int promoted2p = promoted2;
01027 if (y1 > y2 || (y1 == y2 && !promoted1 && promoted2))
01028 {
01029 std::swap(y1p, y2p);
01030 std::swap(promoted1p, promoted2p);
01031 }
01032 table[index(same_player, promoted1, promoted2,
01033 y1, y2)] =
01034 orig_table[index(same_player, promoted1p, promoted2p,
01035 y1p, y2p)];
01036 }
01037 else
01038 {
01039 if (y1 + y2 > 10 || y1 == 0 ||
01040 (y1 + y2 == 10 && promoted1))
01041 {
01042 const int idx = index(same_player, promoted1, promoted2,
01043 y1, y2);
01044 table[idx] = orig_table[idx];
01045 }
01046 else
01047 {
01048 table[index(same_player, promoted1, promoted2,
01049 y1, y2)] =
01050 -orig_table[index(same_player, promoted2, promoted1,
01051 (10 - y2) % 10, (10 - y1) % 10)];
01052 }
01053 }
01054 }
01055 }
01056 }
01057 }
01058 }
01059 }
01060
01061 osl::MultiInt osl::eval::ml::
01062 RookRook::eval(const NumEffectState &state)
01063 {
01064 MultiInt result;
01065 Piece rook1 = state.pieceOf(PtypeTraits<ROOK>::indexMin);
01066 Piece rook2 = state.pieceOf(PtypeTraits<ROOK>::indexMin + 1);
01067 if (rook1.owner() == rook2.owner())
01068 {
01069 if (rook1.owner() == BLACK)
01070 {
01071 result += table[index<true, BLACK>(rook1, rook2)];
01072 }
01073 else
01074 {
01075 result -= table[index<true, WHITE>(rook1, rook2)];
01076 }
01077 }
01078 else
01079 {
01080 if (rook1.owner() != BLACK)
01081 {
01082 std::swap(rook1, rook2);
01083 }
01084 result += table[index<false, BLACK>(rook1, rook2)];
01085 }
01086 return result;
01087 }
01088
01089
01090 osl::CArray<MultiInt, 128>
01091 osl::eval::ml::RookRookPiece::table;
01092
01093 void osl::eval::ml::
01094 RookRookPiece::setUp(const Weights &weights)
01095 {
01096 for (size_t i = 0; i < ONE_DIM; ++i)
01097 {
01098 for (int s=0; s<NStages; ++s)
01099 table[i][s] = weights.value(i + ONE_DIM*s);
01100 }
01101 }
01102
01103 osl::MultiInt osl::eval::ml::
01104 RookRookPiece::eval(const NumEffectState &state)
01105 {
01106 MultiInt result;
01107 const Piece rook1 = state.pieceOf(PtypeTraits<ROOK>::indexMin);
01108 const Piece rook2 = state.pieceOf(PtypeTraits<ROOK>::indexMin + 1);
01109 if (!rook1.isOnBoard() || !rook2.isOnBoard() ||
01110 rook1.owner() == rook2.owner())
01111 return result;
01112
01113 if (state.hasEffectByPtype<ROOK>(rook2.owner(), rook1.square()))
01114 return result;
01115
01116 Direction dir;
01117 bool vertical = false;
01118 if (rook1.square().x() == rook2.square().x())
01119 {
01120 vertical = true;
01121 if (rook1.square().y() < rook2.square().y())
01122 {
01123 dir = D;
01124 }
01125 else
01126 {
01127 dir = U;
01128 }
01129 }
01130 else if (rook1.square().y() == rook2.square().y())
01131 {
01132 if (rook1.square().x() < rook2.square().x())
01133 {
01134 dir = L;
01135 }
01136 else
01137 {
01138 dir = R;
01139 }
01140 }
01141 else
01142 {
01143 return result;
01144 }
01145
01146 Square p1 = state.mobilityOf(dir, rook1.number());
01147 Square p2 = state.mobilityOf(inverse(dir), rook2.number());
01148 assert(p1.isOnBoard() && p2.isOnBoard());
01149 if (p1 == p2)
01150 {
01151 const Piece p = state.pieceAt(p1);
01152 const bool black_with_support =
01153 state.hasEffectAt<BLACK>(rook1.owner() == BLACK ?
01154 rook1.square() : rook2.square());
01155 const bool white_with_support =
01156 state.hasEffectAt<WHITE>(rook1.owner() == WHITE ?
01157 rook1.square() : rook2.square());
01158 if (p.owner() == BLACK)
01159 {
01160 result += table[index(p.ptype(), black_with_support,
01161 white_with_support, vertical)];
01162 }
01163 else
01164 {
01165 result -= table[index(p.ptype(), white_with_support,
01166 black_with_support, vertical)];
01167 }
01168 }
01169 return result;
01170 }
01171
01172
01173 osl::CArray<MultiInt, 32>
01174 osl::eval::ml::BishopStandFile5::table;
01175
01176 void osl::eval::ml::
01177 BishopStandFile5::setUp(const Weights &weights)
01178 {
01179 for (size_t i = 0; i < ONE_DIM; ++i)
01180 {
01181 for (int s=0; s<NStages; ++s)
01182 table[i][s] = weights.value(i + ONE_DIM*s);
01183 }
01184 }
01185
01186 osl::MultiInt osl::eval::ml::
01187 BishopStandFile5::eval(const NumEffectState &state)
01188 {
01189 MultiInt result;
01190 if (state.hasPieceOnStand<BISHOP>(BLACK))
01191 {
01192 result += table[ptypeOIndex(state.pieceAt(Square(5, 3)).ptypeO())];
01193 }
01194 if (state.hasPieceOnStand<BISHOP>(WHITE))
01195 {
01196 PtypeO ptypeO = state.pieceAt(Square(5, 7)).ptypeO();
01197 ptypeO = altIfPiece(ptypeO);
01198 result -= table[ptypeOIndex(ptypeO)];
01199 }
01200 return result;
01201 }
01202
01203
01204
01205 osl::CArray<MultiInt, osl::eval::ml::MajorCheckWithCapture::ONE_DIM>
01206 osl::eval::ml::MajorCheckWithCapture::table;
01207
01208 void osl::eval::ml::
01209 MajorCheckWithCapture::setUp(const Weights &weights)
01210 {
01211 for (size_t i = 0; i < ONE_DIM; ++i)
01212 {
01213 for (int s=0; s<NStages; ++s)
01214 table[i][s] = weights.value(i + ONE_DIM*s);
01215 }
01216 }
01217
01218 template <osl::Player Owner>
01219 osl::MultiInt osl::eval::ml::
01220 MajorCheckWithCapture::addOne(const NumEffectState &state)
01221 {
01222 const Square king = state.kingSquare(Owner);
01223 PieceMask pieces = state.effectedMask(alt(Owner));
01224 pieces &= state.piecesOnBoard(Owner);
01225 pieces &= ~state.effectedMask(Owner);
01226 MultiInt sum;
01227 while (pieces.any()) {
01228 const Piece p = state.pieceOf(pieces.takeOneBit());
01229 const Square sq = p.square();
01230 if (state.hasLongEffectAt<ROOK>(alt(Owner), sq)
01231 && state.hasEffectIf(newPtypeO(BLACK,ROOK), sq, king)) {
01232 if (Owner == BLACK)
01233 sum += table[index(p.ptype(), true, sq.canPromote(alt(Owner)))];
01234 else
01235 sum -= table[index(p.ptype(), true, sq.canPromote(alt(Owner)))];
01236 }
01237 if (state.hasLongEffectAt<BISHOP>(alt(Owner), sq)
01238 && state.hasEffectIf(newPtypeO(BLACK,BISHOP), sq, king)) {
01239 if (Owner == BLACK)
01240 sum += table[index(p.ptype(), false, sq.canPromote(alt(Owner)))];
01241 else
01242 sum -= table[index(p.ptype(), false, sq.canPromote(alt(Owner)))];
01243 }
01244 }
01245 return sum;
01246 }
01247
01248 osl::MultiInt osl::eval::ml::
01249 MajorCheckWithCapture::eval(const NumEffectState &state)
01250 {
01251 return addOne<BLACK>(state) + addOne<WHITE>(state);
01252 }
01253
01254
01255 osl::CArray<MultiInt, osl::eval::ml::RookSilverKnight::ONE_DIM>
01256 osl::eval::ml::RookSilverKnight::table;
01257
01258 void osl::eval::ml::
01259 RookSilverKnight::setUp(const Weights &weights)
01260 {
01261 for (size_t i = 0; i < ONE_DIM; ++i)
01262 {
01263 for (int s=0; s<NStages; ++s)
01264 table[i][s] = weights.value(i + ONE_DIM*s);
01265 }
01266 }
01267
01268 osl::MultiInt osl::eval::ml::
01269 RookSilverKnight::eval(const NumEffectState &state)
01270 {
01271 MultiInt result;
01272 for (int i = PtypeTraits<ROOK>::indexMin;
01273 i < PtypeTraits<ROOK>::indexLimit;
01274 ++i)
01275 {
01276 const Piece rook = state.pieceOf(i);
01277 if (!rook.isOnBoard())
01278 {
01279 continue;
01280 }
01281 for (int i = PtypeTraits<SILVER>::indexMin;
01282 i < PtypeTraits<SILVER>::indexLimit;
01283 ++i)
01284 {
01285 const Piece silver = state.pieceOf(i);
01286 if (!silver.isOnBoard() || silver.isPromoted() ||
01287 silver.owner() != rook.owner())
01288 {
01289 continue;
01290 }
01291 for (int i = PtypeTraits<KNIGHT>::indexMin;
01292 i < PtypeTraits<KNIGHT>::indexLimit;
01293 ++i)
01294 {
01295 const Piece knight = state.pieceOf(i);
01296 if (!knight.isOnBoard() || knight.isPromoted() ||
01297 knight.owner() != rook.owner())
01298 {
01299 continue;
01300 }
01301
01302 if (rook.owner() == BLACK)
01303 {
01304 if (rook.square().x() > 5)
01305 {
01306 result += table[index(9 - rook.square().x(), rook.square().y() - 1,
01307 9 - silver.square().x(), silver.square().y() - 1,
01308 9 - knight.square().x(), knight.square().y() - 1)];
01309 }
01310 else
01311 {
01312 result += table[index(rook.square().x() - 1, rook.square().y() - 1,
01313 silver.square().x() - 1, silver.square().y() - 1,
01314 knight.square().x() - 1, knight.square().y() - 1)];
01315 }
01316 }
01317 else
01318 {
01319 if (rook.square().x() >= 5)
01320 {
01321 result -= table[index(9 - rook.square().x(), 9 - rook.square().y(),
01322 9 - silver.square().x(), 9 - silver.square().y(),
01323 9 - knight.square().x(), 9 - knight.square().y())];
01324 }
01325 else
01326 {
01327 result -= table[index(rook.square().x() - 1, 9 - rook.square().y(),
01328 silver.square().x() - 1, 9 - silver.square().y(),
01329 knight.square().x() - 1, 9 - knight.square().y())];
01330 }
01331 }
01332 }
01333 }
01334 }
01335 return result;
01336 }
01337
01338
01339 osl::CArray<MultiInt, osl::eval::ml::BishopSilverKnight::ONE_DIM>
01340 osl::eval::ml::BishopSilverKnight::table;
01341
01342 void osl::eval::ml::
01343 BishopSilverKnight::setUp(const Weights &weights)
01344 {
01345 for (size_t i = 0; i < ONE_DIM; ++i)
01346 {
01347 for (int s=0; s<NStages; ++s)
01348 table[i][s] = weights.value(i + ONE_DIM*s);
01349 }
01350 }
01351
01352 osl::MultiInt osl::eval::ml::
01353 BishopSilverKnight::eval(const NumEffectState &state)
01354 {
01355 MultiInt result;
01356 for (int i = PtypeTraits<BISHOP>::indexMin;
01357 i < PtypeTraits<BISHOP>::indexLimit;
01358 ++i)
01359 {
01360 const Piece bishop = state.pieceOf(i);
01361 if (!bishop.isOnBoard())
01362 {
01363 continue;
01364 }
01365 for (int i = PtypeTraits<SILVER>::indexMin;
01366 i < PtypeTraits<SILVER>::indexLimit;
01367 ++i)
01368 {
01369 const Piece silver = state.pieceOf(i);
01370 if (!silver.isOnBoard() || silver.isPromoted() ||
01371 silver.owner() != bishop.owner())
01372 {
01373 continue;
01374 }
01375 for (int i = PtypeTraits<KNIGHT>::indexMin;
01376 i < PtypeTraits<KNIGHT>::indexLimit;
01377 ++i)
01378 {
01379 const Piece knight = state.pieceOf(i);
01380 if (!knight.isOnBoard() || knight.isPromoted() ||
01381 knight.owner() != bishop.owner())
01382 {
01383 continue;
01384 }
01385
01386 if (bishop.owner() == BLACK)
01387 {
01388 if (bishop.square().x() > 5)
01389 {
01390 result += table[index(9 - bishop.square().x(), bishop.square().y() - 1,
01391 9 - silver.square().x(), silver.square().y() - 1,
01392 9 - knight.square().x(), knight.square().y() - 1)];
01393 }
01394 else
01395 {
01396 result += table[index(bishop.square().x() - 1, bishop.square().y() - 1,
01397 silver.square().x() - 1, silver.square().y() - 1,
01398 knight.square().x() - 1, knight.square().y() - 1)];
01399 }
01400 }
01401 else
01402 {
01403 if (bishop.square().x() >= 5)
01404 {
01405 result -= table[index(9 - bishop.square().x(), 9 - bishop.square().y(),
01406 9 - silver.square().x(), 9 - silver.square().y(),
01407 9 - knight.square().x(), 9 - knight.square().y())];
01408 }
01409 else
01410 {
01411 result -= table[index(bishop.square().x() - 1, 9 - bishop.square().y(),
01412 silver.square().x() - 1, 9 - silver.square().y(),
01413 knight.square().x() - 1, 9 - knight.square().y())];
01414 }
01415 }
01416 }
01417 }
01418 }
01419 return result;
01420 }
01421
01422
01423 osl::CArray<MultiInt, osl::eval::ml::AttackMajorsInBase::ONE_DIM>
01424 osl::eval::ml::AttackMajorsInBase::table;
01425
01426 void osl::eval::ml::
01427 AttackMajorsInBase::setUp(const Weights &weights)
01428 {
01429 for (size_t i = 0; i < ONE_DIM; ++i) {
01430 for (int s=0; s<NStages; ++s)
01431 table[i][s] = weights.value(i + ONE_DIM*s);
01432 if (i > 0)
01433 table[i] += table[0];
01434 }
01435 }
01436
01437 template <osl::Player P>
01438 void osl::eval::ml::
01439 AttackMajorsInBase::addOne(const NumEffectState &state, Piece rook, MultiInt& result)
01440 {
01441 Square sq = rook.square();
01442 if (state.hasEffectAt(alt(P), sq)
01443 || sq.squareForBlack(P).y() < 8)
01444 return;
01445 typedef std::pair<Offset,Square> pair_t;
01446 const CArray<pair_t, 7> bishop_attack =
01447 {{
01448 pair_t(Offset::make<P,U>(), sq.neighbor<P, UL>()),
01449 pair_t(Offset::make<P,U>(), sq.neighbor<P, UR>()),
01450 pair_t(Offset::make<P,L>(), sq.neighbor<P, UL>()),
01451 pair_t(Offset::make<P,R>(), sq.neighbor<P, UR>()),
01452 pair_t(Offset::make<P,D>(), sq.neighbor<P, UL>()),
01453 pair_t(Offset::make<P,D>(), sq.neighbor<P, UR>()),
01454 pair_t(Offset::make<P,U>(), sq.neighbor<P, U>()),
01455 }};
01456 const bool has_gold = state.hasPieceOnStand(alt(P), GOLD);
01457 const bool rook_support = state.hasEffectAt(P, sq);
01458 for (pair_t pair: bishop_attack) {
01459 const Square attack_square = pair.second;
01460 if (! state[attack_square].isEmpty()
01461 || state.countEffect(P, attack_square) > 1)
01462 continue;
01463 const Square bishop_square = attack_square + pair.first;
01464 Piece p = state[bishop_square];
01465 if (! p.isPlayerPtype(P,BISHOP)
01466 || state.hasEffectAt(alt(P), bishop_square))
01467 continue;
01468 int a = state.countEffect(alt(P), attack_square) + has_gold;
01469 if (a <= state.countEffect(P, attack_square))
01470 continue;
01471 const int i = index(state.findCheapAttack(P, attack_square).ptype(),
01472 state.findCheapAttack(alt(P), attack_square).ptype(),
01473 has_gold, rook_support,
01474 state.hasEffectNotBy(P, rook, bishop_square));
01475 if (P == BLACK)
01476 result += table[i];
01477 else
01478 result -= table[i];
01479 }
01480 }
01481
01482 osl::MultiInt osl::eval::ml::
01483 AttackMajorsInBase::eval(const NumEffectState &state)
01484 {
01485 MultiInt result;
01486 for (int i=0; i<state.nthLimit<ROOK>(); ++i) {
01487 const Piece rook = state.nth<ROOK>(i);
01488 if (! rook.isOnBoard() || rook.isPromoted())
01489 continue;
01490 Player P = rook.owner();
01491 if (P == BLACK)
01492 addOne<BLACK>(state, rook, result);
01493 else
01494 addOne<WHITE>(state, rook, result);
01495 }
01496 return result;
01497 }
01498
01499
01500 namespace osl
01501 {
01502 namespace eval
01503 {
01504 namespace ml
01505 {
01506 template class MajorY<true, ROOK>;
01507 template class MajorY<false, ROOK>;
01508 template class MajorY<true, BISHOP>;
01509 template class MajorY<false, BISHOP>;
01510 template class RookPawn<true>;
01511 template class RookPawn<false>;
01512 template class MajorGoldSilverAttacked<false>;
01513 template MultiInt KingRookBishop::evalOne<BLACK>(const NumEffectState &state);
01514 template MultiInt KingRookBishop::evalOne<WHITE>(const NumEffectState &state);
01515 }
01516 }
01517 }
01518
01519
01520
01521
01522