8#include <thirdParty/connect4/Solver.hpp>
10#include <unordered_map>
17using Clock = std::chrono::steady_clock;
19using Clock = std::chrono::high_resolution_clock;
23void writeToCSV(
const std::vector<std::tuple<float, float>>& data,
24 const std::string& filename) {
25 std::ofstream file(filename);
26 if (!file.is_open()) {
27 std::cerr <<
"Error: Unable to open file " << filename << std::endl;
32 file <<
"Bitbully,Pons-C4\n";
35 for (
const auto& [val1, val2] : data) {
36 file << std::fixed << std::setprecision(5)
37 << val1 <<
"," << val2 <<
"\n";
41 std::cout <<
"Data successfully written to " << filename << std::endl;
44std::unordered_map<std::string, std::string> parseArgs(
45 const int argc,
const char*
const argv[]) {
46 std::unordered_map<std::string, std::string> args;
48 for (
int i = 1; i < argc; i += 2) {
50 args[argv[i]] = argv[i + 1];
52 std::cerr <<
"Error: Missing value for argument " << argv[i] << std::endl;
60int main(
const int argc,
const char*
const argv[]) {
68 auto args = parseArgs(argc, argv);
69 if (args.find(
"--nply") != args.end()) nPly = std::stoi(args[
"--nply"]);
70 if (args.find(
"--nrepeats") != args.end())
71 nRepeats = std::stoi(args[
"--nrepeats"]);
72 if (args.find(
"--filename") != args.end())
73 filename = args[
"--filename"];
75 filename =
"../times_" + std::to_string(nPly) +
"_ply_" +
76 std::to_string(nRepeats) +
"_pos.csv";
77 if (args.find(
"--reset_tt") !=
79 reset_tt = std::stoi(args[
"--reset_tt"]);
81 std::vector<std::tuple<float, float>> times = {};
83 using duration = std::chrono::duration<float>;
88 for (
auto i = 0; i < nRepeats; i++) {
89 auto [b, mvSequence] = BitBully::Board::randomBoard(nPly,
true);
91 if (reset_tt > 0 && i % reset_tt == 0) {
93 bb.resetTranspositionTable();
97 auto tStart = Clock::now();
98 const int scoreBitbully = bb.mtdf(b, 0);
99 auto tEnd = Clock::now();
100 auto timeBitbully =
static_cast<float>(duration(tEnd - tStart).count());
106 std::accumulate(mvSequence.begin(), mvSequence.end(), std::string(
""),
107 [](
const std::string& a,
const int mv) {
108 return a + std::to_string(mv + 1);
110 if (P.
play(mvSequenceStr) != b.countTokens()) {
111 std::cerr <<
"Error: (P.play(mvSequenceStr) != b.countTokens())";
114 tStart = Clock::now();
115 const int scorePonsC4 = solverPonsC4.solve(P,
false);
117 auto timePonsC4 =
static_cast<float>(duration(tEnd - tStart).count());
118 times.emplace_back(timeBitbully, timePonsC4);
120 if (scorePonsC4 != scoreBitbully) {
121 std::cerr <<
"Error: " << b.toString() <<
"Pons-C4: " << scorePonsC4
122 <<
" BitBully: " << scoreBitbully << std::endl;
126 if (i % (std::max(nRepeats, 100) / 100) == 0) {
127 std::cout <<
"Done with " << i <<
" iterations" << std::endl;
130 writeToCSV(times, filename);
132 std::cout <<
"Node Count Pons-C4: " << solverPonsC4.getNodeCount() <<
", "
133 <<
"BitBully: " << bb.getNodeCounter() <<
" Percent: "
134 <<
static_cast<double>(bb.getNodeCounter() -
135 solverPonsC4.getNodeCount()) /
136 bb.getNodeCounter() * 100.0
137 <<
" %" << std::endl;
void play(position_t move)