Skip to content

Commit

Permalink
Add ability to randomly delay moves in gtp
Browse files Browse the repository at this point in the history
  • Loading branch information
lightvector committed Jun 17, 2024
1 parent 18dfa77 commit c25fd8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cpp/command/gtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,13 @@ struct GTPEngine {
double desiredDynamicPDAForWhite;
std::unique_ptr<PatternBonusTable> patternBonusTable;

double delayMoveScale;
double delayMoveMax;

Player perspective;

Rand gtpRand;

double genmoveTimeSum;
//Positions during this game when genmove was called
std::vector<Sgf::PositionSample> genmoveSamples;
Expand All @@ -377,6 +382,7 @@ struct GTPEngine {
bool assumeMultiBlackHandicap, bool prevtEncore, bool autoPattern,
double dynamicPDACapPerOppLead, bool staticPDAPrecedence,
double normAvoidRepeatedPatternUtility, double hcapAvoidRepeatedPatternUtility,
double delayScale, double delayMax,
Player persp, int pvLen,
std::unique_ptr<PatternBonusTable>&& pbTable
)
Expand Down Expand Up @@ -406,6 +412,8 @@ struct GTPEngine {
lastSearchFactor(1.0),
desiredDynamicPDAForWhite(0.0),
patternBonusTable(std::move(pbTable)),
delayMoveScale(delayScale),
delayMoveMax(delayMax),
perspective(persp),
genmoveTimeSum(0.0),
genmoveSamples()
Expand Down Expand Up @@ -1043,6 +1051,21 @@ struct GTPEngine {
return;
}

SearchNode* rootNode = bot->getSearch()->rootNode;
if(rootNode != NULL && delayMoveScale > 0.0 && delayMoveMax > 0.0) {
const NNOutput* humanOutput = rootNode->getHumanOutput();
const float* policyProbs = humanOutput != NULL ? humanOutput->getPolicyProbsMaybeNoised() : NULL;
int pos = bot->getSearch()->getPos(moveLoc);
if(policyProbs != NULL) {
double prob = std::max(0.0,(double)policyProbs[pos]);
double meanWait = 0.5 * delayMoveScale / (prob + 0.10);
double waitTime = gtpRand.nextGamma(2.0) * meanWait / 2.0;
waitTime = std::min(waitTime,delayMoveMax);
waitTime = std::max(waitTime,0.0001);
std::this_thread::sleep_for(std::chrono::duration<double>(waitTime));
}
}

ReportedSearchValues values;
double winLossValue;
double lead;
Expand Down Expand Up @@ -1856,6 +1879,8 @@ int MainCmds::gtp(const vector<string>& args) {
const double normalAvoidRepeatedPatternUtility = initialGenmoveParams.avoidRepeatedPatternUtility;
const double handicapAvoidRepeatedPatternUtility = cfg.contains("avoidRepeatedPatternUtility") ?
initialGenmoveParams.avoidRepeatedPatternUtility : 0.005;
const double delayMoveScale = cfg.contains("delayMoveScale") ? cfg.getDouble("delayMoveScale",0.0,10000.0) : 0.0;
const double delayMoveMax = cfg.contains("delayMoveMax") ? cfg.getDouble("delayMoveMax",0.0,1000000.0) : 1000000.0;

int defaultBoardXSize = -1;
int defaultBoardYSize = -1;
Expand Down Expand Up @@ -1897,6 +1922,7 @@ int MainCmds::gtp(const vector<string>& args) {
dynamicPlayoutDoublingAdvantageCapPerOppLead,
staticPDATakesPrecedence,
normalAvoidRepeatedPatternUtility, handicapAvoidRepeatedPatternUtility,
delayMoveScale,delayMoveMax,
perspective,analysisPVLen,
std::move(patternBonusTable)
);
Expand Down
5 changes: 5 additions & 0 deletions cpp/configs/gtp_human5k_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ maxVisits = 40
numSearchThreads = 1
lagBuffer = 1.0

# Rough scale in seconds to ranomly delay moving, so as not to respond instantly.
# Some moves will delay longer, some moves will delay a little less.
delayMoveScale = 2.0
delayMoveMax = 10.0

# ===========================================================================
# HUMAN SL PARAMETERS
# ===========================================================================
Expand Down

0 comments on commit c25fd8b

Please sign in to comment.