1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
//-----------------------------------------------------------------------------
/** @file twogtp/Analyze.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "Analyze.h"
#include <fstream>
#include <map>
#include "libboardgame_base/FmtSaver.h"
#include "libboardgame_base/Statistics.h"
#include "libboardgame_base/StringUtil.h"
using namespace std;
using libboardgame_base::from_string;
using libboardgame_base::split;
using libboardgame_base::trim;
using libboardgame_base::FmtSaver;
using libboardgame_base::Statistics;
using libboardgame_base::StatisticsExt;
//-----------------------------------------------------------------------------
namespace {
void write_result(const Statistics<>& stat)
{
FmtSaver saver(cout);
cout << fixed << setprecision(1) << stat.get_mean() * 100 << "+/-"
<< stat.get_error() * 100;
}
} // namespace
//-----------------------------------------------------------------------------
void analyze(const string& file)
{
FmtSaver saver(cout);
ifstream in(file);
Statistics<> stat_result;
map<unsigned, Statistics<>> stat_result_player;
map<double, unsigned> result_count;
StatisticsExt<> stat_length;
StatisticsExt<> stat_cpu_b;
StatisticsExt<> stat_cpu_w;
StatisticsExt<> stat_fast_open;
string line;
while (getline(in, line))
{
line = trim(line);
if (! line.empty() && line[0] == '#')
continue;
auto columns = split(line, '\t');
if (columns.empty())
continue;
double result;
unsigned length;
unsigned player;
double cpu_b;
double cpu_w;
unsigned fast_open;
if (columns.size() != 7
|| ! from_string(columns[1], result)
|| ! from_string(columns[2], length)
|| ! from_string(columns[3], player)
|| ! from_string(columns[4], cpu_b)
|| ! from_string(columns[5], cpu_w)
|| ! from_string(columns[6], fast_open))
throw runtime_error("invalid format");
stat_result.add(result);
stat_result_player[player].add(result);
++result_count[result];
stat_length.add(length);
stat_cpu_b.add(cpu_b);
stat_cpu_w.add(cpu_w);
stat_fast_open.add(fast_open);
}
auto count = stat_result.get_count();
cout << "Gam " << count;
if (count == 0)
{
cout << '\n';
return;
}
cout << ", Res ";
write_result(stat_result);
cout << " (";
bool is_first = true;
for (auto& i : stat_result_player)
{
if (! is_first)
cout << ", ";
else
is_first = false;
cout << i.first << ": ";
write_result(i.second);
}
cout << ")\nResFreq";
for (auto& i : result_count)
{
cout << ' ' << i.first << "=";
{
FmtSaver saver(cout);
auto fraction = i.second / count;
cout << fixed << setprecision(1) << fraction * 100
<< "+/-" << sqrt(fraction * (1 - fraction) / count) * 100;
}
}
cout << "\nCpuB ";
stat_cpu_b.write(cout, true, 3, false, true);
cout << "\nCpuW ";
stat_cpu_w.write(cout, true, 3, false, true);
auto cpu_b = stat_cpu_b.get_mean();
auto cpu_w = stat_cpu_w.get_mean();
auto err_cpu_b = stat_cpu_b.get_error();
auto err_cpu_w = stat_cpu_w.get_error();
cout << "\nCpuB/CpuW ";
if (cpu_b > 0 && cpu_w > 0)
cout << fixed << setprecision(3) << cpu_b / cpu_w << "+/-"
<< cpu_b / cpu_w * hypot(err_cpu_b / cpu_b, err_cpu_w / cpu_w);
else
cout << "-";
cout << ", Len ";
stat_length.write(cout, true, 1, true, true);
if (stat_fast_open.get_mean() > 0)
{
cout << ", Fast ";
stat_fast_open.write(cout, true, 1, true, true);
}
cout << '\n';
}
//-----------------------------------------------------------------------------
|