Geschrieben von:/Posted by: Sven Schüle at 10 August 2004 17:24:56:
Hi,
I am currently thinking about a very basic thing in chess programming, and I am not quite sure if I have done it right in "Surprise". Now I wonder how you are doing this.
First the simple rules again: if a position occurs for the third time in a game (including search tree) with identical board contents, side to move, castle rights, and ep square, then the game is a draw. So far everything is clear, including how to implement the repetition detection in general.
If a position occurs for the second time, i.e. is repeated for the first time, then we have two cases during the search (assuming current depth > 0):
A. The first occurrence was before the current search started and belongs to "game history".
B. The first occurrence belongs to the search tree.
See also Bruce Moreland's page for more details:
http://www.brucemo.com/compchess/progra ... tition.htm
Now I have implemented this stuff in "Surprise" similar to the following pseudo code:
void handleRepetition(int depth, bool & shouldExpand)
{
if (isRepetition()) {
int oldRepCount = get_repetition_count_of_repeated_position();
int newRepCount = set_repetition_count_of_current_position(oldRepCount + 1);
int plyDistance = get_distance_to_repeated_position_in_plies();
if (plyDistance > depth) {
// game history position repeated
if (newRepCount >= 2) {
// final position of game
set_repetition_flag_in_current_position();
shouldExpand = false;
} else {
// THE CRITICAL CASE:
// continue search, both sides might want to escape from draw
shouldExpand = true;
}
} else {
// tree position repeated; will be handled as if it were a
// final position of game
set_repetition_flag_in_current_position();
shouldExpand = false;
}
} else {
// nothing special
shouldExpand = true;
}
}
int treeSearch(int alpha, int beta, int depth, int maxDepth)
{
// ...
bool shouldExpand;
handleRepetition(depth, shouldExpand);
if (!shouldExpand) {
return 0; // draw score
}
// ... else ... perhaps call the evaluation function
}
Additionally, the evaluation function (called mainly at leaf nodes of full-width search) gives a bonus of up to 0.6 pawn units if the current position has a repetition count of 1 (THE CRITICAL CASE, see above) and the side to move is down at least 0.01 pawns; but with the bonus the score will not go positive.
Now what I have observed frequently is that "Surprise" likes to repeat a position once before making some progress with a better variation.
Sometimes, even worse, in a clearly won game "Surprise" seems to love the repeated position and wants to see it one more time, loosing a half point. But this might be something very different, most likely a bug.
Long introduction - short question: how to handle the first repetition correctly, especially how to avoid that the engine repeats before making any progress (which might lead to some 50-moves trouble)?
Any helpful comments are welcome!
Bye,
Sven
Surprise