Introduction to Artificial Intelligence
Problem-solving in AI
Chapter 5 : Adversarial Search Algorithms
MASTER : SCIENCE DES DONNÉES ET ANALYTIQUE
(SDA)
Jamal BAKKAS
Département Informatique
École Supérieure de Technologie-Safi
1
Search Algorithms: From Single-Agent to Multi-Agent Environments
• Single-agent environments: One agent operating independently within the
environment.
• Focus on finding the optimal solution in static environments.
• Examples: Pathfinding, puzzle solving.
• Multi-agent environments: Multiple agents interacting within the
environment.
• Two main types of interactions:
▪ Cooperative: Agents work together toward a shared goal.
▪ Competitive: Agents have conflicting objectives.
• Challenges in Competitive Scenarios:
• Requires strategic decision-making to outmaneuver opponents.
• The environment becomes dynamic and unpredictable due to agents' actions.
• Adversarial Search Algorithms are designed for competitive multi-agent scenarios,
often modeled as games, to navigate these challenges.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 2
Adversarial Search Problem
• The adversarial search problem in AI typically involves a two-player,
zero-sum game, where one player's gain is the other player's loss.
• The goal is to find the best sequence of moves (actions) that lead to a
favorable outcome for the searching agent.
• Adversarial search problems = games
• Key Characteristics:
o Two opposing agents with conflicting objectives.
o Players alternate turns to make strategic moves.
o The aim is to maximize one's own payoff while minimizing the opponent's, by
anticipating and countering their responses.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 3
Adversarial Search
• In games, other agents:
o Want our agent to lose.
o Introduce uncertainty, don’t know what will they do.
o The unpredictability leads to numerous possible contingencies.
• Key Assumptions for Our Focus
o Deterministic: The outcome of each action is predictable.
o Fully Observable: All agents have complete knowledge of the game
state.
o Two Agents: The game involves two agents taking alternating actions.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 4
Applications of Adversarial Algorithms
• Game-playing AI:
• Deep Blue: Utilized the minimax algorithm with alpha-beta pruning to defeat Garry Kasparov in chess.
• AlphaGo/AlphaZero: Integrated minimax, Monte Carlo tree search, and reinforcement learning to
master complex games like Go, chess, and shogi.
• Algorithmic Trading in Financial Systems: Competes in real time with other trading
algorithms to optimize profit in stock markets.
• Military and Security Simulations: Tactical Simulations: Military AI predicts and counters
adversary strategies in simulated conflict scenarios.
• Security Games: Models attacker behaviors to protect critical infrastructure, such as
airports and ports.
• Negotiation Simulations in Conflict Resolution: Simulates negotiations between parties
with conflicting interests, such as resource sharing or dispute resolution.
• Intrusion Detection Systems (IDS) in Cybersecurity: Leverages adversarial modeling to
detect, predict, and mitigate cyberattacks effectively.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 5
Adversarial Search
• Assumptions in Adversarial Search
o Two players compete directly against each other
o The play is sequential (agents taking turns).
o The opponent is playing perfectly.
• Adversarial Search Algorithms
o Minimax
o Alpha-Beta Pruning
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 6
Formalization of the problem:
A game can be defined as a type of search in AI which can be formalized of the following
elements:
• Initial state: It specifies how the game is set up at the start.
• Player(s): It specifies which player has moved in the state space.
• Action(s): It returns the set of legal moves in state space.
• Result(s, a): It is the transition model, which specifies the result of moves in the state space.
• Terminal-Test(s): Terminal test is true if the game is over, else it is false at any case. The game
ends in terminal states.
• Utility(s, p): A utility function gives the final numeric value for a game that ends in terminal
states s for player p.
o It is also called payoff function.
o For Chess, the outcomes are a win, loss, or draw and its payoff values are +1, -1, 0 (or ½).
o For tic-tac-toe, utility values are +1, -1, and 0.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 7
Minimax Algorithm
• Core Concept:
• A recursive algorithm to determine the optimal move for a player in a
competitive setting.
• Evaluates possible moves and their consequences.
• How it Works:
• Construct a game tree.
• Assign utility values to terminal states.
• Apply the minimax principle to propagate values up the tree.
• The player chooses the move with the highest utility value.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 8
Minimax Algorithm
• Maximizing Player (Max): Aims to maximize their score or utility
by making moves that improve their chances of success.
• Minimizing Player (Min): Aims to beat Max by minimizing their
score or utility, reducing their chances of success.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 9
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 10
Minimax Algorithm
For a state s minimax(s) =
Utility(s) if Terminal-test(s)
max a∈Actions(s) minimax(Result(s,a)) if Player(s) = Max
min a∈Actions(s) minimax(Result(s,a)) if Player(s) = Min
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 11
Minimax Algorithm
Utility
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 12
Minimax Algorithm
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 13
Minimax Algorithm : Pseudocode
function minimax(node, depth, maximizingPlayer) is
if depth = 0 or node is a terminal node then
return the heuristic value of node
if maximizingPlayer then // maximizing player
value := −∞
for each child of node do
value := max(value, minimax(child, depth − 1, FALSE))
return value
else // minimizing player
value := +∞
for each child of node do
value := min(value, minimax(child, depth − 1, TRUE))
return value
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 14
Minimax Algorithm : Practical Example
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 15
Properties of minimax
• Optimal : (if opponent plays optimally) and complete (finite tree)
• DFS time: O(bm )
• DFS space: O(bm)
– Tic-Tac-Toe
o ≈ 5 legal moves on average, total of 9 moves (9 plies).
o 59 = 1, 953, 125
o 9! = 362, 880 terminal nodes
– Chess
o b ≈ 35 (average branching factor)
o d ≈ 100 (depth of game tree for a typical game)
o bd ≈ 35 100 ≈ 10154 nodes
– Go branching factor starts at 361 (19X19 board)
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 16
Case of limited resources
• Problem: In real games, we are limited in time, so we can’t search
the leaves.
• To be practical and run in a reasonable amount of time and
memory, minimax can only search to some depth.
• More layers make a big difference.
• Solution:
1. Replace terminal utilities with an evaluation function for non-terminal
positions.
2. Use Iterative Deepening Search (IDS).
3. Use pruning: eliminate large parts of the tree.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 17
α-β pruning algorithm
• Alpha-beta pruning is an optimization technique for the minimax algorithm.
• It reduces the number of game states examined in the minimax search, which grows exponentially with
tree depth.
• The technique uses two threshold parameters, Alpha and Beta, to decide whether a branch of the tree
should be pruned (cut off from further evaluation).
• Pruning allows for skipping some nodes without affecting the correctness of the minimax decision.
• Alpha-beta pruning can prune entire subtrees, not just leaves, making it more efficient than the
standard minimax algorithm.
• The two-parameter can be defined as:
o Alpha: The best (highest-value) choice we have found so far at any point along the path of Maximizer. The initial value
of alpha is -∞.
o Beta: The best (lowest-value) choice we have found so far at any point along the path of Minimizer. The initial value of
beta is +∞.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 18
α-β pruning algorithm
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 19
α-β pruning algorithm : Pseudocode
function ALPHA-BETA(node, depth, alpha, beta, maximizingPlayer):
if depth == 0 or node is a terminal node:
return the heuristic value of node
if maximizingPlayer:
for each child of node:
alpha = max(alpha, ALPHA-BETA(child, depth - 1, alpha, beta, FALSE))
if beta <= alpha:
break // Beta cut-off
return alpha
else:
for each child of node:
beta = min(beta, ALPHA-BETA(child, depth - 1, alpha, beta, TRUE))
if beta <= alpha:
break // Alpha cut-off
return beta
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 20
α-β pruning algorithm
• Optimal : (if opponent plays optimally) and complete (finite tree)
• DFS time: O(bm/2 )
• DFS space: O(bm)
• Questions:
o Is alpha-beta pruning an alternative to the minimax algorithm?
o Does the alpha-beta pruning algorithm produce different results compared to the
minimax algorithm?
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 21
α-β pruning algorithm : practical example
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 22
Real-time decisions
• Minimax: generates the entire game search space
• α-β algorithm: prune large chunks of the trees
• BUT α-β still has to go all the way to the leaves
• Impractical in real-time (moves has to be done in a reasonable
amount of time)
• Solution: bound the depth of search (cut off search) and replace
utiliy(s) with eval(s), an evaluation function to estimate value of
current board configurations
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 23
Real-time decisions
• eval(s) is a heuristic at state s
o E.g., Chess: Value of all white pieces Value of all black pieces turn non-
terminal nodes into terminal leaves!
• An ideal evaluation function would rank terminal states in the
same way as the true utility function; but must be fast
• Represent the function as a linear weighted sum of the features.
• Utilize domain knowledge to design the most effective and
meaningful features.
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 24
Real-time decisions
• How does it works?
• Select useful features f1 , . . . , fn
o e.g., Chess: # pieces on board, value of pieces (1 for pawn, 3 for bishop, etc.)
• Weighted linear function:
• Learn wi from the examples
• Deep blue uses about 6,000 features!
j.bakkas@uca.ac.ma Master SDA Chapter 5 : Adversarial Search 25