3 Commits

Author SHA1 Message Date
f68dedeb20 made v6, does iterative deepening until the
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled
thinking time runs out
2025-02-16 10:32:15 +01:00
4eae988999 fixed warnings 2025-02-16 10:32:09 +01:00
62a35ecafd made ai better at endgames (it's just a start)
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled
2025-02-16 10:12:06 +01:00
9 changed files with 151 additions and 27 deletions

View File

@ -13,24 +13,27 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// std::string pos = // std::string pos =
// "r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 "; // "r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 ";
// std::string pos = "8/6K1/5P2/8/1k6/8/8/8 w - - 0 1"; std::string pos = "3r4/3r4/3k4/8/3K4/8/8/8 w - - 0 1";
// pos for ai timing< // pos for ai timing<
std::string pos = // std::string pos =
"r3k2r/p1ppqpb1/Bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPB1PPP/R3K2R b KQkq - 0 3"; // "r3k2r/p1ppqpb1/Bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPB1PPP/R3K2R b KQkq - 0
// 3";
Board b = Board::setup_fen_position(pos); Board b = Board::setup_fen_position(pos);
ai::v0_random p1(true, std::chrono::milliseconds(1000)); ai::v0_random p1(true, std::chrono::milliseconds(1000));
// ai::v1_pure_minimax p2(false, std::chrono::milliseconds(20000)); // ai::v1_pure_minimax p2(false, std::chrono::milliseconds(20000));
// ai::v2_alpha_beta p2(false, std::chrono::milliseconds(20000)); // ai::v2_alpha_beta p2(false, std::chrono::milliseconds(20000));
ai::v3_AB_ordering p2(false, std::chrono::milliseconds(20000)); // ai::v3_AB_ordering p2(false, std::chrono::milliseconds(20000));
// ai::v4_search_captures p2(false, std::chrono::milliseconds(20000)); // ai::v4_search_captures p2(false, std::chrono::milliseconds(20000));
// ai::v5_better_endgame p2(false, std::chrono::milliseconds(20000));
ai::v6_iterative_deepening p2(false, std::chrono::milliseconds(2000));
// GUI gui; GUI gui;
NoOpView gui; // NoOpView gui;
AIvsAIController manual(b, gui, p1, p2); // AIvsAIController manual(b, gui, p1, p2);
// HumanVsAIController manual(b, gui, p2); HumanVsAIController manual(b, gui, p2);
Controller& controller = manual; Controller& controller = manual;

View File

@ -46,34 +46,58 @@ namespace ai {
class v2_alpha_beta : public AI { class v2_alpha_beta : public AI {
// looks two moves ahead, with alpha-beta pruning (no move ordering) // looks two moves ahead, with alpha-beta pruning (no move ordering)
int _search(const Board&, int, int, int); virtual int _search(const Board&, int, int, int);
public: public:
v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {} v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override; virtual Move _search(const Board&) override;
int eval(const Board&) override; virtual int eval(const Board&) override;
}; };
class v3_AB_ordering : public AI { class v3_AB_ordering : public AI {
// looks two moves ahead, with alpha-beta pruning, with move ordering // looks two moves ahead, with alpha-beta pruning, with move ordering
virtual int _search(const Board&, int, int, int); virtual int _ab_search(const Board&, int, int, int);
public: public:
v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {} v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override; virtual Move _search(const Board&) override;
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 {
protected:
// same as v3, but looking at only at captures when leaf is reached, // same as v3, but looking at only at captures when leaf is reached,
// until no captures are left // until no captures are left
int _search(const Board&, int, int, int) override; virtual int _ab_search(const Board&, int, int, int) override;
int _search_captures(const Board&, int, int); virtual int _search_captures(const Board&, int, int);
public: public:
v4_search_captures(bool w, std::chrono::milliseconds tt) v4_search_captures(bool w, std::chrono::milliseconds tt)
: v3_AB_ordering(w, tt) {} : v3_AB_ordering(w, tt) {}
}; };
class v5_better_endgame : public v4_search_captures {
// same as v4, but with a better evaluation function, that forces the
// king towards the corner of the board for endgames
public:
v5_better_endgame(bool w, std::chrono::milliseconds tt)
: v4_search_captures(w, tt) {}
virtual int eval(const Board&) override;
};
class v6_iterative_deepening : public v5_better_endgame {
// same as v5, but instead of just looking 2 moves ahead, it does
// iterative depening until and keeps on searching until the thinking
// time runs out
public:
v6_iterative_deepening(bool w, std::chrono::milliseconds tt)
: v5_better_endgame(w, tt) {}
virtual Move _search(const Board&) override;
};
} // namespace ai } // namespace ai

View File

@ -25,9 +25,11 @@ Move ai::v3_AB_ordering::_search(const Board& b) {
std::map<Move, std::future<int>> futures; std::map<Move, std::future<int>> futures;
for (const Move& move : moves) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); Board tmp_board = b.make_move(move);
futures.insert({move, pool.enqueue([&, tmp_board]() { futures.insert(
return _search(tmp_board, 3, -INFINITY, INFINITY); {move, pool.enqueue([&, tmp_board]() {
})}); return _ab_search(tmp_board, 3, -INFINITY, INFINITY);
})}
);
} }
int counter = 0; int counter = 0;
@ -58,7 +60,7 @@ Move ai::v3_AB_ordering::_search(const Board& b) {
return best_move; return best_move;
} }
int ai::v3_AB_ordering::_search( int ai::v3_AB_ordering::_ab_search(
const Board& b, int depth, int alpha, int beta const Board& b, int depth, int alpha, int beta
) { ) {
if (depth == 0 || stop_computation) if (depth == 0 || stop_computation)
@ -78,7 +80,7 @@ int ai::v3_AB_ordering::_search(
Move best_move; Move best_move;
for (const Move& move : moves) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1, -beta, -alpha); int tmp_eval = -_ab_search(tmp_board, depth - 1, -beta, -alpha);
if (tmp_eval >= beta) if (tmp_eval >= beta)
return beta; return beta;
alpha = std::max(alpha, tmp_eval); alpha = std::max(alpha, tmp_eval);

View File

@ -9,7 +9,7 @@
static int position_counter; static int position_counter;
int ai::v4_search_captures::_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
) { ) {
if (depth == 0 || stop_computation) if (depth == 0 || stop_computation)
@ -29,7 +29,7 @@ int ai::v4_search_captures::_search(
Move best_move; Move best_move;
for (const Move& move : moves) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1, -beta, -alpha); int tmp_eval = -_ab_search(tmp_board, depth - 1, -beta, -alpha);
if (tmp_eval >= beta) if (tmp_eval >= beta)
return beta; return beta;
alpha = std::max(alpha, tmp_eval); alpha = std::max(alpha, tmp_eval);

View File

@ -0,0 +1,52 @@
#include "../pieces/piece.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <algorithm>
static int force_king_to_corner(
int8_t attacking_king, int8_t defending_king, float endgame_weight
) {
int eval = 0;
Coords def_xy = Coords::from_index(defending_king);
Coords def_dist_to_center{
std::max(3 - def_xy.x, def_xy.x - 4),
std::max(3 - def_xy.y, def_xy.y - 4)
};
eval += def_dist_to_center.x + def_dist_to_center.y;
// make attacking king go closer to defending king to cut off escape routes
Coords attack_xy = Coords::from_index(attacking_king);
Coords dist_between_kings{
std::abs(attack_xy.x - def_xy.x),
std::abs(attack_xy.y - def_xy.y)
};
int distance = dist_between_kings.x + dist_between_kings.y;
eval += 14 - distance;
return (int) (eval * 10 * endgame_weight);
}
static float endgame_phase_weight(int material_count_no_pawns) {
static int endgame_material_start =
RookValue * 2 + BishopValue + KnightValue;
float multiplier = 1.f / endgame_material_start;
return 1.f - std::min(1.f, material_count_no_pawns * multiplier);
}
int ai::v5_better_endgame::eval(const Board& b) {
int old_eval = v4_search_captures::eval(b);
Colour attacking_colour = b.white_to_play ? White : Black;
Colour defending_colour = b.white_to_play ? Black : White;
return old_eval
+ force_king_to_corner(
b.get_king_of(attacking_colour),
b.get_king_of(defending_colour),
endgame_phase_weight(
count_material(b, attacking_colour, false)
+ count_material(b, defending_colour, false)
)
);
}

View File

@ -0,0 +1,40 @@
#include "../pieces/piece.hpp"
#include "../utils/threadpool.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <map>
Move ai::v6_iterative_deepening::_search(const Board& b) {
ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves();
Move best_move;
int best_eval = -INFINITY;
std::map<Move, std::future<int>> futures;
for (int depth = 1; !stop_computation; depth++) {
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert(
{move, pool.enqueue([&, tmp_board]() {
return _ab_search(tmp_board, depth, -INFINITY, INFINITY);
})}
);
}
int counter = 0;
for (auto& [move, future] : futures) {
int eval = future.get();
counter++;
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
futures.clear();
}
return best_move;
}

View File

@ -8,7 +8,6 @@
struct Board { struct Board {
private: private:
int8_t get_king_of(int8_t) const;
bool _no_legal_moves_for(Colour) const; bool _no_legal_moves_for(Colour) const;
bool _is_check_for(Colour) const; bool _is_check_for(Colour) const;
bool nlm = false, check = false; bool nlm = false, check = false;
@ -25,6 +24,7 @@ struct Board {
static Board setup_fen_position(std::string fen); static Board setup_fen_position(std::string fen);
int8_t get_king_of(int8_t) const;
Board skip_turn() const; Board skip_turn() const;
Board make_move(Move, bool = true) const; Board make_move(Move, bool = true) const;
std::string to_fen() const; std::string to_fen() const;

View File

@ -26,10 +26,13 @@ int piece_value(Piece p) {
} }
} }
int count_material(const Board& b, int8_t colour) { int count_material(const Board& b, int8_t colour, bool count_pawns) {
int ret = 0; int ret = 0;
for (int i = 0; i < 64; i++) for (int i = 0; i < 64; i++) {
if (b.piece_at(i) == Pawn && !count_pawns)
continue;
if (b.colour_at(i) == colour) if (b.colour_at(i) == colour)
ret += piece_value(b.piece_at(i)); ret += piece_value(b.piece_at(i));
}
return ret; return ret;
} }

View File

@ -7,7 +7,7 @@
#include <vector> #include <vector>
std::vector<int8_t> to_target_square(std::vector<Move>); std::vector<int8_t> to_target_square(std::vector<Move>);
int count_material(const Board&, int8_t); int count_material(const Board&, int8_t, bool = true);
int piece_value(Piece); int piece_value(Piece);
const int INFINITY = std::numeric_limits<int>::max(); const int INFINITY = std::numeric_limits<int>::max();