00001 #include "osl/eval/mobility.h"
00002 #include "osl/mobility/rookMobility.h"
00003 #include "osl/mobility/bishopMobility.h"
00004 #include "osl/mobility/lanceMobility.h"
00005
00006 using osl::MultiInt;
00007
00008 osl::CArray<MultiInt, 18>
00009 osl::eval::ml::RookMobilityAll::rook_vertical_table;
00010 osl::CArray<MultiInt, 18>
00011 osl::eval::ml::RookMobilityAll::rook_horizontal_table;
00012 osl::CArray<MultiInt, 34>
00013 osl::eval::ml::RookMobilityAll::sum_table;
00014 osl::CArray<MultiInt, 324>
00015 osl::eval::ml::RookMobilityAll::x_table;
00016 osl::CArray<MultiInt, 324>
00017 osl::eval::ml::RookMobilityAll::y_table;
00018 osl::CArray<MultiInt, 17*9>
00019 osl::eval::ml::RookMobilityAll::sumkingx_table;
00020 osl::CArray<MultiInt, 9*2*5*9>
00021 osl::eval::ml::RookMobilityAll::xkingx_table;
00022
00023 osl::CArray<MultiInt, 36>
00024 osl::eval::ml::BishopMobilityAll::bishop_table;
00025 osl::CArray<MultiInt, 18>
00026 osl::eval::ml::BishopMobilityAll::each_table;
00027
00028 osl::CArray<MultiInt, 9>
00029 osl::eval::ml::LanceMobility::lance_table;
00030
00031 void osl::eval::ml::
00032 RookMobility::setUp(const Weights &weights,int stage)
00033 {
00034 for (size_t i = 0; i < 9; ++i)
00035 {
00036 RookMobilityAll::rook_vertical_table[i][stage] = weights.value(i);
00037 }
00038 for (size_t i = 0; i < 9; ++i)
00039 {
00040 RookMobilityAll::rook_horizontal_table[i][stage] = weights.value(i + 9);
00041 }
00042 for (size_t i = 0; i < 9; ++i)
00043 {
00044 RookMobilityAll::rook_vertical_table[i+9][stage] = weights.value(i + 18);
00045 }
00046 for (size_t i = 0; i < 9; ++i)
00047 {
00048 RookMobilityAll::rook_horizontal_table[i+9][stage] = weights.value(i + 27);
00049 }
00050 }
00051
00052 void osl::eval::ml::
00053 RookMobilitySum::setUp(const Weights &weights)
00054 {
00055 for (size_t i = 0; i < ONE_DIM; ++i)
00056 {
00057 for (int s=0; s<NStages; ++s)
00058 RookMobilityAll::sum_table[i][s] = weights.value(i + ONE_DIM*s);
00059 }
00060 }
00061
00062 void osl::eval::ml::
00063 RookMobilityX::setUp(const Weights &weights)
00064 {
00065 for (size_t i = 0; i < ONE_DIM; ++i)
00066 {
00067 for (int s=0; s<NStages; ++s)
00068 RookMobilityAll::x_table[i][s] = weights.value(i + ONE_DIM*s);
00069 }
00070 }
00071
00072 void osl::eval::ml::
00073 RookMobilityY::setUp(const Weights &weights)
00074 {
00075 for (size_t i = 0; i < ONE_DIM; ++i)
00076 {
00077 for (int s=0; s<NStages; ++s)
00078 RookMobilityAll::y_table[i][s] = weights.value(i + ONE_DIM*s);
00079 }
00080 }
00081
00082 void osl::eval::ml::
00083 RookMobilityXKingX::setUp(const Weights &weights)
00084 {
00085 for (size_t i = 0; i < ONE_DIM; ++i)
00086 {
00087 for (int s=0; s<NStages; ++s)
00088 RookMobilityAll::xkingx_table[i][s] = weights.value(i + ONE_DIM*s);
00089 }
00090 }
00091 void osl::eval::ml::
00092 RookMobilitySumKingX::setUp(const Weights &weights)
00093 {
00094 for (size_t i = 0; i < ONE_DIM; ++i)
00095 {
00096 for (int s=0; s<NStages; ++s)
00097 RookMobilityAll::sumkingx_table[i][s] = weights.value(i + ONE_DIM*s);
00098 }
00099 }
00100
00101 template <int Sign> inline
00102 void osl::eval::ml::
00103 RookMobilityAll::adjust(const NumEffectState& state,
00104 bool promoted, int vertical, int horizontal,
00105 Square position, MultiInt& value)
00106 {
00107 int offset=0;
00108 if(promoted) offset=9;
00109 if(Sign>0)
00110 {
00111 if (! promoted) {
00112 const Square king = state.kingSquare<BLACK>();
00113 value += (xkingx_table[indexXKingX<Sign>(position, king, vertical, true)]
00114 + xkingx_table[indexXKingX<Sign>(position, king, horizontal, false)]
00115 + sumkingx_table[vertical + horizontal + 17*std::abs(king.x()-position.x())]
00116 );
00117 }
00118 value+= (rook_vertical_table[vertical+offset]+
00119 rook_horizontal_table[horizontal+offset] +
00120 sum_table[vertical+horizontal+(promoted ? 17 : 0)] +
00121 x_table[indexX(position, promoted, vertical, true)] +
00122 x_table[indexX(position, promoted, horizontal, false)] +
00123 y_table[indexY<Sign>(position, promoted, vertical, true)] +
00124 y_table[indexY<Sign>(position, promoted, horizontal, false)]);
00125 }
00126 else{
00127 if (! promoted) {
00128 const Square king = state.kingSquare<WHITE>();
00129 value -= (xkingx_table[indexXKingX<Sign>(position, king, vertical, true)]
00130 + xkingx_table[indexXKingX<Sign>(position, king, horizontal, false)]
00131 + sumkingx_table[vertical + horizontal + 17*std::abs(king.x()-position.x())]
00132 );
00133 }
00134 value-= (rook_vertical_table[vertical+offset]+
00135 rook_horizontal_table[horizontal+offset] +
00136 sum_table[vertical+horizontal+(promoted ? 17 : 0)] +
00137 x_table[indexX(position, promoted, vertical, true)] +
00138 x_table[indexX(position, promoted, horizontal, false)] +
00139 y_table[indexY<Sign>(position, promoted, vertical, true)] +
00140 y_table[indexY<Sign>(position, promoted, horizontal, false)]);
00141 }
00142 }
00143
00144 void osl::eval::ml::
00145 RookMobilityAll::eval(const NumEffectState& state, MultiInt& out)
00146 {
00147 out.clear();
00148 for (int i = PtypeTraits<ROOK>::indexMin;
00149 i < PtypeTraits<ROOK>::indexLimit;
00150 ++i)
00151 {
00152 const Piece rook = state.pieceOf(i);
00153 if (! rook.isOnBoard())
00154 continue;
00155 if (rook.owner() == BLACK)
00156 {
00157 const int vertical = osl::mobility::RookMobility::countVerticalAll<BLACK>(state,i);
00158 const int horizontal = osl::mobility::RookMobility::countHorizontalAll<BLACK>(
00159 state, i);
00160 adjust<1>(state, rook.isPromoted(), vertical, horizontal, rook.square(), out);
00161 }
00162 else
00163 {
00164 const int vertical = osl::mobility::RookMobility::countVerticalAll<WHITE>(state,i);
00165 const int horizontal = osl::mobility::RookMobility::countHorizontalAll<WHITE>(
00166 state, i);
00167 adjust<-1>(state, rook.isPromoted(), vertical, horizontal, rook.square(), out);
00168 }
00169 }
00170 }
00171
00172
00173
00174
00175 void osl::eval::ml::
00176 BishopMobility::setUp(const Weights &weights,int stage)
00177 {
00178 for (size_t i = 0; i < 18; ++i)
00179 {
00180 BishopMobilityAll::bishop_table[i][stage] = weights.value(i);
00181 }
00182 for (size_t i = 0; i < 18; ++i)
00183 {
00184 BishopMobilityAll::bishop_table[i+18][stage] = weights.value(i + 18);
00185 }
00186 }
00187
00188 void osl::eval::ml::
00189 BishopMobilityEach::setUp(const Weights &weights)
00190 {
00191 for (size_t i = 0; i < ONE_DIM; ++i)
00192 {
00193 for (int s=0; s<NStages; ++s)
00194 BishopMobilityAll::each_table[i][s] = weights.value(i + ONE_DIM*s);
00195 }
00196 }
00197
00198 template <int Sign> inline
00199 void osl::eval::ml::
00200 BishopMobilityAll::adjust(bool promoted, int mobility1, int mobility2,
00201 MultiInt& value)
00202 {
00203 int count=0;
00204 int each_offset = 0;
00205 if(promoted)
00206 {
00207 count=18;
00208 each_offset = 9;
00209 }
00210 if(Sign>0)
00211 {
00212 value += (bishop_table[mobility1 + mobility2 + count] +
00213 each_table[mobility1 + each_offset] +
00214 each_table[mobility2 + each_offset]);
00215 }
00216 else
00217 {
00218 value -= (bishop_table[mobility1 + mobility2 + count] +
00219 each_table[mobility1 + each_offset] +
00220 each_table[mobility2 + each_offset]);
00221 }
00222 }
00223
00224 void osl::eval::ml::
00225 BishopMobilityAll::eval(const NumEffectState& state, MultiInt& out)
00226 {
00227 out.clear();
00228 for (int i = PtypeTraits<BISHOP>::indexMin;
00229 i < PtypeTraits<BISHOP>::indexLimit;
00230 ++i)
00231 {
00232 const Piece bishop = state.pieceOf(i);
00233 if (! bishop.isOnBoard())
00234 continue;
00235 if (bishop.owner() == BLACK)
00236 {
00237 const int mobility1 =
00238 mobility::BishopMobility::countAllDir<BLACK, UL>(state, bishop) +
00239 mobility::BishopMobility::countAllDir<BLACK, DR>(state, bishop);
00240 const int mobility2 =
00241 mobility::BishopMobility::countAllDir<BLACK, UR>(state, bishop) +
00242 mobility::BishopMobility::countAllDir<BLACK, DL>(state, bishop);
00243 adjust<1>(bishop.isPromoted(), mobility1, mobility2, out);
00244 }
00245 else
00246 {
00247 const int mobility1 =
00248 mobility::BishopMobility::countAllDir<WHITE, UL>(state, bishop) +
00249 mobility::BishopMobility::countAllDir<WHITE, DR>(state, bishop);
00250 const int mobility2 =
00251 mobility::BishopMobility::countAllDir<WHITE, UR>(state, bishop) +
00252 mobility::BishopMobility::countAllDir<WHITE, DL>(state, bishop);
00253 adjust<-1>(bishop.isPromoted(), mobility1, mobility2, out);
00254 }
00255 }
00256 }
00257
00258
00259
00260 void osl::eval::ml::
00261 LanceMobility::setUp(const Weights &weights,int stage)
00262 {
00263 for (size_t i = 0; i < 9; ++i)
00264 {
00265 lance_table[i][stage] = weights.value(i);
00266 }
00267 }
00268
00269 template <int Sign> inline
00270 void osl::eval::ml::
00271 LanceMobilityAll::adjust(int mobility, MultiInt& value)
00272 {
00273 if(Sign>0)
00274 value += LanceMobility::lance_table[mobility];
00275 else
00276 value -= LanceMobility::lance_table[mobility];
00277 }
00278
00279 void osl::eval::ml::
00280 LanceMobilityAll::eval(const NumEffectState &state, MultiInt& out)
00281 {
00282 out.clear();
00283 for (int i = PtypeTraits<LANCE>::indexMin;
00284 i < PtypeTraits<LANCE>::indexLimit;
00285 ++i)
00286 {
00287 const Piece lance = state.pieceOf(i);
00288 if (!lance.isOnBoardNotPromoted())
00289 continue;
00290 if (lance.pieceIsBlack())
00291 {
00292 const int mobility = osl::mobility::LanceMobility::countAll<BLACK>(
00293 state, lance.square(),i);
00294 adjust<1>(mobility, out);
00295 }
00296 else
00297 {
00298 const int mobility = osl::mobility::LanceMobility::countAll<WHITE>(
00299 state, lance.square(),i);
00300 adjust<-1>(mobility, out);
00301 }
00302 }
00303 }
00304
00305
00306
00307
00308