I am trying to implement PVS (Principle Variation Search) into my engine. I read the PVS article on the Chess Programming Wiki and it showed two different implementations:
Code: Select all
int pvSearch( int alpha, int beta, int depth ) {
if( depth == 0 ) return quiesce( alpha, beta );
for ( all moves) {
make
if ( first move ) {
score = -pvSearch(-beta, -alpha, depth - 1);
} else {
score = -pvSearch(-alpha-1, -alpha, depth - 1);
if ( score > alpha && beta - alpha > 1 )
// beta - alpha > 1 prevents redundant re-search of Non-PV nodes.
// Note that in rare cases this conditions is true for PV nodes.
// Some engines prevent that by setting a boolean PvNode variable
// to determine if position is in a PV node. But it's generally
// considered as too rare to cause considereable stength loss.
score = -pvSearch(-beta, -alpha, depth - 1); // re-search
}
unmake
if( score >= beta )
return beta; // fail-hard beta-cutoff
if( score > alpha ) {
alpha = score; // alpha acts like max in MiniMax
}
}
return alpha; // fail-hard
}
Code: Select all
int pvSearch( int alpha, int beta, int depth ) {
if( depth == 0 ) return quiesce(alpha, beta);
bool bSearchPv = true;
for ( all moves) {
make
if ( bSearchPv ) {
score = -pvSearch(-beta, -alpha, depth - 1);
} else {
score = -zwSearch(-alpha, depth - 1);
if ( score > alpha ) // in fail-soft ... && score < beta ) is common
score = -pvSearch(-beta, -alpha, depth - 1); // re-search
}
unmake
if( score >= beta )
return beta; // fail-hard beta-cutoff
if( score > alpha ) {
alpha = score; // alpha acts like max in MiniMax
bSearchPv = false; // *1)
}
}
return alpha;
}
// fail-hard zero window search, returns either beta-1 or beta
int zwSearch(int beta, int depth ) {
// alpha == beta - 1
// this is either a cut- or all-node
if( depth == 0 ) return quiesce(beta-1, beta);
for ( all moves) {
make
score = -zwSearch(1-beta, depth - 1);
unmake
if( score >= beta )
return beta; // fail-hard beta-cutoff
}
return beta-1; // fail-hard, return alpha
}
Thank you for your time,
Jeff Osborne