Minimax Algorithm in Python

The minimax algorithm is a decision-making rule in different fields, such as artificial intelligence, decision theory, game theory, statistics, and philosophy. It is designed to minimize the potential loss in the worst-case scenario (maximum loss).

The minimax algorithm is a recursive algorithm used to make decisions and determine the best move for a player in game theory, assuming that their opponent also plays optimally. It is commonly used in only two-player games like tic-tac-toe, chess, Mancala, Backgammon, and more.

Minimax Algorithm in Python

Properties of minimax algorithm

  • The solution can be found within the finite game tree.
  • It is optimal if both players make the best possible move.
  • Minimax algorithm has time complexity O() due to depth-first search (DFS) on the game tree, where b represents the branching factor, and m represents the maximum depth of the tree.
  • The Minimax algorithm has a space complexity of O(bm), the same as depth-first search.
  • The Minimax algorithm creates a tree of possible moves in a game. Each level of the tree represents a turn a maximizing or minimizing player takes. The tree is evaluated using an evaluation function at its leaves. The algorithm selects the move that produces the best outcome for the maximizing player.
  • The minimax algorithm is a backtracking algorithm. It starts at the top of the tree and proceeds to the terminal node. Once it reaches the terminal node, it backtracks the tree as the recursion.

Game theory

In the game theory algorithm Minimax, two participants are the maximizer and the minimizer. The maximizer aims to achieve the highest score possible, while the minimizer wants to obtain the lowest score. Each board state has a corresponding value. If the maximizer has the advantage in a particular situation, the board score will usually be positive. Conversely, the board score will typically be negative if the minimizer has the advantage.

A game tree is a hierarchical representation of all possible moves and states that can arise in a two-player, sequential, deterministic, and perfect-information game. Each node in the tree symbolises a game state, and the branches originating from a node represent possible moves that can be made from that state.

The minimax algorithm is a backtracking algorithm. It starts at the top of the tree and proceeds to the terminal node. Once it reaches the terminal node, it backtracks the tree as the recursion.

The minmax algorithm's main strategy is to minimize the maximum possible loss, so the minmax algorithm starts by constructing a tree of all possible moves and their outcomes.

Example

Let us consider the game tree

Imagine a game that consists of 4 final states. To reach these final states, you must follow paths from the root of a perfect binary tree to 4 leaves. As the maximizing player, you will have the first chance to move, starting at the tree's root while your opponent will be at the next level.

The minimax algorithm recursively evaluates all possible moves for the current and opponent players. The algorithm alternates between maximizing and minimizing the node's value at each tree level.

  1. Start at the root node (20), the MAX player's turn.
  2. Generate all possible moves (selecting child nodes, i.e., 10 and 30).
  3. Calculate the Minimax value for each of the child nodes:
  4. For the left child (10):
    • It is now the MIN player's turn.
    • Generate all possible moves for the MIN player (5 and 15).
  5. Calculate the Minimax value for each of the MIN player's moves:
    • For 5: The value is 5 (a leaf node).
    • For 15: The value is 15 (a leaf node).
    • The MIN player wants to minimize, so the value for 10 becomes the minimum of the two child values, which is 5.
  6. For the right child (30):
    • It is now the MIN player's turn.
    • Generate all possible moves for the MIN player (25 and 35).
  7. Calculate the Minimax value for each of the MIN player's moves:
    • For 25: The value is 25 (a leaf node).
    • For 35: The value is 35 (a leaf node).
    • The MIN player wants to minimize, so the value for 30 becomes the minimum of the two child values, which is 25.
  8. Now, it is the MAX player's turn again. Calculate the Minimax value for the root node (20):
    • For 10: The value is 5.
    • For 30: The value is 25.
    • The MAX player wants to maximize, so the value for 20 becomes the maximum of the two child values, which is 25.

Final score:

The final score for the player trying to maximize their score is 25. This is because the minimax algorithm has selected the move that leads to the highest score for the maximizing player at each level of the tree. Therefore, the optimal move for the MAX player is to choose the right child node with a value of 30.

Alpha-beta pruning

Alpha-beta pruning optimizes the minimax algorithm by reducing the number of evaluated nodes. It achieves this by eliminating branches of the game tree that cannot lead to a better outcome for the current player; this makes the algorithm more efficient, especially when dealing with large game trees.

pseudo code

In this pseudocode:

  • The term "node" refers to the current state of the game.
  • Depth represents the current depth in the game tree. You can set a maximum depth to limit the search.
  • MaximizingPlayer is a boolean value indicating whether the current player is attempting to maximize their score (True) or minimize their opponent's score (False).
  • The algorithm explores the game tree recursively by considering all possible moves and their outcomes.
  • It selects the child node with the highest value for the maximizing player.
  • For the minimizing player, it selects the child node with the lowest value (best move for the opponent).
  • The base case occurs when the depth is 0 or the current node is terminal, such as a win, loss, or draw. In these cases, a heuristic value is returned for the node.

You should call the minimax function with the initial game state and set maximizingPlayer to True for the player whose turn is to maximize their score. The function returns the best move for that player.

Advantages

The advantages of the minimax algorithm are as follows:

  • It is a complete algorithm, meaning it always finds a solution to the problem, provided that one exists.
  • The algorithm is optimal, assuming the opponent is also playing optimally.
  • The algorithm is easy to understand and implement.
  • It has been used to create strong AI players for chess, checkers, Go, etc.
  • Although initially designed for two-player zero-sum games, the Minimax algorithm can be adapted and extended to solve many decision-making problems.

Limitation

The minimax algorithm's most significant drawback is its tendency to slow down significantly when playing complex games like chess or Go. The minimax algorithm becomes very slow due to the many possible moves. These games have many branches, and players have numerous options. However, using alpha-beta pruning can clear this drawback.

Applications

  • Game playing: The minimax algorithm is commonly used in two-player games like chess, checkers, and Go to determine the optimal move for a player, assuming that the opponent is also playing optimally.
  • Decision making: The minimax algorithm is not only limited to game theory. It can also be applied to finance, economics, and military strategy. It's a useful tool to determine the best investment strategy, set optimal prices for goods and services, and develop military strategies that result in minimal losses.
  • AI Research: The minimax algorithm is a foundational tool in artificial intelligence research. It is used to develop new AI algorithms and test the performance of existing ones.
  • Stock trading: The minimax algorithm can create trading strategies that minimize losses and maximize profits.
  • Self-driving cars: Self-driving cars utilize the minimax algorithm to determine the best route while considering obstacles and traffic conditions.

Conclusion

The minimax algorithm is a powerful decision-making tool for two-player zero-sum games. It has proven to be a highly effective tool for developing intelligent AI players in games like chess, checkers, and Go. It has also found applications in non-game settings, such as decision-making, machine learning, and self-driving cars. The algorithm's versatility and accuracy make it popular for various applications.

Although the minimax algorithm has limitations, such as high computational cost and inability to handle uncertainty, it remains useful for various applications.