Skip to content

Commit

Permalink
add tt and small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Mika committed May 9, 2020
1 parent 072b72f commit b954dbd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
3 changes: 2 additions & 1 deletion TODOs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- test against TSCP in terms of strength, speed (move generation) etc.
look at https://chess.stackexchange.com/questions/12790/how-to-measure-strength-of-my-own-chess-engine

- extend search (PV, TT etc) and evaluation
- analyse and improve search by adding some techniques and also evaluation

- improve quiesce

- end game, opening book

13 changes: 6 additions & 7 deletions board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,6 @@ void printBoard2(const U64& b)

U64 Perft(chessBoard& board, int depth)
{
chessBoard boardCpy;
U64 nodes = 0;

if (depth == 0)
Expand All @@ -1425,10 +1424,10 @@ U64 Perft(chessBoard& board, int depth)
board.numOfMvs = 0;
board.genMoves(moves);

chessBoard boardCpy = board;

for (int i = 0; i < board.numOfMvs; i++)
{
boardCpy = board;

if (board.makeMove(moves[i]))
{
int tmp = Perft(board, depth - 1);
Expand Down Expand Up @@ -1472,7 +1471,7 @@ void debugMoveGen () {


// same positions, not as many moves
debugInput.push_back(debugItem("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
/*debugInput.push_back(debugItem("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
{20, 400, 8902, 197281 }));
debugInput.push_back(debugItem("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -",
{ 48, 2039, 97862 }));
Expand All @@ -1483,10 +1482,10 @@ void debugMoveGen () {
debugInput.push_back(debugItem("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8 ",
{ 44, 1486, 62379, 2103487 }));
debugInput.push_back(debugItem("r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10",
{ 46, 2079, 89890, 3894594 }));
{ 46, 2079, 89890, 3894594 }));*/


/*debugInput.push_back(debugItem("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
debugInput.push_back(debugItem("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
{20, 400, 8902, 197281, 4865609, 119060324 }));
debugInput.push_back(debugItem("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -",
{ 48, 2039, 97862, 4085603, 193690690 }));
Expand All @@ -1497,7 +1496,7 @@ void debugMoveGen () {
debugInput.push_back(debugItem("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8 ",
{ 44, 1486, 62379, 2103487, 89941194 }));
debugInput.push_back(debugItem("r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10",
{ 46, 2079, 89890, 3894594, 164075551 }));*/
{ 46, 2079, 89890, 3894594, 164075551 }));

std::vector<debugItem>::iterator it;
for (it = debugInput.begin(); it != debugInput.end(); it++)
Expand Down
44 changes: 36 additions & 8 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <set>
#include <map>

// value biger than max alpha/beta so safe to use it to
// signal that no valid entry in tt
#define NO_TT_ENTRY 22000000
#define NUM_OF_TT_ENTRIES 33554432

#define MAX_NUM_OF_MVS 250

enum { EP=1, CASTLING, PROMOTE_R, PROMOTE_N, PROMOTE_B, PROMOTE_Q };
Expand All @@ -23,6 +28,28 @@ enum {
typedef unsigned long long U64;


enum ttEntryType {ALPHA, BETA, FULL};

struct ttEntry {

U64 hash;
int depth;
ttEntryType eType;
int alpha;
int beta;

};

struct transpositionTable {

ttEntry table[NUM_OF_TT_ENTRIES];

void save(U64 hash, int depth, ttEntryType eType, int alpha, int beta);
int probe(U64 hash, int depth, int alpha, int beta);

};


struct pieceValue {

std::map<char,int> values;
Expand Down Expand Up @@ -191,9 +218,6 @@ struct magics {
};


extern magics mag;
extern pieceValue pValue;


// TODO: if use char here then need to convert char to int and vice-versa
// is it good solution ?
Expand Down Expand Up @@ -223,11 +247,6 @@ struct chessBoard {
int ep;
int fifty;

// TODO: is it enough ? what about castling etc ?
unsigned long long int squaresHash[64][6][2];
unsigned long long int sideHash;
unsigned long long int epHash[64];

unsigned long long int currPosHash;
std::vector<unsigned long long int> historyHash;

Expand Down Expand Up @@ -314,3 +333,12 @@ bool savePosInFEN(chessBoard& board, std::string& pos);
bool readInPosFromFEN(chessBoard& board, std::string pos);


extern magics mag;
extern pieceValue pValue;
extern transpositionTable tt;

// TODO: is it enough ? what about castling etc ?
extern unsigned long long int squaresHash[64][6][2];
extern unsigned long long int sideHash;
extern unsigned long long int epHash[64];

14 changes: 10 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,15 @@ void testSearch(){
readInPosFromFEN (board, pos);
board.populateOccup();
board.populateCharBoard();
board.calcHash();

for (int i = 0; i < 3; i++)
for (int i = 0; i < 5; i++)
{
clock_t begin = clock();

searchEngine se;
move m = se.doSearch(board, 3);
move m = se.doSearch(board, i+1);
//move m = se.doSearch(board, 2);

clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
Expand Down Expand Up @@ -461,14 +463,18 @@ void handler(int sig) {

magics mag;
pieceValue pValue;
transpositionTable tt;
unsigned long long int squaresHash[64][6][2];
unsigned long long int sideHash;
unsigned long long int epHash[64];

int main ()
{
signal(SIGSEGV, handler);

testSearch();
//testSearch();

//debugMoveGen();
debugMoveGen();
//testMoveGenSpeed();
//playDummyGame();
//engineVsEngine();
Expand Down
32 changes: 23 additions & 9 deletions search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,18 @@ move searchEngine::searchMain(chessBoard& board, int depth)
nodes++;

move m(0, 0, 0);
chessBoard boardCpy;
int score = -20000000, tmp;

move moves[MAX_NUM_OF_MVS];
board.numOfMvs = 0;
board.genMoves(moves);

chessBoard boardCpy = board;

for (int i = 0; i < board.numOfMvs; i++)
{
sortMoves(moves, board.numOfMvs, i);

boardCpy = board;

if (board.makeMove(moves[i]))
{
tmp = -search(board, depth - 1, -20000000, -score);
Expand All @@ -85,11 +84,20 @@ move searchEngine::searchMain(chessBoard& board, int depth)
board = boardCpy;
}

tt.save(board.currPosHash, depth, FULL, score, 20000000);

return m;
}

int searchEngine::search(chessBoard& board, int depth, int alpha, int beta)
{
//assert(alpha < beta);
int origAlpha = alpha;

int ttScore = tt.probe(board.currPosHash, depth, alpha, beta);
if (ttScore != NO_TT_ENTRY)
return ttScore;

if (depth == 0)
{
return quiesce(board, alpha, beta);
Expand Down Expand Up @@ -120,22 +128,24 @@ int searchEngine::search(chessBoard& board, int depth, int alpha, int beta)
board.numOfMvs = 0;
board.genMoves(moves);

chessBoard boardCpy;
chessBoard boardCpy = board;
int tmp = 0;

for (int i = 0; i < board.numOfMvs; i++)
{
sortMoves(moves, board.numOfMvs, i);

boardCpy = board;

if (board.makeMove(moves[i]))
{
playedMoves = true;

tmp = -search(board, depth - 1, -beta, -alpha);
if (tmp >= beta)
{
// needs to be boardCpy as board has been modified at this point
tt.save(boardCpy.currPosHash, depth, BETA, alpha, beta);
return beta;
}
if (tmp > alpha)
alpha = tmp;
}
Expand All @@ -158,6 +168,11 @@ int searchEngine::search(chessBoard& board, int depth, int alpha, int beta)
if (board.fifty >= 100)
return 0;

if (alpha == origAlpha)
tt.save(board.currPosHash, depth, ALPHA, alpha, beta);
else
tt.save(board.currPosHash, depth, FULL, alpha, beta);

return alpha;
}

Expand All @@ -183,6 +198,7 @@ int searchEngine::quiesce(chessBoard& board, int alpha, int beta ) {
// TODO have to modify it to handle mate etc
// TODO might be worth adding some margin
int standPat = eval(board);

if( standPat >= beta )
return beta;
if( alpha < standPat )
Expand All @@ -192,7 +208,7 @@ int searchEngine::quiesce(chessBoard& board, int alpha, int beta ) {
board.numOfMvs = 0;
board.genMoves(moves);

chessBoard boardCpy;
chessBoard boardCpy = board;
int tmp = 0;

// TODO add checks, promotions etc
Expand All @@ -205,8 +221,6 @@ int searchEngine::quiesce(chessBoard& board, int alpha, int beta ) {

if (board.charBoard[(int)(moves[i].to)] != ' ' || moves[i].flag == EP)
{
boardCpy = board;

if (board.makeMove(moves[i]))
{
tmp = -quiesce(board, -beta, -alpha);
Expand Down

0 comments on commit b954dbd

Please sign in to comment.