made v1_simple look 4 moves into the future, it's

parallelized
This commit is contained in:
Karma Riuk 2025-02-07 00:32:34 +01:00
parent c71cabb3cd
commit 2764787e63
2 changed files with 35 additions and 11 deletions

View File

@ -10,21 +10,26 @@
#include <chrono>
int main(int argc, char* argv[]) {
// std::string pos =
// "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 4 3";
Board b = Board::setup_fen_position(pos);
ai::v0_random p1(true, std::chrono::milliseconds(500));
ai::v0_random p2(false, std::chrono::milliseconds(500));
// ai::v0_random p1(true, std::chrono::milliseconds(500));
ai::v1_simple p1(false, std::chrono::milliseconds(100000));
// ai::v1_simple p2(false, std::chrono::milliseconds(100000));
// ai::v0_random p2(false, std::chrono::milliseconds(10000));
GUI gui;
AIvsAIController manual(b, gui, p1, p2);
// AIvsAIController manual(b, gui, p1, p2);
HumanVsAIController manual(b, gui, p1);
Controller& controller = manual;
controller.start();
// perft();
// perft(pos);
return 0;
}

View File

@ -2,18 +2,29 @@
#include "../utils/threadpool.hpp"
#include "ai.hpp"
#include <map>
static int INFINITY = std::numeric_limits<int>::max();
int position_counter;
Move ai::v1_simple::_search(const Board& b) {
ThreadPool pool(std::thread::hardware_concurrency());
Move best_move;
int best_eval = -INFINITY;
std::vector<Move> moves = b.all_legal_moves();
std::map<Move, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int eval = _search(tmp_board, 4);
futures.insert({move, pool.enqueue([&, tmp_board]() {
return _search(tmp_board, 3);
})});
}
Move best_move;
int best_eval = -INFINITY;
for (auto& [move, future] : futures) {
int eval = future.get();
if (!am_white)
eval *= -1;
if (eval > best_eval) {
@ -22,18 +33,22 @@ Move ai::v1_simple::_search(const Board& b) {
}
}
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move;
}
int ai::v1_simple::_search(const Board& b, int depth) {
if (b.is_terminal()) {
if (b.is_checkmate_for(b.white_to_play ? White : Black))
return -INFINITY;
return 0;
}
if (depth == 0 || stop_computation)
return eval(b);
std::vector<Move> moves = b.all_legal_moves();
int best_evaluation = 0;
int best_evaluation = -INFINITY;
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
@ -69,12 +84,16 @@ int count_material(const Board& b, int8_t colour) {
case Piece::Queen:
ret += QueenValue;
break;
case Piece::King:
case Piece::None:
break;
}
}
return ret;
}
int ai::v1_simple::eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);