Alpha-Beta-Pruning. Weird results

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Peng1993
Posts: 2
Joined: Mon Jun 06, 2016 7:31 pm

Alpha-Beta-Pruning. Weird results

Post by Peng1993 »

Hey guys,
I implemented alpha-beta pruning but sometimes I get weird results. When (for example) black is winning in 2 moves and the evaluation says that it is winning it doesn't actually make the move for some reason.

Here is the implementation I am using:

Code: Select all

  public  int alphabeta(final CheckerBoard board,int alpha,int beta,int depth){
      nodeCount++;
        

        if(depth==0 || board.isTerminalState()){
           return board.color*GameEvaluation.evaluate(board);
        }


    ArrayList<Move>sucessors =MGenerator.getMoves&#40;board&#41;;
        int bestValue =Integer.MIN_VALUE;
        for&#40;Move  move &#58; sucessors&#41;&#123;
            board.makeMove&#40;move&#41;;
            int value =-alphabeta&#40;board, -beta, -alpha, depth - 1, true&#41;;
            board.undoMove&#40;move&#41;;
            if&#40;value>bestValue&#41;&#123;
                bestValue =value;
            &#125;
            alpha =Math.max&#40;alpha,value&#41;;
            if&#40;alpha>=beta&#41;&#123;
                break;
            &#125;
        &#125;

        return bestValue;
    &#125;
and the following code to start the search:

Code: Select all

 public int findBestMove&#40;int depth&#41;&#123;
        ArrayList<Move> sucessors =MGenerator.getMoves&#40;board&#41;;
        int max = Integer.MIN_VALUE;
        for&#40;Move m &#58; sucessors&#41;&#123;
            board.makeMove&#40;m&#41;;
            int value =-alphabeta&#40;board, -1000000, 1000000, depth&#41;;
            board.undoMove&#40;m&#41;;
            if&#40;value>max&#41;&#123;
                max=value;
                bestMove=m;
            &#125;
        &#125;

        board.makeMove&#40;bestMove&#41;;
        return max;
    &#125;
Is there anything wrong with my implementation. I am puzzled by the weird results I am getting. :(
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: Alpha-Beta-Pruning. Weird results

Post by syzygy »

Peng1993 wrote:I implemented alpha-beta pruning but sometimes I get weird results. When (for example) black is winning in 2 moves and the evaluation says that it is winning it doesn't actually make the move for some reason.
I can't see what is happening, but maybe there are many winning moves and the search picks one that is winning but does not bring the win closer?

For example in KQvK any move that does not lose the queen (or causes stalemate) is winning, but randomly playing winning moves will likely not win the game.
Peng1993
Posts: 2
Joined: Mon Jun 06, 2016 7:31 pm

Re: Alpha-Beta-Pruning. Weird results

Post by Peng1993 »

syzygy wrote:
Peng1993 wrote:I implemented alpha-beta pruning but sometimes I get weird results. When (for example) black is winning in 2 moves and the evaluation says that it is winning it doesn't actually make the move for some reason.
I can't see what is happening, but maybe there are many winning moves and the search picks one that is winning but does not bring the win closer?

For example in KQvK any move that does not lose the queen (or causes stalemate) is winning, but randomly playing winning moves will likely not win the game.
Maybe. But there isn't anything specifically wrong with my implementation ?
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Alpha-Beta-Pruning. Weird results

Post by AlvaroBegue »

Does `GameEvaluation.evaluate(board)' return the correct values for terminal states? The way I would normally write the code, it would look something like this:

Code: Select all

  // ...

  int final_result;
  if &#40;board.isTerminalState&#40;final_result&#41;)
    return board.color * final_result;
  
  if &#40;depth <= 0&#41;
    return board.color * GameEvaluation.evaluate&#40;board&#41;;

  // ...
If `GameEvaluation.evaluate(board)' is returning some heuristic value even for terminal positions, your code may prefer a larger material advantage than actually winning, e.g.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Alpha-Beta-Pruning. Weird results

Post by bob »

Peng1993 wrote:Hey guys,
I implemented alpha-beta pruning but sometimes I get weird results. When (for example) black is winning in 2 moves and the evaluation says that it is winning it doesn't actually make the move for some reason.

Here is the implementation I am using:

Code: Select all

  public  int alphabeta&#40;final CheckerBoard board,int alpha,int beta,int depth&#41;&#123;
      nodeCount++;
        

        if&#40;depth==0 || board.isTerminalState&#40;))&#123;
           return board.color*GameEvaluation.evaluate&#40;board&#41;;
        &#125;


    ArrayList<Move>sucessors =MGenerator.getMoves&#40;board&#41;;
        int bestValue =Integer.MIN_VALUE;
        for&#40;Move  move &#58; sucessors&#41;&#123;
            board.makeMove&#40;move&#41;;
            int value =-alphabeta&#40;board, -beta, -alpha, depth - 1, true&#41;;
            board.undoMove&#40;move&#41;;
            if&#40;value>bestValue&#41;&#123;
                bestValue =value;
            &#125;
            alpha =Math.max&#40;alpha,value&#41;;
            if&#40;alpha>=beta&#41;&#123;
                break;
            &#125;
        &#125;

        return bestValue;
    &#125;
and the following code to start the search:

Code: Select all

 public int findBestMove&#40;int depth&#41;&#123;
        ArrayList<Move> sucessors =MGenerator.getMoves&#40;board&#41;;
        int max = Integer.MIN_VALUE;
        for&#40;Move m &#58; sucessors&#41;&#123;
            board.makeMove&#40;m&#41;;
            int value =-alphabeta&#40;board, -1000000, 1000000, depth&#41;;
            board.undoMove&#40;m&#41;;
            if&#40;value>max&#41;&#123;
                max=value;
                bestMove=m;
            &#125;
        &#125;

        board.makeMove&#40;bestMove&#41;;
        return max;
    &#125;
Is there anything wrong with my implementation. I am puzzled by the weird results I am getting. :(
I don't see all the details. Do you return a mate score that is something like "MATE - ply" so that deeper mates get worse scores than shallow mates???
Edsel Apostol
Posts: 803
Joined: Mon Jul 17, 2006 5:53 am
Full name: Edsel Apostol

Re: Alpha-Beta-Pruning. Weird results

Post by Edsel Apostol »

Code: Select all

             if&#40;&#91;b&#93;value>max&#91;/b&#93;)&#123;
                max=value; 
                bestMove=m; 
            &#125;
That code is problematic along with the return value of Integer.MIN_VALUE when there is a mate found deeper in the tree, lets say it is going to be mated in the next ply. The Integer.MIN_VALUE will be propagated up in the tree and will have this statement:

if (Integer.MIN_VALUE > Integer.MIN_VALUE)

which will not be true and therefore bestMove will not be updated.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Alpha-Beta-Pruning. Weird results

Post by hgm »

Peng1993 wrote:When (for example) black is winning in 2 moves and the evaluation says that it is winning it doesn't actually make the move for some reason.
It would be more enlightening if you could show an example of this.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Alpha-Beta-Pruning. Weird results

Post by Henk »

What's the value the search returns when win in zero moves ?
Daniel Anulliero
Posts: 759
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: Alpha-Beta-Pruning. Weird results

Post by Daniel Anulliero »

Henk wrote:What's the value the search returns when win in zero moves ?
Lol
No win in zero moves in search tree
Win in zero moves means the user played his moves and checkmated the engine
In the search tree it may have a win in one move and the value returned is MATE - 1
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Alpha-Beta-Pruning. Weird results

Post by Joost Buijs »

In my search tree a win in one move returns mateValue - ply which is something entirely different.