Minimax with Alpha-Beta pruning; class variables or sending them through the recursion?

StackOverflow https://stackoverflow.com/questions/8242475

  •  07-03-2021
  •  | 
  •  

Question

When using Minimax with Alpha-Beta pruning, is it possible to have alpha and beta as class variables instead of sending them through the recursion?

Instead of:

private ValuedMove AlphaBetaSearch(Board state)
{
    return MaxValue(state, 0, int.MinValue, int.MaxValue);
}

private ValuedMove MaxValue(Board state, int d, int alpha, int beta)
{
    if (d == depth || state.GameRunning == false)
        return new ValuedMove(Heuristic.BoardValue(state, Player));

    ValuedMove v = new ValuedMove(int.MinValue);
    foreach (Move move in LegalMoves)
    {
        ValuedMove minCheck = MinValue(state.ImagineMove(move), d + 1, alpha, beta);
        if (v.Value >= beta)
            return v;
        alpha = Max(alpha, v.Value);
    }

    return v;
}

private ValuedMove MinValue(Board state, int d, int alpha, int beta)
{
    //Minimax and Alpha-Beta logic here
}

Can I write:

int alpha, beta;

private ValuedMove AlphaBetaSearch(Board state)
{
    alpha = int.MinValue;
    beta = int.MaxValue;
    return MaxValue(state, 0);
}

private ValuedMove MaxValue(Board state, int d)
{
    //Minimax and Alpha-Beta logic here
}

private ValuedMove MinValue(Board state, int d)
{
    //Minimax and Alpha-Beta logic here
}

I am asking because when I tried to optimize the code by doing so (my thought was that if I didn't need to send the ints through to each recursion, I might be able to peel of a little time), my chess player suddenly became an idiot, sacrificing his queen to kill a pawn, and doing other silly mistakes.

He constantly performs a lot poorer than his "regular Alpha-Beta" opponent, which I guess is because he also searches only a small percentage of the tree compared to his opponent (they both use the same depth, but the modified player seems to prune more aggressive, and thereby reducing the number of nodes visited). I have done this twice now, to make sure, and I do not change anything else than what I scetch out here.

If I have understood the Alpha-Beta algorithm correct, this shouldn't make any difference, but for my chess player, it does. Am I doing anything wrong?

So, my main question now is not whether it is a optimization wise or code practice wise good thing to do, but rather whether it should be possible to do or not.

Was it helpful?

Solution

You can't really do this. AlphaBeta is a recursive Algorithm. That means that it calls itself. Each time it calls itself it does so with (possibly) different values for alpha and beta. Each recursion needs its own version of the variables that will hold different values. If you include the variables in the class you will only have one set of variables shared between all (recursive) calls to AlphaBeta.

This being said, replacing function parameters with class members is probably not a good optimization. Both techniques have a cost. The parameters need to be pushed on the stack before the call and the members need to be accessed indirectly via a pointer (the hidden this pointer). Let's forget about which cost is the higher. This micro-optimization is probably too insignificant to make any difference at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top