7#include <thirdParty/connect4/Solver.hpp>
9#include <unordered_map>
15void writeToCSV(
const std::vector<std::tuple<float, float>>& data,
16 const std::string& filename) {
17 std::ofstream file(filename);
18 if (!file.is_open()) {
19 std::cerr <<
"Error: Unable to open file " << filename << std::endl;
24 file <<
"Bitbully,Pons-C4\n";
27 for (
const auto& [val1, val2] : data) {
28 file << std::fixed << std::setprecision(5)
29 << val1 <<
"," << val2 <<
"\n";
33 std::cout <<
"Data successfully written to " << filename << std::endl;
36std::unordered_map<std::string, std::string> parseArgs(
37 const int argc,
const char*
const argv[]) {
38 std::unordered_map<std::string, std::string> args;
40 for (
int i = 1; i < argc; i += 2) {
42 args[argv[i]] = argv[i + 1];
44 std::cerr <<
"Error: Missing value for argument " << argv[i] << std::endl;
52int main(
const int argc,
const char*
const argv[]) {
59 auto args = parseArgs(argc, argv);
60 if (args.find(
"--nply") != args.end()) nPly = std::stoi(args[
"--nply"]);
61 if (args.find(
"--nrepeats") != args.end())
62 nRepeats = std::stoi(args[
"--nrepeats"]);
63 if (args.find(
"--filename") != args.end())
64 filename = args[
"--filename"];
66 filename =
"../times_" + std::to_string(nPly) +
"_ply_" +
67 std::to_string(nRepeats) +
"_pos.csv";
69 std::vector<std::tuple<float, float>> times = {};
71 using duration = std::chrono::duration<float>;
76 for (
auto i = 0; i < nRepeats; i++) {
77 auto [b, mvSequence] = BitBully::Board::randomBoard(nPly,
true);
80 auto tStart = std::chrono::high_resolution_clock::now();
81 const int scoreBitbully = bb.mtdf(b, 0);
82 auto tEnd = std::chrono::high_resolution_clock::now();
83 auto timeBitbully =
static_cast<float>(duration(tEnd - tStart).count());
89 std::accumulate(mvSequence.begin(), mvSequence.end(), std::string(
""),
90 [](
const std::string& a,
const int mv) {
91 return a + std::to_string(mv + 1);
93 if (P.
play(mvSequenceStr) != b.countTokens()) {
94 std::cerr <<
"Error: (P.play(mvSequenceStr) != b.countTokens())";
97 tStart = std::chrono::high_resolution_clock::now();
98 const int scorePonsC4 = solverPonsC4.solve(P,
false);
99 tEnd = std::chrono::high_resolution_clock::now();
100 auto timePonsC4 =
static_cast<float>(duration(tEnd - tStart).count());
101 times.emplace_back(timeBitbully, timePonsC4);
103 if (scorePonsC4 != scoreBitbully) {
104 std::cerr <<
"Error: " << b.toString() <<
"Pons-C4: " << scorePonsC4
105 <<
" BitBully: " << scoreBitbully << std::endl;
109 if (i % (std::max(nRepeats, 100) / 100) == 0) {
110 std::cout <<
"Done with " << i <<
" iterations" << std::endl;
113 writeToCSV(times, filename);
115 std::cout <<
"Node Count Pons-C4: " << solverPonsC4.getNodeCount() <<
", "
116 <<
"BitBully: " << bb.getNodeCounter() <<
" Percent: "
117 <<
static_cast<double>(bb.getNodeCounter() -
118 solverPonsC4.getNodeCount()) /
119 bb.getNodeCounter() * 100.0
120 <<
" %" << std::endl;
void play(position_t move)