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

add friendlyMaxVisitsFactor setting #533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions cpp/command/gtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ struct GTPEngine {

double genmoveTimeSum;

ClockTimer gameTimer;
double gameGenmoveTimeSum;
double friendlyMaxVisitsFactor;

GTPEngine(
const string& modelFile, SearchParams initialParams, Rules initialRules,
bool assumeMultiBlackHandicap, bool prevtEncore,
Expand All @@ -357,7 +361,8 @@ struct GTPEngine {
double genmoveWRN, double analysisWRN,
bool genmoveAntiMir, bool analysisAntiMir,
Player persp, int pvLen,
std::unique_ptr<PatternBonusTable>&& pbTable
std::unique_ptr<PatternBonusTable>&& pbTable,
double friendlyMaxVisitsFact
)
:nnModelFile(modelFile),
assumeMultipleStartingBlackMovesAreHandicap(assumeMultiBlackHandicap),
Expand Down Expand Up @@ -387,7 +392,9 @@ struct GTPEngine {
avoidMYTDaggerHack(avoidDagger),
patternBonusTable(std::move(pbTable)),
perspective(persp),
genmoveTimeSum(0.0)
genmoveTimeSum(0.0),
gameGenmoveTimeSum(0.0),
friendlyMaxVisitsFactor(friendlyMaxVisitsFact)
{
}

Expand All @@ -406,7 +413,8 @@ struct GTPEngine {
}

void clearStatsForNewGame() {
//Currently nothing
gameTimer.reset();
gameGenmoveTimeSum = 0.0;
}

//Specify -1 for the sizes for a default
Expand Down Expand Up @@ -861,6 +869,19 @@ struct GTPEngine {
double searchFactor = PlayUtils::getSearchFactor(searchFactorWhenWinningThreshold,searchFactorWhenWinning,params,recentWinLossValues,pla);
lastSearchFactor = searchFactor;

//Adjust to opponent's playing speed
if (friendlyMaxVisitsFactor > 0.0) {
double opponentMoveCount = round(moveHistory.size() / 2.0);
if (opponentMoveCount > 0) {
double timeSpentOpponent = (gameTimer.getSeconds() - gameGenmoveTimeSum);
params.maxVisits = lround(timeSpentOpponent / opponentMoveCount * friendlyMaxVisitsFactor);
} else {
// KataGo plays the very first move of the game, take 1s
params.maxVisits = lround(friendlyMaxVisitsFactor);
}
bot->setParams(params);
}

Loc moveLoc;
bot->setAvoidMoveUntilByLoc(args.avoidMoveUntilByLocBlack,args.avoidMoveUntilByLocWhite);
if(args.analyzing) {
Expand Down Expand Up @@ -914,6 +935,7 @@ struct GTPEngine {
//output of various things.
double timeTaken = timer.getSeconds();
genmoveTimeSum += timeTaken;
gameGenmoveTimeSum += timeTaken;

//Chatting and logging ----------------------------

Expand Down Expand Up @@ -1550,6 +1572,7 @@ int MainCmds::gtp(int argc, const char* const* argv) {
const double normalAvoidRepeatedPatternUtility = initialParams.avoidRepeatedPatternUtility;
const double handicapAvoidRepeatedPatternUtility = (cfg.contains("avoidRepeatedPatternUtility") || cfg.contains("avoidRepeatedPatternUtility0")) ?
initialParams.avoidRepeatedPatternUtility : 0.005;
const double friendlyMaxVisitsFactor = cfg.contains("friendlyMaxVisitsFactor") ? cfg.getDouble("friendlyMaxVisitsFactor", 0.0, 1e10) : 0.0;

const int defaultBoardXSize =
cfg.contains("defaultBoardXSize") ? cfg.getInt("defaultBoardXSize",2,Board::MAX_LEN) :
Expand Down Expand Up @@ -1591,12 +1614,13 @@ int MainCmds::gtp(int argc, const char* const* argv) {
genmoveWideRootNoise,analysisWideRootNoise,
genmoveAntiMirror,analysisAntiMirror,
perspective,analysisPVLen,
std::move(patternBonusTable)
std::move(patternBonusTable),
friendlyMaxVisitsFactor
);
engine->setOrResetBoardSize(cfg,logger,seedRand,defaultBoardXSize,defaultBoardYSize);

//If nobody specified any time limit in any way, then assume a relatively fast time control
if(!cfg.contains("maxPlayouts") && !cfg.contains("maxVisits") && !cfg.contains("maxTime")) {
if(!cfg.contains("maxPlayouts") && !cfg.contains("maxVisits") && !cfg.contains("maxTime") && !cfg.contains("friendlyMaxVisitsFactor")) {
double mainTime = 1.0;
double byoYomiTime = 5.0;
int byoYomiPeriods = 5;
Expand Down
5 changes: 5 additions & 0 deletions cpp/configs/gtp_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ maxVisits = 500
# If provided, cap search time at this many seconds.
# maxTime = 10

# If provided, adapt maxVisits to averageOpponentTimePerMove * friendlyMaxVisitsFactor. (The static maxVisits setting will be ignored.)
# Setting this to 150% of the visits/s you normally get on your hardware makes KataGo play approximately equally fast as the opponent.
# Useful if you're running a ranked bot on a go server, and you want to be friendly if the opponent is also playing fast.
# friendlyMaxVisitsFactor = 500

# Ponder on the opponent's turn?
ponderingEnabled = false
maxTimePondering = 60 # Maximum time to ponder, in seconds. Comment out to make unlimited.
Expand Down
5 changes: 5 additions & 0 deletions cpp/program/gtpconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ resignConsecTurns = 3
# If provided, cap search time at this many seconds.
$$MAX_TIME

# If provided, adapt maxVisits to averageOpponentTimePerMove * friendlyMaxVisitsFactor. (The static maxVisits setting will be ignored.)
# Setting this to 150% of the visits/s you normally get on your hardware makes KataGo play approximately equally fast as the opponent.
# Useful if you're running a ranked bot on a go server, and you want to be friendly if the opponent is also playing fast.
# friendlyMaxVisitsFactor = 500

# Ponder on the opponent's turn?
$$PONDERING
# Note: you can also set "maxVisitsPondering" or "maxPlayoutsPondering" too.
Expand Down