jchoi1009
February 3rd, 2009, 03:15 PM
Hi,
I am having trouble implementing alpha-beta pruning algorithm for an AI. I have followed various pseudo codes available but none worked, so I assume I am doing something wrong implementing it.
Here is a simplified version of my code:
int AlphaBeta(char *successor, int depth, bool max, int alpha, int beta){
int val = heuristic_function(successor);
if (terminal_node(val))
return val;
char **successors = generate_successors(successor);
if (max) val = INT_MIN;
else val = INT_MAX;
for (n = 0 ; n < num_successors ; n++){
int tmp = AlphaBeta(successors[n], depth-1, !max, alpha, beta);
if (max){
val = MAX(val, tmp);
if (val >= beta)
return val;
alpha = MAX(alpha, val);
}
else{
val = MIN(val, tmp);
if (val <= alpha)
return val;
beta = MIN(beta, val);
}
}
return val;
}
max is a boolean variable which is true when it is Max's turn, and false when it is Min's turn. The function is initially called with max=true.
I know other parts of my program are correctly written because it works great with Minimax algorithm. But with Alpha-beta pruning, it seems that branches that shouldn't be pruned ARE pruned, thus the AI agent becoming incredibly stupid.
Could anybody help me with this?
Thanks alot!
I am having trouble implementing alpha-beta pruning algorithm for an AI. I have followed various pseudo codes available but none worked, so I assume I am doing something wrong implementing it.
Here is a simplified version of my code:
int AlphaBeta(char *successor, int depth, bool max, int alpha, int beta){
int val = heuristic_function(successor);
if (terminal_node(val))
return val;
char **successors = generate_successors(successor);
if (max) val = INT_MIN;
else val = INT_MAX;
for (n = 0 ; n < num_successors ; n++){
int tmp = AlphaBeta(successors[n], depth-1, !max, alpha, beta);
if (max){
val = MAX(val, tmp);
if (val >= beta)
return val;
alpha = MAX(alpha, val);
}
else{
val = MIN(val, tmp);
if (val <= alpha)
return val;
beta = MIN(beta, val);
}
}
return val;
}
max is a boolean variable which is true when it is Max's turn, and false when it is Min's turn. The function is initially called with max=true.
I know other parts of my program are correctly written because it works great with Minimax algorithm. But with Alpha-beta pruning, it seems that branches that shouldn't be pruned ARE pruned, thus the AI agent becoming incredibly stupid.
Could anybody help me with this?
Thanks alot!