Skip to content

Commit e8d1e33

Browse files
committed
Added basic aspiration window, tweaked some parameters, and cleand up code
1 parent 3832041 commit e8d1e33

File tree

7 files changed

+66
-60
lines changed

7 files changed

+66
-60
lines changed

Mr Bob.exe

-409 Bytes
Binary file not shown.

engine/bitboard.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ void Bitboard::undoMove() {
18441844

18451845

18461846

1847-
int Bitboard::evaluate(int alpha, int beta) {
1847+
int Bitboard::evaluate() {
18481848

18491849
// if (count_population(blacks & pieces[3]) != countRooksB) {
18501850
// std::cout << unsigned(countRooksB) << " " << unsigned(count_population(blacks & pieces[3])) << std::endl;
@@ -1947,7 +1947,7 @@ int Bitboard::evaluate(int alpha, int beta) {
19471947

19481948

19491949
ret += evaluateMobility(whitePawns, blackPawns, whiteKnights, blackKnights, whiteBishops, blackBishops, whiteRooks, blackRooks, whiteQueens, blackQueens, endgame);
1950-
ret += evaluateKingSafety(whiteKingIndex, blackKingIndex, whitePawns, blackPawns, whiteKnights, blackKnights, whiteBishops, blackBishops, whiteRooks, blackRooks, whiteQueens, blackQueens);
1950+
ret += evaluateKingSafety(whiteKingIndex, blackKingIndex, whiteKnights, blackKnights, whiteBishops, blackBishops, whiteRooks, blackRooks, whiteQueens, blackQueens);
19511951
ret += evaluatePawns(whitePawns, blackPawns);
19521952

19531953

@@ -1963,7 +1963,7 @@ int Bitboard::evaluate(int alpha, int beta) {
19631963

19641964

19651965

1966-
int Bitboard::evaluateKingSafety(uint8_t whiteKingIndex, uint8_t whitePawns, uint8_t blackPawns, uint8_t blackKingIndex, uint64_t whiteKnights, uint64_t blackKnights,
1966+
int Bitboard::evaluateKingSafety(uint8_t whiteKingIndex, uint8_t blackKingIndex, uint64_t whiteKnights, uint64_t blackKnights,
19671967
uint64_t whiteBishops, uint64_t blackBishops, uint64_t whiteRooks, uint64_t blackRooks, uint64_t whiteQueens, uint64_t blackQueens) {
19681968

19691969
int ret = 0;

engine/bitboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Bitboard{
6969
void undoMove();
7070

7171
// Evaluate position
72-
int evaluate(int alpha, int beta);
72+
int evaluate();
7373

7474
// Reseting position to original
7575
void resetBoard();
@@ -282,7 +282,7 @@ class Bitboard{
282282
// Evaluation functions
283283
int evaluateMobility(uint64_t whitePawns, uint64_t blackPawns, uint64_t whiteKnights, uint64_t blackKnights,
284284
uint64_t whiteBishops, uint64_t blackBishops, uint64_t whiteRooks, uint64_t blackRooks, uint64_t whiteQueens, uint64_t blackQueens, bool endgame);
285-
int evaluateKingSafety(uint8_t whiteKingIndex, uint8_t blackKingIndex, uint8_t whitePawns, uint8_t blackPawns, uint64_t whiteKnights, uint64_t blackKnights,
285+
int evaluateKingSafety(uint8_t whiteKingIndex, uint8_t blackKingIndex, uint64_t whiteKnights, uint64_t blackKnights,
286286
uint64_t whiteBishops, uint64_t blackBishops, uint64_t whiteRooks, uint64_t blackRooks, uint64_t whiteQueens, uint64_t blackQueens);
287287
int evaluatePawns(uint64_t whitePawns, uint64_t blackPawns);
288288

engine/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
std::string engineName = "'Mr Bob'";
9-
std::string version = "v0.4.1";
9+
std::string version = "v0.5.0";
1010

1111

1212

engine/main.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ void search(Bitboard &bitboard, TranspositionTable &tt, int depth, bool color, u
113113
std::string bestMove = "";
114114
std::string pv = "";
115115

116-
int cp;
116+
int cp = 0;
117+
int alpha;
118+
int beta;
119+
int delta = 35;
117120
int seldepth;
118-
int mateInPlies;
119121
uint8_t numBestMove = 0;
120122

121123
ReturnInfo bMove;
@@ -136,39 +138,63 @@ void search(Bitboard &bitboard, TranspositionTable &tt, int depth, bool color, u
136138
}
137139
seldepth = i;
138140

139-
// Time the search
141+
if (i >= 5) {
142+
delta = 35;
143+
alpha = cp - delta;
144+
beta = cp + delta;
145+
}
146+
else {
147+
alpha = -INFINITY_VAL;
148+
beta = INFINITY_VAL;
149+
}
150+
151+
140152
auto t1 = std::chrono::high_resolution_clock::now();
141-
// std::string k = minimaxRoot(color, bitboard, i);
142-
bMove = searchRoot(color, bitboard, tt, i, seldepth, vMoves);
143-
// bMove = alphabetaRoot(color, bitboard, i, depth);
144-
auto t2 = std::chrono::high_resolution_clock::now();
153+
while (true) {
154+
155+
bMove = searchRoot(color, bitboard, tt, i, seldepth, vMoves, alpha, beta);
156+
for (std::vector<Bitboard::Move>::iterator it = vMoves.begin(); it != vMoves.end(); ++it) {
157+
if (*it == bMove.move) {
158+
it->score = 4000000 + i;
159+
}
160+
}
161+
162+
bitboard.scoreMoves(vMoves, tempM, i, !color);
163+
std::stable_sort(vMoves.begin(), vMoves.end());
145164

146165

147-
for (std::vector<Bitboard::Move>::iterator it = vMoves.begin(); it != vMoves.end(); ++it) {
148-
if (*it == bMove.move) {
149-
it->score = 4000000 + i;
166+
// Record best move and the score
167+
bestMove = bMove.bestMove;
168+
cp = bMove.score;
169+
170+
if (cp >= beta) {
171+
beta = cp + delta;
172+
}
173+
else if (cp <= alpha || (bMove.move.fromLoc == 0 && bMove.move.toLoc == 0)) {
174+
beta = (alpha + beta) / 2;
175+
alpha = cp - delta;
176+
}
177+
else {
178+
break;
150179
}
151-
}
152180

153-
bitboard.scoreMoves(vMoves, tempM, i, !color);
154-
std::stable_sort(vMoves.begin(), vMoves.end());
181+
delta = delta * 1.25 + 4;
155182

183+
}
184+
auto t2 = std::chrono::high_resolution_clock::now();
156185

157-
// Record best move and the score
158-
bestMove = bMove.bestMove;
159-
cp = bMove.score;
160186

161187
seldepth = std::abs(seldepth) - 1 + i;
162188
pv = tt.getPV(bitboard);
163189

164190

165191
// If checkmate is found, store in variable to print later.
166-
if (bMove.mateIn) {
167-
mateInPlies = i - bMove.mateIn;
168-
}
169-
else {
170-
mateInPlies = 0;
171-
}
192+
// if (bMove.mateIn) {
193+
// mateInPlies = i - bMove.mateIn;
194+
// }
195+
// else {
196+
// mateInPlies = 0;
197+
// }
172198

173199
if (prevBestMove == bestMove) {
174200
numBestMove++;
@@ -393,7 +419,7 @@ int main() {
393419

394420
// Evaluate current board position
395421
if (command == "evaluate") {
396-
std::cout << x.evaluate(-5000, 5000) << std::endl;
422+
std::cout << x.evaluate() << std::endl;
397423
continue;
398424
}
399425

engine/search.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ int quiesceSearchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, i
3434
TranspositionTable::ZobristVal hashedBoard;
3535
uint64_t hashF = bitboard.getPosKey();
3636
bool ttRet = false;
37-
bool hashed = tt.probeTT(hashF, hashedBoard, depth, ttRet, alpha, beta);
37+
tt.probeTT(hashF, hashedBoard, depth, ttRet, alpha, beta);
3838

3939
if (ttRet) {
4040
return hashedBoard.score;
4141
}
4242

4343

44-
int standPat = bitboard.evaluate(alpha, beta);
44+
int standPat = bitboard.evaluate();
4545
if (standPat >= beta) {
4646
return standPat;
4747
}
@@ -50,7 +50,7 @@ int quiesceSearchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, i
5050
alpha = standPat;
5151
}
5252

53-
int ret = -1000000;
53+
int ret = -INFINITY_VAL;
5454
Bitboard::Move bestMove;
5555

5656
// SEARCHING STEP 2: Transposition Principal variation search:
@@ -114,7 +114,7 @@ int quiesceSearchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, i
114114

115115
}
116116

117-
if (ret != -1000000) {
117+
if (ret != -INFINITY_VAL) {
118118

119119
if (ret <= origAlpha) {
120120
tt.saveTT(bestMove, ret, depth, 2, hashF);
@@ -127,7 +127,7 @@ int quiesceSearchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, i
127127

128128

129129

130-
return ret == -1000000? standPat : ret;
130+
return ret == -INFINITY_VAL? standPat : ret;
131131

132132
}
133133

@@ -178,12 +178,12 @@ int searchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, int dept
178178
traversedNodes++;
179179

180180

181-
int ret = -1000000;
181+
int ret = -INFINITY_VAL;
182182
int moveNumber = 0;
183183
int newDepth = depth;
184184
bool hasMove = false;
185185
Bitboard::Move bestMove;
186-
int eval = bitboard.evaluate(alpha, beta);
186+
int eval = bitboard.evaluate();
187187
bool isCheck = !bitboard.filterCheck(!whiteMove); // Determine if player is in check.
188188

189189

@@ -292,14 +292,6 @@ int searchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, int dept
292292
int prevRet = ret;
293293
Bitboard::Move move = bitboard.pickMove(vMoves);
294294

295-
296-
// if (!IsPv && !move.quiet && !isCheck && depth == 1) {
297-
// if (bitboard.evaluate(alpha, beta) + 250 < beta) {
298-
// continue;
299-
// }
300-
// }
301-
302-
303295
newDepth = depth;
304296
bitboard.movePiece(move);
305297

@@ -331,30 +323,18 @@ int searchR(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, int dept
331323
// SERACHING STEP 4: Late Move Reduction:
332324
// We can also reduce the depth of late moves to reduce the size of the tree.
333325
// Again, this is assuming that the good moves are on the front of the list.
334-
if (moveNumber > 2 && depth >= 3 && move.quiet && !isCheck && !giveCheck) {
326+
if (moveNumber > 0 && depth >= 3 && move.quiet && !isCheck && !giveCheck) {
335327
int reduction = 1;
336328

337329
if (moveNumber > 6) {
338330
reduction += 1;
339331
}
340332

341-
// if (moveNumber > 14) {
342-
// reduction += 1;
343-
// }
344-
//
345-
// if (moveNumber > 19) {
346-
// reduction += 1;
347-
// }
348-
349333
// Reduce reduction if in PV
350334
if (IsPv || (hashed && hashedBoard.flag == 0)) {
351335
reduction -= 2;
352336
}
353337

354-
// if (hashed && hashedBoard.flag == 1) {
355-
// reduction += 2;
356-
// }
357-
358338
reduction = std::max(0, reduction);
359339
ret = std::max(ret, -searchR(!whiteMove, bitboard, tt, newDepth - reduction - 1, -alpha-1, -alpha, seldepth, nullMoves));
360340

@@ -448,11 +428,10 @@ ReturnInfo searchRoot(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt
448428

449429
ReturnInfo retInfo = ReturnInfo{};
450430
uint64_t hashF = bitboard.getPosKey();
451-
int ret = alpha;
431+
int ret = -INFINITY_VAL;
452432
uint8_t numMoves = 0;
453433
bool isCheck = !bitboard.filterCheck(!whiteMove);
454434
int newDepth = depth;
455-
bool hasMove = false;
456435

457436

458437
Bitboard::Move bestMove;

engine/search.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
extern uint64_t traversedNodes;
1717
const int MATE_VALUE = 32500;
18+
const int INFINITY_VAL = 1000000;
1819

1920
// Debugging variables
2021
extern uint64_t pruning;
@@ -42,5 +43,5 @@ struct ReturnInfo {
4243
};
4344

4445

45-
ReturnInfo searchRoot(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, int depth, int &seldepth, std::vector<Bitboard::Move> &vMoves, int alpha=-100000000, int beta=100000000, bool isMain=true);
46+
ReturnInfo searchRoot(bool whiteMove, Bitboard &bitboard, TranspositionTable &tt, int depth, int &seldepth, std::vector<Bitboard::Move> &vMoves, int alpha=-INFINITY_VAL, int beta=INFINITY_VAL, bool isMain=true);
4647
#endif

0 commit comments

Comments
 (0)