Skip to content

Commit

Permalink
Merge pull request #46 from RomainGoussault/uciImprovement
Browse files Browse the repository at this point in the history
add uci info
pv info is still to do
  • Loading branch information
RomainGoussault authored Dec 5, 2016
2 parents 1e7d40f + 96e9597 commit b3310d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
66 changes: 44 additions & 22 deletions src/Search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ int Search::negaMax(const int depth, int alpha, const int beta, const bool isNul
myPvTable[myPly][i] = myPvTable[myPly+1][i] ; // copy the pv from deeper ply
}
myPvLength[myPly] = myPvLength[myPly+1];
// std::cout << "table at ply = " << myPly << std::endl;
// printPvTable(myPly+1);
}
}
}
Expand Down Expand Up @@ -341,8 +339,6 @@ int Search::negaMaxRoot(const int depth)
myPvTable[myPly][i] = myPvTable[myPly+1][i] ; // copy the pv from the deeper ply
}
myPvLength[myPly] = myPvLength[myPly+1]; // update the length of the PV to the one assigned
// std::cout << "table at ply = " << myPly << std::endl;
// printPvTable(myPly+1);
}
}

Expand All @@ -351,23 +347,26 @@ int Search::negaMaxRoot(const int depth)
return alpha;
}

int Search::negaMaxRootIterativeDeepening(const int allocatedTimeMS)
int Search::negaMaxRootIterativeDeepening(const unsigned int allocatedTimeMS)
{
clearSearchData();

int alpha = -999999;
int beta = -alpha;
int score = 0;
int minDepth = 2;
unsigned int minDepth = 2;

myMovesSearched = 0;
myPly=0;
myPvLength[myPly] = myPly; // Current length of the local PV

//Starting time
std::chrono::high_resolution_clock::time_point startTime =
std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point time;
auto dur = time - startTime;

int depth = 1;
myDepth = 1;

while(true)
{
Expand All @@ -376,15 +375,14 @@ int Search::negaMaxRootIterativeDeepening(const int allocatedTimeMS)
score = 0;
bool isPvs = false;

std::chrono::high_resolution_clock::time_point time = std::chrono::high_resolution_clock::now();
auto dur = time - startTime;
int durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();


//check for time
if(durationMS > 0.5 * allocatedTimeMS && depth >= minDepth) return alpha;

if (mySearchDurationMS > 0.5 * allocatedTimeMS && myDepth >= minDepth)
{
return alpha;
}
auto currentKey = myBoard->key;
auto ttEntry = globalTT.probeTT(currentKey, depth); // returns non nullpr if key exists and depth is greater
auto ttEntry = globalTT.probeTT(currentKey, myDepth); // returns non nullpr if key exists and depth is greater

if(ttEntry) // we have a match in the transposition table with a greater depth
{
Expand Down Expand Up @@ -413,9 +411,10 @@ int Search::negaMaxRootIterativeDeepening(const int allocatedTimeMS)
// Check for time fist
time = std::chrono::high_resolution_clock::now();
dur = time - startTime;
durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
mySearchDurationMS = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();

if(durationMS > allocatedTimeMS && depth >= minDepth && isMovelistHalfDone){
if(mySearchDurationMS > allocatedTimeMS && myDepth >= minDepth && isMovelistHalfDone)
{
return alpha;
}

Expand All @@ -424,16 +423,16 @@ int Search::negaMaxRootIterativeDeepening(const int allocatedTimeMS)
myPly++;

if (isPvs) {
score = -negaMax(depth - 1, -alpha - 1, -alpha);
score = -negaMax(myDepth - 1, -alpha - 1, -alpha);

if ((score > alpha) && (score < beta)) // Check for failure.
{
score = -negaMax(depth - 1, -beta, -alpha);
score = -negaMax(myDepth - 1, -beta, -alpha);
}
}
else
{
score = -negaMax(depth - 1, -beta, -alpha);
score = -negaMax(myDepth - 1, -beta, -alpha);
}

if( score > alpha )
Expand All @@ -448,15 +447,25 @@ int Search::negaMaxRootIterativeDeepening(const int allocatedTimeMS)
}
myPvLength[myPly] = myPvLength[myPly+1];
}

myBoard->undoMove(currentMove);
myEval.rewindEvalAttributes(currentMove);
myPly--;
}
}


globalTT.setTTEntry(myBoard->key, depth, alpha, NodeType::EXACT, myBestMove, myBoard->getPly());
globalTT.setTTEntry(myBoard->key, myDepth, alpha, NodeType::EXACT, myBestMove, myBoard->getPly());
}

depth++;
//update duration and send info to UCI
time = std::chrono::high_resolution_clock::now();
dur = time - startTime;

mySearchDurationMS = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
mySearchDurationMS = std::max(mySearchDurationMS, (unsigned int) 1);
sendInfoToUCI(alpha);

myDepth++;
}

return alpha;
Expand All @@ -468,6 +477,19 @@ int Search::evaluate()
return (-2*myBoard->getColorToPlay() + 1)*myEval.evaluate(); //evaluate()/* returns +evaluate for WHITE, -evaluate for BLACK */
}

void Search::sendInfoToUCI(int alpha)
{
// Send info to uci console
std::cout << "info";
std::cout << " depth " << myDepth;
std::cout << " score " << alpha;
std::cout << " nodes " << myMovesSearched;
unsigned int nps = 1000 * myMovesSearched / mySearchDurationMS;
std::cout << " nps " << nps;
std::cout << " time " << mySearchDurationMS;
// std::cout << " pv "; TODO
std::cout << std::endl;
}

void Search::printPvTable(const unsigned int numLines)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Search
void clearSearchData();

int negaMaxRoot(const int depth);
int negaMaxRootIterativeDeepening(const int timeSec);
int negaMaxRootIterativeDeepening(const unsigned int allocatedTimeMS);
inline int getCurrentScore() {return myEval.evaluate();};
bool isInsufficentMatingMaterial() const;
Move16 myBestMove;
Expand All @@ -46,7 +46,8 @@ class Search
U64 myMovesSearched; // TODO Count nodes not moves
inline Move getPVMove(const unsigned int ply, const unsigned int depth) const {return myPvTable[ply][depth];}
void printPvTable(const unsigned numLines);

unsigned int mySearchDurationMS;
unsigned int myDepth;
private:

std::shared_ptr<Board> myBoard;
Expand All @@ -58,6 +59,7 @@ class Search
int negaMax(const int depth, int alpha, const int beta, const bool isNullMoveAuth);
int evaluate();
int qSearch(int alpha, const int beta);
void sendInfoToUCI(int alpha);

Move myPvTable[MAX_DEPTH][MAX_DEPTH]; // Quadratic PV-table
unsigned int myPvLength[MAX_DEPTH]; // Length of the PV line at this ply
Expand Down
2 changes: 1 addition & 1 deletion src/Uci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Uci
{
public :

Uci() : wtime(1000), btime(1000), winc(0), binc(0), movestogo(0),
Uci() : wtime(10000), btime(10000), winc(0), binc(0), movestogo(0),
myBoardPtr(std::shared_ptr<Board>(new Board())), mySearch(Search(myBoardPtr)), myOptionsMap()
{
//Init Options map
Expand Down

0 comments on commit b3310d9

Please sign in to comment.