mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 01:29:36 +00:00
Big qsearch() cleanup
It is more clear and should be a bit faster too. Reverted also previous optimization patch because seems do not increase actual speed. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
9337c6da46
commit
452f0d1696
1 changed files with 41 additions and 51 deletions
|
@ -1481,16 +1481,15 @@ namespace {
|
||||||
EvalInfo ei;
|
EvalInfo ei;
|
||||||
StateInfo st;
|
StateInfo st;
|
||||||
Move ttMove, move;
|
Move ttMove, move;
|
||||||
Value staticValue, bestValue, value, futilityBase;
|
Value bestValue, value, futilityValue, futilityBase;
|
||||||
bool isCheck, enoughMaterial, moveIsCheck, evasionPrunable;
|
bool isCheck, deepChecks, enoughMaterial, moveIsCheck, evasionPrunable;
|
||||||
const TTEntry* tte = NULL;
|
const TTEntry* tte;
|
||||||
int moveCount = 0;
|
|
||||||
int ply = pos.ply();
|
|
||||||
Value oldAlpha = alpha;
|
Value oldAlpha = alpha;
|
||||||
Value futilityValue = VALUE_INFINITE;
|
int ply = pos.ply();
|
||||||
|
|
||||||
TM.incrementNodeCounter(pos.thread());
|
TM.incrementNodeCounter(pos.thread());
|
||||||
ss->init(ply);
|
ss->pv[ply] = ss->pv[ply + 1] = ss->currentMove = MOVE_NONE;
|
||||||
|
ss->eval = VALUE_NONE;
|
||||||
|
|
||||||
// Check for an instant draw or maximum ply reached
|
// Check for an instant draw or maximum ply reached
|
||||||
if (pos.is_draw() || ply >= PLY_MAX - 1)
|
if (pos.is_draw() || ply >= PLY_MAX - 1)
|
||||||
|
@ -1511,29 +1510,27 @@ namespace {
|
||||||
|
|
||||||
// Evaluate the position statically
|
// Evaluate the position statically
|
||||||
if (isCheck)
|
if (isCheck)
|
||||||
staticValue = -VALUE_INFINITE;
|
|
||||||
else if (tte && tte->static_value() != VALUE_NONE)
|
|
||||||
{
|
{
|
||||||
staticValue = tte->static_value();
|
bestValue = futilityBase = -VALUE_INFINITE;
|
||||||
ei.kingDanger[pos.side_to_move()] = tte->king_danger();
|
deepChecks = enoughMaterial = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
staticValue = evaluate(pos, ei);
|
|
||||||
|
|
||||||
if (!isCheck)
|
|
||||||
{
|
{
|
||||||
ss->eval = staticValue;
|
if (tte && tte->static_value() != VALUE_NONE)
|
||||||
update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
|
{
|
||||||
|
ei.kingDanger[pos.side_to_move()] = tte->king_danger();
|
||||||
|
bestValue = tte->static_value();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
bestValue = evaluate(pos, ei);
|
||||||
|
|
||||||
// Initialize "stand pat score", and return it immediately if it is
|
ss->eval = bestValue;
|
||||||
// at least beta.
|
update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
|
||||||
bestValue = staticValue;
|
|
||||||
|
|
||||||
|
// Stand pat. Return immediately if static value is at least beta
|
||||||
if (bestValue >= beta)
|
if (bestValue >= beta)
|
||||||
{
|
{
|
||||||
// Store the score to avoid a future costly evaluation() call
|
if (!tte) // FIXME, remove condition
|
||||||
if (!isCheck && !tte)
|
|
||||||
TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
|
TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
|
||||||
|
|
||||||
return bestValue;
|
return bestValue;
|
||||||
|
@ -1543,7 +1540,12 @@ namespace {
|
||||||
alpha = bestValue;
|
alpha = bestValue;
|
||||||
|
|
||||||
// If we are near beta then try to get a cutoff pushing checks a bit further
|
// If we are near beta then try to get a cutoff pushing checks a bit further
|
||||||
bool deepChecks = (depth == -OnePly && staticValue >= beta - PawnValueMidgame / 8);
|
deepChecks = (depth == -OnePly && bestValue >= beta - PawnValueMidgame / 8);
|
||||||
|
|
||||||
|
// Futility pruning parameters, not needed when in check
|
||||||
|
futilityBase = bestValue + FutilityMarginQS + ei.kingDanger[pos.side_to_move()];
|
||||||
|
enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize a MovePicker object for the current position, and prepare
|
// Initialize a MovePicker object for the current position, and prepare
|
||||||
// to search the moves. Because the depth is <= 0 here, only captures,
|
// to search the moves. Because the depth is <= 0 here, only captures,
|
||||||
|
@ -1551,8 +1553,6 @@ namespace {
|
||||||
// and we are near beta) will be generated.
|
// and we are near beta) will be generated.
|
||||||
MovePicker mp = MovePicker(pos, ttMove, deepChecks ? Depth(0) : depth, H);
|
MovePicker mp = MovePicker(pos, ttMove, deepChecks ? Depth(0) : depth, H);
|
||||||
CheckInfo ci(pos);
|
CheckInfo ci(pos);
|
||||||
enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame;
|
|
||||||
futilityBase = staticValue + FutilityMarginQS + ei.kingDanger[pos.side_to_move()];
|
|
||||||
|
|
||||||
// Loop through the moves until no moves remain or a beta cutoff occurs
|
// Loop through the moves until no moves remain or a beta cutoff occurs
|
||||||
while ( alpha < beta
|
while ( alpha < beta
|
||||||
|
@ -1562,24 +1562,15 @@ namespace {
|
||||||
|
|
||||||
moveIsCheck = pos.move_is_check(move, ci);
|
moveIsCheck = pos.move_is_check(move, ci);
|
||||||
|
|
||||||
// Update current move
|
|
||||||
moveCount++;
|
|
||||||
ss->currentMove = move;
|
|
||||||
|
|
||||||
// Futility pruning
|
// Futility pruning
|
||||||
if ( !PvNode
|
if ( !PvNode
|
||||||
&& enoughMaterial
|
|
||||||
&& !isCheck
|
&& !isCheck
|
||||||
&& !moveIsCheck
|
&& !moveIsCheck
|
||||||
&& move != ttMove
|
&& move != ttMove
|
||||||
|
&& enoughMaterial
|
||||||
&& !move_is_promotion(move)
|
&& !move_is_promotion(move)
|
||||||
&& !pos.move_is_passed_pawn_push(move))
|
&& !pos.move_is_passed_pawn_push(move))
|
||||||
{
|
{
|
||||||
// Can only decrease from previous move because of
|
|
||||||
// MVV ordering so we don't need to recheck.
|
|
||||||
if (futilityValue < alpha)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
futilityValue = futilityBase
|
futilityValue = futilityBase
|
||||||
+ pos.endgame_value_of_piece_on(move_to(move))
|
+ pos.endgame_value_of_piece_on(move_to(move))
|
||||||
+ (move_is_ep(move) ? PawnValueEndgame : Value(0));
|
+ (move_is_ep(move) ? PawnValueEndgame : Value(0));
|
||||||
|
@ -1607,6 +1598,9 @@ namespace {
|
||||||
&& pos.see_sign(move) < 0)
|
&& pos.see_sign(move) < 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// Update current move
|
||||||
|
ss->currentMove = move;
|
||||||
|
|
||||||
// Make and search the move
|
// Make and search the move
|
||||||
pos.do_move(move, st, ci, moveIsCheck);
|
pos.do_move(move, st, ci, moveIsCheck);
|
||||||
value = -qsearch<PvNode>(pos, ss+1, -beta, -alpha, depth-OnePly);
|
value = -qsearch<PvNode>(pos, ss+1, -beta, -alpha, depth-OnePly);
|
||||||
|
@ -1628,17 +1622,13 @@ namespace {
|
||||||
|
|
||||||
// All legal moves have been searched. A special case: If we're in check
|
// All legal moves have been searched. A special case: If we're in check
|
||||||
// and no legal moves were found, it is checkmate.
|
// and no legal moves were found, it is checkmate.
|
||||||
if (!moveCount && isCheck) // Mate!
|
if (isCheck && bestValue == -VALUE_INFINITE)
|
||||||
return value_mated_in(ply);
|
return value_mated_in(ply);
|
||||||
|
|
||||||
// Update transposition table
|
// Update transposition table
|
||||||
Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
|
Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
|
||||||
if (bestValue <= oldAlpha)
|
if (bestValue <= oldAlpha)
|
||||||
{
|
|
||||||
// If bestValue isn't changed it means it is still the static evaluation
|
|
||||||
// of the node, so keep this info to avoid a future evaluation() call.
|
|
||||||
TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
|
TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
|
||||||
}
|
|
||||||
else if (bestValue >= beta)
|
else if (bestValue >= beta)
|
||||||
{
|
{
|
||||||
move = ss->pv[ply];
|
move = ss->pv[ply];
|
||||||
|
|
Loading…
Add table
Reference in a new issue