Improve mobility performance (WIP)

This commit is contained in:
Calcitem 2021-06-14 17:36:16 +08:00
parent a18f96e1d5
commit c0f92c2dcd
3 changed files with 74 additions and 2 deletions

View File

@ -59,10 +59,12 @@ Value Evaluation::value()
case Phase::placing:
#ifdef EVALUATE_MOBILITY
if (gameOptions.getConsiderMobility()) {
value += (Value)pos.calculate_mobility_diff();
//value += (Value)pos.calculate_mobility_diff();
value += pos.get_mobility_diff();
} else if (gameOptions.getDrawOnHumanExperience()) {
if (pos.piece_on_board_count(WHITE) + pos.piece_on_board_count(BLACK) <= 5) {
value += (Value)pos.calculate_mobility_diff();
//value += (Value)pos.calculate_mobility_diff();
value += pos.get_mobility_diff();
}
//break;
}

View File

@ -561,6 +561,8 @@ bool Position::reset()
pieceInHandCount[WHITE] = pieceInHandCount[BLACK] = rule.piecesCount;
pieceToRemoveCount = 0;
mobilityDiff = 0;
MoveList<LEGAL>::create();
create_mill_table();
currentSquare = SQ_0;
@ -633,6 +635,8 @@ bool Position::put_piece(Square s, bool updateRecord)
update_key(s);
updateMobility(MOVETYPE_PLACE, s);
if (updateRecord) {
snprintf(record, RECORD_LEN_MAX, "(%1u,%1u)", file_of(s), rank_of(s));
}
@ -719,8 +723,12 @@ bool Position::put_piece(Square s, bool updateRecord)
update_key(s);
revert_key(currentSquare);
updateMobility(MOVETYPE_PLACE, s);
board[currentSquare] = NO_PIECE;
updateMobility(MOVETYPE_REMOVE, currentSquare);
CLEAR_BIT(byTypeBB[ALL_PIECES], currentSquare);
CLEAR_BIT(byTypeBB[type_of(pc)], currentSquare);
CLEAR_BIT(byColorBB[color_of(pc)], currentSquare);
@ -730,6 +738,9 @@ bool Position::put_piece(Square s, bool updateRecord)
SET_BIT(byColorBB[color_of(pc)], s);
currentSquare = s;
const int n = mills_count(currentSquare);
if (n == 0
@ -781,6 +792,8 @@ bool Position::remove_piece(Square s, bool updateRecord)
revert_key(s);
updateMobility(MOVETYPE_REMOVE, s);
if (rule.hasBannedLocations && phase == Phase::placing) {
// Remove and put ban
Piece pc = board[s];
@ -1341,6 +1354,53 @@ void Position::reset_bb()
}
}
void Position::updateMobility(MoveType mt, Square s)
{
if (mt == MOVETYPE_PLACE) {
for (MoveDirection d = MD_BEGIN; d < MD_NB; ++d) {
auto adjacentSquare = static_cast<Square>(MoveList<LEGAL>::adjacentSquares[s][d]);
if (!adjacentSquare) {
break;;
}
if (board[adjacentSquare] & W_STONE) {
mobilityDiff--;
} else if (board[adjacentSquare] & B_STONE) {
mobilityDiff++;
} else {
if (side_to_move() == WHITE) {
mobilityDiff++;
} else {
mobilityDiff--;
}
}
}
} else if (mt == MOVETYPE_REMOVE) {
for (MoveDirection d = MD_BEGIN; d < MD_NB; ++d) {
auto adjacentSquare = static_cast<Square>(MoveList<LEGAL>::adjacentSquares[s][d]);
if (!adjacentSquare) {
break;
}
if (board[adjacentSquare] & W_STONE) {
mobilityDiff++;
} else if (board[adjacentSquare] & B_STONE) {
mobilityDiff--;
} else {
if (side_to_move() == WHITE) {
mobilityDiff++;
} else {
mobilityDiff--;
}
}
}
} else {
assert(0);
}
}
void Position::mirror(vector <string> &moveHistory, bool cmdChange /*= true*/)
{
Piece ch;

View File

@ -145,6 +145,10 @@ public:
int piece_to_remove_count() const;
int get_mobility(Color c) const;
int get_mobility_diff() const;
void updateMobility(MoveType mt, Square s);
//template <typename Mt> void updateMobility(Square from, Square to);
int calculate_mobility_diff();
static bool is_star_square(Square s);
@ -172,6 +176,7 @@ public:
int pieceInHandCount[COLOR_NB] { 0, 9, 9 };
int pieceOnBoardCount[COLOR_NB] { 0, 0, 0 };
int pieceToRemoveCount{ 0 };
int mobilityDiff { 0 };
int gamePly { 0 };
Color sideToMove { NOCOLOR };
Thread *thisThread {nullptr};
@ -344,4 +349,9 @@ inline int Position::piece_to_remove_count() const
return pieceToRemoveCount;
}
inline int Position::get_mobility_diff() const
{
return mobilityDiff;
}
#endif // #ifndef POSITION_H_INCLUDED