foRei
foRei

Reputation: 9

Minimax Alpha Beta Pruning not working, but Minimax alone does

My problem is when applying Alpha/Beta Pruning to Minimax. It does very wierd and bad moves. When I use Minimax without Alpha/Beta, it works fine. The two functions look like this:

Minimax With Alpha/Beta Pruning:

public int minimaxAB(Piece[,] board, int depth, int a, int b, bool maximizingPlayer, bool WhiteToPlay)
{
    if (depth == 0)
    {
        return EvaluatePosition(board, WhiteToPlay);
    }

    var moves = GenerateMoves(board, WhiteToPlay);
    if (maximizingPlayer)
    {
        int value = int.MinValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimaxAB(move, depth - 1, a, b, false, !WhiteToPlay);
            value = Math.Max(value, minmaxResult);

            a = Math.Max(a, value);

            if (a >= b)
                return a;

            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
    else
    {
        int value = int.MaxValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimaxAB(move, depth - 1, a, b, true, !WhiteToPlay);
            value = Math.Min(value, minmaxResult);

            b = Math.Min(b, value);

            if (b <= a)
                return b;

            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
}

Minimax without A/B:

public int minimax(Piece[,] board, int depth, bool maximizingPlayer, bool WhiteToPlay)
{
    if (depth == 0)
    {
        int result = EvaluatePosition(board, WhiteToPlay);
        return result;
    }

    var moves = GenerateMoves(board, WhiteToPlay);
    if (maximizingPlayer)
    {
        int value = int.MinValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimax(move, depth - 1, false, !WhiteToPlay);
            value = Math.Max(value, minmaxResult);
            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
    else
    {
        int value = int.MaxValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimax(move, depth - 1, true, !WhiteToPlay);
            value = Math.Min(value, minmaxResult);
            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
}

My evaluation function:

public int EvaluatePosition(Piece[,] boardPos, bool ForWhite)
{
    int eval_W = 0;
    int eval_B = 0;
    int eval = 0;
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (boardPos[i, j] != Piece.Empty)
            {
                if (IsPieceWhite(boardPos[i, j]))
                {
                    eval_W += GetPieceWorth(boardPos[i, j]) + DistanceToCenter(i, j);
                    eval += GetPieceWorth(boardPos[i, j]);
                }
                else if (IsPieceBlack(boardPos[i, j]))
                {
                    eval_B += GetPieceWorth(boardPos[i, j]) + DistanceToCenter(i, j);
                    eval -= GetPieceWorth(boardPos[i, j]);
                }
            }
        }
    }

    if (ForWhite)
        return eval_W - eval_B;
    else
        return eval_B - eval_W;
}

I call with: minimaxAB(CurrentBoard, depthB, int.MinValue, int.MaxValue, true, whiteToMove);

I am aware that Minimax with AB is suppose to produce exactly the same result, but in my case it does not. I hope someone is able to spot what I did wrong.

Upvotes: 0

Views: 322

Answers (1)

foRei
foRei

Reputation: 9

I figured it out, I needed an alpha and beta for both white and black. The reason for this is that I call the minimaxAB function for both white and black moves.

Working method:

public int minimaxAB(Piece[,] board, int depth, int alpha_White, int beta_White, int alpha_Black, int beta_Black, bool maximizingPlayer, bool WhiteToPlay)
{
    if (depth == 0 || !HasKings(board))
    {
        return EvaluatePosition(board, WhiteToPlay);
    }

    var moves = GenerateMoves(board, WhiteToPlay);
    if (maximizingPlayer)
    {
        int value = int.MinValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimaxAB(move, depth - 1, alpha_White, beta_White, alpha_Black, beta_Black, false, !WhiteToPlay);
            value = Math.Max(value, minmaxResult);

            if (WhiteToPlay)
            {
                alpha_White = Math.Max(alpha_White, value);
                if (alpha_White >= beta_White)
                    return alpha_White;
            }
            else
            {
                alpha_Black = Math.Max(alpha_Black, value);
                if (alpha_Black >= beta_Black)
                    return alpha_Black;
            }

            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
    else
    {
        int value = int.MaxValue;
        foreach (var move in moves)
        {
            int minmaxResult = minimaxAB(move, depth - 1, alpha_White, beta_White, alpha_Black, beta_Black, true, !WhiteToPlay);
            value = Math.Min(value, minmaxResult);

            if (WhiteToPlay)
            {
                beta_White = Math.Min(beta_White, value);
                if (beta_White <= alpha_White)
                    return beta_White;
            }
            else
            {
                beta_Black = Math.Min(beta_Black, value);
                if (beta_Black <= alpha_Black)
                    return beta_Black;
            }
            if (depth == depthB)
            {
                moveScores.Add(move, minmaxResult);
            }
        }
        return value;
    }
}

Called with:

minimaxAB(CurrentBoard, depthB, int.MinValue, int.MaxValue, int.MinValue, int.MaxValue, true, whiteToMove);

Upvotes: -1

Related Questions