who understands my code

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: who understands my code

Post by cdani »

Maybe you copied if from Stockfish?

Code: Select all

  if (value > bestValue)
      {
          bestValue = value;

          if (value > alpha)
          {
flok

Re: who understands my code

Post by flok »

No; must've been a website/wiki somewhere.
The only code I copied from an other chess-program is a 1-liner from fairymax for calculating how much time to allocate for a move.
ZirconiumX
Posts: 1361
Joined: Sun Jul 17, 2011 11:14 am
Full name: Hannah Ravensloft

Re: who understands my code

Post by ZirconiumX »

flok wrote:No; must've been a website/wiki somewhere.
The only code I copied from an other chess-program is a 1-liner from fairymax for calculating how much time to allocate for a move.
It's fail-soft. That's where you would have found it.
tu ne cede malis, sed contra audentior ito
JVMerlino
Posts: 1407
Joined: Wed Mar 08, 2006 10:15 pm
Location: San Francisco, California

Re: who understands my code

Post by JVMerlino »

kbhearn wrote:then why'd you write it? ;)

the break in this case does something outside the scope of the quoted code - it removes you from the innermost loop you're currently in perhaps to the final 'return bestVal;' line (which you now have scored as the current score > beta). It may avoid some code duplication if the TT update is similar enough for the fail high and fail low cases to write it in this manner.
The OP didn't write it. Look at the original code snippet. It was a comment added by Robert.

jm
Joost Buijs
Posts: 1680
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: who understands my code

Post by Joost Buijs »

flok wrote: copy/paste from somewhere
This is how computer chess works these days.
flok

Re: who understands my code

Post by flok »

Joost Buijs wrote:
flok wrote: copy/paste from somewhere
This is how computer chess works these days.
Don't whine. There are plenty of other things to focus on while implementing a chess program that are far more interesting than the 1000th implementation of what we're discussing here.
Joost Buijs
Posts: 1680
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: who understands my code

Post by Joost Buijs »

flok wrote:
Joost Buijs wrote:
flok wrote: copy/paste from somewhere
This is how computer chess works these days.
Don't whine. There are plenty of other things to focus on while implementing a chess program that are far more interesting than the 1000th implementation of what we're discussing here.
At least I would expect some understanding about what you copy/paste.

LOL
User avatar
Robert
Posts: 20
Joined: Tue Oct 07, 2008 2:53 am
Location: Brasil

Re: who understands my code

Post by Robert »

Hi Folkert,
flok wrote:
Robert wrote:

Code: Select all

  if (score > bestVal)
  {
          bestVal = score;
          // remember move for sibling-node-best-move
          selMove = *newCause.causingMove;
  
          if (score > alpha)
          {
                  alpha = score;
  
                  // remember PV
                  newPv.assign(tempPv, false);
  
                  if (score >= beta)
                          break;              <= I don't understand This!!!
          }
  }
beta cut-off; stop the loop and return the best bestval.
What "loop" ?
There are no loops in this code.
ZirconiumX
Posts: 1361
Joined: Sun Jul 17, 2011 11:14 am
Full name: Hannah Ravensloft

Re: who understands my code

Post by ZirconiumX »

Robert wrote:Hi Folkert,
flok wrote:
Robert wrote:

Code: Select all

  if (score > bestVal)
  {
          bestVal = score;
          // remember move for sibling-node-best-move
          selMove = *newCause.causingMove;
  
          if (score > alpha)
          {
                  alpha = score;
  
                  // remember PV
                  newPv.assign(tempPv, false);
  
                  if (score >= beta)
                          break;              <= I don't understand This!!!
          }
  }
beta cut-off; stop the loop and return the best bestval.
What "loop" ?
There are no loops in this code.
There is implicitly - you need a move loop to traverse all the moves, no?
tu ne cede malis, sed contra audentior ito
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: who understands my code

Post by Sven »

flok wrote:I wonder why is there a if (score > bestval) and also a if (score > alpha)?
Because if > bestval then always also > alpha.
As HGM stated, it depends on the search algorithm you are using.

a) If you are using fail-soft alphabeta (where you initialize bestval to -INFINITY) then you need both variables. Updating also "alpha" whenever "score" exceeds it could as well be replaced by passing "-max(alpha, bestval)" instead of "alpha" to the recursive search call, which may be considered a matter of taste. The "-max(...)" seems to require slightly less source code and appears more readable to me. In your version the case where "alpha" and "bestval" remain different is still handled correctly (if "score" never exceeds "alpha"), so the small possible benefit of fail-soft is still kept.

b) For fail-hard alphabeta, sticking only with "alpha" would be valid, unless you need the original value of "alpha" as well - but the latter is often the case, so even here using two variables seems appropriate. But then you don't want to update "alpha", you just keep it unchanged.

There is one more issue, however: you are updating the PV only if the current score is > alpha, which is correct in general. But then "alpha" should be the original alpha, not the updated one, otherwise keeping track of the PV would be slightly incorrect. So this would be a vote for using the "-max(...)" style.

To sum up what I prefer: I would always keep "alpha" unchanged as "original alpha", use "bestval" [or how you want to name it] as the current best score so far (initialized to -INFINITY or to alpha depending on fail-soft or fail-hard), always return "bestval" of course (and not "beta" in case of cutoff), and always pass "-max(alpha, bestval)" to the recursive search (which is the same as "alpha" in case of fail-hard). This reduces the difference between fail-soft and fail-hard to just one line: the initialization of "bestval".