00001
00002
00003 #include "osl/eval/ml/piecePairKing.h"
00004 #include "osl/eval/ml/weights.h"
00005 #include "osl/misc/fixedCapacityVector.h"
00006 #include "osl/misc/cstdint.h"
00007
00008 osl::CArray<osl::int16_t, 1488375> osl::eval::ml::PiecePairKing::table;
00009
00010 void osl::eval::ml::
00011 PiecePairKing::setUp(const Weights &weights)
00012 {
00013 for (size_t i=0; i<weights.dimension(); ++i)
00014 table[i] = weights.value(i);
00015
00016 for (int x=1; x<=5; ++x)
00017 {
00018 for (int y=1; y<=3; ++y)
00019 {
00020 bool flipx;
00021 const int king = indexKing(WHITE, Position(x,y), flipx);
00022 for (int i=0; i<45*7; ++i)
00023 for (int j=i+1; j<45*7; ++j)
00024 table[composeIndex(king, j, i)] = table[composeIndex(king, i, j)];
00025 }
00026 }
00027 }
00028
00029 osl::CArray<int,2> osl::eval::ml::
00030 PiecePairKing::eval(const NumEffectState& state)
00031 {
00032 CArray<int,2> ret;
00033 ret[BLACK] = evalOne<BLACK>(state);
00034 ret[WHITE] = evalOne<WHITE>(state);
00035 return ret;
00036 }
00037
00038 template <osl::Player King>
00039 int osl::eval::ml::
00040 PiecePairKing::evalOne(const NumEffectState& state)
00041 {
00042 FixedCapacityVector<Piece,38> pieces;
00043 if (state.template getKingPosition<King>().template positionForBlack<King>().y() < 7)
00044 return 0;
00045
00046 PieceMask bitset = state.getOnBoardMask(King) & ~state.promotedPieces();
00047 bitset.clearBit<KING>();
00048 while (! bitset.none())
00049 {
00050 const Piece p = state.getPieceOf(bitset.takeOneBit());
00051 if (p.position().positionForBlack<King>().y() >= 5)
00052 pieces.push_back(p);
00053 }
00054 int sum = 0;
00055 bool flipx;
00056 const int index_king = indexKing(King, state.getKingPosition(King), flipx);
00057 if (flipx)
00058 {
00059 for (size_t i=0; i<pieces.size(); ++i)
00060 {
00061 const unsigned int i0 = indexPiece<true>(King, pieces[i].position(), pieces[i].ptype());
00062 for (size_t j=i+1; j<pieces.size(); ++j)
00063 {
00064 const unsigned int i1 = indexPiece<true>(King, pieces[j].position(), pieces[j].ptype());
00065 const unsigned int index = composeIndex(index_king, i0, i1);
00066 sum += table[index];
00067 }
00068 }
00069 }
00070 else
00071 {
00072 for (size_t i=0; i<pieces.size(); ++i)
00073 {
00074 const unsigned int i0 = indexPiece<false>(King, pieces[i].position(), pieces[i].ptype());
00075 for (size_t j=i+1; j<pieces.size(); ++j)
00076 {
00077 const unsigned int i1 = indexPiece<false>(King, pieces[j].position(), pieces[j].ptype());
00078 const unsigned int index = composeIndex(index_king, i0, i1);
00079 sum += table[index];
00080 }
00081 }
00082 }
00083 return (King == BLACK) ? sum : -sum;
00084 }
00085
00086 template <osl::Player King>
00087 int osl::eval::ml::
00088 PiecePairKing::add(const NumEffectState& state, Position to, Ptype ptype)
00089 {
00090 const Position king = state.getKingPosition(King);
00091 bool flipx;
00092 const int index_king = indexKing(King, king, flipx);
00093 int sum = 0;
00094 PieceMask bitset = state.getOnBoardMask(King) & ~state.promotedPieces();
00095 bitset.clearBit<KING>();
00096 unsigned int i0;
00097 if (flipx)
00098 {
00099 i0 = indexPiece<true>(King, to, ptype);
00100 while (! bitset.none())
00101 {
00102 const Piece p = state.getPieceOf(bitset.takeOneBit());
00103 if (p.position().positionForBlack(King).y() < 5)
00104 continue;
00105 const unsigned int i1 = indexPiece<true>(King, p.position(), p.ptype());
00106 const unsigned int index = composeIndex(index_king, i0, i1);
00107 sum += table[index];
00108 }
00109 }
00110 else
00111 {
00112 i0 = indexPiece<false>(King, to, ptype);
00113 while (! bitset.none())
00114 {
00115 const Piece p = state.getPieceOf(bitset.takeOneBit());
00116 if (p.position().positionForBlack(King).y() < 5)
00117 continue;
00118 const unsigned int i1 = indexPiece<false>(King, p.position(), p.ptype());
00119 const unsigned int index = composeIndex(index_king, i0, i1);
00120 sum += table[index];
00121 }
00122 }
00123 sum -= table[composeIndex(index_king, i0, i0)];
00124 return (King == BLACK) ? sum : -sum;
00125 }
00126 template <osl::Player King>
00127 int osl::eval::ml::
00128 PiecePairKing::sub(const NumEffectState& state, Position from, Ptype ptype)
00129 {
00130 const Position king = state.getKingPosition(King);
00131 bool flipx;
00132 const int index_king = indexKing(King, king, flipx);
00133 int sum = 0;
00134 PieceMask bitset = state.getOnBoardMask(King) & ~state.promotedPieces();
00135 bitset.clearBit<KING>();
00136 if (flipx)
00137 {
00138 const unsigned int i0 = indexPiece<true>(King, from, ptype);
00139 while (! bitset.none())
00140 {
00141 const Piece p = state.getPieceOf(bitset.takeOneBit());
00142 if (p.position().positionForBlack(King).y() < 5)
00143 continue;
00144 const unsigned int i1 = indexPiece<true>(King, p.position(), p.ptype());
00145 const unsigned int index = composeIndex(index_king, i0, i1);
00146 sum -= table[index];
00147 }
00148 }
00149 else
00150 {
00151 const unsigned int i0 = indexPiece<false>(King, from, ptype);
00152 while (! bitset.none())
00153 {
00154 const Piece p = state.getPieceOf(bitset.takeOneBit());
00155 if (p.position().positionForBlack(King).y() < 5)
00156 continue;
00157 const unsigned int i1 = indexPiece<false>(King, p.position(), p.ptype());
00158 const unsigned int index = composeIndex(index_king, i0, i1);
00159 sum -= table[index];
00160 }
00161 }
00162 return (King == BLACK) ? sum : -sum;
00163 }
00164 template <osl::Player King>
00165 int osl::eval::ml::
00166 PiecePairKing::addSub(const NumEffectState& state, Position to, Ptype ptype, Position from)
00167 {
00168 const Position king = state.getKingPosition(King);
00169 bool flipx;
00170 const int index_king = indexKing(King, king, flipx);
00171 unsigned int i0, s0;
00172 int sum = 0;
00173 PieceMask bitset = state.getOnBoardMask(King) & ~state.promotedPieces();
00174 bitset.clearBit<KING>();
00175 FixedCapacityVector<Piece,38> pieces;
00176 if (flipx)
00177 {
00178 i0 = indexPiece<true>(King, to, ptype);
00179 s0 = indexPiece<true>(King, from, ptype);
00180 while (! bitset.none())
00181 {
00182 const Piece p = state.getPieceOf(bitset.takeOneBit());
00183 if (p.position().positionForBlack(King).y() < 5)
00184 continue;
00185 const unsigned int i1 = indexPiece<true>(King, p.position(), p.ptype());
00186 const unsigned int index = composeIndex(index_king, i0, i1);
00187 sum += table[index];
00188 const unsigned int sub_index = composeIndex(index_king, s0, i1);
00189 sum -= table[sub_index];
00190 }
00191 }
00192 else
00193 {
00194 i0 = indexPiece<false>(King, to, ptype);
00195 s0 = indexPiece<false>(King, from, ptype);
00196 while (! bitset.none())
00197 {
00198 const Piece p = state.getPieceOf(bitset.takeOneBit());
00199 if (p.position().positionForBlack(King).y() < 5)
00200 continue;
00201 const unsigned int i1 = indexPiece<false>(King, p.position(), p.ptype());
00202 const unsigned int index = composeIndex(index_king, i0, i1);
00203 sum += table[index];
00204 const unsigned int sub_index = composeIndex(index_king, s0, i1);
00205 sum -= table[sub_index];
00206 }
00207 }
00208 sum -= table[composeIndex(index_king, i0, i0)];
00209 sum += table[composeIndex(index_king, s0, i0)];
00210 return (King == BLACK) ? sum : -sum;
00211 }
00212
00213 template <osl::Player P>
00214 void osl::eval::ml::
00215 PiecePairKing::evalWithUpdateBang(const NumEffectState& state, Move moved, CArray<int,2>& last_value)
00216 {
00217 assert(P == moved.player());
00218 if (moved.isPass())
00219 return;
00220 const Player Opponent = PlayerTraits<P>::opponent;
00221 const Ptype captured = moved.capturePtype();
00222 bool adjust_capture = (captured != PTYPE_EMPTY)
00223 && ! isPromoted(captured)
00224 && moved.to().positionForBlack(alt(P)).y() >= 5;
00225 if (adjust_capture)
00226 {
00227 const Position roking = state.getKingPosition(alt(P)).positionForBlack(alt(P));
00228 adjust_capture = roking.y() >= 7;
00229 }
00230 if (moved.ptype() == KING)
00231 {
00232 last_value[P] = evalOne<P>(state);
00233 if (adjust_capture)
00234 last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
00235 return;
00236 }
00237 const Position rking = state.getKingPosition(P).positionForBlack(P);
00238 if (rking.y() < 7)
00239 {
00240 if (adjust_capture)
00241 last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
00242 return;
00243 }
00244 const Position rto = moved.to().positionForBlack(P);
00245 if (moved.isDrop())
00246 {
00247 if (rto.y() >= 5)
00248 last_value[P] += add<P>(state, moved.to(), moved.ptype());
00249 return;
00250 }
00251 const Position rfrom = moved.from().positionForBlack(P);
00252 if (adjust_capture)
00253 last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
00254
00255 if (isPromoted(moved.oldPtype()))
00256 return;
00257 if (rfrom.y() < 5)
00258 {
00259 if (rto.y() >= 5 && ! isPromoted(moved.ptype()))
00260 last_value[P] += add<P>(state, moved.to(), moved.ptype());
00261 return;
00262 }
00263 if (rto.y() < 5 || isPromoted(moved.ptype()))
00264 last_value[P] += sub<P>(state, moved.from(), moved.oldPtype());
00265 else
00266 last_value[P] += addSub<P>(state, moved.to(), moved.ptype(), moved.from());
00267 }
00268
00269 namespace osl
00270 {
00271 namespace eval
00272 {
00273 namespace ml
00274 {
00275 template void PiecePairKing::evalWithUpdateBang<BLACK>(const NumEffectState&, Move, CArray<int,2>&);
00276 template void PiecePairKing::evalWithUpdateBang<WHITE>(const NumEffectState&, Move, CArray<int,2>&);
00277 }
00278 }
00279 }
00280
00281
00282
00283
00284