Skip to content

Commit 2cd15d4

Browse files
committed
Fix: Improve search and evaluation functions
This commit includes several fixes and improvements to the search and evaluation functions: - **tt.cpp:** Reduced `depthMargin` to 0. - **Makefile:** Removed `-DNDEBUG` flag from CXXFLAGS. - **eval.h:** Added include for "tt.hpp" and `setEpd` and `setFen` functions. Removed `PV_MOVE` from `EvalKey` enum. - **tt.hpp:** Modified `TTEntry` to store the score in the `bestMove` field. - **search.hpp:** Added includes for `<chrono>` and `<atomic>`. Added `stop_requested` variable. - **eval.cpp:** Added documentation for `EvalWeights` map. Added `evaltt` transposition table. - **search.cpp:** Implemented iterative deepening, quiescence search, null move pruning, and LMR. - **main.cpp:** Implemented UCI protocol for setting up the board, starting the search, and printing the best move. Added time management.
1 parent 82dd915 commit 2cd15d4

File tree

8 files changed

+347
-224
lines changed

8 files changed

+347
-224
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $(EXEC): $(OBJ)
1717

1818
# Rule to build object files from source files
1919
%.o: %.cpp
20-
$(CXX) $(CXXFLAGS) -DNDEBUG -std=c++17 -c $< -o $@
20+
$(CXX) $(CXXFLAGS) -std=c++17 -c $< -o $@
2121

2222
# Clean up the compiled files
2323
clean:

eval.cpp

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
#include "eval.h"
22

