Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
97b3eeb984 | |||
982f933698 | |||
d21fcde21c |
@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
#include "board.hpp"
|
|
||||||
#include "stickfosh.hpp"
|
#include "stickfosh.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -6,7 +5,7 @@
|
|||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
std::string pos =
|
std::string pos =
|
||||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
// Board b = Board::setup_fen_position(pos);
|
std::string move = search(pos, 4);
|
||||||
perft();
|
std::cout << move << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
187
cpp/src/perft.cpp
Normal file
187
cpp/src/perft.cpp
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
#include "perft.hpp"
|
||||||
|
|
||||||
|
#include "board.hpp"
|
||||||
|
#include "move.hpp"
|
||||||
|
#include "threadpool.hpp"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <map>
|
||||||
|
#include <ostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
static std::string tick = "\033[92m✔️\033[0m"; // Green Tick;
|
||||||
|
static std::string cross = "\033[91m❌\033[0m"; // Red Cross
|
||||||
|
|
||||||
|
static std::map<std::string, std::map<int, int>> pos2expected{
|
||||||
|
// -- Position 1
|
||||||
|
{
|
||||||
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||||
|
{
|
||||||
|
{1, 20}, // 0
|
||||||
|
{2, 400}, // 1
|
||||||
|
{3, 8902}, // 38
|
||||||
|
{4, 197281}, // 971
|
||||||
|
// {5, 4865609}, // 23032
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 2
|
||||||
|
{
|
||||||
|
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 2",
|
||||||
|
{
|
||||||
|
{1, 48}, // 0
|
||||||
|
{2, 2039}, // 16
|
||||||
|
{3, 97862}, // 602
|
||||||
|
// {4, 4085603}, // 26612
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 3
|
||||||
|
{
|
||||||
|
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 3",
|
||||||
|
{
|
||||||
|
{1, 14}, // 0
|
||||||
|
{2, 191}, // 1
|
||||||
|
{3, 2812}, // 11
|
||||||
|
{4, 43238}, // 157
|
||||||
|
{5, 674624}, // 2199
|
||||||
|
// {6, 11030083},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 4a
|
||||||
|
{
|
||||||
|
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 4",
|
||||||
|
{
|
||||||
|
{1, 6}, // 0
|
||||||
|
{2, 264}, // 1
|
||||||
|
{3, 9467}, // 69
|
||||||
|
{4, 422333}, // 3085
|
||||||
|
// {5, 15833292}, // 124452
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 4b
|
||||||
|
{
|
||||||
|
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5",
|
||||||
|
{
|
||||||
|
{1, 6}, // 0
|
||||||
|
{2, 264}, // 2
|
||||||
|
{3, 9467}, // 104
|
||||||
|
{4, 422333}, // 3742
|
||||||
|
// {5, 15833292}, // 136784
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 5
|
||||||
|
{
|
||||||
|
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 6",
|
||||||
|
{
|
||||||
|
{1, 44}, // 0
|
||||||
|
{2, 1486}, // 12
|
||||||
|
{3, 62379}, // 357
|
||||||
|
// {4, 2103487}, // 13804
|
||||||
|
// {5, 89941194}, // 1230428
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// -- Position 6
|
||||||
|
{
|
||||||
|
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 "
|
||||||
|
"7",
|
||||||
|
{
|
||||||
|
{1, 46}, // 0
|
||||||
|
{2, 2079}, // 16
|
||||||
|
{3, 89890}, // 602
|
||||||
|
// {4, 3894594}, // 26612
|
||||||
|
// {5, 164075551}, // 1230428
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::stringstream res;
|
||||||
|
|
||||||
|
int move_generation_test(
|
||||||
|
const Board& b, int depth, int max_depth, ThreadPool& pool
|
||||||
|
) {
|
||||||
|
if (depth == max_depth) {
|
||||||
|
res.str("");
|
||||||
|
res.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b.is_terminal())
|
||||||
|
return 0;
|
||||||
|
if (depth == 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
if (depth == 1)
|
||||||
|
return moves.size();
|
||||||
|
|
||||||
|
int num_pos = 0;
|
||||||
|
|
||||||
|
if (depth == max_depth) {
|
||||||
|
// Parallel execution at the top level
|
||||||
|
std::vector<std::future<int>> futures;
|
||||||
|
for (const Move& move : moves) {
|
||||||
|
Board tmp_board = b.make_move(move);
|
||||||
|
futures.push_back(pool.enqueue(
|
||||||
|
move_generation_test,
|
||||||
|
tmp_board,
|
||||||
|
depth - 1,
|
||||||
|
max_depth,
|
||||||
|
std::ref(pool)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& future : futures)
|
||||||
|
num_pos += future.get(); // Retrieve the result of each task
|
||||||
|
} else {
|
||||||
|
// Regular sequential execution
|
||||||
|
for (const Move& move : moves) {
|
||||||
|
Board tmp_board = b.make_move(move);
|
||||||
|
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
|
||||||
|
if (depth == max_depth)
|
||||||
|
res << move << ": " << n << std::endl;
|
||||||
|
num_pos += n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return num_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void perft() {
|
||||||
|
for (auto& [key, value] : pos2expected)
|
||||||
|
perft(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void perft(std::string pos) {
|
||||||
|
std::cout << pos << std::endl;
|
||||||
|
std::map<int, int> expected = pos2expected[pos];
|
||||||
|
Board b = Board::setup_fen_position(pos);
|
||||||
|
ThreadPool pool(std::thread::hardware_concurrency());
|
||||||
|
|
||||||
|
for (const auto& [depth, expected_n_moves] : expected) {
|
||||||
|
std::cout << "Depth: " << depth << " " << std::flush;
|
||||||
|
auto start = std::chrono::steady_clock::now();
|
||||||
|
int moves = move_generation_test(b, depth, depth, pool);
|
||||||
|
auto end = std::chrono::steady_clock::now();
|
||||||
|
auto elapsed =
|
||||||
|
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
||||||
|
.count();
|
||||||
|
|
||||||
|
std::cout << "Results: " << moves << " ";
|
||||||
|
if (moves == expected_n_moves)
|
||||||
|
std::cout << tick << " ";
|
||||||
|
else
|
||||||
|
std::cout << cross << " (expected " << expected_n_moves << ") ";
|
||||||
|
std::cout << "positions Time: " << elapsed << " milliseconds"
|
||||||
|
<< std::endl;
|
||||||
|
if (moves != expected_n_moves) {
|
||||||
|
std::cout << std::endl << res.str();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
cpp/src/perft.hpp
Normal file
6
cpp/src/perft.hpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void perft(std::string pos);
|
||||||
|
void perft();
|
@ -1,187 +1,96 @@
|
|||||||
#include "stickfosh.hpp"
|
#include "stickfosh.hpp"
|
||||||
|
|
||||||
#include "board.hpp"
|
#include "pieces/piece.hpp"
|
||||||
#include "move.hpp"
|
|
||||||
#include "threadpool.hpp"
|
#include "threadpool.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <future>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <ostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
static std::string tick = "\033[92m✔️\033[0m"; // Green Tick;
|
static int INFINITY = std::numeric_limits<int>::max();
|
||||||
static std::string cross = "\033[91m❌\033[0m"; // Red Cross
|
|
||||||
|
|
||||||
static std::map<std::string, std::map<int, int>> pos2expected{
|
std::string search(std::string pos, int depth) {
|
||||||
// -- Position 1
|
|
||||||
{
|
|
||||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
|
||||||
{
|
|
||||||
{1, 20}, // 0
|
|
||||||
{2, 400}, // 1
|
|
||||||
{3, 8902}, // 38
|
|
||||||
{4, 197281}, // 971
|
|
||||||
// {5, 4865609}, // 23032
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 2
|
|
||||||
{
|
|
||||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 2",
|
|
||||||
{
|
|
||||||
{1, 48}, // 0
|
|
||||||
{2, 2039}, // 16
|
|
||||||
{3, 97862}, // 602
|
|
||||||
// {4, 4085603}, // 26612
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 3
|
|
||||||
{
|
|
||||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 3",
|
|
||||||
{
|
|
||||||
{1, 14}, // 0
|
|
||||||
{2, 191}, // 1
|
|
||||||
{3, 2812}, // 11
|
|
||||||
{4, 43238}, // 157
|
|
||||||
{5, 674624}, // 2199
|
|
||||||
// {6, 11030083},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 4a
|
|
||||||
{
|
|
||||||
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 4",
|
|
||||||
{
|
|
||||||
{1, 6}, // 0
|
|
||||||
{2, 264}, // 1
|
|
||||||
{3, 9467}, // 69
|
|
||||||
{4, 422333}, // 3085
|
|
||||||
// {5, 15833292}, // 124452
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 4b
|
|
||||||
{
|
|
||||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5",
|
|
||||||
{
|
|
||||||
{1, 6}, // 0
|
|
||||||
{2, 264}, // 2
|
|
||||||
{3, 9467}, // 104
|
|
||||||
{4, 422333}, // 3742
|
|
||||||
// {5, 15833292}, // 136784
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 5
|
|
||||||
{
|
|
||||||
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 6",
|
|
||||||
{
|
|
||||||
{1, 44}, // 0
|
|
||||||
{2, 1486}, // 12
|
|
||||||
{3, 62379}, // 357
|
|
||||||
// {4, 2103487}, // 13804
|
|
||||||
// {5, 89941194}, // 1230428
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// -- Position 6
|
|
||||||
{
|
|
||||||
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 "
|
|
||||||
"7",
|
|
||||||
{
|
|
||||||
{1, 46}, // 0
|
|
||||||
{2, 2079}, // 16
|
|
||||||
{3, 89890}, // 602
|
|
||||||
// {4, 3894594}, // 26612
|
|
||||||
// {5, 164075551}, // 1230428
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static std::stringstream res;
|
|
||||||
|
|
||||||
int move_generation_test(
|
|
||||||
const Board& b, int depth, int max_depth, ThreadPool& pool
|
|
||||||
) {
|
|
||||||
if (depth == max_depth) {
|
|
||||||
res.str("");
|
|
||||||
res.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b.is_terminal())
|
|
||||||
return 0;
|
|
||||||
if (depth == 0)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
|
||||||
if (depth == 1)
|
|
||||||
return moves.size();
|
|
||||||
|
|
||||||
int num_pos = 0;
|
|
||||||
|
|
||||||
if (depth == max_depth) {
|
|
||||||
// Parallel execution at the top level
|
|
||||||
std::vector<std::future<int>> futures;
|
|
||||||
for (const Move& move : moves) {
|
|
||||||
Board tmp_board = b.make_move(move);
|
|
||||||
futures.push_back(pool.enqueue(
|
|
||||||
move_generation_test,
|
|
||||||
tmp_board,
|
|
||||||
depth - 1,
|
|
||||||
max_depth,
|
|
||||||
std::ref(pool)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& future : futures)
|
|
||||||
num_pos += future.get(); // Retrieve the result of each task
|
|
||||||
} else {
|
|
||||||
// Regular sequential execution
|
|
||||||
for (const Move& move : moves) {
|
|
||||||
Board tmp_board = b.make_move(move);
|
|
||||||
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
|
|
||||||
if (depth == max_depth)
|
|
||||||
res << move << ": " << n << std::endl;
|
|
||||||
num_pos += n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return num_pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
void perft() {
|
|
||||||
for (auto& [key, value] : pos2expected)
|
|
||||||
perft(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void perft(std::string pos) {
|
|
||||||
std::cout << pos << std::endl;
|
|
||||||
std::map<int, int> expected = pos2expected[pos];
|
|
||||||
Board b = Board::setup_fen_position(pos);
|
Board b = Board::setup_fen_position(pos);
|
||||||
|
|
||||||
ThreadPool pool(std::thread::hardware_concurrency());
|
ThreadPool pool(std::thread::hardware_concurrency());
|
||||||
|
|
||||||
for (const auto& [depth, expected_n_moves] : expected) {
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
std::cout << "Depth: " << depth << " " << std::flush;
|
std::map<std::string, std::future<int>> futures;
|
||||||
auto start = std::chrono::steady_clock::now();
|
for (const Move& move : moves) {
|
||||||
int moves = move_generation_test(b, depth, depth, pool);
|
Board tmp_board = b.make_move(move);
|
||||||
auto end = std::chrono::steady_clock::now();
|
futures.insert(
|
||||||
auto elapsed =
|
{move.to_string(), pool.enqueue(minimax, tmp_board, depth - 1)}
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
);
|
||||||
.count();
|
}
|
||||||
|
|
||||||
std::cout << "Results: " << moves << " ";
|
std::string best_move;
|
||||||
if (moves == expected_n_moves)
|
int best_eval = -INFINITY;
|
||||||
std::cout << tick << " ";
|
for (auto& [move, future] : futures) {
|
||||||
else
|
int eval = future.get();
|
||||||
std::cout << cross << " (expected " << expected_n_moves << ") ";
|
if (eval > best_eval) {
|
||||||
std::cout << "positions Time: " << elapsed << " milliseconds"
|
best_eval = eval;
|
||||||
<< std::endl;
|
best_move = move;
|
||||||
if (moves != expected_n_moves) {
|
|
||||||
std::cout << std::endl << res.str();
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return best_move;
|
||||||
|
}
|
||||||
|
|
||||||
|
int minimax(const Board& b, int depth) {
|
||||||
|
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
||||||
|
return -INFINITY;
|
||||||
|
|
||||||
|
if (depth == 0)
|
||||||
|
return eval(b);
|
||||||
|
|
||||||
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
int best_evaluation = 0;
|
||||||
|
Move best_move;
|
||||||
|
for (const Move& move : moves) {
|
||||||
|
Board tmp_board = b.make_move(move);
|
||||||
|
int tmp_eval = -minimax(tmp_board, depth - 1);
|
||||||
|
best_evaluation = std::max(best_evaluation, tmp_eval);
|
||||||
|
}
|
||||||
|
return best_evaluation;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int PawnValue = 100;
|
||||||
|
static int KnightValue = 300;
|
||||||
|
static int BishopValue = 320;
|
||||||
|
static int RookValue = 500;
|
||||||
|
static int QueenValue = 900;
|
||||||
|
|
||||||
|
int count_material(const Board& b, int8_t colour) {
|
||||||
|
int ret = 0;
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
if (b.colour_at(i) == colour)
|
||||||
|
switch (b.squares[i] & 0b111) {
|
||||||
|
case Piece::Pawn:
|
||||||
|
ret += PawnValue;
|
||||||
|
break;
|
||||||
|
case Piece::Knigt:
|
||||||
|
ret += KnightValue;
|
||||||
|
break;
|
||||||
|
case Piece::Bishop:
|
||||||
|
ret += BishopValue;
|
||||||
|
break;
|
||||||
|
case Piece::Rook:
|
||||||
|
ret += RookValue;
|
||||||
|
break;
|
||||||
|
case Piece::Queen:
|
||||||
|
ret += QueenValue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eval(const Board& b) {
|
||||||
|
int white_eval = count_material(b, Colour::White);
|
||||||
|
int black_eval = count_material(b, Colour::Black);
|
||||||
|
|
||||||
|
int evaluation = white_eval - black_eval;
|
||||||
|
|
||||||
|
int perspective = b.white_to_play ? 1 : -1;
|
||||||
|
|
||||||
|
return perspective * evaluation;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "board.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void perft(std::string pos);
|
std::string search(std::string, int);
|
||||||
void perft();
|
int minimax(const Board&, int);
|
||||||
|
int eval(const Board&);
|
||||||
|
Reference in New Issue
Block a user