Skip to content

Commit

Permalink
Merge branch 'random_eval_perturb_2' of github.com:Sopel97/Stockfish …
Browse files Browse the repository at this point in the history
…into HalfKAv2_hm
  • Loading branch information
MichaelB7 committed Aug 12, 2021
2 parents 7dc7a9a + 56a8a4f commit de13aa9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/evaluate.cpp
Expand Up @@ -23,6 +23,7 @@
#include <fstream>
#include <iomanip>
#include <sstream>
#include <random>
#include <iostream>
#include <streambuf>
#include <vector>
Expand Down Expand Up @@ -63,6 +64,8 @@ namespace Eval {
bool useNNUE;
string eval_file_loaded = "None";

int NNUE::RandomEvalPerturb = 0;

/// NNUE::init() tries to load a NNUE network at startup time, or when the engine
/// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
/// The name of the NNUE network is always retrieved from the EvalFile option.
Expand Down Expand Up @@ -1081,6 +1084,10 @@ namespace {

Value Eval::evaluate(const Position& pos) {

static thread_local std::mt19937_64 tls_rng = [](){
return std::mt19937_64(std::time(0));
}();

Value v;

if (!Eval::useNNUE)
Expand All @@ -1107,6 +1114,7 @@ Value Eval::evaluate(const Position& pos) {
int r50 = pos.rule50_count();
Value psq = Value(abs(eg_value(pos.psq_score())));
bool classical = psq * 5 > (850 + pos.non_pawn_material() / 64) * (5 + r50);
classical = false;

v = classical ? Evaluation<NO_TRACE>(pos).value() // classical
: adjusted_NNUE(); // NNUE
Expand All @@ -1115,6 +1123,12 @@ Value Eval::evaluate(const Position& pos) {
// Damp down the evaluation linearly when shuffling
v = v * (100 - pos.rule50_count()) / 100;

std::normal_distribution<float> d(0.0, RookValueEg);
float r = d(tls_rng);
r = std::clamp<float>(r, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);

v = (NNUE::RandomEvalPerturb * Value(r) + (100 - NNUE::RandomEvalPerturb) * v) / 100;

// Guarantee evaluation does not hit the tablebase range
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);

Expand Down
2 changes: 2 additions & 0 deletions src/evaluate.h
Expand Up @@ -43,6 +43,8 @@ namespace Eval {

namespace NNUE {

extern int RandomEvalPerturb;

std::string trace(Position& pos);
Value evaluate(const Position& pos, bool adjusted = false);

Expand Down
2 changes: 2 additions & 0 deletions src/ucioption.cpp
Expand Up @@ -42,6 +42,7 @@ void on_clear_hash(const Option&) { Search::clear(); }
void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
void on_logger(const Option& o) { start_logger(o); }
void on_threads(const Option& o) { Threads.set(size_t(o)); }
void on_eval_perturb(const Option& o) { Eval::NNUE::RandomEvalPerturb = o; }
void on_tb_path(const Option& o) { Tablebases::init(o); }
void on_use_NNUE(const Option& ) { Eval::NNUE::init(); }
void on_eval_file(const Option& ) { Eval::NNUE::init(); }
Expand All @@ -62,6 +63,7 @@ void init(OptionsMap& o) {

o["Debug Log File"] << Option("", on_logger);
o["Threads"] << Option(1, 1, 512, on_threads);
o["RandomEvalPerturb"] << Option(0, 0, 100, on_eval_perturb);
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
o["Clear Hash"] << Option(on_clear_hash);
o["Ponder"] << Option(false);
Expand Down

0 comments on commit de13aa9

Please sign in to comment.