33
// Initialize static weight map
4+
/**
5+
* @brief Static map of evaluation weights used for chess position scoring
6+
*
7+
* This map contains predefined integer weights for various chess evaluation criteria,
8+
* including pawn structure, piece development, board control, and tactical considerations.
9+
*
10+
* The weights are used to calculate a comprehensive evaluation score for a chess position,
11+
* covering aspects such as:
12+
* - Pawn structure (doubled, backward, isolated pawns)
13+
* - Piece development and positioning
14+
* - King safety
15+
* - Tactical opportunities (pins, forks, discovered attacks)
16+
* - Piece values
17+
*
18+
* Weights are defined as static const to ensure consistent evaluation across the chess engine.
19+
*/
420
const std::unordered_map<EvalKey, int> EvalWeights::weights = {
521
{EvalKey::DOUBLED, 10}, {EvalKey::BACKWARD, 8}, {EvalKey::BLOCKED, 5},
622
{EvalKey::ISLANDED, 6}, {EvalKey::ISOLATED, 12}, {EvalKey::DBLISOLATED, 20},
@@ -17,9 +33,8 @@ const std::unordered_map<EvalKey, int> EvalWeights::weights = {
1733
{EvalKey::TEMPO_FREEDOM_WEIGHT, 8}, {EvalKey::TEMPO_OPPONENT_MOBILITY_PENALTY, 6},
1834
{EvalKey::UNDERPROMOTE, 5}, {EvalKey::PAWN, 100}, {EvalKey::KNIGHT, 300},
1935
{EvalKey::BISHOP, 300}, {EvalKey::ROOK, 500}, {EvalKey::QUEEN, 900},
20-
{EvalKey::COUNTER, 1000}, {EvalKey::PV_MOVE, MATE(0)}
36+
{EvalKey::COUNTER, 1000}
2137
};
22-
//std::unordered_map<U64, int> transposition; // Faster lookup
2338

2439
// Declare only missing functions
2540
int evaluatePawnStructure(const chess::Position&);
@@ -110,7 +125,6 @@ std::vector<Square> scan_reversed(chess::Bitboard bb)
110125
}
111126
return iter;
112127
}
113-
#ifdef DBG
114128
// **1. Evaluate Bad Bishops**
115129
int evaluateBadBishops(const chess::Position& board) {
116130
auto myBishops = board.pieces(chess::PieceType::underlying::BISHOP, board.sideToMove());
@@ -126,7 +140,6 @@ int evaluateBadBishops(const chess::Position& board) {
126140
}
127141
return score;
128142
}
129-
#endif
130143
int evaluateKingSafety(const chess::Position& board) {
131144
// 1) One-time fetches
132145
const int side = board.sideToMove();
@@ -175,8 +188,6 @@ int evaluateKingSafety(const chess::Position& board) {
175188
int p = phase(board); // 1=open, 2=mid, 3=end
176189
return score * phaseWeight[p];
177190
}
178-
#ifdef DBG
179-
180191
// **4. Evaluate Rooks on Open/Semi-Open Files**
181192
int evaluateRooksOnFiles(const chess::Position& board) {
182193
auto myRooks = board.pieces(chess::PieceType::underlying::ROOK, board.sideToMove());
@@ -926,7 +937,6 @@ int underpromote(chess::Position &board) {
926937

927938
return (lastMove.promotionType().internal() != chess::PieceType::QUEEN) ? reward : 0;
928939
}
929-
#endif
930940

931941
// PAWN
932942
const int PAWN_PSQT[2][64] = {
@@ -1110,16 +1120,14 @@ int psqt_eval(const chess::Position& board) {
11101120
}
11111121
return score; // Negate for Black
11121122
}
1123+
TranspositionTable evaltt(5);
11131124
unsigned long long evalhits=0;
11141125
int eval(chess::Position &board) {
1115-
/*U64 hash = board.hash();
1116-
// Faster lookup & insertion
1117-
auto [it, inserted] = transposition.emplace(hash, 0);
1118-
if (!inserted){
1119-
++evalhits;
1120-
return it->second; // Already exists, return stored evaluation
1121-
}*/
1122-
1126+
U64 hash = board.hash();
1127+
if (TTEntry* entry=evaltt.probe(hash)){
1128+
evalhits++;
1129+
return entry->score();
1130+
}
11231131
// Precompute piece counts (avoid multiple function calls)
11241132
int pieceCount[10] = {
11251133
board.pieces(chess::PieceType::PAWN, chess::Color::WHITE).count(),
@@ -1143,7 +1151,6 @@ int eval(chess::Position &board) {
11431151

11441152
// Evaluate positional aspects
11451153
eval += evaluateKingSafety(board);
1146-
#ifdef DBG
11471154
eval += evaluatePawnStructure(board);
11481155
eval += evaluatePieces(board);
11491156
eval += evaluateTactics(board);
@@ -1153,12 +1160,10 @@ int eval(chess::Position &board) {
11531160
eval += center_control(board);
11541161
eval += key_center_squares_control(board);
11551162
eval += evaluatePieceActivity(board);
1156-
#endif
11571163
eval += psqt_eval(board);
11581164
board.makeNullMove();
11591165
// Evaluate positional aspects
11601166
eval -= evaluateKingSafety(board);
1161-
#ifdef DBG
11621167
eval -= evaluatePawnStructure(board);
11631168
eval -= evaluatePieces(board);
11641169
eval -= evaluateTactics(board);
@@ -1168,14 +1173,11 @@ int eval(chess::Position &board) {
11681173
eval -= center_control(board);
11691174
eval -= key_center_squares_control(board);
11701175
eval -= evaluatePieceActivity(board);
1171-
#endif
11721176
eval -= psqt_eval(board);
11731177
board.unmakeNullMove();
1174-
1175-
//it->second = eval; // Store result
1178+
evaltt.store(hash, chess::Move(), eval, 0, TTFlag::EXACT);
11761179
return eval;
11771180
}
1178-
#ifdef DBG
11791181
// Use `const chess::Position&` to avoid copies
11801182
int evaluatePawnStructure(const chess::Position& board) {
11811183
chess::Position b=board;
@@ -1189,18 +1191,4 @@ int evaluatePawnStructure(const chess::Position& board) {
11891191
int evaluatePieces(const chess::Position& board) {
11901192
return -(evaluateBadBishops(board) + evaluateTrappedPieces(board) + evaluateKnightForks(board)) +
11911193
(evaluateFianchetto(board) + evaluateRooksOnFiles(board));
1192-
}
1193-
#endif
1194-
int piece_value(chess::PieceType piece) {
1195-
static const int lookup[] = {
1196-
0, // NONE
1197-
EvalWeights::getWeight(EvalKey::PAWN),
1198-
EvalWeights::getWeight(EvalKey::KNIGHT),
1199-
EvalWeights::getWeight(EvalKey::BISHOP),
1200-
EvalWeights::getWeight(EvalKey::ROOK),
1201-
EvalWeights::getWeight(EvalKey::QUEEN),
1202-
0 // KING or others → 0
1203-
};
1204-
int idx = static_cast<int>(piece);
1205-
return (idx >= 0 && idx < 7) ? lookup[idx] : 0;
1206-
}
1194+
}

eval.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <cstdint>
66
#include <cstring>
7+
#include "tt.hpp"
78
#if __cplusplus >= 202002L
89
#define POPCOUNT64(bits) std::popcount(bits);
910
#elif defined(USE_POPCNT)
@@ -173,7 +174,14 @@ namespace chess
173174
public:
174175
Position() : Board(), move_stack() {}
175176
Position(std::string_view fen) : Board(fen), move_stack() {}
176-
177+
bool setFen(std::string_view fen){
178+
move_stack.clear();
179+
return Board::setFen(fen);
180+
}
181+
bool setEpd(std::string_view fen){
182+
move_stack.clear();
183+
return Board::setEpd(fen);
184+
}
177185
Position(const Position &other)
178186
{
179187
Board::operator=(other);
@@ -292,8 +300,7 @@ enum class EvalKey
292300
BISHOP,
293301
ROOK,
294302
QUEEN,
295-
COUNTER,
296-
PV_MOVE
303+
COUNTER
297304
};
298305
// Struct to hold evaluation weights
299306
struct EvalWeights
@@ -305,4 +312,4 @@ struct EvalWeights
305312
auto it = weights.find(key);
306313
return (it != weights.end()) ? it->second : 0; // Default if key not found
307314
}
308-
};
315+
};

0 commit comments

Comments
 (0)