Skip to content

Commit

Permalink
refactor: add helper methods to stats (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin authored Sep 30, 2024
1 parent e45ea86 commit 83caa1d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 32 deletions.
7 changes: 3 additions & 4 deletions app/src/elo/elo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class EloBase {
virtual ~EloBase() = default;

[[nodiscard]] std::string getElo() const noexcept;
[[nodiscard]] virtual std::string los() const noexcept = 0;
[[nodiscard]] virtual std::string drawRatio(const Stats& stats) const noexcept = 0;
[[nodiscard]] virtual std::string printScore() const noexcept = 0;
[[nodiscard]] virtual std::string nElo() const noexcept = 0;
[[nodiscard]] virtual std::string los() const noexcept = 0;
[[nodiscard]] virtual std::string printScore() const noexcept = 0;
[[nodiscard]] virtual std::string nElo() const noexcept = 0;

double scoreToEloDiff(double score) noexcept { return -400.0 * std::log10(1.0 / score - 1.0); }

Expand Down
6 changes: 0 additions & 6 deletions app/src/elo/elo_pentanomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ std::string EloPentanomial::los() const noexcept {
return ss.str();
}

std::string EloPentanomial::drawRatio(const Stats& stats) const noexcept {
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << ((stats.penta_WL + stats.penta_DD) / pairs_) * 100.0 << " %";
return ss.str();
}

std::string EloPentanomial::printScore() const noexcept {
std::stringstream ss;
ss << std::fixed << std::setprecision(3) << score_;
Expand Down
1 change: 0 additions & 1 deletion app/src/elo/elo_pentanomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class EloPentanomial : public EloBase {
EloPentanomial(const Stats& stats);

[[nodiscard]] std::string los() const noexcept override;
[[nodiscard]] std::string drawRatio(const Stats& stats) const noexcept override;
[[nodiscard]] std::string printScore() const noexcept override;
[[nodiscard]] std::string nElo() const noexcept override;

Expand Down
6 changes: 0 additions & 6 deletions app/src/elo/elo_wdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ std::string EloWDL::los() const noexcept {
return ss.str();
}

std::string EloWDL::drawRatio(const Stats& stats) const noexcept {
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << ((stats.draws) / games_) * 100.0 << " %";
return ss.str();
}

std::string EloWDL::printScore() const noexcept {
std::stringstream ss;
ss << std::fixed << std::setprecision(3) << score_;
Expand Down
1 change: 0 additions & 1 deletion app/src/elo/elo_wdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class EloWDL : public EloBase {
EloWDL(const Stats& stats);

[[nodiscard]] std::string los() const noexcept override;
[[nodiscard]] std::string drawRatio(const Stats& stats) const noexcept override;
[[nodiscard]] std::string printScore() const noexcept override;
[[nodiscard]] std::string nElo() const noexcept override;

Expand Down
3 changes: 1 addition & 2 deletions app/src/matchmaking/output/output_cutechess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class Cutechess : public IOutput {
const std::string&) override {
const elo::EloWDL elo(stats);

return fmt::format("Elo difference: {}, LOS: {}, DrawRatio: {}\n", elo.getElo(), elo.los(),
elo.drawRatio(stats));
return fmt::format("Elo difference: {}, LOS: {}, DrawRatio: {}\n", elo.getElo(), elo.los(), stats.drawRatio());
}

std::string printSprt(const SPRT& sprt, const Stats& stats) override {
Expand Down
18 changes: 6 additions & 12 deletions app/src/matchmaking/output/output_fastchess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,12 @@ class Fastchess : public IOutput {
const auto hash = formatHash(first_engine.uciOptions(), second_engine.uciOptions());
const auto bookname = getShortName(book);

const auto games = stats.wins + stats.losses + stats.draws;
const auto points = stats.wins + 0.5 * stats.draws;
const auto pointsRatio = points / games * 100;
const auto WLDDRatio = static_cast<double>(stats.penta_WL) / stats.penta_DD;
const auto pairsRatio =
static_cast<double>(stats.penta_WW + stats.penta_WD) / (stats.penta_LD + stats.penta_LL);

auto result =
fmt::format("{}\n{}\n{}\n{}", formatMatchup(first, second, tc, threads, hash, bookname), formatElo(elo),
formatGameStats(*elo, stats, pairsRatio), formatGameResults(stats, points, pointsRatio));
auto result = fmt::format("{}\n{}\n{}\n{}", formatMatchup(first, second, tc, threads, hash, bookname),
formatElo(elo), formatGameStats(*elo, stats, stats.pairsRatio()),
formatGameResults(stats, stats.points(), stats.pointsRatio()));

if (report_penta_) {
result += fmt::format("\n{}", formatPentaStats(stats, WLDDRatio));
result += fmt::format("\n{}", formatPentaStats(stats, stats.wldDRatio()));
}

return result + "\n";
Expand Down Expand Up @@ -152,7 +145,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),
return fmt::format("LOS: {}, DrawRatio: {}{}", elo.los(),
report_penta_ ? stats.drawRatioPenta() : stats.drawRatio(),
report_penta_ ? fmt::format(", PairsRatio: {:.2f}", pairsRatio) : "");
}

Expand Down
16 changes: 16 additions & 0 deletions app/src/matchmaking/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ class Stats {

[[nodiscard]] int sum() const { return wins + losses + draws; }

[[nodiscard]] double points() const { return wins + 0.5 * draws; }

[[nodiscard]] double wldDRatio() const { return static_cast<double>(penta_WL) / static_cast<double>(penta_DD); }

[[nodiscard]] double drawRatio() const { return 100.0 * draws / static_cast<double>(sum()); }

[[nodiscard]] double drawRatioPenta() const {
return ((penta_WL + penta_DD) / static_cast<double>(totalPairs())) * 100.0;
}

[[nodiscard]] double pairsRatio() const { return static_cast<double>(penta_WW + penta_WD) / (penta_LD + penta_LL); }

[[nodiscard]] double pointsRatio() const { return points() / sum() * 100; }

[[nodiscard]] int totalPairs() const { return penta_WW + penta_WD + penta_WL + penta_DD + penta_LD + penta_LL; }

int wins = 0;
int losses = 0;
int draws = 0;
Expand Down

0 comments on commit 83caa1d

Please sign in to comment.