Compare commits
No commits in common. "f68dedeb2087394512b5760d3d226290255b52c0" and "62a35ecafdf81d2d6462349d2e64ad35bb79458f" have entirely different histories.
f68dedeb20
...
62a35ecafd
@ -27,8 +27,7 @@ int main(int argc, char* argv[]) {
|
|||||||
// 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::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;
|
||||||
|
@ -46,32 +46,31 @@ 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)
|
||||||
virtual int _search(const Board&, int, int, int);
|
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) {}
|
||||||
|
|
||||||
virtual Move _search(const Board&) override;
|
Move _search(const Board&) override;
|
||||||
virtual int eval(const Board&) override;
|
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 _ab_search(const Board&, int, int, int);
|
virtual int _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) {}
|
||||||
|
|
||||||
virtual Move _search(const Board&) override;
|
Move _search(const Board&) override;
|
||||||
virtual int eval(const Board&) override;
|
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
|
||||||
virtual int _ab_search(const Board&, int, int, int) override;
|
int _search(const Board&, int, int, int) override;
|
||||||
virtual int _search_captures(const Board&, int, int);
|
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)
|
||||||
@ -86,18 +85,6 @@ 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;
|
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
|
||||||
|
@ -25,11 +25,9 @@ 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(
|
futures.insert({move, pool.enqueue([&, tmp_board]() {
|
||||||
{move, pool.enqueue([&, tmp_board]() {
|
return _search(tmp_board, 3, -INFINITY, INFINITY);
|
||||||
return _ab_search(tmp_board, 3, -INFINITY, INFINITY);
|
})});
|
||||||
})}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
@ -60,7 +58,7 @@ Move ai::v3_AB_ordering::_search(const Board& b) {
|
|||||||
return best_move;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v3_AB_ordering::_ab_search(
|
int ai::v3_AB_ordering::_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)
|
||||||
@ -80,7 +78,7 @@ int ai::v3_AB_ordering::_ab_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 = -_ab_search(tmp_board, depth - 1, -beta, -alpha);
|
int tmp_eval = -_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);
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
static int position_counter;
|
static int position_counter;
|
||||||
|
|
||||||
int ai::v4_search_captures::_ab_search(
|
int ai::v4_search_captures::_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::_ab_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 = -_ab_search(tmp_board, depth - 1, -beta, -alpha);
|
int tmp_eval = -_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);
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user