Weird Behavior in Quiescence Search

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

oriyonay
Posts: 32
Joined: Tue Jun 01, 2021 5:46 am
Full name: ori yonay

Weird Behavior in Quiescence Search

Post by oriyonay »

Howdy everyone - I hope you're having a fantastic day so far!
I'm working on my engine right now and have been facing some weird bugs that all have to do with qsearch (whenever I don't use it, everything works perfectly): if I run 'go depth 6', followed by 'go depth 8' and 'go depth 9' (on the same position), everything works fine. But when I then try 'go depth 8' again, it gives me values close to -INF and INF.
Here is my code - I also do move ordering but I removed it to make this simpler.

Code: Select all

int quiescence(int alpha, int beta) {
  nodes_evaluated++;

  // static evaluation:
  int eval = evaluate();

  // alpha/beta escape conditions:
  if (eval >= beta) return beta;
  if (eval > alpha) alpha = eval;

  // generate all possible moves from this position:
  int moves[MAX_POSITION_MOVES];
  int num_moves = b.get_nonquiet_moves(moves);

  // if there are no more nonquiet moves to make, return the current static eval:
  if (num_moves == 0) return eval;

  // recursively qsearch the horizon:
  for (int i = 0; i < num_moves; i++) {
    // make move & recursively call negamax helper function:
    b.make_move(moves[i]);
    int score = -quiescence(-beta, -alpha);
    b.undo_move();

    if (score > alpha) {
      alpha = score;
      if (score >= beta) return beta;
    }
  }

  return alpha;
}
Any help, tips, or a nudge in the right direction would be greatly appreciated!
Thank you and enjoy the rest of your day!
federico
Posts: 32
Joined: Sun Oct 22, 2017 4:36 am
Location: Canada
Full name: Federico Rojo

Re: Weird Behavior in Quiescence Search

Post by federico »

If you run the command go depth 9 and works fine, but if you run again you don't get the same results then my guess is that it has to do with the transposition tables.

If you have them i would look there. Try disabling them and see if the issue persists.


Fede
oriyonay
Posts: 32
Joined: Tue Jun 01, 2021 5:46 am
Full name: ori yonay

Re: Weird Behavior in Quiescence Search

Post by oriyonay »

federico wrote: Wed Jul 28, 2021 2:21 am If you run the command go depth 9 and works fine, but if you run again you don't get the same results then my guess is that it has to do with the transposition tables.

If you have them i would look there. Try disabling them and see if the issue persists.


Fede
Thank you for the reply! That's the thing - I haven't implemented transposition tables yet...
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Weird Behavior in Quiescence Search

Post by amanjpro »

You probably recycle pv table incorrectly, or something along this line
Karlo Bala
Posts: 373
Joined: Wed Mar 22, 2006 10:17 am
Location: Novi Sad, Serbia
Full name: Karlo Balla

Re: Weird Behavior in Quiescence Search

Post by Karlo Bala »

oriyonay wrote: Wed Jul 28, 2021 1:10 am Howdy everyone - I hope you're having a fantastic day so far!
I'm working on my engine right now and have been facing some weird bugs that all have to do with qsearch (whenever I don't use it, everything works perfectly): if I run 'go depth 6', followed by 'go depth 8' and 'go depth 9' (on the same position), everything works fine. But when I then try 'go depth 8' again, it gives me values close to -INF and INF.
Here is my code - I also do move ordering but I removed it to make this simpler.

Code: Select all

int quiescence(int alpha, int beta) {
  nodes_evaluated++;

  // static evaluation:
  int eval = evaluate();

  // alpha/beta escape conditions:
  if (eval >= beta) return beta;
  if (eval > alpha) alpha = eval;

  // generate all possible moves from this position:
  int moves[MAX_POSITION_MOVES];
  int num_moves = b.get_nonquiet_moves(moves);

  // if there are no more nonquiet moves to make, return the current static eval:
  if (num_moves == 0) return eval;

  // recursively qsearch the horizon:
  for (int i = 0; i < num_moves; i++) {
    // make move & recursively call negamax helper function:
    b.make_move(moves[i]);
    int score = -quiescence(-beta, -alpha);
    b.undo_move();

    if (score > alpha) {
      alpha = score;
      if (score >= beta) return beta;
    }
  }

  return alpha;
}
Any help, tips, or a nudge in the right direction would be greatly appreciated!
Thank you and enjoy the rest of your day!
What do you do when the king is in check?
Best Regards,
Karlo Balla Jr.
oriyonay
Posts: 32
Joined: Tue Jun 01, 2021 5:46 am
Full name: ori yonay

Re: Weird Behavior in Quiescence Search

Post by oriyonay »

Karlo Bala wrote: Wed Jul 28, 2021 3:29 pm What do you do when the king is in check?
Oh my god I didn't even think of that! Right now I'm not doing anything special in qsearch when the king is in check. What should I do?
aryan1508
Posts: 31
Joined: Sat Mar 20, 2021 3:44 pm
Full name: Aryan Parekh

Re: Weird Behavior in Quiescence Search

Post by aryan1508 »

oriyonay wrote: Wed Jul 28, 2021 5:33 pm
Karlo Bala wrote: Wed Jul 28, 2021 3:29 pm What do you do when the king is in check?
Oh my god I didn't even think of that! Right now I'm not doing anything special in qsearch when the king is in check. What should I do?
In Bit-Genie I avoid going into Qsearch if the king is in check, a kind of check extension
oriyonay
Posts: 32
Joined: Tue Jun 01, 2021 5:46 am
Full name: ori yonay

Re: Weird Behavior in Quiescence Search

Post by oriyonay »

aryan1508 wrote: Wed Jul 28, 2021 5:40 pm In Bit-Genie I avoid going into Qsearch if the king is in check, a kind of check extension
I replaced the (somewhat dumb) line

Code: Select all

if (num_moves == 0) return eval;
with

Code: Select all

if (b.is_check()) return eval;
and it seems to work. Should I be returning the static evaluation in this case, or alpha?
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Weird Behavior in Quiescence Search

Post by amanjpro »

oriyonay wrote: Wed Jul 28, 2021 6:05 pm
aryan1508 wrote: Wed Jul 28, 2021 5:40 pm In Bit-Genie I avoid going into Qsearch if the king is in check, a kind of check extension
I replaced the (somewhat dumb) line

Code: Select all

if (num_moves == 0) return eval;
with

Code: Select all

if (b.is_check()) return eval;
and it seems to work. Should I be returning the static evaluation in this case, or alpha?
You probably want to return alpha when there is no move to play... And when in check you should search all moves not only captures... To make sure you don't put the king into a mating net
Karlo Bala
Posts: 373
Joined: Wed Mar 22, 2006 10:17 am
Location: Novi Sad, Serbia
Full name: Karlo Balla

Re: Weird Behavior in Quiescence Search

Post by Karlo Bala »

oriyonay wrote: Wed Jul 28, 2021 6:05 pm
aryan1508 wrote: Wed Jul 28, 2021 5:40 pm In Bit-Genie I avoid going into Qsearch if the king is in check, a kind of check extension
I replaced the (somewhat dumb) line

Code: Select all

if (num_moves == 0) return eval;
with

Code: Select all

if (b.is_check()) return eval;
and it seems to work. Should I be returning the static evaluation in this case, or alpha?
Quick and dirty solution:

Search:
If (!inCheck && depth <= 0) return QSearch(...)

QSearch:
if (inCheck) return Search(...)
Best Regards,
Karlo Balla Jr.