Skip to content

Commit

Permalink
Late move pruning, more aggressive reductions
Browse files Browse the repository at this point in the history
  • Loading branch information
Plan B committed Nov 4, 2018
1 parent b24de0a commit 3881d25
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 85 deletions.
24 changes: 13 additions & 11 deletions src/attacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ void initAttacks() {
}
}

uint64_t getBishopAttacks(const Board& b, int color, int sqr) {
uint64_t getBishopAttacks(const Board& b, const uint64_t& valid, int color, int sqr) {
uint64_t finalAttacks = 0ull;
for (int i = 0; i < 4; ++i) {
const uint64_t attacks = bishopAttacks[sqr][i];
const uint64_t blockers = ~b.colors[NO_COLOR] & attacks;
finalAttacks |= ((!blockers) ? attacks : (attacks ^ bishopAttacks[generalBitscan(blockers, bishopDirs[i])][i]) & ~b.colors[color]);
finalAttacks |= ((!blockers) ? attacks : (attacks ^ bishopAttacks[generalBitscan(blockers, bishopDirs[i])][i]) & valid);
}
return finalAttacks;
}
Expand All @@ -122,12 +122,12 @@ uint64_t getBishopAttacks(const uint64_t& occupied, int sqr) {
return finalAttacks;
}

uint64_t getRookAttacks(const Board& b, int color, int sqr) {
uint64_t getRookAttacks(const Board& b, const uint64_t& valid, int color, int sqr) {
uint64_t finalAttacks = 0ull;
for (int i = 0; i < 4; ++i) {
const uint64_t attacks = rookAttacks[sqr][i];
const uint64_t blockers = ~b.colors[NO_COLOR] & attacks;
finalAttacks |= ((!blockers) ? attacks : (attacks ^ rookAttacks[generalBitscan(blockers, rookDirs[i])][i]) & ~b.colors[color]);
finalAttacks |= ((!blockers) ? attacks : (attacks ^ rookAttacks[generalBitscan(blockers, rookDirs[i])][i]) & valid);
}
return finalAttacks;
}
Expand All @@ -142,30 +142,32 @@ uint64_t getRookAttacks(const uint64_t& occupied, int sqr) {
return finalAttacks;
}

uint64_t getQueenAttacks(const Board& b, int color, int sqr) {
uint64_t getQueenAttacks(const Board& b, const uint64_t& valid, int color, int sqr) {
uint64_t finalAttacks = 0ull;
for (int i = 0; i < 8; ++i) {
const uint64_t attacks = queenAttacks[sqr][i];
const uint64_t blockers = ~b.colors[NO_COLOR] & attacks;
finalAttacks |= ((!blockers) ? attacks : (attacks ^ queenAttacks[generalBitscan(blockers, queenDirs[i])][i]) & ~b.colors[color]);
finalAttacks |= ((!blockers) ? attacks : (attacks ^ queenAttacks[generalBitscan(blockers, queenDirs[i])][i]) & valid);
}
return finalAttacks;
}

bool squareIsAttacked(const Board& b, int color, int sqr) {
uint64_t enemy = b.colors[!color];
const uint64_t enemy = b.colors[!color];
const uint64_t valid = ~b.colors[color];
return (pawnAttacks[sqr][color] & enemy & b.pieces[PAWN])
|| (knightAttacks[sqr] & enemy & b.pieces[KNIGHT])
|| (getBishopAttacks(b, color, sqr) & enemy & (b.pieces[BISHOP] | b.pieces[QUEEN]))
|| (getRookAttacks(b, color, sqr) & enemy & (b.pieces[ROOK] | b.pieces[QUEEN]))
|| (getBishopAttacks(b, valid, color, sqr) & enemy & (b.pieces[BISHOP] | b.pieces[QUEEN]))
|| (getRookAttacks(b, valid, color, sqr) & enemy & (b.pieces[ROOK] | b.pieces[QUEEN]))
|| (kingAttacks[sqr] & enemy & b.pieces[KING]);
}

uint64_t squareAttackers(const Board& b, int color, int sqr) {
const uint64_t valid = ~b.colors[color];
return ((pawnAttacks[sqr][color] & b.pieces[PAWN])
| (knightAttacks[sqr] & b.pieces[KNIGHT])
| (getBishopAttacks(b, color, sqr) & (b.pieces[BISHOP] | b.pieces[QUEEN]))
| (getRookAttacks(b, color, sqr) & (b.pieces[ROOK] | b.pieces[QUEEN]))
| (getBishopAttacks(b, valid, color, sqr) & (b.pieces[BISHOP] | b.pieces[QUEEN]))
| (getRookAttacks(b, valid, color, sqr) & (b.pieces[ROOK] | b.pieces[QUEEN]))
| (kingAttacks[sqr] & b.pieces[KING])) & b.colors[!color];
}

Expand Down
6 changes: 3 additions & 3 deletions src/attacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ void initKingAttacks(const uint64_t& bb, int sqr);

void initAttacks();

uint64_t getBishopAttacks(const Board& b, int color, int sqr);
uint64_t getBishopAttacks(const Board& b, const uint64_t& valid, int color, int sqr);
uint64_t getBishopAttacks(const uint64_t& occupied, int sqr);
uint64_t getRookAttacks(const Board& b, int color, int sqr);
uint64_t getRookAttacks(const Board& b, const uint64_t& valid, int color, int sqr);
uint64_t getRookAttacks(const uint64_t& occupied, int sqr);
uint64_t getQueenAttacks(const Board& b, int color, int sqr);
uint64_t getQueenAttacks(const Board& b, const uint64_t& valid, int color, int sqr);

bool squareIsAttacked(const Board& b, int color, int sqr);
uint64_t squareAttackers(const Board& b, int color, int sqr);
Expand Down
2 changes: 0 additions & 2 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ void setSquare(Board& b, int piece, int sqr) {
bool drawnByRepetition(const Board& b) {
int count = 0;
for (int i = b.moveNum - 2; i >= 0; i -= 2) {
// Draw before a pawn move / capture is impossible
if (i < b.moveNum - b.fiftyMove) break;
if (b.history[i] == b.key && (++count == 2)) return true;
}
return false;
Expand Down
23 changes: 14 additions & 9 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ int evaluatePawns(const Board& b, uint64_t pawns, const uint64_t& enemyPawns, in
// Phalanx pawns
eval += (countBits(pawns & (pawns >> 1) & ~fileHMask) + countBits(pawns & (pawns << 1) & ~fileAMask)) * phalanxPawnBonus;

// Doubled pawns
for (int i = 0; i < 8; ++i) {
if (countBits(pawns & fileMasks[i]) > 1) eval -= doubledPawnPenalty;
}

while (pawns) {
int sqr = popBit(pawns);
// Passed pawn
int passedRank = passed(b, sqr, enemyPawns, color);
int passedScore = taperedScore(passedBonus[passedRank][MG], passedBonus[passedRank][EG], phase);
int forward = (color == WHITE) ? 8 : -8;
// Reduce bonus if passed pawn is blockaded
eval += (b.squares[sqr + forward] == EMPTY) ? passedScore : passedScore * passedBlockReduction;
eval += (b.squares[sqr + forward] == EMPTY) ? passedScore : passedScore / passedBlockReduction;
}
return eval;
}
Expand All @@ -110,8 +115,9 @@ int evaluateKnights(const Board& b, uint64_t pieces, const uint64_t& safeSquares
int evaluateBishops(const Board& b, uint64_t pieces, const uint64_t& safeSquares, const uint64_t& enemyKingRing, int color) {
int eval = 0;
int count = 0;
const uint64_t valid = ~b.colors[b.turn];
while (pieces) {
uint64_t attacks = getBishopAttacks(b, color, popBit(pieces));
uint64_t attacks = getBishopAttacks(b, valid, color, popBit(pieces));
// Mobility
eval += bishopMobility[countBits(attacks & safeSquares)];
// King attacks
Expand All @@ -127,9 +133,10 @@ int evaluateBishops(const Board& b, uint64_t pieces, const uint64_t& safeSquares

int evaluateRooks(const Board& b, uint64_t pieces, const uint64_t& safeSquares, const uint64_t& enemyKingRing, int color) {
int eval = 0;
const uint64_t valid = ~b.colors[b.turn];
while (pieces) {
int sqr = popBit(pieces);
uint64_t attacks = getRookAttacks(b, color, sqr);
uint64_t attacks = getRookAttacks(b, valid, color, sqr);
// Mobility
eval += rookMobility[countBits(attacks & safeSquares)];
// King attacks
Expand All @@ -144,8 +151,9 @@ int evaluateRooks(const Board& b, uint64_t pieces, const uint64_t& safeSquares,

int evaluateQueens(const Board& b, uint64_t pieces, const uint64_t& safeSquares, const uint64_t& enemyKingRing, int color) {
int eval = 0;
const uint64_t valid = ~b.colors[b.turn];
while (pieces) {
uint64_t attacks = getQueenAttacks(b, color, popBit(pieces));
uint64_t attacks = getQueenAttacks(b, valid, color, popBit(pieces));
// Mobility
eval += queenMobility[countBits(attacks & safeSquares)];
// King attacks
Expand Down Expand Up @@ -189,11 +197,8 @@ uint64_t genKingRing(int sqr) {

float getPhase(const Board& b) {
int material = 0;
for (int i = 0; i < SQUARE_NUM; ++i) {
int piece = pieceType(b.squares[i]);
if (piece < KING) {
material += pieceValues[piece][MG];
}
for (int i = PAWN; i <= QUEEN; ++i) {
material += pieceValues[i][MG] * countBits(b.pieces[i]);
}
return std::min((float)1.0, material / materialSum);
}
11 changes: 6 additions & 5 deletions src/evaluate.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,31 @@ static constexpr int psqtFileTable[8] = { 0, 1, 2, 3, 3, 2, 1, 0 };

static constexpr int supportedPawnBonus = 12;
static constexpr int phalanxPawnBonus = 8;
static constexpr int doubledPawnPenalty = 12;

static constexpr int passedBonus[7][2] = {
{0, 0}, {0, 10}, {5, 20}, {15, 30}, {45, 65}, {125, 175}, {250, 275}
};
static constexpr double passedBlockReduction = 0.5;
static constexpr double passedBlockReduction = 2;

static constexpr int knightMobility[28]
{
-50, -35, -15, -5, 5, 10, 15, 20, 25
-50, -35, -10, 0, 5, 10, 15, 20, 25
};

static constexpr int bishopMobility[14]
{
-35, -20, -5, 0, 5, 10, 15, 20, 24, 28, 32, 34, 36, 38
-50, -30, -5, 5, 10, 15, 20, 24, 28, 32, 34, 36, 38, 40
};

static constexpr int rookMobility[15]
{
-25, -18, -12, -8, -4, 2, 7, 12, 16, 20, 23, 26, 29, 31, 34
-40, -24, -12, -8, -4, 2, 7, 12, 16, 20, 23, 26, 29, 31, 34
};

static constexpr int queenMobility[28]
{
-30, -15, -10, -5, 0, 5, 9, 12, 15, 18, 21, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 44, 45, 45, 46, 46, 46
-30, -20, -10, -5, 0, 5, 9, 12, 15, 18, 21, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 44, 45, 45, 46, 46, 46
};

static constexpr int kingAttackBonus = 12;
Expand Down
Binary file modified src/main.cpp
Binary file not shown.
10 changes: 7 additions & 3 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void addPawnMoves(std::vector<Move>& moves, uint64_t bb, const int shift) {
}
}
}

void addPieceMoves(std::vector<Move>& moves, uint64_t bb, const int from) {
while (bb) {
moves.emplace_back(from, popBit(bb), NORMAL_MOVE);
Expand Down Expand Up @@ -63,29 +64,32 @@ void genKnightMoves(const Board& b, std::vector<Move>& moves, bool noisyOnly) {

void genBishopMoves(const Board& b, std::vector<Move>& moves, bool noisyOnly) {
uint64_t bishops = b.pieces[BISHOP] & b.colors[b.turn];
const uint64_t valid = ~b.colors[b.turn];
while (bishops) {
int sqr = popBit(bishops);
uint64_t bb = getBishopAttacks(b, (b.squares[sqr] == B_BISHOP), sqr) & ~b.pieces[KING];
uint64_t bb = getBishopAttacks(b, valid, b.turn, sqr) & ~b.pieces[KING];
if (noisyOnly) bb &= b.colors[!b.turn];
addPieceMoves(moves, bb, sqr);
}
}

void genRookMoves(const Board& b, std::vector<Move>& moves, bool noisyOnly) {
uint64_t rooks = b.pieces[ROOK] & b.colors[b.turn];
const uint64_t valid = ~b.colors[b.turn];
while (rooks) {
int sqr = popBit(rooks);
uint64_t bb = getRookAttacks(b, (b.squares[sqr] == B_ROOK), sqr) & ~b.pieces[KING];
uint64_t bb = getRookAttacks(b, valid, b.turn, sqr) & ~b.pieces[KING];
if (noisyOnly) bb &= b.colors[!b.turn];
addPieceMoves(moves, bb, sqr);
}
}

void genQueenMoves(const Board& b, std::vector<Move>& moves, bool noisyOnly) {
uint64_t queens = b.pieces[QUEEN] & b.colors[b.turn];
const uint64_t valid = ~b.colors[b.turn];
while (queens) {
int sqr = popBit(queens);
uint64_t bb = getQueenAttacks(b, (b.squares[sqr] == B_QUEEN), sqr) & ~b.pieces[KING];
uint64_t bb = getQueenAttacks(b, valid, b.turn, sqr) & ~b.pieces[KING];
if (noisyOnly) bb &= b.colors[!b.turn];
addPieceMoves(moves, bb, sqr);
}
Expand Down
Loading

0 comments on commit 3881d25

Please sign in to comment.