BitBully 0.0.56-a6
Loading...
Searching...
No Matches
main.cpp
1#include <chrono>
2#include <fstream>
3#include <iomanip> // For setting precision
4#include <iostream>
5#include <numeric>
6#include <sstream>
7#include <string>
8#include <thirdParty/connect4/Solver.hpp>
9#include <tuple>
10#include <unordered_map>
11#include <vector>
12
13#include "BitBully.h"
14#include "Board.h"
15
16#ifdef _WIN32 // Check if we're on a Windows platform
17using Clock = std::chrono::steady_clock; // Use steady_clock on Windows
18#else
19using Clock = std::chrono::high_resolution_clock; // Use high_resolution_clock
20 // on other platforms
21#endif
22
23void writeToCSV(const std::vector<std::tuple<float, float>>& data,
24 const std::string& filename) {
25 std::ofstream file(filename); // Open file for writing
26 if (!file.is_open()) {
27 std::cerr << "Error: Unable to open file " << filename << std::endl;
28 return;
29 }
30
31 // Write header
32 file << "Bitbully,Pons-C4\n";
33
34 // Write data
35 for (const auto& [val1, val2] : data) {
36 file << std::fixed << std::setprecision(5) // Control float precision
37 << val1 << "," << val2 << "\n";
38 }
39
40 file.close();
41 std::cout << "Data successfully written to " << filename << std::endl;
42}
43
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;
47
48 for (int i = 1; i < argc; i += 2) {
49 if (i + 1 < argc) {
50 args[argv[i]] = argv[i + 1];
51 } else {
52 std::cerr << "Error: Missing value for argument " << argv[i] << std::endl;
53 exit(EXIT_FAILURE);
54 }
55 }
56
57 return args;
58}
59
60int main(const int argc, const char* const argv[]) {
61 // Default values
62 int nPly = 8;
63 int nRepeats = 1000;
64 std::string filename;
65
66 // Parse command-line arguments
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"];
73 else
74 filename = "../times_" + std::to_string(nPly) + "_ply_" +
75 std::to_string(nRepeats) + "_pos.csv";
76
77 std::vector<std::tuple<float, float>> times = {};
78
79 using duration = std::chrono::duration<float>;
80
83
84 for (auto i = 0; i < nRepeats; i++) {
85 auto [b, mvSequence] = BitBully::Board::randomBoard(nPly, true);
86
87 // Bitbully:
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());
92
93 // Pons-C4:
95 // Convert move sequence into a string representation:
96 auto mvSequenceStr =
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);
100 });
101 if (P.play(mvSequenceStr) != b.countTokens()) {
102 std::cerr << "Error: (P.play(mvSequenceStr) != b.countTokens())";
103 exit(EXIT_FAILURE);
104 }
105 tStart = Clock::now();
106 const int scorePonsC4 = solverPonsC4.solve(P, false);
107 tEnd = Clock::now();
108 auto timePonsC4 = static_cast<float>(duration(tEnd - tStart).count());
109 times.emplace_back(timeBitbully, timePonsC4);
110
111 if (scorePonsC4 != scoreBitbully) {
112 std::cerr << "Error: " << b.toString() << "Pons-C4: " << scorePonsC4
113 << " BitBully: " << scoreBitbully << std::endl;
114 exit(EXIT_FAILURE);
115 }
116
117 if (i % (std::max(nRepeats, 100) / 100) == 0) {
118 std::cout << "Done with " << i << " iterations" << std::endl;
119 }
120 }
121 writeToCSV(times, filename);
122
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;
129 return 0;
130}
void play(position_t move)
Definition Position.hpp:113