made v1_simple look 4 moves into the future, it's
parallelized
This commit is contained in:
parent
c71cabb3cd
commit
2764787e63
@ -10,21 +10,26 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
// std::string pos =
|
||||||
|
// "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
std::string pos =
|
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);
|
Board b = Board::setup_fen_position(pos);
|
||||||
|
|
||||||
ai::v0_random p1(true, std::chrono::milliseconds(500));
|
// ai::v0_random p1(true, std::chrono::milliseconds(500));
|
||||||
ai::v0_random p2(false, 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;
|
GUI gui;
|
||||||
AIvsAIController manual(b, gui, p1, p2);
|
// AIvsAIController manual(b, gui, p1, p2);
|
||||||
|
HumanVsAIController manual(b, gui, p1);
|
||||||
|
|
||||||
Controller& controller = manual;
|
Controller& controller = manual;
|
||||||
|
|
||||||
controller.start();
|
controller.start();
|
||||||
|
|
||||||
// perft();
|
// perft(pos);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,29 @@
|
|||||||
#include "../utils/threadpool.hpp"
|
#include "../utils/threadpool.hpp"
|
||||||
#include "ai.hpp"
|
#include "ai.hpp"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
static int INFINITY = std::numeric_limits<int>::max();
|
static int INFINITY = std::numeric_limits<int>::max();
|
||||||
|
|
||||||
|
int position_counter;
|
||||||
|
|
||||||
Move ai::v1_simple::_search(const Board& b) {
|
Move ai::v1_simple::_search(const Board& b) {
|
||||||
ThreadPool pool(std::thread::hardware_concurrency());
|
ThreadPool pool(std::thread::hardware_concurrency());
|
||||||
|
|
||||||
Move best_move;
|
|
||||||
int best_eval = -INFINITY;
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
|
||||||
|
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);
|
||||||
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)
|
if (!am_white)
|
||||||
eval *= -1;
|
eval *= -1;
|
||||||
if (eval > best_eval) {
|
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;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v1_simple::_search(const Board& b, int depth) {
|
int ai::v1_simple::_search(const Board& b, int depth) {
|
||||||
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
if (b.is_terminal()) {
|
||||||
return -INFINITY;
|
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
||||||
|
return -INFINITY;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (depth == 0 || stop_computation)
|
if (depth == 0 || stop_computation)
|
||||||
return eval(b);
|
return eval(b);
|
||||||
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
int best_evaluation = 0;
|
int best_evaluation = -INFINITY;
|
||||||
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);
|
||||||
@ -69,12 +84,16 @@ int count_material(const Board& b, int8_t colour) {
|
|||||||
case Piece::Queen:
|
case Piece::Queen:
|
||||||
ret += QueenValue;
|
ret += QueenValue;
|
||||||
break;
|
break;
|
||||||
|
case Piece::King:
|
||||||
|
case Piece::None:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v1_simple::eval(const Board& b) {
|
int ai::v1_simple::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);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user