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

refactor: cleanup finish output #718

Merged
merged 1 commit into from
Sep 29, 2024
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
4 changes: 2 additions & 2 deletions app/src/matchmaking/output/output_fastchess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ class Fastchess : public IOutput {
}

std::string formatGameStats(const elo::EloBase& elo, const Stats& stats, double pairsRatio) const {
return fmt::format("LOS: {}, DrawRatio: {}{}", elo.los(), elo.drawRatio(stats), report_penta_ ?
fmt::format(", PairsRatio: {:.2f}", pairsRatio) : "");
return fmt::format("LOS: {}, DrawRatio: {}{}", elo.los(), elo.drawRatio(stats),
report_penta_ ? fmt::format(", PairsRatio: {:.2f}", pairsRatio) : "");
}

std::string formatGameResults(const Stats& stats, double points, double pointsRatio) const {
Expand Down
7 changes: 7 additions & 0 deletions app/src/matchmaking/scoreboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class StatsMap {

class ScoreBoard {
public:
// Returns true if the pair was completed, false otherwise.
// Call after updating the pair
[[nodiscard]] bool isPairCompleted(std::uint64_t round_id) {
std::lock_guard<std::mutex> lock(game_pair_cache_mutex_);
return game_pair_cache_.find(round_id) == game_pair_cache_.end();
}

// Updates the stats of engine1 vs engine2
// Always returns true because it was immediately updated
bool updateNonPair(const GamePair<EngineConfiguration, EngineConfiguration>& configs, const Stats& stats) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/matchmaking/sprt/sprt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ double SPRT::getLLR(int win, int draw, int loss) const noexcept {
if (!enabled_) return 0.0;

const bool regularize = ((win == 0) + (draw == 0) + (loss == 0)) >= 2;
const double games = win + draw + loss + 1.5 * regularize;
const double games = win + draw + loss + 1.5 * regularize;
if (games == 0) return 0.0;
const double W = (win + 0.5 * regularize) / games;
const double D = (draw + 0.5 * regularize) / games;
Expand Down Expand Up @@ -93,7 +93,8 @@ double SPRT::getLLR(int win, int draw, int loss) const noexcept {
double SPRT::getLLR(int penta_WW, int penta_WD, int penta_WL, int penta_DD, int penta_LD, int penta_LL) const noexcept {
if (!enabled_) return 0.0;

const bool regularize = ((penta_WW == 0) + (penta_WD == 0) + ((penta_WL + penta_DD) == 0) + (penta_LD == 0) + (penta_LL == 0)) >= 4;
const bool regularize =
((penta_WW == 0) + (penta_WD == 0) + ((penta_WL + penta_DD) == 0) + (penta_LD == 0) + (penta_LL == 0)) >= 4;
const double pairs = penta_WW + penta_WD + penta_WL + penta_DD + penta_LD + penta_LL + 2.5 * regularize;
if (pairs == 0) return 0.0;
const double WW = (penta_WW + 0.5 * regularize) / pairs;
Expand Down
21 changes: 10 additions & 11 deletions app/src/matchmaking/tournament/roundrobin/roundrobin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,19 @@ void RoundRobin::createMatch(std::size_t i, std::size_t j, std::size_t round_id,

output_->endGame(configs, stats, reason, game_id);

bool report = cfg.report_penta ? scoreboard_.updatePair(configs, stats, round_id)
: scoreboard_.updateNonPair(configs, stats);
if (cfg.report_penta) {
scoreboard_.updatePair(configs, stats, round_id);
} else {
scoreboard_.updateNonPair(configs, stats);
}

// round_id and match_count_ starts 0 so we add 1
const auto ratinginterval_index = cfg.report_penta ? round_id + 1 : match_count_ + 1;
const auto scoreinterval_index = match_count_ + 1;
const auto updated_stats = scoreboard_.getStats(first.name, second.name);
const auto updated_stats = scoreboard_.getStats(first.name, second.name);

// print score result based on scoreinterval if output format is cutechess
if ((scoreinterval_index % cfg.scoreinterval == 0) || match_count_ + 1 == total_) {
if (shouldPrintScoreInterval() || allMatchesPlayed()) {
output_->printResult(updated_stats, first.name, second.name);
}

// Only print the interval if the pair is complete or we are not tracking
// penta stats.
if ((report && ratinginterval_index % cfg.ratinginterval == 0) || match_count_ + 1 == total_) {
if ((shouldPrintRatingInterval(round_id) && scoreboard_.isPairCompleted(round_id)) || allMatchesPlayed()) {
output_->printInterval(sprt_, updated_stats, first.name, second.name, engines, cfg.opening.file);
}

Expand All @@ -132,6 +129,7 @@ void RoundRobin::createMatch(std::size_t i, std::size_t j, std::size_t round_id,
};

playGame(configs, start, finish, opening, round_id, game_id);

if (config::TournamentConfig.get().wait > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(config::TournamentConfig.get().wait));
}
Expand All @@ -146,6 +144,7 @@ void RoundRobin::updateSprtStatus(const std::vector<EngineConfiguration>& engine
atomic::stop = true;

Logger::info("SPRT test finished: {} {}", sprt_.getBounds(), sprt_.getElo());

output_->printResult(stats, engine_configs[0].name, engine_configs[1].name);
output_->printInterval(sprt_, stats, engine_configs[0].name, engine_configs[1].name, engines,
config::TournamentConfig.get().opening.file);
Expand Down
20 changes: 20 additions & 0 deletions app/src/matchmaking/tournament/roundrobin/roundrobin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ class RoundRobin : public BaseTournament {
void create() override;

private:
bool shouldPrintRatingInterval(std::size_t round_id) const noexcept {
const auto& cfg = config::TournamentConfig.get();

// round_id and match_count_ starts 0 so we add 1
const auto ratinginterval_index = cfg.report_penta ? round_id + 1 : match_count_ + 1;

return ratinginterval_index % cfg.ratinginterval == 0;
}

// print score result based on scoreinterval if output format is cutechess
bool shouldPrintScoreInterval() const noexcept {
const auto& cfg = config::TournamentConfig.get();

const auto scoreinterval_index = match_count_ + 1;

return scoreinterval_index % cfg.scoreinterval == 0;
}

bool allMatchesPlayed() const noexcept { return match_count_ + 1 == total_; }

void createMatch(std::size_t i, std::size_t j, std::size_t round_id, int g, std::optional<std::size_t> opening_id);

// update the current running sprt. SPRT Config has to be valid.
Expand Down
18 changes: 9 additions & 9 deletions app/src/pgn/pgn_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ std::string PgnBuilder::addMove(chess::Board &board, const MoveData &move, std::
if (move.book) {
ss << addComment("book");
} else {
ss << addComment((move.score_string + "/" + std::to_string(move.depth)) + " " +
formatTime(move.elapsed_millis),
pgn_config_.track_timeleft ? "tl=" + formatTime(move.timeleft) : "", //
pgn_config_.track_nodes ? "n=" + std::to_string(move.nodes) : "", //
pgn_config_.track_seldepth ? "sd=" + std::to_string(move.seldepth) : "", //
pgn_config_.track_nps ? "nps=" + std::to_string(move.nps) : "", //
pgn_config_.track_hashfull ? "hashfull=" + std::to_string(move.hashfull) : "", //
pgn_config_.track_tbhits ? "tbhits=" + std::to_string(move.tbhits) : "", //
last ? match_.reason : "" //
ss << addComment(
(move.score_string + "/" + std::to_string(move.depth)) + " " + formatTime(move.elapsed_millis),
pgn_config_.track_timeleft ? "tl=" + formatTime(move.timeleft) : "", //
pgn_config_.track_nodes ? "n=" + std::to_string(move.nodes) : "", //
pgn_config_.track_seldepth ? "sd=" + std::to_string(move.seldepth) : "", //
pgn_config_.track_nps ? "nps=" + std::to_string(move.nps) : "", //
pgn_config_.track_hashfull ? "hashfull=" + std::to_string(move.hashfull) : "", //
pgn_config_.track_tbhits ? "tbhits=" + std::to_string(move.tbhits) : "", //
last ? match_.reason : "" //
);
}
}
Expand Down