Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve time management + improve evaluation + improve move ordering #6

Merged
merged 4 commits into from
Mar 6, 2025
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
2 changes: 1 addition & 1 deletion src/engine/tt_hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TtHashTable {
}

private:
constexpr static inline std::size_t s_hashTableSizeMb { 64 };
constexpr static inline std::size_t s_hashTableSizeMb { 128 };
constexpr static inline std::size_t s_ttHashSize { (s_hashTableSizeMb * 1024 * 1024) / sizeof(TtHashEntry) };
constexpr static inline uint8_t s_generationMask { 0xF }; /* 4-bit wrapping generation (max 16 generations allowed) */

Expand Down
69 changes: 47 additions & 22 deletions src/evaluation/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,38 @@ class Evaluator {
{
using namespace std::chrono;

/* allow some extra time for processing etc */
constexpr auto buffer = 5ms;

const auto timeLeft = board.player == PlayerWhite ? milliseconds(m_whiteTime) : milliseconds(m_blackTime);
const auto timeInc = board.player == PlayerWhite ? milliseconds(m_whiteMoveInc) : milliseconds(m_blackMoveInc);
const auto buffer = timeBuffer(board);

if (m_moveTime) {
m_endTime = start + milliseconds(m_moveTime.value()) - buffer + timeInc;
} else if (m_movesToGo) {
const auto time = timeLeft / m_movesToGo;
m_endTime = start + time - buffer + timeInc;
} else {
/* TODO: how long should we search if no time controls are set? */
const auto time = timeLeft / (s_defaultAmountMoves - board.fullMoves);
m_endTime = start + time - buffer + timeInc;
/* Dynamic time allocation based on game phase */
constexpr uint32_t openingMoves = 20;
constexpr uint32_t lateGameMoves = 50;

/* Estimate remaining moves */
uint32_t movesRemaining = std::clamp(s_defaultAmountMoves - board.fullMoves, openingMoves, lateGameMoves);

/* Allocate time proportionally */
const auto time = timeLeft / movesRemaining;

/* Adjust based on game phase (early = slightly faster, late = slightly deeper) */
const float phaseFactor = 1.0 + (static_cast<float>(board.fullMoves) / s_defaultAmountMoves) * 0.5;
const auto adjustedTime = duration_cast<milliseconds>(time * phaseFactor);

m_endTime = start + adjustedTime - buffer + timeInc;
}

/* just to make sure that we actually search something - better to run out of time than to not search any moves.. */
if (m_endTime <= start) {
m_endTime = start + buffer + timeInc;
m_endTime = start + milliseconds(250) + timeInc;
}
};

Expand All @@ -185,6 +199,29 @@ class Evaluator {
if (m_isStopped)
break;

/* always allow full scan on first move - will be good for the hash table :) */
if (board.fullMoves > 0) {
const auto now = std::chrono::system_clock::now();
const auto timeLeft = m_endTime - now;
const auto timeSpent = now - startTime;

/* factor is "little less than half" meaning that we give juuust about half the time we spent to search a new depth
* might need tweaking - will do when game phases are implemented */
const auto timeLimit = timeSpent / 1.9;

/* uncommment for debugging */

/* m_logger.log("d: {}, timeLeft: {}, timeSpent: {}, timeLimit: {}", d, */
/* std::chrono::duration_cast<std::chrono::milliseconds>(timeLeft), */
/* std::chrono::duration_cast<std::chrono::milliseconds>(timeSpent), */
/* std::chrono::duration_cast<std::chrono::milliseconds>(timeLimit)); */

if (timeLeft < timeLimit) {
/* m_logger.log("Stopped early; saved: {}", std::chrono::duration_cast<std::chrono::milliseconds>(timeLeft)); */
break;
}
}

m_scoring.pvTable().setIsFollowing(true);
int32_t score = negamax(d, board, alpha, beta);

Expand All @@ -211,11 +248,12 @@ class Evaluator {

constexpr void printScoreInfo(std::chrono::time_point<std::chrono::system_clock> startTime, int32_t score, uint8_t currentDepth)
{
using namespace std::chrono;

/* if we're stopped to early and no PV was found - don't print empty eval */
if (m_scoring.pvTable().size() == 0)
/* most likely means that we've been stopped early / before a pv line was found - no need to print it */
if (m_scoring.pvTable().size() == 0) {
return;
}

using namespace std::chrono;

const auto endTime = system_clock::now();
const auto timeDiff = duration_cast<milliseconds>(endTime - startTime).count();
Expand Down Expand Up @@ -473,19 +511,6 @@ class Evaluator {
}
}

constexpr std::chrono::milliseconds timeBuffer(const BitBoard& board)
{
using namespace std::chrono_literals;

if (board.fullMoves < 4) {
return 500ms;
} else if (board.fullMoves < 6) {
return 250ms;
} else {
return 5ms;
}
}

struct MoveResult {
uint64_t hash;
BitBoard board;
Expand Down
37 changes: 8 additions & 29 deletions src/evaluation/material_scoring.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "src/bit_board.h"
#include "src/evaluation/pesto_tables.h"
#include "src/evaluation/positioning.h"

#include <cstdint>
Expand All @@ -13,49 +14,28 @@

namespace evaluation {

namespace {

/* material score for each piece - lookup based on Piece enum */
constexpr static inline auto s_pieceScoring = std::to_array<int32_t>({
100, /* White Pawn */
300, /* White Knight */
350, /* White Bishop */
500, /* White Rook */
1000, /* White Queen */
10000, /* White King */
-100, /* Black Pawn */
-300, /* Black Knight */
-350, /* Black Bishop */
-500, /* Black Rook */
-1000, /* Black Queen */
-10000, /* Black King */
});

}

constexpr int32_t materialScore(const BitBoard& board)
{
int32_t score = 0;
/* we first evaluate based on "pesto score"
* then we add mobility, double pawn etc. */
int32_t score = pesto::evaluate(board);

// Material scoring
for (const auto piece : magic_enum::enum_values<Piece>()) {
score += std::popcount(board.pieces[piece]) * s_pieceScoring[piece];

switch (piece) {
case WhitePawn:
score += position::getPawnScore<PlayerWhite>(board, board.pieces[piece]);
break;
case WhiteKnight:
score += position::getKnightScore<PlayerWhite>(board.pieces[piece]);
break;
case WhiteBishop:
score += position::getBishopScore<PlayerWhite>(board, board.pieces[piece]);
score += position::getBishopScore(board, board.pieces[piece]);
break;
case WhiteRook:
score += position::getRookScore<PlayerWhite>(board, board.pieces[piece]);
break;
case WhiteQueen:
score += position::getQueenScore<PlayerWhite>(board, board.pieces[piece]);
score += position::getQueenScore(board, board.pieces[piece]);
break;
case WhiteKing:
score += position::getKingScore<PlayerWhite>(board, board.pieces[piece]);
Expand All @@ -67,16 +47,15 @@ constexpr int32_t materialScore(const BitBoard& board)
score -= position::getPawnScore<PlayerBlack>(board, board.pieces[piece]);
break;
case BlackKnight:
score -= position::getKnightScore<PlayerBlack>(board.pieces[piece]);
break;
case BlackBishop:
score -= position::getBishopScore<PlayerBlack>(board, board.pieces[piece]);
score -= position::getBishopScore(board, board.pieces[piece]);
break;
case BlackRook:
score -= position::getRookScore<PlayerBlack>(board, board.pieces[piece]);
break;
case BlackQueen:
score -= position::getQueenScore<PlayerBlack>(board, board.pieces[piece]);
score -= position::getQueenScore(board, board.pieces[piece]);
break;
case BlackKing:
score -= position::getKingScore<PlayerBlack>(board, board.pieces[piece]);
Expand Down
13 changes: 13 additions & 0 deletions src/evaluation/move_scoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ class MoveScoring {
return 20000;
}

switch (move.promotionType()) {
case PromotionNone:
break;
case PromotionQueen:
return 19000;
case PromotionKnight:
return 18000;
case PromotionBishop:
return 16000;
case PromotionRook:
return 17000;
}

const auto attacker = board.getPieceAtSquare(move.fromSquare());
const auto victim = board.getPieceAtSquare(move.toSquare());

Expand Down
Loading