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