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[]) {
67 auto args = parseArgs(argc, argv);
68 if (args.find(
"--nply") != args.end()) nPly = std::stoi(args[
"--nply"]);
69 if (args.find(
"--nrepeats") != args.end())
70 nRepeats = std::stoi(args[
"--nrepeats"]);
71 if (args.find(
"--filename") != args.end())
72 filename = args[
"--filename"];
74 filename =
"../times_" + std::to_string(nPly) +
"_ply_" +
75 std::to_string(nRepeats) +
"_pos.csv";
77 std::vector<std::tuple<float, float>> times = {};
79 using duration = std::chrono::duration<float>;
84 for (
auto i = 0; i < nRepeats; i++) {
85 auto [b, mvSequence] = BitBully::Board::randomBoard(nPly,
true);
88 auto tStart = Clock::now();
89 const int scoreBitbully = bb.mtdf(b, 0);
90 auto tEnd = Clock::now();
91 auto timeBitbully =
static_cast<float>(duration(tEnd - tStart).count());
97 std::accumulate(mvSequence.begin(), mvSequence.end(), std::string(
""),
98 [](
const std::string& a,
const int mv) {
99 return a + std::to_string(mv + 1);
101 if (P.
play(mvSequenceStr) != b.countTokens()) {
102 std::cerr <<
"Error: (P.play(mvSequenceStr) != b.countTokens())";
105 tStart = Clock::now();
106 const int scorePonsC4 = solverPonsC4.solve(P,
false);
108 auto timePonsC4 =
static_cast<float>(duration(tEnd - tStart).count());
109 times.emplace_back(timeBitbully, timePonsC4);
111 if (scorePonsC4 != scoreBitbully) {
112 std::cerr <<
"Error: " << b.toString() <<
"Pons-C4: " << scorePonsC4
113 <<
" BitBully: " << scoreBitbully << std::endl;
117 if (i % (std::max(nRepeats, 100) / 100) == 0) {
118 std::cout <<
"Done with " << i <<
" iterations" << std::endl;
121 writeToCSV(times, filename);
123 std::cout <<
"Node Count Pons-C4: " << solverPonsC4.getNodeCount() <<
", "
124 <<
"BitBully: " << bb.getNodeCounter() <<
" Percent: "
125 <<
static_cast<double>(bb.getNodeCounter() -
126 solverPonsC4.getNodeCount()) /
127 bb.getNodeCounter() * 100.0
128 <<
" %" << std::endl;
void play(position_t move)