00001 #include "osl/progress/ml/newProgress.h"
00002 #include "osl/eval/ml/weights.h"
00003 #include "osl/eval/ml/midgame.h"
00004 #include "osl/eval/ml/minorPiece.h"
00005 #include "osl/effect_util/additionalEffect.h"
00006 #include "osl/pieceStand.h"
00007 #include "osl/oslConfig.h"
00008 #include <boost/foreach.hpp>
00009 #include <iostream>
00010 #include <fstream>
00011
00012 bool osl::progress::ml::
00013 operator==(const NewProgressData& l, const NewProgressData& r)
00014 {
00015 return l.progresses == r.progresses
00016 && l.attack5x5_progresses == r.attack5x5_progresses
00017 && l.stand_progresses == r.stand_progresses
00018 && l.effect_progresses == r.effect_progresses
00019 && l.defenses == r.defenses
00020 && l.rook == r.rook && l.bishop == r.bishop && l.gold == r.gold
00021 && l.silver == r.silver && l.promoted == r.promoted
00022 && l.king_relative_attack == r.king_relative_attack
00023 && l.king_relative_defense == r.king_relative_defense
00024 && l.non_pawn_ptype_attacked_pair == r.non_pawn_ptype_attacked_pair
00025 && l.non_pawn_ptype_attacked_pair_eval == r.non_pawn_ptype_attacked_pair_eval;
00026 }
00027
00028 osl::misc::CArray<int, 81*15*10>
00029 osl::progress::ml::NewProgress::attack_relative;
00030 osl::misc::CArray<int, 81*15*10>
00031 osl::progress::ml::NewProgress::defense_relative;
00032 osl::misc::CArray<int, osl::Piece::SIZE>
00033 osl::progress::ml::NewProgress::stand_weight;
00034 osl::misc::CArray<int, 1125>
00035 osl::progress::ml::NewProgress::attack5x5_weight;
00036 osl::misc::CArray<int, 5625>
00037 osl::progress::ml::NewProgress::attack5x5_x_weight;
00038 osl::misc::CArray<int, 10125>
00039 osl::progress::ml::NewProgress::attack5x5_y_weight;
00040 osl::misc::CArray<int, 75>
00041 osl::progress::ml::NewProgress::effectstate_weight;
00042 osl::misc::CArray<int, 4284>
00043 osl::progress::ml::NewProgress::king_relative_weight;
00044 osl::CArray<int, 262144>
00045 osl::progress::ml::NewProgress::attacked_ptype_pair_weight;
00046 osl::CArray<int, 10>
00047 osl::progress::ml::NewProgress::pawn_facing_weight;
00048 osl::CArray<int, 16>
00049 osl::progress::ml::NewProgress::promotion37_weight;
00050 osl::CArray<int, 56>
00051 osl::progress::ml::NewProgress::piecestand7_weight;
00052 int osl::progress::ml::NewProgress::max_progress;
00053 bool osl::progress::ml::NewProgress::initialized_flag;
00054
00055 bool osl::progress::ml::NewProgress::setUp(const char *filename)
00056 {
00057 if (initialized_flag)
00058 return true;
00059
00060 static CArray<int, 25> effect_weight;
00061 static CArray<int, 225> effect_x_weight, effect_y_weight;
00062 static CArray<int, 25> effect_defense_weight;
00063 static CArray<int, 225> effect_per_effect;
00064 static CArray<int, 225> effect_per_effect_defense;
00065 static CArray<int, 2025> effect_per_effect_y, effect_per_effect_x;
00066 std::ifstream is(filename);
00067 int read_count = 0;
00068
00069 osl::eval::ml::Weights weights(25);
00070 for (size_t i = 0; i < weights.dimension(); ++i)
00071 {
00072 int val;
00073 is >> val;
00074 effect_weight[i] = val;
00075 ++read_count;
00076 }
00077 for (size_t i = 0; i < 225; ++i)
00078 {
00079 int val;
00080 is >> val;
00081 effect_x_weight[i] = val;
00082 ++read_count;
00083 }
00084 for (size_t i = 0; i < 225; ++i)
00085 {
00086 int val;
00087 is >> val;
00088 effect_y_weight[i] = val;
00089 ++read_count;
00090 }
00091 weights.resetDimension(25);
00092 for (size_t i = 0; i < weights.dimension(); ++i)
00093 {
00094 int val;
00095 is >> val;
00096 effect_defense_weight[i] = val;
00097 ++read_count;
00098 }
00099 weights.resetDimension(225);
00100 for (size_t i = 0; i < weights.dimension(); ++i)
00101 {
00102 int val;
00103 is >> val;
00104 effect_per_effect[i] = val;
00105 ++read_count;
00106 }
00107 weights.resetDimension(225);
00108 for (size_t i = 0; i < weights.dimension(); ++i)
00109 {
00110 int val;
00111 is >> val;
00112 effect_per_effect_defense[i] = val;
00113 ++read_count;
00114 }
00115
00116 weights.resetDimension(2025);
00117 for (size_t i = 0; i < weights.dimension(); ++i)
00118 {
00119 int val;
00120 is >> val;
00121 effect_per_effect_y[i] = val;
00122 ++read_count;
00123 }
00124 weights.resetDimension(2025);
00125 for (size_t i = 0; i < weights.dimension(); ++i)
00126 {
00127 int val;
00128 is >> val;
00129 effect_per_effect_x[i] = val;
00130 ++read_count;
00131 }
00132 weights.resetDimension(Piece::SIZE);
00133 for (size_t i = 0; i < weights.dimension(); ++i)
00134 {
00135 int val;
00136 is >> val;
00137 stand_weight[i] = val;
00138 ++read_count;
00139 }
00140 weights.resetDimension(1125);
00141 for (size_t i = 0; i < weights.dimension(); ++i)
00142 {
00143 int val;
00144 is >> val;
00145 attack5x5_weight[i] = val;
00146 ++read_count;
00147 }
00148 weights.resetDimension(75);
00149 for (size_t i = 0; i < weights.dimension(); ++i)
00150 {
00151 int val;
00152 is >> val;
00153 effectstate_weight[i] = val;
00154 ++read_count;
00155 }
00156 weights.resetDimension(5625);
00157 for (size_t i = 0; i < weights.dimension(); ++i)
00158 {
00159 int val;
00160 is >> val;
00161 attack5x5_x_weight[i] = val;
00162 ++read_count;
00163 }
00164 weights.resetDimension(10125);
00165 for (size_t i = 0; i < weights.dimension(); ++i)
00166 {
00167 int val;
00168 is >> val;
00169 attack5x5_y_weight[i] = val;
00170 ++read_count;
00171 }
00172 weights.resetDimension(4284);
00173 for (size_t i = 0; i < weights.dimension(); ++i)
00174 {
00175 int val;
00176 is >> val;
00177 king_relative_weight[i] = val;
00178 ++read_count;
00179 }
00180 weights.resetDimension(262144);
00181 for (size_t i = 0; i < weights.dimension(); ++i)
00182 {
00183 int val;
00184 is >> val;
00185 attacked_ptype_pair_weight[i] = val;
00186 ++read_count;
00187 }
00188 weights.resetDimension(10);
00189 for (size_t i = 0; i < weights.dimension(); ++i)
00190 {
00191 int val;
00192 is >> val;
00193 pawn_facing_weight[i] = val;
00194 ++read_count;
00195 }
00196 weights.resetDimension(16);
00197 for (size_t i = 0; i < weights.dimension(); ++i)
00198 {
00199 int val;
00200 is >> val;
00201 promotion37_weight[i] = val;
00202 ++read_count;
00203 }
00204 weights.resetDimension(56);
00205 for (size_t i = 0; i < weights.dimension(); ++i)
00206 {
00207 int val;
00208 is >> val;
00209 piecestand7_weight[i] = val;
00210 ++read_count;
00211 }
00212 {
00213 int val;
00214 is >> val;
00215 max_progress = val;
00216 ++read_count;
00217 #ifdef EVAL_QUAD
00218 while (((max_progress/ProgressScale) % 3) && max_progress > 0)
00219 --max_progress;
00220 #endif
00221 }
00222 for(int king_x=1;king_x<=9;king_x++){
00223 for(int king_y=1;king_y<=9;king_y++){
00224 Square king(king_x,king_y);
00225 int king_index=(king_x-1)*9+king_y-1;
00226 const Square center = Centering5x3::adjustCenter(king);
00227 const int min_x = center.x() - 2;
00228 const int min_y = center.y() - 1;
00229 int i=0;
00230 for (int dx=0; dx<5; ++dx)
00231 {
00232 for (int dy=0; dy<3; ++dy,++i)
00233 {
00234 const Square target(min_x+dx,min_y+dy);
00235 int index0=king_index*15+i;
00236 int index_a=index0*10;
00237 int index_d=index0*10;
00238 attack_relative[index_a]=
00239 effect_weight[index<BLACK>(king, target)] +
00240 effect_x_weight[indexX<BLACK>(king, target)] +
00241 effect_y_weight[indexY<BLACK>(king, target)];
00242 defense_relative[index_d]=
00243 effect_defense_weight[index<BLACK>(king, target)];
00244 for(int count=0;count<=8;count++){
00245 attack_relative[index_a+count+1]=
00246 effect_per_effect[indexPerEffect<BLACK>(king, target, count)] +
00247 effect_per_effect_y[indexPerEffectY<BLACK>(king, target, count)] +
00248 effect_per_effect_x[indexPerEffectX<BLACK>(king, target, count)];
00249 defense_relative[index_d+count+1]=
00250 effect_per_effect_defense[indexPerEffect<BLACK>(king, target, count)];
00251 }
00252 }
00253 }
00254 }
00255 }
00256 for(int king_x=1;king_x<=5;king_x++)
00257 for(int promoted=0;promoted<=4;promoted++)
00258 for(int silver=0;silver<=4;silver++)
00259 for(int gold=0;gold<=4;gold++)
00260 for(int bishop=0;bishop<=2;bishop++)
00261 for(int rook=0;rook<=2;rook++){
00262 int index0=promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
00263 int index1=king_x - 1 + 5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
00264 attack5x5_x_weight[index1]+=attack5x5_weight[index0];
00265 }
00266 for (int i=0; i<PTYPE_SIZE*2*PTYPE_SIZE; ++i)
00267 for (int j=i+1; j<PTYPE_SIZE*2*PTYPE_SIZE; ++j) {
00268 attacked_ptype_pair_weight[eval::ml::NonPawnAttackedPtypePair::index2(j,i)]
00269 = attacked_ptype_pair_weight[eval::ml::NonPawnAttackedPtypePair::index2(i,j)];
00270 }
00271
00272 for (int i=2; i<10; ++i)
00273 pawn_facing_weight[i] += pawn_facing_weight[i-1];
00274
00275 initialized_flag = is;
00276 if (!initialized_flag)
00277 {
00278 std::cerr << "Failed to load NewProgress data " << read_count << std::endl;
00279 }
00280 return initialized_flag;
00281 }
00282
00283 bool osl::progress::ml::NewProgress::setUp()
00284 {
00285 return setUp(defaultFilename().c_str());
00286 }
00287
00288 std::string osl::progress::ml::NewProgress::defaultFilename()
00289 {
00290 std::string filename = OslConfig::home();
00291 filename += "/data/progress.txt";
00292 return filename;
00293 }
00294
00295 template <osl::Player P>
00296 void osl::progress::ml::NewProgress::progressOne(
00297 const NumEffectState &state, int &attack, int &defense)
00298 {
00299 const Square king = state.kingSquare<P>();
00300 const Square center = Centering5x3::adjustCenter(king);
00301 const int min_x = center.x() - 2;
00302 const int min_y = center.y() - 1;
00303
00304 attack = defense = 0;
00305 Square kingRel=king;
00306 if(P==WHITE){
00307 kingRel=kingRel.rotate180();
00308 }
00309 int index0=((kingRel.x()-1)*9+kingRel.y()-1)*15;
00310 int index_a=index0*10 + (P==WHITE ? 10*14 : 0);
00311 for (int dx=0; dx<5; ++dx)
00312 {
00313 for (int dy=0; dy<3; ++dy)
00314 {
00315 const Square target(min_x+dx,min_y+dy);
00316 const int attack_count =
00317 state.countEffect(alt(P), target);
00318 const int defense_count =
00319 state.countEffect(P, target);
00320 attack += attack_count *attack_relative[index_a]+
00321 attack_relative[index_a+std::min(attack_count,8)+1];
00322 defense +=
00323 defense_count * defense_relative[index_a]+
00324 defense_relative[index_a+std::min(defense_count,8)+1];
00325 if(P==BLACK){
00326 index_a+=10;
00327 }
00328 else{
00329 index_a-=10;
00330 }
00331 }
00332 }
00333 }
00334
00335 template <osl::Player P>
00336 void osl::progress::ml::NewProgress::updateAttack5x5PiecesAndState(
00337 const NumEffectState &state)
00338 {
00339 const Square king = state.kingSquare<P>();
00340 const int min_x = std::max(1, king.x() - 2);
00341 const int max_x = std::min(9, king.x() + 2);
00342 const int min_y = std::max(1, king.y() - 2);
00343 const int max_y = std::min(9, king.y() + 2);
00344 effect_progresses[P] = 0;
00345
00346 PieceMask mask;
00347 for (int y = min_y; y <= max_y; ++y)
00348 {
00349 for (int x = min_x; x <= max_x; ++x)
00350 {
00351 const Square target(x, y);
00352 const NumBitmapEffect effect = state.effectSetAt(target);
00353 const int effect_diff =
00354 effect.countEffect(alt(P)) - effect.countEffect(P);
00355 const int x_diff = std::abs(x - king.x());
00356 const int y_diff = (P == WHITE ? king.y() - y : y - king.y());
00357 int index = std::max(std::min(effect_diff, 2), -2) + 2 + 5 * x_diff +
00358 5 * 3 * (y_diff + 2);
00359 effect_progresses[P] += effectstate_weight[index];
00360 mask |= effect;
00361 }
00362 }
00363 updateAttack5x5Pieces<P>(mask, state);
00364 }
00365
00366 template <osl::Player P>
00367 void osl::progress::ml::NewProgress::updateAttack5x5Pieces(
00368 PieceMask mask, const NumEffectState& state)
00369 {
00370 const Player attack = PlayerTraits<P>::opponent;
00371 mask &= state.piecesOnBoard(attack);
00372
00373 rook[attack] = mask.selectBit<ROOK>().countBit();
00374 bishop[attack] = mask.selectBit<BISHOP>().countBit();
00375 gold[attack] = mask.selectBit<GOLD>().countBit();
00376 silver[attack] =
00377 (mask & ~state.promotedPieces()).selectBit<SILVER>().countBit();
00378 PieceMask promoted_pieces = mask & state.promotedPieces();
00379 promoted_pieces.clearBit<ROOK>();
00380 promoted_pieces.clearBit<BISHOP>();
00381 promoted[attack] =
00382 std::min(promoted_pieces.countBit(), 4);
00383 }
00384
00385 template <osl::Player P>
00386 int osl::progress::ml::NewProgress::attack5x5Value(
00387 const NumEffectState &state) const
00388 {
00389 const Player attack = PlayerTraits<P>::opponent;
00390 int king_x = state.kingSquare<P>().x();
00391 if (king_x > 5)
00392 king_x = 10 - king_x;
00393 const int king_y = (P == BLACK ? state.kingSquare<P>().y() :
00394 10 - state.kingSquare<P>().y());
00395 return (attack5x5_x_weight[index5x5x(
00396 rook[attack] + state.countPiecesOnStand<ROOK>(attack),
00397 bishop[attack] + state.countPiecesOnStand<BISHOP>(attack),
00398 gold[attack] + state.countPiecesOnStand<GOLD>(attack),
00399 silver[attack] + state.countPiecesOnStand<SILVER>(attack),
00400 promoted[attack], king_x)] +
00401 attack5x5_y_weight[index5x5y(
00402 rook[attack] + state.countPiecesOnStand<ROOK>(attack),
00403 bishop[attack] + state.countPiecesOnStand<BISHOP>(attack),
00404 gold[attack] + state.countPiecesOnStand<GOLD>(attack),
00405 silver[attack] + state.countPiecesOnStand<SILVER>(attack),
00406 promoted[attack], king_y)]);
00407 }
00408
00409 void
00410 osl::progress::ml::NewProgress::updatePieceKingRelativeBonus(
00411 const NumEffectState &state)
00412 {
00413 const CArray<Square,2> kings = {{
00414 state.kingSquare(BLACK),
00415 state.kingSquare(WHITE),
00416 }};
00417 king_relative_attack.fill(0);
00418 king_relative_defense.fill(0);
00419 for (int i = 0; i < Piece::SIZE; ++i)
00420 {
00421 const Piece piece = state.pieceOf(i);
00422 if (piece.ptype() == osl::KING || !piece.isOnBoard())
00423 continue;
00424 Player pl = piece.owner();
00425 const int index_attack = indexRelative(piece.owner(), kings[alt(pl)],
00426 piece);
00427 const int index_defense = indexRelative(piece.owner(), kings[pl],
00428 piece) + 2142;
00429 king_relative_attack[pl] += king_relative_weight[index_attack];
00430 king_relative_defense[pl] += king_relative_weight[index_defense];
00431 }
00432 }
00433
00434 template <osl::Player Owner>
00435 void osl::progress::ml::NewProgress::
00436 updateNonPawnAttackedPtypePairOne(const NumEffectState& state)
00437 {
00438 PieceMask attacked = state.effectedMask(alt(Owner)) & state.piecesOnBoard(Owner);
00439 attacked.reset(state.kingPiece<Owner>().number());
00440 mask_t ppawn = state.promotedPieces().getMask<PAWN>() & attacked.selectBit<PAWN>();
00441 attacked.clearBit<PAWN>();
00442 attacked.orMask(PtypeFuns<PAWN>::indexNum, ppawn);
00443 PieceVector pieces;
00444 while (attacked.any())
00445 {
00446 const Piece piece = state.pieceOf(attacked.takeOneBit());
00447 pieces.push_back(piece);
00448 }
00449 typedef eval::ml::NonPawnAttackedPtypePair feature_t;
00450 int result = 0;
00451 MultiInt result_eval;
00452 for (size_t i=0; i<pieces.size(); ++i) {
00453 const int i0 = feature_t::index1(state, pieces[i]);
00454 result += attacked_ptype_pair_weight[feature_t::index2(0,i0)];
00455 for (size_t j=i+1; j<pieces.size(); ++j) {
00456 const int i1 = feature_t::index1(state, pieces[j]);
00457 result += attacked_ptype_pair_weight[feature_t::index2(i0,i1)];
00458 if (Owner == BLACK)
00459 result_eval += feature_t::table[feature_t::index2(i0, i1)];
00460 else
00461 result_eval -= feature_t::table[feature_t::index2(i0, i1)];
00462 }
00463 }
00464 non_pawn_ptype_attacked_pair[Owner] = result;
00465 non_pawn_ptype_attacked_pair_eval[Owner] = result_eval;
00466 }
00467
00468 void osl::progress::ml::NewProgress::
00469 updateNonPawnAttackedPtypePair(const NumEffectState& state)
00470 {
00471 updateNonPawnAttackedPtypePairOne<BLACK>(state);
00472 updateNonPawnAttackedPtypePairOne<WHITE>(state);
00473 }
00474
00475 void osl::progress::ml::NewProgress::
00476 updatePawnFacing(const NumEffectState& state)
00477 {
00478 PieceMask attacked = state.effectedMask(WHITE) & state.piecesOnBoard(BLACK);
00479 mask_t pawn = attacked.selectBit<PAWN>()
00480 & ~(state.promotedPieces().getMask<PAWN>());
00481 int count = 0;
00482 while (pawn.any()) {
00483 const Piece p(state.pieceOf(pawn.takeOneBit()+PtypeFuns<PAWN>::indexNum*32));
00484 if (state.hasEffectByPtypeStrict<PAWN>(WHITE, p.square()))
00485 ++count;
00486 }
00487 pawn_facing = pawn_facing_weight[count];
00488 }
00489
00490 template <osl::Player P>
00491 void osl::progress::ml::
00492 NewProgress::promotion37One(const NumEffectState& state, int rank)
00493 {
00494 typedef eval::ml::Promotion37 feature_t;
00495 CArray<int,PTYPE_SIZE> count = {{ 0 }};
00496 for (int x=1; x<=9; ++x) {
00497 const Square target(x, rank);
00498 if (! state[target].isEmpty())
00499 continue;
00500 int a = state.countEffect(P, target);
00501 const int d = state.countEffect(alt(P), target);
00502 if (a > 0 && a == d)
00503 a += AdditionalEffect::hasEffect(state, target, P);
00504 if (a < d)
00505 continue;
00506 const Ptype ptype = state.findCheapAttack(P, target).ptype();
00507 if (isPiece(ptype) && ! isPromoted(ptype))
00508 count[ptype]++;
00509 }
00510 for (int p=PTYPE_BASIC_MIN; p<=PTYPE_MAX; ++p) {
00511 if (count[p] > 0) {
00512 promotion37 += promotion37_weight[p];
00513 promotion37_eval += feature_t::table[p]*playerToMul(P);
00514 }
00515 if (count[p] > 1) {
00516 promotion37 += promotion37_weight[p-8]*(count[p]-1);
00517 promotion37_eval += feature_t::table[p-8]*(playerToMul(P)*(count[p]-1));
00518 }
00519 }
00520 }
00521
00522 void osl::progress::ml::NewProgress::
00523 updatePromotion37(const NumEffectState& state)
00524 {
00525 promotion37 = 0;
00526 promotion37_eval = MultiInt();
00527 promotion37One<BLACK>(state, 3);
00528 promotion37One<WHITE>(state, 7);
00529 }
00530
00531 void osl::progress::ml::NewProgress::
00532 updatePieceStand7(const NumEffectState& state)
00533 {
00534 piecestand7 = 0;
00535 for (int z=0; z<2; ++z) {
00536 CArray<int,7> stand = {{ 0 }};
00537 int filled = 0;
00538 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00539 if (state.hasPieceOnStand(indexToPlayer(z), ptype))
00540 stand[filled++] = ptype-PTYPE_BASIC_MIN;
00541 for (int i=0; i<std::min(7,filled+1); ++i)
00542 piecestand7 += piecestand7_weight[stand[i] + 8*i];
00543 }
00544 }
00545
00546 osl::progress::ml::NewProgress::NewProgress(
00547 const NumEffectState &state)
00548 {
00549 assert(initialized_flag);
00550
00551 progressOne<BLACK>(state,
00552 progresses[BLACK],
00553 defenses[WHITE]);
00554 progressOne<WHITE>(state,
00555 progresses[WHITE],
00556 defenses[BLACK]);
00557 updateAttack5x5PiecesAndState<BLACK>(state);
00558 updateAttack5x5PiecesAndState<WHITE>(state);
00559 attack5x5_progresses[BLACK] =
00560 attack5x5Value<BLACK>(state);
00561 attack5x5_progresses[WHITE] =
00562 attack5x5Value<WHITE>(state);
00563 stand_progresses.fill(0);
00564 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00565 {
00566 const int black_count =
00567 state.countPiecesOnStand(BLACK, ptype);
00568 const int white_count =
00569 state.countPiecesOnStand(WHITE, ptype);
00570 for (int j = 0; j < black_count; ++j)
00571 {
00572 stand_progresses[WHITE] +=
00573 stand_weight[Ptype_Table.getIndexMin(ptype) + j];
00574 }
00575 for (int j = 0; j < white_count; ++j)
00576 {
00577 stand_progresses[BLACK] +=
00578 stand_weight[Ptype_Table.getIndexMin(ptype) + j];
00579 }
00580 }
00581 updatePieceKingRelativeBonus(state);
00582 updateNonPawnAttackedPtypePair(state);
00583 updatePawnFacing(state);
00584 updatePromotion37(state);
00585 updatePieceStand7(state);
00586 }
00587
00588 template<osl::Player P>
00589 inline
00590 void osl::progress::ml::NewProgress::updateMain(
00591 const NumEffectState &new_state,
00592 Move last_move)
00593 {
00594 const Player altP=PlayerTraits<P>::opponent;
00595 assert(new_state.turn()==altP);
00596 assert(last_move.player()==P);
00597 const Square kb = new_state.kingSquare<BLACK>(), kw = new_state.kingSquare<WHITE>();
00598 const BoardMask mb = new_state.changedEffects(BLACK), mw = new_state.changedEffects(WHITE);
00599 const bool king_move = last_move.ptype() == KING;
00600 if ((king_move && altP == BLACK) || mb.anyInRange(Board_Mask_Table5x3_Center.mask(kw)) || mw.anyInRange(Board_Mask_Table5x3_Center.mask(kw)))
00601 {
00602 progressOne<WHITE>(new_state,progresses[WHITE],defenses[BLACK]);
00603 }
00604 if ((king_move && altP == WHITE) || mw.anyInRange(Board_Mask_Table5x3_Center.mask(kb)) || mb.anyInRange(Board_Mask_Table5x3_Center.mask(kb)))
00605 {
00606 progressOne<BLACK>(new_state,progresses[BLACK],defenses[WHITE]);
00607 }
00608
00609 const Ptype captured = last_move.capturePtype();
00610
00611 if (last_move.isDrop())
00612 {
00613 const int count =
00614 new_state.countPiecesOnStand(P, last_move.ptype()) + 1;
00615 const int value =
00616 stand_weight[Ptype_Table.getIndexMin(last_move.ptype()) + count - 1];
00617 stand_progresses[altP] -= value;
00618 }
00619 else if (captured != PTYPE_EMPTY)
00620 {
00621 Ptype ptype = unpromote(captured);
00622 const int count = new_state.countPiecesOnStand(P, ptype);
00623 const int value =
00624 stand_weight[(Ptype_Table.getIndexMin(ptype) + count - 1)];
00625 stand_progresses[altP] += value;
00626 }
00627
00628 if (king_move)
00629 {
00630 updatePieceKingRelativeBonus(new_state);
00631 }
00632 else
00633 {
00634 const CArray<Square,2> kings = {{
00635 new_state.kingSquare(BLACK),
00636 new_state.kingSquare(WHITE),
00637 }};
00638 if (!last_move.isDrop())
00639 {
00640 const int index_attack =
00641 indexRelative<P>(kings[altP],
00642 last_move.oldPtype(), last_move.from());
00643 const int index_defense =
00644 indexRelative<P>(kings[P],
00645 last_move.oldPtype(), last_move.from()) + 2142;
00646 king_relative_attack[P] -=
00647 king_relative_weight[index_attack];
00648 king_relative_defense[P] -=
00649 king_relative_weight[index_defense];
00650 }
00651 {
00652 const int index_attack =
00653 indexRelative<P>(kings[altP],
00654 last_move.ptype(), last_move.to());
00655 const int index_defense =
00656 indexRelative<P>(kings[P],
00657 last_move.ptype(), last_move.to()) + 2142;
00658 king_relative_attack[P] +=
00659 king_relative_weight[index_attack];
00660 king_relative_defense[P] +=
00661 king_relative_weight[index_defense];
00662 }
00663 if (captured != PTYPE_EMPTY)
00664 {
00665 const int index_attack =
00666 indexRelative<altP>(kings[P],
00667 captured, last_move.to());
00668 const int index_defense =
00669 indexRelative<altP>(kings[altP],
00670 captured, last_move.to()) + 2142;
00671 king_relative_attack[altP] -=
00672 king_relative_weight[index_attack];
00673 king_relative_defense[altP] -=
00674 king_relative_weight[index_defense];
00675 }
00676 }
00677 updateNonPawnAttackedPtypePair(new_state);
00678 updatePawnFacing(new_state);
00679 updatePromotion37(new_state);
00680 updatePieceStand7(new_state);
00681 }
00682
00683 template<osl::Player P>
00684 void osl::progress::ml::NewProgress::updateSub(
00685 const NumEffectState &new_state,
00686 Move last_move)
00687 {
00688 const Player altP=PlayerTraits<P>::opponent;
00689 assert(new_state.turn()==altP);
00690 if (last_move.isPass())
00691 return;
00692 const Square kb = new_state.kingSquare<BLACK>(), kw = new_state.kingSquare<WHITE>();
00693 const BoardMask mb = new_state.changedEffects(BLACK), mw = new_state.changedEffects(WHITE);
00694 const bool king_move = last_move.ptype() == KING;
00695 const Ptype captured = last_move.capturePtype();
00696
00697 if ((king_move && altP == BLACK) ||
00698 mb.anyInRange(Board_Mask_Table5x5.mask(kw)) ||
00699 mw.anyInRange(Board_Mask_Table5x5.mask(kw)))
00700 {
00701 updateAttack5x5PiecesAndState<WHITE>(new_state);
00702 attack5x5_progresses[WHITE] =
00703 attack5x5Value<WHITE>(new_state);
00704 }
00705 else if (altP == WHITE &&(last_move.isDrop() || captured != PTYPE_EMPTY))
00706 {
00707 attack5x5_progresses[WHITE] =
00708 attack5x5Value<WHITE>(new_state);
00709 }
00710 if ((king_move && altP == WHITE) ||
00711 mw.anyInRange(Board_Mask_Table5x5.mask(kb)) ||
00712 mb.anyInRange(Board_Mask_Table5x5.mask(kb)))
00713 {
00714 updateAttack5x5PiecesAndState<BLACK>(new_state);
00715 attack5x5_progresses[BLACK] =
00716 attack5x5Value<BLACK>(new_state);
00717 }
00718 else if (altP == BLACK && (last_move.isDrop() || captured != PTYPE_EMPTY))
00719 {
00720 attack5x5_progresses[BLACK] =
00721 attack5x5Value<BLACK>(new_state);
00722 }
00723 updateMain<P>(new_state, last_move);
00724 }
00725
00726 osl::progress::ml::NewProgressDebugInfo osl::progress::ml::NewProgress::debugInfo() const
00727 {
00728 NewProgressDebugInfo info;
00729
00730 info.black_values[NewProgressDebugInfo::ATTACK_5X3] = progresses[0];
00731 info.black_values[NewProgressDebugInfo::DEFENSE_5X3] = defenses[0];
00732 info.black_values[NewProgressDebugInfo::ATTACK5X5] = attack5x5_progresses[0];
00733 info.black_values[NewProgressDebugInfo::STAND] = stand_progresses[0];
00734 info.black_values[NewProgressDebugInfo::EFFECT5X5] = effect_progresses[0];
00735 info.black_values[NewProgressDebugInfo::KING_RELATIVE_ATTACK] = king_relative_attack[0];
00736 info.black_values[NewProgressDebugInfo::KING_RELATIVE_DEFENSE] = king_relative_defense[0];
00737 info.black_values[NewProgressDebugInfo::NON_PAWN_ATTACKED_PAIR] = non_pawn_ptype_attacked_pair[0];
00738
00739 info.white_values[NewProgressDebugInfo::ATTACK_5X3] = progresses[1];
00740 info.white_values[NewProgressDebugInfo::DEFENSE_5X3] = defenses[1];
00741 info.white_values[NewProgressDebugInfo::ATTACK5X5] = attack5x5_progresses[1];
00742 info.white_values[NewProgressDebugInfo::STAND] = stand_progresses[1];
00743 info.white_values[NewProgressDebugInfo::EFFECT5X5] = effect_progresses[1];
00744 info.white_values[NewProgressDebugInfo::KING_RELATIVE_ATTACK] = king_relative_attack[1];
00745 info.white_values[NewProgressDebugInfo::KING_RELATIVE_DEFENSE] = king_relative_defense[1];
00746 info.white_values[NewProgressDebugInfo::NON_PAWN_ATTACKED_PAIR] = non_pawn_ptype_attacked_pair[1];
00747
00748 return info;
00749 }
00750
00751 namespace osl
00752 {
00753 namespace progress
00754 {
00755 namespace ml
00756 {
00757 template void osl::progress::ml::NewProgress::updateSub<osl::BLACK>(const NumEffectState &new_state,Move last_move);
00758 template void osl::progress::ml::NewProgress::updateSub<osl::WHITE>(const NumEffectState &new_state,Move last_move);
00759 }
00760 }
00761 }
00762
00763
00764
00765
00766