Skip to content

Commit 03b7ebf

Browse files
Tuning (#8)
* try to tune * Apply clang-format * revert weights, also proper malus in history * Apply clang-format * format+base branch * oopsie wrong locatoin for history * Apply clang-format * exclude current move (just in case) for malus * compile fix (still exclude current move) * perturb values * bench No functional change * Apply clang-format --------- Co-authored-by: GitHub Actions <actions@github.com>
1 parent 0ee5318 commit 03b7ebf

7 files changed

Lines changed: 92 additions & 23 deletions

File tree

eval.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Value *mgPst[] = { nullptr, mg_pawn_table, mg_knight_table, mg_bishop_table, mg_
2929
Value *egPst[] = { nullptr, eg_pawn_table, eg_knight_table, eg_bishop_table, eg_rook_table, eg_queen_table, eg_king_table };
3030

3131
// tuning slop here
32-
/*
32+
3333
TUNE(SetRange(5, 30),
3434
tempo,
3535
SetRange(80, 120),
@@ -225,7 +225,7 @@ TUNE(SetRange(10, 100), minorImWt, SetRange(5, 50), bishopImWt, SetRange(10, 100
225225
TUNE(SetRange(1, 50), rammedPawnPenalty);
226226
TUNE(SetRange(1, 100), rookOnSeventhBonus);
227227
TUNE(SetRange(1, 50), earlyQueenPenalty);
228-
*/
228+
229229
EvalComponents eval_components(const chess::Position &board) {
230230
constexpr int KnightPhase = 1;
231231
constexpr int BishopPhase = 1;

main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using namespace engine;
1212
int main() {
1313
std::cout << std::unitbuf;
1414
std::cout << "cppchess_engine version " << BUILD_VERSION << '\n';
15+
options.add("Threads", Option(1, 1, 1));
1516
options.add("Move Overhead", Option(10, 0, 1000));
1617
options.add("Hash", Option(16, 1, 1 << 25, [](const Option &o) {
1718
try {

search.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Value doSearch(
358358
Value maxScore = -VALUE_INFINITE;
359359
Move bestMove = Move::none();
360360
int movesSearched = 0;
361-
361+
Movelist quietsSearched;
362362
for (size_t i = 0; i < moves.size(); ++i) {
363363
Move move = moves[i];
364364
if (ply == 0 && !session.tc.searchmoves.empty() &&
@@ -419,7 +419,7 @@ Value doSearch(
419419
ext = 1;
420420
if (ext == 0 && moves.size() == 1)
421421
ext = 1;
422-
if (ext == 0 && isCapture && movesSearched == 0)
422+
if (ext == 0 && isCapture)
423423
ext = 1;
424424
board.doMove(move);
425425

@@ -453,30 +453,28 @@ Value doSearch(
453453

454454
board.undoMove();
455455
movesSearched++;
456-
456+
if (!isCapture && !givesCheck)
457+
quietsSearched.push_back(move);
457458
if (score > maxScore) {
458459
maxScore = score;
459460
bestMove = move;
460461
}
461462

462-
if (score > alpha) {
463+
if (score > alpha)
463464
alpha = score;
464465

466+
if (alpha >= beta) {
465467
if (!isCapture) {
466468
int bonus = depth * depth;
467469
if (is_win(score))
468470
bonus += 4 * depth * depth;
469471
session.historyHeuristic[(int)move.from()][(int)move.to()] =
470472
std::clamp(session.historyHeuristic[(int)move.from()][(int)move.to()] + bonus, -16384, 16384);
471-
}
472-
} /*else if (!isCapture && depth > 0) {
473-
int malus = -depth * depth;
474-
session.historyHeuristic[(int)move.from()][(int)move.to()] =
475-
std::clamp(session.historyHeuristic[(int)move.from()][(int)move.to()] + malus, -16384, 16384);
476-
}*/
477-
478-
if (alpha >= beta) {
479-
if (!isCapture) {
473+
int malus = 300 * depth - 250;
474+
for (Move move_ : quietsSearched)
475+
if (move_ != move)
476+
session.historyHeuristic[(int)move_.from()][(int)move_.to()] =
477+
std::clamp(session.historyHeuristic[(int)move_.from()][(int)move_.to()] - malus, -16384, 16384);
480478
if (session.killerMoves[ply][0] != move) {
481479
session.killerMoves[ply][1] = session.killerMoves[ply][0];
482480
session.killerMoves[ply][0] = move;
@@ -528,7 +526,7 @@ std::string extract_pv(const chess::Position &root, int maxPly) {
528526
return pv;
529527
}
530528

531-
void search(const chess::Position &board, const timeman::LimitsType timecontrol) {
529+
uint64_t search(const chess::Position &board, const timeman::LimitsType timecontrol) {
532530
stopSearch = false;
533531
tt.newSearch();
534532
static double originalTimeAdjust = -1;
@@ -655,5 +653,6 @@ void search(const chess::Position &board, const timeman::LimitsType timecontrol)
655653
if (entry && entry->getMove() != Move::none().raw())
656654
report(chess::uci::moveToUci(Move(entry->getMove()), board.chess960()));
657655
}
656+
return session.nodes;
658657
}
659658
} // namespace engine::search

search.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "eval.h"
33
#include "timeman.h"
44
#include "tt.h"
5+
#include <cstdint>
56
#include <position.h>
67
namespace engine::search {
78
struct Session {
@@ -18,7 +19,7 @@ struct Session {
1819
chess::Color ogcolor;
1920
};
2021
void stop();
21-
void search(const chess::Position &, const timeman::LimitsType);
22+
uint64_t search(const chess::Position &, const timeman::LimitsType);
2223
bool isStopped();
2324
extern engine::TranspositionTable tt;
2425
} // namespace engine::search

timeman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ void TimeManagement::init(LimitsType &limits, chess::Color us, int ply, double &
2222

2323
// If we have no time, we don't need to fully initialize TM.
2424
// startTime is used by movetime and useNodesTime is used in elapsed calls.
25-
startTime = limits.startTime;
25+
startTime = now();
2626
if (limits.movetime != 0) {
2727
optimumTime = maximumTime = TimePoint(limits.movetime);
2828
return;
2929
}
30-
if (limits.time[us] == 0 && limits.movetime == 0) {
30+
if (!limits.use_time_management()) {
3131
optimumTime = maximumTime = INFINITE_TIME;
3232
return;
3333
}

timeman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct LimitsType {
1515

1616
// Init explicitly due to broken value-initialization of non POD in MSVC
1717
LimitsType() {
18-
time[chess::WHITE] = time[chess::BLACK] = inc[chess::WHITE] = inc[chess::BLACK] = movetime = startTime = TimePoint(0);
18+
time[chess::WHITE] = time[chess::BLACK] = inc[chess::WHITE] = inc[chess::BLACK] = movetime = TimePoint(0);
1919
movestogo = mate = perft = infinite = 0;
2020
depth = 64;
2121
nodes = 0;
@@ -25,7 +25,7 @@ struct LimitsType {
2525
bool use_time_management() const { return time[chess::WHITE] || time[chess::BLACK]; }
2626

2727
std::vector<std::string> searchmoves;
28-
TimePoint time[chess::COLOR_NB], inc[chess::COLOR_NB], movetime, startTime;
28+
TimePoint time[chess::COLOR_NB], inc[chess::COLOR_NB], movetime;
2929
int movestogo, depth, mate, perft, infinite;
3030
uint64_t nodes;
3131
bool ponderMode;

uci.cpp

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ timeman::LimitsType parse_limits(std::istream &is) {
8383
timeman::LimitsType limits;
8484
std::string token;
8585

86-
limits.startTime = timeman::now(); // The search starts as early as possible
87-
8886
while (is >> token)
8987
if (token == "searchmoves") // Needs to be the last command on the line
9088
while (is >> token) {
@@ -240,6 +238,76 @@ void execCmd(const std::string &line) {
240238
score = -score;
241239
std::cout << score << std::endl;
242240
break;
241+
} else if (token == "bench") {
242+
uint64_t nodes = 0;
243+
const std::vector<std::string> Defaults = {
244+
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
245+
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
246+
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
247+
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
248+
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14 moves d4e6",
249+
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14 moves g2g4",
250+
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
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1P/2P5/5B2 b - - 0 1",
269+
"5k2/7R/4P2p/5K2/p1r2P1p/8/8/8 b - - 0 1",
270+
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
271+
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
272+
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
273+
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1",
274+
"5rk1/q6p/2p3bR/1pPp1rP1/1P1Pp3/P3B1Q1/1K3P2/R7 w - - 93 90",
275+
"4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21",
276+
"r3k2r/3nnpbp/q2pp1p1/p7/Pp1PPPP1/4BNN1/1P5P/R2Q1RK1 w kq - 0 16",
277+
"3Qb1k1/1r2ppb1/pN1n2q1/Pp1Pp1Pr/4P2p/4BP2/4B1R1/1R5K b - - 11 40",
278+
"4k3/3q1r2/1N2r1b1/3ppN2/2nPP3/1B1R2n1/2R1Q3/3K4 w - - 5 1",
279+
"1r6/1P4bk/3qr1p1/N6p/3pp2P/6R1/3Q1PP1/1R4K1 w - - 1 42",
280+
281+
// 5-man positions
282+
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
283+
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
284+
"8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
285+
286+
// 6-man positions
287+
"8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
288+
"8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
289+
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
290+
291+
// 7-man positions
292+
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
293+
294+
// Mate and stalemate positions
295+
"6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1",
296+
"r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1",
297+
"8/8/8/8/8/6k1/6p1/6K1 w - -",
298+
"7k/7P/6K1/8/3B4/8/8/8 b - -",
299+
};
300+
timeman::LimitsType tc{};
301+
tc.depth = 10;
302+
auto start = std::chrono::high_resolution_clock::now();
303+
for (auto &fen : Defaults) {
304+
std::cerr << fen << '\n';
305+
chess::Position pos(fen);
306+
nodes += search::search(pos, tc);
307+
}
308+
auto end = std::chrono::high_resolution_clock::now();
309+
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
310+
std::cout << "nodes " << nodes << " nps " << nodes / (duration_ms / 1000.0) << std::endl;
243311
} else if (token == "export_weights") {
244312
std::string weights_header = "Weights.h";
245313
ss >> weights_header;

0 commit comments

Comments
 (0)