BitBully 0.0.39
Loading...
Searching...
No Matches
main.cpp
1#include <fstream>
2#include <iomanip> // For setting precision
3#include <iostream>
4#include <numeric>
5#include <sstream>
6#include <string>
7#include <thirdParty/connect4/Solver.hpp>
8#include <tuple>
9#include <unordered_map>
10#include <vector>
11
12#include "BitBully.h"
13#include "Board.h"
14
15void writeToCSV(const std::vector<std::tuple<float, float>>& data,
16 const std::string& filename) {
17 std::ofstream file(filename); // Open file for writing
18 if (!file.is_open()) {
19 std::cerr << "Error: Unable to open file " << filename << std::endl;
20 return;
21 }
22
23 // Write header
24 file << "Bitbully,Pons-C4\n";
25
26 // Write data
27 for (const auto& [val1, val2] : data) {
28 file << std::fixed << std::setprecision(5) // Control float precision
29 << val1 << "," << val2 << "\n";
30 }
31
32 file.close();
33 std::cout << "Data successfully written to " << filename << std::endl;
34}
35
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;
39
40 for (int i = 1; i < argc; i += 2) {
41 if (i + 1 < argc) {
42 args[argv[i]] = argv[i + 1];
43 } else {
44 std::cerr << "Error: Missing value for argument " << argv[i] << std::endl;
45 exit(EXIT_FAILURE);
46 }
47 }
48
49 return args;
50}
51
52int main(const int argc, const char* const argv[]) {
53 // Default values
54 int nPly = 8;
55 int nRepeats = 1000;
56 std::string filename;
57
58 // Parse command-line arguments
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"];
65 else
66 filename = "../times_" + std::to_string(nPly) + "_ply_" +
67 std::to_string(nRepeats) + "_pos.csv";
68
69 std::vector<std::tuple<float, float>> times = {};
70
71 using duration = std::chrono::duration<float>;
72
75
76 for (auto i = 0; i < nRepeats; i++) {
77 auto [b, mvSequence] = BitBully::Board::randomBoard(nPly, true);
78
79 // Bitbully:
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());
84
85 // Pons-C4:
87 // Convert move sequence into a string representation:
88 auto mvSequenceStr =
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);
92 });
93 if (P.play(mvSequenceStr) != b.countTokens()) {
94 std::cerr << "Error: (P.play(mvSequenceStr) != b.countTokens())";
95 exit(EXIT_FAILURE);
96 }
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);
102
103 if (scorePonsC4 != scoreBitbully) {
104 std::cerr << "Error: " << b.toString() << "Pons-C4: " << scorePonsC4
105 << " BitBully: " << scoreBitbully << std::endl;
106 exit(EXIT_FAILURE);
107 }
108
109 if (i % (std::max(nRepeats, 100) / 100) == 0) {
110 std::cout << "Done with " << i << " iterations" << std::endl;
111 }
112 }
113 writeToCSV(times, filename);
114
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;
121 return 0;
122}
void play(position_t move)
Definition Position.hpp:113