generalized the position counter
Some checks failed
pre-release / Pre Release (push) Has been cancelled
tagged-release / Tagged Release (push) Has been cancelled

This commit is contained in:
Karma Riuk 2025-02-16 10:42:30 +01:00
parent f68dedeb20
commit 97f9def306
8 changed files with 25 additions and 32 deletions

View File

@ -4,7 +4,10 @@
#include <ostream> #include <ostream>
#include <thread> #include <thread>
static long int position_counter = 0;
Move ai::AI::search(const Board& b) { Move ai::AI::search(const Board& b) {
position_counter = 0;
Move result; Move result;
std::condition_variable cv; std::condition_variable cv;
@ -47,6 +50,13 @@ Move ai::AI::search(const Board& b) {
// Ensure timer thread is also stopped // Ensure timer thread is also stopped
timer_thread.join(); timer_thread.join();
std::cout << "Took " << elapsed << " ms" << std::endl; std::cout << "Took " << elapsed << " ms, " << "Looked at "
<< position_counter << " positions" << std::endl;
return result; return result;
} }
int ai::AI::eval(const Board& b) {
int ret = _eval(b);
position_counter++;
return ret;
}

View File

@ -20,8 +20,9 @@ namespace ai {
std::atomic<bool> stop_computation = false; std::atomic<bool> stop_computation = false;
Move search(const Board& b); Move search(const Board& b);
int eval(const Board&);
virtual int eval(const Board&) = 0; virtual int _eval(const Board&) = 0;
}; };
struct v0_random : public AI { struct v0_random : public AI {
@ -29,7 +30,7 @@ namespace ai {
Move _search(const Board&) override; Move _search(const Board&) override;
int eval(const Board&) override { int _eval(const Board&) override {
return 0; return 0;
}; };
}; };
@ -41,7 +42,7 @@ namespace ai {
v1_pure_minimax(bool w, std::chrono::milliseconds tt): AI(w, tt) {} v1_pure_minimax(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override; Move _search(const Board&) override;
int eval(const Board&) override; int _eval(const Board&) override;
}; };
class v2_alpha_beta : public AI { class v2_alpha_beta : public AI {
@ -52,7 +53,7 @@ namespace ai {
v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {} v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
virtual Move _search(const Board&) override; virtual Move _search(const Board&) override;
virtual int eval(const Board&) override; virtual int _eval(const Board&) override;
}; };
class v3_AB_ordering : public AI { class v3_AB_ordering : public AI {
@ -63,7 +64,7 @@ namespace ai {
v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {} v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
virtual Move _search(const Board&) override; virtual Move _search(const Board&) override;
virtual int eval(const Board&) override; virtual int _eval(const Board&) override;
}; };
class v4_search_captures : public v3_AB_ordering { class v4_search_captures : public v3_AB_ordering {
@ -86,7 +87,7 @@ namespace ai {
v5_better_endgame(bool w, std::chrono::milliseconds tt) v5_better_endgame(bool w, std::chrono::milliseconds tt)
: v4_search_captures(w, tt) {} : v4_search_captures(w, tt) {}
virtual int eval(const Board&) override; virtual int _eval(const Board&) override;
}; };
class v6_iterative_deepening : public v5_better_endgame { class v6_iterative_deepening : public v5_better_endgame {

View File

@ -7,10 +7,7 @@
#define MULTITHREADED 1 #define MULTITHREADED 1
static int position_counter = 0;
Move ai::v1_pure_minimax::_search(const Board& b) { Move ai::v1_pure_minimax::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
Move best_move; Move best_move;
@ -52,7 +49,6 @@ Move ai::v1_pure_minimax::_search(const Board& b) {
} }
} }
#endif #endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move; return best_move;
} }
@ -78,8 +74,7 @@ int ai::v1_pure_minimax::_search(const Board& b, int depth) {
return best_evaluation; return best_evaluation;
} }
int ai::v1_pure_minimax::eval(const Board& b) { int ai::v1_pure_minimax::_eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White); int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black); int black_eval = count_material(b, Colour::Black);

View File

@ -7,11 +7,7 @@
#define MULTITHREADED 1 #define MULTITHREADED 1
static int position_counter = 0;
Move ai::v2_alpha_beta::_search(const Board& b) { Move ai::v2_alpha_beta::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
Move best_move; Move best_move;
@ -53,7 +49,6 @@ Move ai::v2_alpha_beta::_search(const Board& b) {
} }
} }
#endif #endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move; return best_move;
} }
@ -79,8 +74,7 @@ int ai::v2_alpha_beta::_search(const Board& b, int depth, int alpha, int beta) {
return alpha; return alpha;
} }
int ai::v2_alpha_beta::eval(const Board& b) { int ai::v2_alpha_beta::_eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White); int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black); int black_eval = count_material(b, Colour::Black);

View File

@ -8,11 +8,7 @@
#define MULTITHREADED 1 #define MULTITHREADED 1
static int position_counter;
Move ai::v3_AB_ordering::_search(const Board& b) { Move ai::v3_AB_ordering::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
Move best_move; Move best_move;
@ -56,7 +52,6 @@ Move ai::v3_AB_ordering::_search(const Board& b) {
} }
} }
#endif #endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move; return best_move;
} }
@ -88,8 +83,7 @@ int ai::v3_AB_ordering::_ab_search(
return alpha; return alpha;
} }
int ai::v3_AB_ordering::eval(const Board& b) { int ai::v3_AB_ordering::_eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White); int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black); int black_eval = count_material(b, Colour::Black);

View File

@ -6,9 +6,6 @@
#define MULTITHREADED 1 #define MULTITHREADED 1
static int position_counter;
int ai::v4_search_captures::_ab_search( int ai::v4_search_captures::_ab_search(
const Board& b, int depth, int alpha, int beta const Board& b, int depth, int alpha, int beta
) { ) {

View File

@ -36,8 +36,8 @@ static float endgame_phase_weight(int material_count_no_pawns) {
return 1.f - std::min(1.f, material_count_no_pawns * multiplier); return 1.f - std::min(1.f, material_count_no_pawns * multiplier);
} }
int ai::v5_better_endgame::eval(const Board& b) { int ai::v5_better_endgame::_eval(const Board& b) {
int old_eval = v4_search_captures::eval(b); int old_eval = v4_search_captures::_eval(b);
Colour attacking_colour = b.white_to_play ? White : Black; Colour attacking_colour = b.white_to_play ? White : Black;
Colour defending_colour = b.white_to_play ? Black : White; Colour defending_colour = b.white_to_play ? Black : White;
return old_eval return old_eval

View File

@ -5,6 +5,8 @@
#include <map> #include <map>
static int position_counter = 0;
Move ai::v6_iterative_deepening::_search(const Board& b) { Move ai::v6_iterative_deepening::_search(const Board& b) {
ThreadPool pool(std::thread::hardware_concurrency()); ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();