Commit 8a4015aa authored by Gaurav Kukreja's avatar Gaurav Kukreja

Removed unnecessary printf from pv-split-interactive

parent 683e2cd4
...@@ -26,67 +26,68 @@ extern int num_threads; ...@@ -26,67 +26,68 @@ extern int num_threads;
void ABIDStrategy::searchBestMove() void ABIDStrategy::searchBestMove()
{ {
int alpha = -15000, beta = 15000; int alpha = -15000, beta = 15000;
int nalpha, nbeta, currentValue = 0; int nalpha, nbeta, currentValue = 0;
_pv.clear(_maxDepth); _pv.clear(_maxDepth);
_currentBestMove.type = Move::none; _currentBestMove.type = Move::none;
_currentMaxDepth=1; _currentMaxDepth=1;
/* iterative deepening loop */ /* iterative deepening loop */
do { do {
/* searches on same level with different alpha/beta windows */
while(1) {
nalpha = alpha, nbeta = beta;
_inPV = (_pv[0].type != Move::none);
if (_sc && _sc->verbose()) {
char tmp[100];
sprintf(tmp, "Alpha/Beta [%d;%d] with max depth %d", alpha, beta, _currentMaxDepth);
_sc->substart(tmp);
}
currentValue = pv_split(alpha, beta);
if (_sc && _sc->verbose())
printf("Subsearch finished with currentValue = %d\n", currentValue);
/* stop searching if a win position is found */
if (currentValue > 14900 || currentValue < -14900)
_stopSearch = true;
/* Don't break out if we haven't found a move */
if (_currentBestMove.type == Move::none)
_stopSearch = false;
if (_stopSearch) break;
/* if result is outside of current alpha/beta window,
* the search has to be rerun with widened alpha/beta
*/
if (currentValue <= nalpha) {
alpha = -15000;
if (beta<15000) beta = currentValue+1;
continue;
}
if (currentValue >= nbeta) {
if (alpha > -15000) alpha = currentValue-1;
beta=15000;
continue;
}
break;
}
/* searches on same level with different alpha/beta windows */ /* Window in both directions cause of deepening */
while(1) { alpha = currentValue - 200, beta = currentValue + 200;
nalpha = alpha, nbeta = beta; if (_stopSearch) break;
_inPV = (_pv[0].type != Move::none);
if (_sc && _sc->verbose()) { _currentMaxDepth++;
char tmp[100];
sprintf(tmp, "Alpha/Beta [%d;%d] with max depth %d", alpha, beta, _currentMaxDepth);
_sc->substart(tmp);
}
currentValue = pv_split(alpha, beta);
printf("Subsearch finished with currentValue = %d\n", currentValue);
/* stop searching if a win position is found */
if (currentValue > 14900 || currentValue < -14900)
_stopSearch = true;
/* Don't break out if we haven't found a move */
if (_currentBestMove.type == Move::none)
_stopSearch = false;
if (_stopSearch) break;
/* if result is outside of current alpha/beta window,
* the search has to be rerun with widened alpha/beta
*/
if (currentValue <= nalpha) {
alpha = -15000;
if (beta<15000) beta = currentValue+1;
continue;
}
if (currentValue >= nbeta) {
if (alpha > -15000) alpha = currentValue-1;
beta=15000;
continue;
}
break;
} }
while(_currentMaxDepth <= _maxDepth);
/* Window in both directions cause of deepening */
alpha = currentValue - 200, beta = currentValue + 200;
if (_stopSearch) break;
_currentMaxDepth++; _bestMove = _currentBestMove;
}
while(_currentMaxDepth <= _maxDepth);
_bestMove = _currentBestMove;
} }
int ABIDStrategy::pv_split(int alpha0, int beta0) int ABIDStrategy::pv_split(int alpha0, int beta0)
...@@ -94,7 +95,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -94,7 +95,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
bool cutoff; bool cutoff;
int depth; int depth;
int value; int value;
int currentValue = -15000; int currentValue = -15000;
int slave_id; int slave_id;
int num_slaves; int num_slaves;
int pending_jobs; int pending_jobs;
...@@ -114,7 +115,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -114,7 +115,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
alpha[0] = alpha0; alpha[0] = alpha0;
beta[0] = beta0; beta[0] = beta0;
_currentBestMove.type = Move::none; _currentBestMove.type = Move::none;
// Play moves from the pv until you reach the lowest level // Play moves from the pv until you reach the lowest level
// If pv-moves are not possible use random moves // If pv-moves are not possible use random moves
...@@ -123,13 +124,13 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -123,13 +124,13 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
while (depth < (_currentMaxDepth-1)) while (depth < (_currentMaxDepth-1))
{ {
list.clear(); list.clear();
_board->generateMoves(list); _board->generateMoves(list);
m = _pv[depth]; m = _pv[depth];
// check if pv-move is possible // check if pv-move is possible
if ((m.type != Move::none) && (!list.isElement(m, 0, false))) if ((m.type != Move::none) && (!list.isElement(m, 0, false)))
m.type = Move::none; m.type = Move::none;
// get random move if pv-move is not possible // get random move if pv-move is not possible
if (m.type == Move::none) if (m.type == Move::none)
list.getNext(m, Move::none); list.getNext(m, Move::none);
...@@ -147,7 +148,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -147,7 +148,7 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
// Devide the work at each level // Devide the work at each level
depth = _currentMaxDepth-1; depth = _currentMaxDepth-1;
while (depth >= 0) while (depth >= 0)
{ {
slave_id = 0; slave_id = 0;
list.clear(); list.clear();
_board->generateMoves(list); _board->generateMoves(list);
...@@ -161,13 +162,12 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -161,13 +162,12 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
slave_input.depth = depth+1; slave_input.depth = depth+1;
slave_input.currentMaxDepth = _currentMaxDepth; slave_input.currentMaxDepth = _currentMaxDepth;
printf("Thread 0 testing %d moves at depth = %d\n",list.getLength(), depth);
while ( list.getNext(m, Move::none) ) while ( list.getNext(m, Move::none) )
{ {
slave_input.move = m; slave_input.move = m;
MPI_Send(&slave_input, sizeof(Slave_Input), MPI_BYTE, slave_id+1, 10, MPI_COMM_WORLD); MPI_Send(&slave_input, sizeof(Slave_Input), MPI_BYTE, slave_id+1, 10, MPI_COMM_WORLD);
MPI_Irecv(&slave_output[slave_id], sizeof(Slave_Output), MPI_BYTE, slave_id+1, MPI_Irecv(&slave_output[slave_id], sizeof(Slave_Output), MPI_BYTE, slave_id+1,
10, MPI_COMM_WORLD, &rcv_rq[slave_id]); 10, MPI_COMM_WORLD, &rcv_rq[slave_id]);
slave_id++; slave_id++;
...@@ -181,37 +181,37 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -181,37 +181,37 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
while (pending_jobs > 0) while (pending_jobs > 0)
{ {
MPI_Waitany(num_slaves, rcv_rq, &slave_id, MPI_STATUS_IGNORE); MPI_Waitany(num_slaves, rcv_rq, &slave_id, MPI_STATUS_IGNORE);
_sc->_leavesVisited += slave_output[slave_id].num_leaves; _sc->_leavesVisited += slave_output[slave_id].num_leaves;
_sc->_nodesVisited += slave_output[slave_id].num_nodes; _sc->_nodesVisited += slave_output[slave_id].num_nodes;
value = slave_output[slave_id].eval; value = slave_output[slave_id].eval;
if (value > currentValue) if (value > currentValue)
{
currentValue = value;
_pv = slave_output[slave_id].pv;
if (_sc)
_sc->foundBestMove(depth, _pv[depth], currentValue);
if (currentValue > alpha[depth])
{ {
currentValue = value; alpha[depth] = currentValue;
_pv = slave_output[slave_id].pv; slave_input.beta = -alpha[depth];
if (_sc)
_sc->foundBestMove(depth, _pv[depth], currentValue);
if (currentValue > alpha[depth])
{
alpha[depth] = currentValue;
slave_input.beta = -alpha[depth];
}
/* alpha/beta cut off or win position ... */
if (currentValue>14900 || currentValue >= beta[depth])
cutoff = true;
} }
/* alpha/beta cut off or win position ... */
if (currentValue>14900 || currentValue >= beta[depth])
cutoff = true;
}
if ((list.getNext(m, Move::none)) && (cutoff == false)) if ((list.getNext(m, Move::none)) && (cutoff == false))
{ {
slave_input.move = m; slave_input.move = m;
MPI_Send(&slave_input, sizeof(Slave_Input), MPI_BYTE, slave_id+1, 10, MPI_COMM_WORLD); MPI_Send(&slave_input, sizeof(Slave_Input), MPI_BYTE, slave_id+1, 10, MPI_COMM_WORLD);
MPI_Irecv(&slave_output[slave_id], sizeof(Slave_Output), MPI_BYTE, slave_id+1, MPI_Irecv(&slave_output[slave_id], sizeof(Slave_Output), MPI_BYTE, slave_id+1,
10, MPI_COMM_WORLD, &rcv_rq[slave_id]); 10, MPI_COMM_WORLD, &rcv_rq[slave_id]);
} }
else else
pending_jobs--; pending_jobs--;
} }
if (depth > 0) if (depth > 0)
...@@ -223,11 +223,11 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -223,11 +223,11 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
alpha[depth-1] = currentValue; alpha[depth-1] = currentValue;
} }
if (depth == 0) if (depth == 0)
_currentBestMove = _pv[0]; _currentBestMove = _pv[0];
if (_sc) if (_sc)
_sc->finishedNode(depth, _pv.chain(depth)); _sc->finishedNode(depth, _pv.chain(depth));
depth--; depth--;
} }
free(slave_output); free(slave_output);
free(rcv_rq); free(rcv_rq);
...@@ -247,88 +247,88 @@ int ABIDStrategy::pv_split(int alpha0, int beta0) ...@@ -247,88 +247,88 @@ int ABIDStrategy::pv_split(int alpha0, int beta0)
*/ */
int ABIDStrategy::alphabeta(int depth, int alpha, int beta) int ABIDStrategy::alphabeta(int depth, int alpha, int beta)
{ {
int currentValue = -14999+depth, value; int currentValue = -14999+depth, value;
Move m; Move m;
MoveList list; MoveList list;
bool depthPhase, doDepthSearch; bool depthPhase, doDepthSearch;
/* We make a depth search for the following move types... */
int maxType = (depth < _currentMaxDepth-1) ? Move::maxMoveType :
(depth < _currentMaxDepth) ? Move::maxPushType :
Move::maxOutType;
_board->generateMoves(list);
if (_sc && _sc->verbose()) {
char tmp[100];
sprintf(tmp, "Alpha/Beta [%d;%d], %d moves (%d depth)", alpha, beta,
list.count(Move::none), list.count(maxType));
_sc->startedNode(depth, tmp);
}
// don't use moves from the principal variation
m.type = Move::none;
// first, play all moves with depth search
depthPhase = true;
while (1) {
// get next move
if (m.type == Move::none) {
if (depthPhase)
depthPhase = list.getNext(m, maxType);
if (!depthPhase)
if (!list.getNext(m, Move::none)) break;
}
// we could start with a non-depth move from principal variation
doDepthSearch = depthPhase && (m.type <= maxType);
_board->playMove(m); /* We make a depth search for the following move types... */
int maxType = (depth < _currentMaxDepth-1) ? Move::maxMoveType :
(depth < _currentMaxDepth) ? Move::maxPushType :
Move::maxOutType;
/* check for a win position first */ _board->generateMoves(list);
if (!_board->isValid()) {
/* Shorter path to win position is better */ if (_sc && _sc->verbose()) {
value = 14999-depth; char tmp[100];
} sprintf(tmp, "Alpha/Beta [%d;%d], %d moves (%d depth)", alpha, beta,
else { list.count(Move::none), list.count(maxType));
_sc->startedNode(depth, tmp);
if (doDepthSearch) {
/* opponent searches for its maximum; but we want the
* minimum: so change sign (for alpha/beta window too!)
*/
value = -alphabeta(depth+1, -beta, -alpha);
}
else {
value = evaluate();
}
} }
_board->takeBack(); // don't use moves from the principal variation
m.type = Move::none;
// first, play all moves with depth search
depthPhase = true;
while (1) {
// get next move
if (m.type == Move::none) {
if (depthPhase)
depthPhase = list.getNext(m, maxType);
if (!depthPhase)
if (!list.getNext(m, Move::none)) break;
}
// we could start with a non-depth move from principal variation
doDepthSearch = depthPhase && (m.type <= maxType);
_board->playMove(m);
/* best move so far? */ /* check for a win position first */
if (value > currentValue) { if (!_board->isValid()) {
currentValue = value;
_pv.update(depth, m);
/* alpha/beta cut off or win position ... */ /* Shorter path to win position is better */
if (currentValue>14900 || currentValue >= beta) { value = 14999-depth;
if (_sc) _sc->finishedNode(depth, _pv.chain(depth)); }
return currentValue; else {
}
if (doDepthSearch) {
/* opponent searches for its maximum; but we want the
* minimum: so change sign (for alpha/beta window too!)
*/
value = -alphabeta(depth+1, -beta, -alpha);
}
else {
value = evaluate();
}
}
/* maximize alpha */ _board->takeBack();
if (currentValue > alpha) alpha = currentValue;
/* best move so far? */
if (value > currentValue) {
currentValue = value;
_pv.update(depth, m);
/* alpha/beta cut off or win position ... */
if (currentValue>14900 || currentValue >= beta) {
if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
return currentValue;
}
/* maximize alpha */
if (currentValue > alpha) alpha = currentValue;
}
if (_stopSearch) break; // depthPhase=false;
m.type = Move::none;
} }
if (_stopSearch) break; // depthPhase=false; if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
m.type = Move::none;
}
if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
return currentValue; return currentValue;
} }
// register ourselve // register ourselve
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment