You are on page 1of 5

INTRO TO AI ASSINGMENT#02 (CS-251).

Usman Ghani
2022613.

Title: Enhancing Minimax Search Efficiency in Connect Four

Introduction:
Connect Four is a popular two-player strategy game where players aim to
create a line of four of their colored discs either vertically, horizontally, or
diagonally. The minimax search algorithm is commonly used to determine
the best move in Connect Four. However, in the worst-case scenario of
starting with an empty board, the algorithm faces challenges due to the vast
search space. This report explores various strategies to enhance the efficiency
of minimax search in Connect Four.
The initial state of the game is an empty grid of 7 columns and 6 rows.
Actions:
Each player can take an action by selecting one of the columns (numbered
from 1 to 7) where they want to drop their colored disc.An action results in
placing a disc of the player's color into the lowest available empty slot within
the selected column.
Transition Model (Result Function):
Given a current state and an action, the transition model determines the
resulting state.When a player selects a column, their colored disc is dropped
into the lowest available empty slot in that column, effectively modifying the
current state of the game.
Goal State (Terminal State and Utility):The game reaches a terminal state
when one player successfully forms a horizontal, vertical, or diagonal line of
four of their own discs, indicating a win.If the grid becomes full without
either player achieving a winning condition, the game ends in a draw. The
utility function assigns values to terminal states:
If Player A wins, the utility value is +1.
If Player B wins, the utility value is -1.
If the game ends in a draw, the utility value is 0.

1. Move Ordering:
Move ordering prioritizes certain moves to improve alpha-beta pruning
efficiency. Strategies like center-based move ordering can reduce the search
space by focusing on moves closer to the center column, where more winning
opportunities often arise.
class MinimaxAgent:
def __init__(self, max_depth=3):
self.max_depth = max_depth

def minimax_decision(self, state):


actions = state.actions()
ordered_actions = self.order_actions(actions)

best_action = None
best_value = -float('inf')

for action in ordered_actions:


new_state = state.result(action, 1)
value = self.min_value(new_state, depth=1)
if value > best_value:
best_value = value
best_action = action
return best_action

def order_actions(self, actions):


# Order actions based on their proximity to the center column
center_column = len(actions) // 2
ordered_actions = sorted(actions, key=lambda x: abs(x - center_column))
return ordered_actions

2. Depth Limitation:
Limiting the search depth reduces the number of nodes evaluated by the
minimax algorithm, balancing accuracy and computational complexity. By
setting a maximum depth, the algorithm explores a smaller portion of the
game tree, leading to faster decision-making.

3. Heuristic Evaluation:
Heuristic evaluation functions estimate the value of game states at a given
depth, providing approximate values for non-terminal states. These heuristics
enable the algorithm to make informed decisions without exhaustively
searching the entire game tree.

4.Game Environment:
import numpy as np
def empty_board(shape=(6, 7)):
return np.full(shape=shape, fill_value=0)
print(empty_board())
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]

5. The first few moves:


1. Depth Limitation: Limit the search depth to 3-5 moves initially to manage
the vast search space and speed up decision-making.
2. Opening Book: Create a book with evaluated moves for early game
configurations, favoring center moves and potential forks to quickly select
strong opening moves.
3. Evaluation Function Bias: Slightly favor offensive plays in the early game
to encourage exploration of opportunities to connect four.

4. Randomized Opening Move: For the first move, introduce controlled


randomness favoring center columns (3, 4, 5) over edge columns (1, 2, 6, 7)
to add variety while focusing on strategic areas.
6. Minimax Search with Alpha-Beta Pruning:
The ConnectFourAgent class represents a player in the Connect Four game,
utilizing the minimax algorithm for decision-making. It includes methods for
generating available actions, updating the game state, and evaluating terminal
states and utilities. The minimax_decision method selects the best action
based on maximizing player's utility, while max_value and min_value
functions recursively compute the utility values for maximizing and
minimizing players, respectively. This class efficiently implements the
minimax algorithm for Connect Four gameplay, providing a strategic
approach for decision-making.
7.ANALYSIS OF GAME:
The code is provided in the file.
Wins: 839
Losses: 161
Draws: 0

Conclusion:
In conclusion, employing strategies such as move ordering, depth limitation,
heuristic evaluation, transposition tables, parallelization, and iterative
deepening significantly enhances the efficiency of minimax search in
Connect Four. By implementing these techniques, the algorithm can make
informed decisions efficiently, even in challenging scenarios such as starting
with an empty board. These strategies are essential for developing robust and
high-performing Connect Four agents.

You might also like