00001 #include "osl/progress.h"
00002 #include "osl/eval/weights.h"
00003 #include "osl/eval/midgame.h"
00004 #include "osl/eval/minorPiece.h"
00005 #include "osl/additionalEffect.h"
00006 #include "osl/bits/centering5x3.h"
00007 #include "osl/oslConfig.h"
00008 #include <iostream>
00009 #include <fstream>
00010
00011 bool osl::progress::ml::
00012 operator==(const NewProgressData& l, const NewProgressData& r)
00013 {
00014 return l.progresses == r.progresses
00015 && l.attack5x5_progresses == r.attack5x5_progresses
00016 && l.stand_progresses == r.stand_progresses
00017 && l.effect_progresses == r.effect_progresses
00018 && l.defenses == r.defenses
00019 && l.rook == r.rook && l.bishop == r.bishop && l.gold == r.gold
00020 && l.silver == r.silver && l.promoted == r.promoted
00021 && l.king_relative_attack == r.king_relative_attack
00022 && l.king_relative_defense == r.king_relative_defense
00023 && l.non_pawn_ptype_attacked_pair == r.non_pawn_ptype_attacked_pair
00024 && l.non_pawn_ptype_attacked_pair_eval == r.non_pawn_ptype_attacked_pair_eval;
00025 }
00026
00027 osl::CArray<int, 81*15*10>
00028 osl::progress::ml::NewProgress::attack_relative;
00029 osl::CArray<int, 81*15*10>
00030 osl::progress::ml::NewProgress::defense_relative;
00031 osl::CArray<int, osl::Piece::SIZE>
00032 osl::progress::ml::NewProgress::stand_weight;
00033 osl::CArray<int, 1125>
00034 osl::progress::ml::NewProgress::attack5x5_weight;
00035 osl::CArray<int, 5625>
00036 osl::progress::ml::NewProgress::attack5x5_x_weight;
00037 osl::CArray<int, 10125>
00038 osl::progress::ml::NewProgress::attack5x5_y_weight;
00039 osl::CArray<int, 75>
00040 osl::progress::ml::NewProgress::effectstate_weight;
00041 osl::CArray<int, 4284>
00042 osl::progress::ml::NewProgress::king_relative_weight;
00043 osl::CArray<int, 262144>
00044 osl::progress::ml::NewProgress::attacked_ptype_pair_weight;
00045 osl::CArray<int, 10>
00046 osl::progress::ml::NewProgress::pawn_facing_weight;
00047 osl::CArray<int, 16>
00048 osl::progress::ml::NewProgress::promotion37_weight;
00049 osl::CArray<int, 56>
00050 osl::progress::ml::NewProgress::piecestand7_weight;
00051 int osl::progress::ml::NewProgress::max_progress;
00052 bool osl::progress::ml::NewProgress::initialized_flag;
00053
00054 bool osl::progress::ml::NewProgress::setUp(const char *filename)
00055 {
00056 if (initialized_flag)
00057 return true;
00058
00059 static CArray<int, 25> effect_weight;
00060 static CArray<int, 225> effect_x_weight, effect_y_weight;
00061 static CArray<int, 25> effect_defense_weight;
00062 static CArray<int, 225> effect_per_effect;
00063 static CArray<int, 225> effect_per_effect_defense;
00064 static CArray<int, 2025> effect_per_effect_y, effect_per_effect_x;
00065 std::ifstream is(filename);
00066 int read_count = 0;
00067
00068 osl::eval::ml::Weights weights(25);
00069 for (size_t i = 0; i < weights.dimension(); ++i)
00070 {
00071 int val;
00072 is >> val;
00073 effect_weight[i] = val;
00074 ++read_count;
00075 }
00076 for (size_t i = 0; i < 225; ++i)
00077 {
00078 int val;
00079 is >> val;
00080 effect_x_weight[i] = val;
00081 ++read_count;
00082 }
00083 for (size_t i = 0; i < 225; ++i)
00084 {
00085 int val;
00086 is >> val;
00087 effect_y_weight[i] = val;
00088 ++read_count;
00089 }
00090 weights.resetDimension(25);
00091 for (size_t i = 0; i < weights.dimension(); ++i)
00092 {
00093 int val;
00094 is >> val;
00095 effect_defense_weight[i] = val;
00096 ++read_count;
00097 }
00098 weights.resetDimension(225);
00099 for (size_t i = 0; i < weights.dimension(); ++i)
00100 {
00101 int val;
00102 is >> val;
00103 effect_per_effect[i] = val;
00104 ++read_count;
00105 }
00106 weights.resetDimension(225);
00107 for (size_t i = 0; i < weights.dimension(); ++i)
00108 {
00109 int val;
00110 is >> val;
00111 effect_per_effect_defense[i] = val;
00112 ++read_count;
00113 }
00114
00115 weights.resetDimension(2025);
00116 for (size_t i = 0; i < weights.dimension(); ++i)
00117 {
00118 int val;
00119 is >> val;
00120 effect_per_effect_y[i] = val;
00121 ++read_count;
00122 }
00123 weights.resetDimension(2025);
00124 for (size_t i = 0; i < weights.dimension(); ++i)
00125 {
00126 int val;
00127 is >> val;
00128 effect_per_effect_x[i] = val;
00129 ++read_count;
00130 }
00131 weights.resetDimension(Piece::SIZE);
00132 for (size_t i = 0; i < weights.dimension(); ++i)
00133 {
00134 int val;
00135 is >> val;
00136 stand_weight[i] = val;
00137 ++read_count;
00138 }
00139 weights.resetDimension(1125);
00140 for (size_t i = 0; i < weights.dimension(); ++i)
00141 {
00142 int val;
00143 is >> val;
00144 attack5x5_weight[i] = val;
00145 ++read_count;
00146 }
00147 weights.resetDimension(75);
00148 for (size_t i = 0; i < weights.dimension(); ++i)
00149 {
00150 int val;
00151 is >> val;
00152 effectstate_weight[i] = val;
00153 ++read_count;
00154 }
00155 weights.resetDimension(5625);
00156 for (size_t i = 0; i < weights.dimension(); ++i)
00157 {
00158 int val;
00159 is >> val;
00160 attack5x5_x_weight[i] = val;
00161 ++read_count;
00162 }
00163 weights.resetDimension(10125);
00164 for (size_t i = 0; i < weights.dimension(); ++i)
00165 {
00166 int val;
00167 is >> val;
00168 attack5x5_y_weight[i] = val;
00169 ++read_count;
00170 }
00171 weights.resetDimension(4284);
00172 for (size_t i = 0; i < weights.dimension(); ++i)
00173 {
00174 int val;
00175 is >> val;
00176 king_relative_weight[i] = val;
00177 ++read_count;
00178 }
00179 weights.resetDimension(262144);
00180 for (size_t i = 0; i < weights.dimension(); ++i)
00181 {
00182 int val;
00183 is >> val;
00184 attacked_ptype_pair_weight[i] = val;
00185 ++read_count;
00186 }
00187 weights.resetDimension(10);
00188 for (size_t i = 0; i < weights.dimension(); ++i)
00189 {
00190 int val;
00191 is >> val;
00192 pawn_facing_weight[i] = val;
00193 ++read_count;
00194 }
00195 weights.resetDimension(16);
00196 for (size_t i = 0; i < weights.dimension(); ++i)
00197 {
00198 int val;
00199 is >> val;
00200 promotion37_weight[i] = val;
00201 ++read_count;
00202 }
00203 weights.resetDimension(56);
00204 for (size_t i = 0; i < weights.dimension(); ++i)
00205 {
00206 int val;
00207 is >> val;
00208 piecestand7_weight[i] = val;
00209 ++read_count;
00210 }
00211 {
00212 int val;
00213 is >> val;
00214 max_progress = val;
00215 ++read_count;
00216 #ifdef EVAL_QUAD
00217 while (((max_progress/ProgressScale) % 3) && max_progress > 0)
00218 --max_progress;
00219 #endif
00220 }
00221 for(int king_x=1;king_x<=9;king_x++){
00222 for(int king_y=1;king_y<=9;king_y++){
00223 Square king(king_x,king_y);
00224 int king_index=(king_x-1)*9+king_y-1;
00225 const Square center = Centering5x3::adjustCenter(king);
00226 const int min_x = center.x() - 2;
00227 const int min_y = center.y() - 1;
00228 int i=0;
00229 for (int dx=0; dx<5; ++dx)
00230 {
00231 for (int dy=0; dy<3; ++dy,++i)
00232 {
00233 const Square target(min_x+dx,min_y+dy);
00234 int index0=king_index*15+i;
00235 int index_a=index0*10;
00236 int index_d=index0*10;
00237 attack_relative[index_a]=
00238 effect_weight[index<BLACK>(king, target)] +
00239 effect_x_weight[indexX<BLACK>(king, target)] +
00240 effect_y_weight[indexY<BLACK>(king, target)];
00241 defense_relative[index_d]=
00242 effect_defense_weight[index<BLACK>(king, target)];
00243 for(int count=0;count<=8;count++){
00244 attack_relative[index_a+count+1]=
00245 effect_per_effect[indexPerEffect<BLACK>(king, target, count)] +
00246 effect_per_effect_y[indexPerEffectY<BLACK>(king, target, count)] +
00247 effect_per_effect_x[indexPerEffectX<BLACK>(king, target, count)];
00248 defense_relative[index_d+count+1]=
00249 effect_per_effect_defense[indexPerEffect<BLACK>(king, target, count)];
00250 }
00251 }
00252 }
00253 }
00254 }
00255 for(int king_x=1;king_x<=5;king_x++)
00256 for(int promoted=0;promoted<=4;promoted++)
00257 for(int silver=0;silver<=4;silver++)
00258 for(int gold=0;gold<=4;gold++)
00259 for(int bishop=0;bishop<=2;bishop++)
00260 for(int rook=0;rook<=2;rook++){
00261 int index0=promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
00262 int index1=king_x - 1 + 5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
00263 attack5x5_x_weight[index1]+=attack5x5_weight[index0];
00264 }
00265 for (int i=0; i<PTYPE_SIZE*2*PTYPE_SIZE; ++i)
00266 for (int j=i+1; j<PTYPE_SIZE*2*PTYPE_SIZE; ++j) {
00267 attacked_ptype_pair_weight[eval::ml::NonPawnAttackedPtypePair::index2(j,i)]
00268 = attacked_ptype_pair_weight[eval::ml::NonPawnAttackedPtypePair::index2(i,j)];
00269 }
00270
00271 for (int i=2; i<10; ++i)
00272 pawn_facing_weight[i] += pawn_facing_weight[i-1];
00273
00274 initialized_flag = static_cast<bool>(is);
00275 if (!initialized_flag)
00276 {
00277 std::cerr << "Failed to load NewProgress data " << read_count
00278 << " from file " << filename << 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 = alt(P);
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 = alt(P);
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]*sign(P);
00514 }
00515 if (count[p] > 1) {
00516 promotion37 += promotion37_weight[p-8]*(count[p]-1);
00517 promotion37_eval += feature_t::table[p-8]*(sign(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 for (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 for (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=alt(P);
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=alt(P);
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