Alpha-Beta-Pruning. Weird results
Moderators: hgm, Rebel, chrisw
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: Alpha-Beta-Pruning. Weird results
A win in zero moves returns in my chess program the value MATE_SCORE - plyCount + 1. That is when it captures the king,
-
- Posts: 759
- Joined: Fri Jan 04, 2013 4:55 pm
- Location: Nice
Re: Alpha-Beta-Pruning. Weird results
Yes of course I would say mate -ply (where ply = 1 if it's a win in one move)Joost Buijs wrote:In my search tree a win in one move returns mateValue - ply which is something entirely different.
-
- Posts: 27811
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Alpha-Beta-Pruning. Weird results
It depends on how you define a 'win'. I suppose in Chess you don't actually have to capture the King, and you have already won when the opponent leaves his King in check (blitz rules). Fairy-Max returns a +INF score for such positions. Illegal moves thus get a -INF score after negamaxing, and when all moves (including null move) are illegal the node returns -INF+1 (because of the delayed-loss bonus) for mated-in-0. Mate-in-1 then gets an INF-1 score, etc.
-
- Posts: 931
- Joined: Tue Mar 09, 2010 3:46 pm
- Location: New York
- Full name: Álvaro Begué (RuyDos)
Re: Alpha-Beta-Pruning. Weird results
I should point out that using Integer.MIN_VALUE has another problem: If you compute -Integer.MIN_VALUE you'll get Integer.MIN_VALUE again!!!
Instead of MIN_VALUE I normally use a constant like 1000000000 or 8000 (I use 14 bits to represent a score in the hash table).
Instead of MIN_VALUE I normally use a constant like 1000000000 or 8000 (I use 14 bits to represent a score in the hash table).
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Alpha-Beta-Pruning. Weird results
The move generator I use generates legal moves only.hgm wrote:It depends on how you define a 'win'. I suppose in Chess you don't actually have to capture the King, and you have already won when the opponent leaves his King in check (blitz rules). Fairy-Max returns a +INF score for such positions. Illegal moves thus get a -INF score after negamaxing, and when all moves (including null move) are illegal the node returns -INF+1 (because of the delayed-loss bonus) for mated-in-0. Mate-in-1 then gets an INF-1 score, etc.
When the side to move has no moves at all and his king is in check I return -valueMate + ply (ply == 0 at the root).
When his king is not in check I return valueDraw (which is zero in my case because I don't use contempt).
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Alpha-Beta-Pruning. Weird results
You can also have a win in one move somewhere deep in the tree, of course I understand you mean a win in one move relative to the root.Daniel Anulliero wrote:Yes of course I would say mate -ply (where ply = 1 if it's a win in one move)Joost Buijs wrote:In my search tree a win in one move returns mateValue - ply which is something entirely different.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Alpha-Beta-Pruning. Weird results
Depends on how you implement it. For me, it would be -(MATE - 1, since I got to ply=1 and had no legal moves.Henk wrote:What's the value the search returns when win in zero moves ?
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Alpha-Beta-Pruning. Weird results
Hi Robin,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(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(board); int bestValue =Integer.MIN_VALUE; for(Move move : sucessors){ board.makeMove(move); int value =-alphabeta(board, -beta, -alpha, depth - 1, true); board.undoMove(move); if(value>bestValue){ bestValue =value; } alpha =Math.max(alpha,value); if(alpha>=beta){ break; } } return bestValue; }
Is there anything wrong with my implementation. I am puzzled by the weird results I am getting.
apart from the other comments, especially regarding mate scoring, I see some small issues with your code, not sure whether these are causing trouble but you should check these at least:
- You are using fail-soft alpha-beta. This algorithm works best if you do the recursive call like this:
Code: Select all
int value =-alphabeta(board, -beta, -Math.max(alpha,bestValue), depth - 1);
- There is an additional boolean parameter for your alphabeta() function which is not part of the signature, so probably the code above is not the original code.
As far as mate scoring is concerned, there are several valid solutions around. One is the "delayed loss bonus" preferred by HGM. Probably most popular is to simply return -(MATE_SCORE - distanceToRoot) at a node where the side to move is checkmated. Bob and few others use a variant of this which is based on a slightly different ply counting (root is "ply 1" instead of "ply 0"). If you don't handle mate scoring properly then your program will almost never play the move that leads to the shortest mate, which effectively means it will not be able to win even in a position with a forced mate in N plies. In your implementation above you always return the same mate score so that your program does not know that a mate in 1 ply is better than a mate in 3 plies.
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: Alpha-Beta-Pruning. Weird results
Maybe first implement minimax. If that works and code is kept similar to alpha beta search then probably problem is somehow dependent on alpha beta parameters if your test was right.