Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 114 additions & 110 deletions src/eval_constants.hpp

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/evaltune_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ int main() {
std::cout << std::endl;

std::cout << "inline const PParam KS_NO_QUEEN = " << KS_NO_QUEEN << ";" << std::endl;
std::cout << std::endl;

std::cout << "inline const PParam KS_FLANK_ATTACK = " << KS_FLANK_ATTACK << ";"
<< std::endl;
std::cout << "inline const PParam KS_FLANK_DEFENSE = " << KS_FLANK_DEFENSE << ";"
<< std::endl;
std::cout << "inline const PParam KS_FLANK_DOUBLE_ATTACK = " << KS_FLANK_DOUBLE_ATTACK
<< ";" << std::endl;
std::cout << "inline const PParam KS_FLANK_DOUBLE_DEFENSE = " << KS_FLANK_DOUBLE_DEFENSE
<< ";" << std::endl;
std::cout << std::endl;

std::cout << "inline const PParam PAWN_THREAT_KNIGHT = " << PAWN_THREAT_KNIGHT << ";"
<< std::endl;
Expand Down
17 changes: 17 additions & 0 deletions src/evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ PScore evaluate_king_safety(const Position& pos) {
Bitboard king_ring = king_ring_table[pos.king_sq(color).raw];
Bitboard extended_ring = extended_ring_table[pos.king_sq(color).raw];

Bitboard flank =
king_flank[static_cast<usize>(color)][static_cast<usize>(pos.king_sq(color).file())];

// Piece attacks in inner/outer ring
for (PieceType pt : {PieceType::Pawn, PieceType::Knight, PieceType::Bishop, PieceType::Rook,
PieceType::Queen}) {
Bitboard attacked = pos.attacked_by(opp, pt);
Expand All @@ -353,6 +357,19 @@ PScore evaluate_king_safety(const Position& pos) {
* outer.ipopcount();
}

// Flank attack / defense status
Bitboard defended_by_us = pos.attack_table(color).get_attacked_bitboard();
Bitboard double_defended_by_us = pos.attacked_by_two_or_more(color);

Bitboard attacked_by_them = pos.attack_table(opp).get_attacked_bitboard();
Bitboard double_attacked_by_them = pos.attacked_by_two_or_more(opp);

eval += KS_FLANK_DEFENSE * (defended_by_us & flank).ipopcount();
eval += KS_FLANK_ATTACK * (attacked_by_them & flank).ipopcount();
eval += KS_FLANK_DOUBLE_DEFENSE * (double_defended_by_us & flank).ipopcount();
eval += KS_FLANK_DOUBLE_ATTACK * (double_attacked_by_them & flank).ipopcount();

// King shelter evaluation
eval += king_shelter<color>(pos);

eval += KS_NO_QUEEN * (pos.bitboard_for(opp, PieceType::Queen).empty());
Expand Down
28 changes: 28 additions & 0 deletions src/evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,32 @@ inline Score evaluate_stm_pov(const Position& pos) {
return evaluate_stm_pov(pos, PsqtState{pos});
}

static constexpr std::array<std::array<Bitboard, 8>, 2> king_flank = []() {
std::array<std::array<Bitboard, 8>, 2> result{};

// Queenside files (0-2), Center files (3-4), Kingside files (5-7)
constexpr std::array<i32, 8> flank_start = {0, 0, 0, 2, 2, 4, 4, 4};
constexpr std::array<i32, 8> flank_width = {4, 4, 4, 4, 4, 4, 4, 4};
constexpr Bitboard white_ranks = Bitboard::rank_mask(0) | Bitboard::rank_mask(1)
| Bitboard::rank_mask(2) | Bitboard::rank_mask(3)
| Bitboard::rank_mask(4);
constexpr Bitboard black_ranks = Bitboard::rank_mask(3) | Bitboard::rank_mask(4)
| Bitboard::rank_mask(5) | Bitboard::rank_mask(6)
| Bitboard::rank_mask(7);

for (usize file_idx = 0; file_idx < 8; file_idx++) {
Bitboard flank{};
for (i32 f = flank_start[file_idx]; f < flank_start[file_idx] + flank_width[file_idx];
f++) {
flank |= Bitboard::file_mask(f);
}

result[0][file_idx] = flank & white_ranks;
result[1][file_idx] = flank & black_ranks;
}

return result;
}();


}; // namespace Clockwork
9 changes: 9 additions & 0 deletions src/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ struct Position {
return attack_table(color).get_piece_mask_bitboard(piece_list(color).mask_eq(ptype));
}

[[nodiscard]] Bitboard attacked_by_two_or_more(Color color) const {
const auto& wb = attack_table(color).raw;

u16x64 minus_one = wb - u16x64::splat(1u);
u16x64 at_least_two = wb & minus_one;

return Bitboard{at_least_two.nonzeros().to_bits()};
}

[[nodiscard]] usize mobility_of(Color color, PieceId id) const {
return attack_table(color).count_matching_mask(id.to_piece_mask());
}
Expand Down
8 changes: 8 additions & 0 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void UCIHandler::execute_command(const std::string& line) {
handle_bench(is);
} else if (command == "speedtest") {
handle_speedtest(is);
} else if (command == "debug") {
handle_debug(is);
}
#ifndef EVAL_TUNING
else if (command == "eval") {
Expand All @@ -119,6 +121,12 @@ void UCIHandler::handle_bench(std::istringstream& is) {
Bench::benchmark(searcher, depth);
}

// Note: This function is left here so that one doesn't need to reimplement it every time we need to expose a function through uci.
// The professional thing to do is to empty the body of the function / put a placeholder in here when finished (and before pr).
void UCIHandler::handle_debug(std::istringstream& is) {
std::cout << "readyok" << std::endl;
}

void UCIHandler::handle_go(std::istringstream& is) {
// Clear any previous settings
settings = {};
Expand Down
1 change: 1 addition & 0 deletions src/uci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UCIHandler {
void handle_speedtest(std::istringstream&);

void handle_genfens(std::istringstream&);
void handle_debug(std::istringstream&);
};

} // namespace Clockwork::UCI
Loading