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

Objective pawn eval #4218

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void MainThread::search() {
{
rootMoves.emplace_back(MOVE_NONE);
sync_cout << "info depth 0 score "
<< UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW)
<< UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW, -1)
<< sync_endl;
}
else
Expand Down Expand Up @@ -1854,7 +1854,7 @@ string UCI::pv(const Position& pos, Depth depth, Value alpha, Value beta) {
<< " depth " << d
<< " seldepth " << rootMoves[i].selDepth
<< " multipv " << i + 1
<< " score " << UCI::value(v);
<< " score " << UCI::value(v, pos.game_ply());

if (Options["UCI_ShowWDL"])
ss << UCI::wdl(v, pos.game_ply());
Expand Down
34 changes: 29 additions & 5 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace {

// The win rate model returns the probability of winning (in per mille units) given an
// eval and a game ply. It fits the LTC fishtest statistics rather accurately.
int win_rate_model(Value v, int ply) {
double win_rate_model_double(Value v, int ply) {

// The model only captures up to 240 plies, so limit the input and then rescale
double m = std::min(240, ply) / 64.0;
Expand All @@ -215,10 +215,15 @@ namespace {
// Transform the eval to centipawns with limited range
double x = std::clamp(double(100 * v) / PawnValueEg, -2000.0, 2000.0);

// Return the win rate in per mille units rounded to the nearest value
return int(0.5 + 1000 / (1 + std::exp((a - x) / b)));

return 1 / (1 + std::exp((a - x) / b));

}

int win_rate_model(Value v, int ply) {
// Return the win rate in per mille units rounded to the nearest value
return int(0.5 + 1000*win_rate_model_double(v, ply));
}
} // namespace


Expand Down Expand Up @@ -305,14 +310,17 @@ void UCI::loop(int argc, char* argv[]) {
/// mate <y> Mate in 'y' moves (not plies). If the engine is getting mated,
/// uses negative values for 'y'.

string UCI::value(Value v) {
string UCI::value(Value v, int ply) {

assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);

stringstream ss;

if (abs(v) < VALUE_MATE_IN_MAX_PLY)
ss << "cp " << v * 100 / PawnValueEg;
if (ply >=0)
ss << "cp " << UCI::pawn_eval(v, ply);
else
ss << "cp " << v * 100 / PawnValueEg;
else
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;

Expand All @@ -335,6 +343,22 @@ string UCI::wdl(Value v, int ply) {
return ss.str();
}

/// UCI::pawn_eval() uses the win_rate_model to convert
/// and internal score and ply to an objective
/// pawn evaluation.

int UCI::pawn_eval(Value v, int ply) {

double wdl_w = win_rate_model_double( v, ply);
double wdl_l = win_rate_model_double(-v, ply);
double wdl_d = 1 - wdl_w - wdl_l;
double score = wdl_w + wdl_d / 2;
double r = std::clamp(score / (1-score), 1e-15, 1e15);
int pawn_eval = int(400 * log10(r) + 0.5);

return pawn_eval;
}


/// UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.)

Expand Down
3 changes: 2 additions & 1 deletion src/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ class Option {

void init(OptionsMap&);
void loop(int argc, char* argv[]);
std::string value(Value v);
std::string value(Value v, int ply);
std::string square(Square s);
std::string move(Move m, bool chess960);
std::string pv(const Position& pos, Depth depth, Value alpha, Value beta);
std::string wdl(Value v, int ply);
int pawn_eval(Value v, int ply);
Move to_move(const Position& pos, std::string& str);

} // namespace UCI
Expand Down