You are on page 1of 123

Unit 3: Searching

Hridaya Kandel
hridayakandel@gmail.com
AI Lecturer
BScCSIT Vth

Hridaya Kandel (AI Lecturer) 1


Objective
The student should be familiar with some searching algorithms
The student should be able to understand and analyze the properties
of these algorithms
Analyze a given problem and identify the most suitable search
strategy for the problem.

Hridaya Kandel (AI Lecturer) 2


Problem Solving: Steps
Steps in Problem Solving
Goal formulation
o What are the successful world states
o Goal help to organize behavior by limiting the objectives that the agent is
trying to achieve.
Problem formulation
o What actions and states to consider given the goal
Search
o Determine the possible sequence of actions that lead to the states of known
values and then choosing the best sequence.
Execute
o Give the solution perform the actions.

Hridaya Kandel (AI Lecturer) 3


Searching
A search algorithm takes a problem as input and returns a solution in
form of an action sequence.

A problem is defined by:


An initial state: State from which agent start
Successor function: Description of possible actions available to the
agent.
Goal test: Determine whether the given state is goal state or not
Path cost: Sum of cost of each path from initial state to the given
state.

A solution is a sequence of actions from initial to goal state. Optimal


solution has the lowest path cost.

Hridaya Kandel (AI Lecturer) 4


states vs. nodes
State
(Representation of) a physical configuration
Node
Data structure constituting part of a search tree
Includes parent, children, depth, path cost g(x)

States do not have parents, children, depth, or path cost!

Hridaya Kandel (AI Lecturer) 5


Search strategies
A strategy is defined by picking the order of node expansion
Strategies are evaluated along the following dimensions:
Completeness does it always find a solution if one exists?
Time complexity number of nodes generated/expanded
Space complexity maximum nodes in memory
Optimality does it always find a least-cost solution?

Time and space complexity are measured in terms of:


b maximum branching factor of the search tree (may be infinite)
d depth of the least-cost solution
m maximum depth of the state space (may be infinite)

Hridaya Kandel (AI Lecturer) 6


Search Tree

A state Space Graph A search Tree for state space Graph

Hridaya Kandel (AI Lecturer) 7


Algorithm: Searching For Solutions

Hridaya Kandel (AI Lecturer) 8


Search Strategies
1. Uninformed search (blind search)
Having no information about the number of steps from the
current state to the goal.
Only information available in problem definition

2. Informed search (heuristic search)


More efficient than uninformed search.
Some more information available

Hridaya Kandel (AI Lecturer) 9


Uninformed Search Strategies

Uninformed strategies use only the information


available in the problem definition
Also known as blind searching

Breadth-first search
Uniform-cost search
Depth-first search
Depth-limited search
Iterative deepening search

Hridaya Kandel (AI Lecturer) 10


Breadth-First Search

Expand the shallowest unexpanded node

Place all new successors at the end of a FIFO queue

Hridaya Kandel (AI Lecturer) 11


Breadth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:12Solving 12
Problems by Searching
Breadth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:13Solving 13
Problems by Searching
Breadth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:14Solving 14
Problems by Searching
Breadth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:15Solving 15
Problems by Searching
Properties of Breadth-First Search
Complete
Yes if b (max branching factor) is finite
Time
1 + b + b2 + + bd + b(bd-1) = O(bd+1)
exponential in d
Space
O(bd+1)
Keeps every node in memory
This is the big problem; an agent that generates nodes at 10 MB/sec
will produce 860 MB in 24 hours
Optimal
Yes (if cost is 1 per step); not optimal in general

Hridaya Kandel (AI Lecturer) 16


Depth-First Search

Expand the deepest unexpanded node

Unexplored successors are placed on a stack until fully explored

**LIFO

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:17Solving 17
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:18Solving 18
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:19Solving 19
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:20Solving 20
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:21Solving 21
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:22Solving 22
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:23Solving 23
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:24Solving 24
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:25Solving 25
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:26Solving 26
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:27Solving 27
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:28Solving 28
Problems by Searching
Depth-First Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:29Solving 29
Problems by Searching
Depth-First Search
Complete
No:
fails in infinite-depth spaces,
spaces with loops
Modify to avoid repeated spaces along path
Yes: in finite spaces
Time
O(bm)
Not great if m is much larger than d
But if the solutions are dense, this may be faster than breadth-first search
Space
O(bm)linear space
Optimal
No

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:30Solving 30
Problems by Searching
Depth-Limited Search
a) A variation of depth-first search that uses a depth limit
Alleviates the problem of unbounded trees
Search to a predetermined depth l (ell)
Nodes at depth l have no successors

b) Same as depth-first search if l =


c) Can terminate for failure and cutoff

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:31Solving 31
Problems by Searching
Depth-Limited Search
a) Complete
Incompleteness if l < d
b) Time
O(bl)
c) Space
O(bl)
d) Optimal
No if l > d

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:32Solving 32
Problems by Searching
Iterative Deepening Search
a) Iterative deepening depth-first search
Uses depth-first search
Finds the best depth limit
Gradually increases the depth limit; 0, 1, 2, until a goal is
found

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:33Solving 33
Problems by Searching
Iterative Deepening Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:34Solving 34
Problems by Searching
Iterative Deepening Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:35Solving 35
Problems by Searching
Iterative Deepening Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:36Solving 36
Problems by Searching
Iterative Deepening Search

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:37Solving 37
Problems by Searching
Iterative Deepening Search
Complete
Yes
Time
Algorithm seems costly due to repeated generation of certain states.
Node generation:
level d: once
level d-1: 2
level d-2: 3

level 2: d-1
level 1: d
Total no. of nodes generated:
d.b +(d-1). b2 + (d-2). b3 + ..+1. bd = O(bd)
Space
O(bd)
Optimal
Yes if step cost = 1
Can be modified to explore uniform cost tree
January 26, (AI2003
Hridaya Kandel Lecturer) AI: Chapter 3:38Solving 38
Problems by Searching
Lessons From Iterative Deepening Search

a) Faster than BFS even though IDS generates repeated states


BFS generates nodes up to level d+1
IDS only generates nodes up to level d

b) In general, iterative deepening search is the preferred uninformed


search method when there is a large search space and the depth of
the solution is not known

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:39Solving 39
Problems by Searching
Avoiding Repeated States
a) Complication of wasting time by expanding states that
have already been encountered and expanded before
Failure to detect repeated states can turn a linear problem into
an exponential one

b) Sometimes, repeated states are unavoidable


Problems where the actions are reversable
Route finding
Sliding blocks puzzles

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:40Solving 40
Problems by Searching
Avoiding Repeated States

State Space Search Tree

January 26, (AI2003


Hridaya Kandel Lecturer) AI: Chapter 3:41Solving 41
Problems by Searching
Bidirectional Search
It runs two simultaneous searches. one forward from the initial state and one
backward from the goal, stopping when the two meet in the middle.
The motivation is that bd/2 +bd/2 is much less than bd
Bidirectional Search is implemented by having one or both of the searches check
each node before it is expanded to see if it is in the fringe of the other search tree.
Even if theoretically bidirectional search seems effective than unidirectional search
practically the case may be different because finding out where two search
algorithms (forward and backward search) intersects needs additional data structure
to be maintained and addition implementation steps to identify whether two search
algorithms intersects or not.

Hridaya Kandel (AI Lecturer) 42


Informed Search
Uninformed searches
easy
but very inefficient in most cases of huge
search tree
Informed searches
uses problem-specific information to reduce
the search tree into a small one
resolve time and memory complexities

Hridaya Kandel (AI Lecturer) 43


Heuristic Search
Problem + some other information or Knowledge.
Heuristic Search Uses domain-dependent (heuristic) information in
order to search the space more efficiently
Define a heuristic function, h(n), that estimates the goodness of a
node n.
Specifically, h(n) = estimated cost (or distance) of minimal cost path
from n to a goal state.
The heuristic function is an estimate, based on domain-specific
information that is computable from the current state description, of
how close we are to a goal

Ways of using heuristic information


Decide which node to expand without following strict rules
Decide which successors to generate instead of blindly generating all possible
Hridaya
Deciding that certain nodes should44 be discarded, or pruned, from the search
Kandel (AI Lecturer)

space.
Best-First Search
Idea: use an evaluation function f(n) for each node
estimate of "desirability
Expand most desirable unexpanded node

Implementation:
Order nodes on the nodes list(fringe) by increasing value of an evaluation function,
f(n), that incorporates domain-specific information in some way.

A key component of f(n) is a heuristic function, h(n),which is a additional


knowledge of the problem.

There is a whole family of best-first search strategies, each with a different


evaluation function.

Special cases: based on the evaluation function.


Greedy best-first search
A*search

Hridaya Kandel (AI Lecturer) 45


Greedy best-first search
Selects node to expand believed to be closest (hence greedy) to a
goal node (i.e., select node with smallest f value)
It does not always gives optimal solution. Optimality of the solution
depends upon how good our heuristic is. Once the greedy best first
search takes a node, it never looks backward.

Heuristic used by Greedy Best First Search is:


f(n) = h(n)
Where h(n) is the estimated cost of the path from node n to the goal node.

Hridaya Kandel (AI Lecturer) 46


Greedy best-first search

Let h(n)= Straight Line distance


Hridaya Kandel (AI Lecturer) 47
Greedy best-first search

Hridaya Kandel (AI Lecturer) 48


Greedy best-first search

Hridaya Kandel (AI Lecturer) 49


Greedy best-first search

Hridaya Kandel (AI Lecturer) 50


Greedy best-first search

Hridaya Kandel (AI Lecturer) 51


Evaluation: Greedy best-first search

Completeness:
While minimizing the value of heuristic
greedy may oscillate between two nodes.
Thus it is not complete.
Optimality: (fig)
Greedy search is not optimal. Same as DFS.
Time complexity
In worst case Greedy search is same as DFS
therefore its time complexity is O(bm).
Space Complexity:
Space complexity is O(bm). No nodes can be
deleted from memory.

Hridaya Kandel (AI Lecturer) 52


Evaluation: Greedy best-first search

Similar to depth-first search


not optimal
incomplete

suffers from the problem of repeated states


causing the solution never be found
The time and space complexities
depends on the quality of h

Hridaya Kandel (AI Lecturer) 53


A* search
A star
The most well-known best-first search
evaluates nodes by combining
path cost g(n) and heuristic h(n)
f(n) = g(n) + h(n)
Where
g(n): the actual shortest distance traveled from initial node to
current node
h(n): the estimated (or "heuristic") distance from current node
to goal
f(n): the sum of g(n) and h(n)
What sets A* apart from a greedy best-first search is that it also
takes the distance already traveled into account (the g(n) part of the
heuristic is the cost from the start, and not simply the local cost from
the previously expanded node) 54
Hridaya Kandel (AI Lecturer)
A* search

Hridaya Kandel (AI Lecturer) 55


Hridaya Kandel (AI Lecturer) 56
Hridaya Kandel (AI Lecturer) 57
Hridaya Kandel (AI Lecturer) 58
F(G)= 19 + 0 = 19
F(B)= 20 + 4 = 24

Hridaya Kandel (AI Lecturer) 59


F(G)= 19 + 0 = 19
F(B)= 20 + 4 = 24

Hridaya Kandel (AI Lecturer) 60


Evaluating A* Search:

Completeness:
Yes A* search always gives us solution

Optimality:
A* search gives optimal solution when the heuristic function is admissible
heuristic.

Time complexity:
Exponential with path length i.e. O(bd) where d is length of the goal node from
start node.

Space complexity:
It keeps all generated nodes in memory. Hence space is the major problem not
time

Hridaya Kandel (AI Lecturer) 61


Admissible heuristic
An admissible heuristic never overestimates the cost to reach
the goal, i.e., it is optimistic
i.e
h(n) h*(n)
Where
h(n) = Estimated cost to reach to the goal node from node n
h*(n) = Actual cost to reach to the goal node from node n

Formulating admissible heuristics:


n is a node
h is a heuristic
h(n) is cost indicated by h to reach a goal from n
C(n) is the actual cost to reach a goal from n
h is admissible if
Hridaya Kandel (AI Lecturer) 62
Admissible heuristic: example
Figure shows 8-puzzle start state and
goal state. The solution is 26 steps
long.

h1(n) = number of misplaced tiles


h2(n) = total Manhattan distance
(i.e., no. of squares from desired
location of each tile/ sum of the
distance of the tiles from their goal
position (not diagonal).) Note: Dominance
If h2(n) h1(n) for all n (both admissible)
h1(S) = ? 8 then h2 dominates h1
h2(S) = ? 3+1+2+2+2+3+3+2 = 18 h2 is better for search

Hridaya Kandel (AI Lecturer) 63


Optimality of A* (proof)
A* search gives optimal solution when the heuristic is admissible.
Theorem: If h(n) is admissible, A* using TREE-SEARCH is optimal
Suppose some suboptimal goal G2 has been generated and is in the fringe. Let
n be an unexpanded node in the fringe such that n is on a shortest path to an
optimal goal G and C* be the cost of optimal goal node.
f(G2 ) = h (G2) + g(G2 )
f(G2) = g(G2) since h(G2) = 0
f(G2 ) > C* ..(1)
Again
Since h(n) is admissible, It does not overestimates the cost of completing the
solution path.
f(n) = g(n) + h(n) C* (2)
Now from (1) and (2)
f(n) C* < f(G2)
Since f(G2) > f(n), A* will never select G2 for expansion. Thus A* gives us
optimal solution when heuristic function is admissible.
Hridaya Kandel (AI Lecturer) 64
Consistent heuristics
A heuristic is said to be consistent if for any node N and any successor N of N ,
estimated cost to reach to the goal from node N to goal node is less than the sum of
step cost from N to N and estimated cost from node N to goal node.
i.e
h(n) d(n, n) + h(n)
Where
h(n) = Estimated cost to reach to the goal node from node n
d(n, n) = actual cost from n to n

A heuristic is consistent if for every node n, every successor n' of n generated by


any action a, h(n) c(n,a,n') + h(n')
Proof: If h(n) is consistent , then the values of f(n) along the path are
nondecreasing.

Hridaya Kandel (AI Lecturer) 65


Consistent heuristics
Proof: If h(n) is consistent , then the values of f(n) along the path
are nondecreasing.
Suppose n is successor of n, then
g(n) = g(n) + C(n,a,n)
we know that,
f(n) = g(n) + h(n)
f(n) = g(n) + C(n,a,n) + h(n) .(1)
A heuristic is consistent if
h(n) C(n,a,n) + h(n)(2)
Now from (1) and (2)
f(n) = g(n) + C(n,a,n) + h(n) g(n) + h(n) = f(n)
f(n) f(n)
f(n) is nondecreasing along any path.

Hridaya Kandel (AI Lecturer) 66


Relaxed problems
A problem with fewer restrictions on the actions is called a
relaxed problem
The cost of an optimal solution to a relaxed problem is an
admissible heuristic for the original problem

For the 8 queen discussed above.


If the rules of the 8-puzzle are relaxed so that a tile can move anywhere,
then h1(n) gives the shortest solution
If the rules are relaxed so that a tile can move to any adjacent square,
then h2(n) gives the shortest solution

Hridaya Kandel (AI Lecturer) 67


Global Search Vs. Local Search
Take an example of N-queen.
To search for a solution, there are two fundamentally
different techniques
1. Global (systematic) search Strategy
Place one queen at a time, starting with one in the first row, and
second in second row so on following the constraint.
At some point(say i row) we may not find the open position in a
row.
We go back(i-1th row) and find the position for queen in i-1 row to
shift. We may not find the position so we may have to go back to the
queen in i-2nd position. (Backtracking)
This will eventually search full space of all possible placement of
queens. (what if space is exponentially large)

Hridaya Kandel (AI Lecturer) 68


Global Search Vs. Local Search
2. Local (incremental improvement) search Strategy
Start with a random guess at a solution
Placing a queen in each row, Where position within a row is
chosen randomly.
We can have one or more pairs attacking each other.

We now select one queen on board that is under attack.


We move this queen to another location in its row finding the
position that is not attacked.
If all position in row attacked find the least one.

If after shifting a queen which is referred as local move


or local modification, we still have queens under attack,
we again select the queen randomly and find the least
attack position for that queen.
Hridaya Kandel (AI Lecturer) 69
This Strategy is surprisingly effective
Global Search Vs. Local Search
Local (incremental improvement) search Strategy -benefits
It requires only a limited amount of memory (only the current state is
stored)
It is easy to implement efficiently
If one or more solution exists in search space, it can be surprisingly
effective at finding it.

Drawback
An important drawback of local search is that if no
solution exists, a local search method will simply
continue to make local modification indefinitely.

Hridaya Kandel (AI Lecturer) 70


Local search algorithms
So far, we are finding solution paths by searching
(Initial state goal state)
In many problems, however,
the path to goal is irrelevant to solution
e.g., 8-queens problem
solution
the final configuration
not the order they are added or modified
Hence we can consider other kinds of method
Do not worry about the paths at all.
Local search

Hridaya Kandel (AI Lecturer) 71


Local search algorithms
Operates using a single current state (rather than multiple paths) and
generally move only to neighbors of that state.
The paths followed by the search are not retained hence the method is
not systematic
Two advantages :
1. uses little memory a constant amount
for current state and some information
2. can find reasonable solutions in large or infinite (continuous) state
spaces where systematic algorithms are unsuitable
suitable for optimization problems in which the aim is to find the best
state according to an objective function
Some examples:
Hill climbing
Simulated annealing
Local beam search
Kandel (AIGenetic
Hridaya Lecturer) algorithms 72

Tabu search
Local search algorithms
To understand local search, consider the state space landscape(fig).
A landscape has both location (defined by the state) and the
elevation( defined by the value of the heuristic cost function or
objective function).
If elevation corresponds to cost then, the aim is to find lowest valley(
global minimum).
If elevation corresponds to an objective function, then the aim is to
find highest peak( global maximum).
A complete local search algorithm
always finds a goal if one exists
An optimal algorithm
always finds a global maximum/minimum

Hridaya Kandel (AI Lecturer) 73


Local search algorithms
Elevation

Location
Figure: One Dimensional State space Landscape

Hridaya Kandel (AI Lecturer) 74


Multi dimensional state space landscape

Hridaya Kandel (AI Lecturer) 75


Hill-climbing search
greedy local search (grabs a good neighbor state without
thinking ahead about where to go next)
It always selects the most promising successor of the node
last expanded.
simply a loop
It continually moves in the direction of increasing value
"Like climbing Everest in thick fog with amnesia"
i.e., uphill
a) It terminate when it reaches the peak.
b) No search tree is maintained
c) The node need only record
the state
its evaluation (value, real number)
Hridaya Kandel (AI Lecturer) 76
Drawbacks of Hill-climbing search
Hill climbing often gets stuck for the following reasons.
Local Maxima
A local maxima is the pick that is higher than each of its
neighboring states but lower than global maxima.
The algorithm stops even though the solution is far from
satisfactory
Figure: Ridges
Ridges
The grid of states is overlapped on a ridge rising from left to right
Ridges result in a sequence of local maxima that is very difficult
for a greedy algorithm to navigate.

Platuex

Hridaya Kandel (AI Lecturer) 77


Drawbacks of Hill-climbing search
Platuex
An area of the state space landscape where the evaluation function
is flat
It can be flat Local Maxima, from which no uphill exit exits. or
Can be a shoulder, from which it is possible to make progress.
A hill climbing search might be unable to find its way off the
plateau.
Note:
The difference between the hill climbing search method and the best first
search method is the following one:
the best first search method selects for expansion the most
promising leaf node of the current search tree;
the hill climbing search method selects for expansion the most
promising successor of the node last expanded.

Hridaya Kandel (AI Lecturer) 78


Simulated annealing
It is motivated by the physical annealing process in which
material is heated and slowly cooled into a uniform
structure.
Idea: escape local maxima by allowing some "bad" moves
but gradually decrease their frequency
SA can avoid becoming trapped at local minima.
SA uses a random search that accepts changes that increase
objective function f, as well as some that decrease it.
SA uses a control parameter T, which by analogy with the
original application is known as the system
temperature.
T starts out high and gradually decreases toward 0.

Hridaya Kandel (AI Lecturer) 79


Simulated annealing
The probability of accepting a worse state (Bad)is given by the
equation
P = exp(-c /t) > r
Where
c = the change in the evaluation function
t = the current value
r = a random number between 0 and 1

The higher the temperature, the more likely it is that a bad move can be
made.
As T tends to zero, this probability tends to zero, and SA becomes
more like hill climbing
If T is lowered slowly enough, SA is complete and admissible.

Hridaya Kandel (AI Lecturer) 80


Local beam search
Keeping only one current state is no good
Hence local beam search keeps
Begin with k states
all k states are randomly generated initially
at each step,
all successors of k states are generated
If any one is a goal, then halt!!
else select the k best successors
from the complete list and repeat

Stochastic beam search: Probability of keeping a state is


a function of its heuristic value
Hridaya Kandel (AI Lecturer) 81
Genetic Algorithms
GA
a variant of stochastic beam search
successor states are generated by
combining two parent states
rather than modifying a single state
successor state is called an offspring
a) GA works by first making a population
a set of k randomly generated states

Hridaya Kandel (AI Lecturer) 82


Genetic Algorithms
Each state, or individual is represented as a string over a finite
alphabet, e.g., binary or 1 to 8, etc.
The production of next generation of states
is rated by the evaluation function
or fitness function
returns higher values for better states
Next generation is chosen
based on some probabilities fitness function

Hridaya Kandel (AI Lecturer) 83


Genetic Algorithms
Operations for reproduction
cross-over
combining two parent states randomly
cross-over point is randomly chosen from the
positions in the string
mutation

modifying the state randomly with a small
independent probability
Efficiency and effectiveness
are based on the state representation
different algorithms

Hridaya Kandel (AI Lecturer) 84


Tabu search

Problem: Hill climbing can get stuck on local maxima


Solution: Maintain a list of k previously visited states, and
prevent the search from revisiting them

Hridaya Kandel (AI Lecturer) 85


Game Search
So far, in problem solving, single agent search
The machine is exploring the search space by itself.
No opponents or collaborators.

Games require generally multiagent (MA) environments:


Any given agent need to consider the actions of the other agent and
to know how do they affect its success?
Distinction should be made between cooperative and competitive
MA environments.
Competitive environments: give rise to adversarial search: playing a
game with an opponent.

Hridaya Kandel (AI Lecturer) 86


Game Search
We will consider the general case of a game with two players, whom
we will call MAX and M1N
Max always moves first.
Min is the opponent.
A game can be formally defined as a kind of search problem with the
following components:
An initial state. includes the board position and an indication of whose move
it is
A set of operators. define the legal moves that a player can make.
A terminal test (which tells us when the game is over).
A utility function/payoff function (evaluation function). gives a numeric
value for the outcome of a game. In chess, the outcome is a win, loss, or
draw, which we can represent by the values +1, 1, or 0.
The utility function is like the heuristic function we have seen in the
past, except it evaluates a node in terms of how good it is for each
player. Positive values indicate states advantageous for Max,
Hridaya Kandel (AI Lecturer) 87

negative values indicate states advantageous for Min.


Games as Search Problems

Games have a state space search


Each potential board or game position is a state
Each possible move is an operation to another
state i.e action

The state space can be HUGE!!!!!!!


Large branching factor (about 35 for chess)
Terminal state could be deep (about 50 for chess)

Hridaya Kandel (AI Lecturer) 88


Games vs. Search Problems

Unpredictable opponent
Solution is a strategy
Specifying a move for every possible opponent
reply
Time limits
Unlikely to find the goalagent must
approximate

Hridaya Kandel (AI Lecturer) 89


Game Trees
The initial state and the legal moves for each side define the game
tree for the game.

The root of the tree is the initial state


Next level is all of MAXs moves
Next level is all of MINs moves

Example: Tic-Tac-Toe
Root has 9 blank squares (MAX)
Level 1 has 8 blank squares (MIN)
Level 2 has 7 blank squares (MAX)

Utility function:
win for X is +1
Hridaya Kandel
win(AI Lecturer)
for O is -1 90

Draw 0
Tic-Tac toe
Figure below shows part of the game tree for tic-tac-toe

Each level of search nodes in the tree


corresponds to all possible board configurations
for a particular player MAX or MIN.

Utility values found at the end can be returned


back to their parent nodes.

Idea: MAX chooses the board with the max


utility value, MIN the minimum.

Hridaya Kandel (AI Lecturer) 91


A (partial) search tree for the game of Tic-Tac-Toe.
Minimax Strategy
Given a game tree, the optimal strategy can be determined by examining the
minimax value of each node, which we write as MINIMAX- VALUE(n).
Basic Idea:
Choose the move with the highest minimax value
Choose moves that will lead to a win, even though min is trying to block
Maxs goal: get to 1
Mins goal: get to -1
Minimax value of a node (backed up value):
If N is terminal, use the utility value
If N is a Max move, take max of successors
If N is a Min move, take min of successors
Or
Furthermore, given a choice, MAX will prefer to move to a state of maximum value, whereas
MIN prefers a state of minimum value. So we have the following:

MINMAX-VALUE(n): =

Hridaya Kandel (AI Lecturer) 92


Minimax example
A simple abstract game.
Max makes a move, then Min replies.
Even a simple game like tic-tac-toe is too complex for us to draw the entire game tree, so we
will switch to the trivial game as in the figure below:
An action by one player is called a ply, two ply (a action and a counter action) is called a move.

A1 A3
A2

A11 A21 A22 A31 A32


A12 A13 A23 A33

3 12 8 2 4 6 14 5 2

nodes are "MAX nodes," in which it is MAX'S turn to move, and the nodes are
"MIN nodes.
MAX'S best move at the root is A1, because it leads to the successor with the highest
minimax value, and MIN'S best reply is A11, because it leads to the successor with the
Hridaya Kandel (AI Lecturer) 93
lowest minimax value
An optimal procedure: The Min-Max
method
Designed to find the optimal strategy for Max and find best move:

1. Generate the whole game tree to leaves


2. Apply utility (payoff) function to leaves
3. Back-up values from leaves toward the root:
a Max node computes the max of its child values
a Min node computes the Min of its child values
4. When value reaches the root: choose max value and the
corresponding move.

However: It is impossible to develop the whole search tree, instead


develop part of the tree and evaluate promise of leaves using a static
evaluation function.
Hridaya Kandel (AI Lecturer) 94
Min-Max Algorithm
The minimax algorithm computes the minimax decision from the current
state.

It uses a simple recursive computation of the minimax values of each


successor state, directly implementing the defining equations.

The recursion proceeds all the way down to the leaves of the tree, and then
the minimax values are backed up through the tree as the recursion unwinds

Basic Algorithm
Let us assign the following values for the game: 1 for win by X, 0 for draw, -1
for loss by X.
Given the values of the terminal nodes (win for X (1), loss for X (-1), or draw
(0)), the values of the non-terminal nodes are computed as follows:
the value of a node where it is the turn of player X to move is the maximum
of the values of its successors (because X tries to maximize its outcome);
the value of a node where it is the turn of player O to move is the minimum
ofHridaya
theKandel
values of its successors (because
(AI Lecturer) 95 O tries to minimize the outcome of

X).
Min-Max Algorithm

Hridaya Kandel (AI Lecturer) 96


Minimax example
.

A1 A3
A2

A11
A12 A13 A21 A22 A23 A31 A32 A33

3 12 8 2 4 6 14 5 2

MINIMAX-VALUE(root) = max(min(3,12,8), min(2,4,6), min(14,5,2))


= max(3,2,2)
=3
Hridaya Kandel (AI Lecturer) 97
Evaluation: minimax
The minimax algorithm performs a complete depth-first exploration
of the game tree.
Complete
Yes if the tree is finite (e.g. chess has specific rules for this)
Optimal
Yes, against an optimal opponent
Time
O(bm)
Space
O(bm) depth first exploration of the state space

Limitations
Not always feasible to traverse entire tree
Time limitations
Hridaya Kandel (AI Lecturer) 98
Alpha-Beta Pruning
The problem with minimax search is that the number of game states
it has to examine is exponential in the number of moves.

Unfortunately we can't eliminate the exponent, but we can effectively


cut it in half. The trick is that it is possible to compute the correct
minimax decision without looking at every node in the game tree.

Is there some way we can search deeper in the same amount of time?

The particular technique we will examine is called alpha-beta


pruning. When applied to a standard minimax tree, it returns the
same move as minimax would, but prunes away branches that
cannot possibly influence the final decision.
Hridaya Kandel (AI Lecturer) 99
Alpha-Beta Pruning
Alpha-beta pruning gets its name from the following two parameters
that describe bounds on the backed-up values that appear anywhere
along the path:

(Alpha)=the value of the best (i.e., highest-value) choice we have


found so far at any choice point along the path for MAX.

(Beta)= the value of the best (i.e., lowest-value) choice we have


found so far at any choice point along the path for MIN.

Hridaya Kandel (AI Lecturer) 100


Example: Alpha-Beta Pruning

The first leaf below B has the value 3. Hence, B, which is a MIN
node, has a value of at most 3

Hridaya Kandel (AI Lecturer) 101


Example: Alpha-Beta Pruning

The second leaf below B has a value of 12; MIN would avoid this
move, so the value of B is still at most 3

Hridaya Kandel (AI Lecturer) 102


Example: Alpha-Beta Pruning

The third leaf below B has a value of 8; we have seen all B's
successors, so the value of B is exactly 3. Now, we can infer that the
value of the root is at least 3, because MAX has a choice worth 3 at
the root.
Hridaya Kandel (AI Lecturer) 103
Example: Alpha-Beta Pruning

The first leaf below C has the value 2. Hence, C, which is a MIN
node, has a value of at most 2. But we know that B is worth 3, so
MAX would never choose C. Therefore, there is no point in looking
at the other successors of C.
Hridaya Kandel (AI Lecturer) 104
Example: Alpha-Beta Pruning

The first leaf below D has the value 14, so D is worth at most 14.
This is still higher than MAX'S best alternative (i.e., 3), so we need
to keep exploring D's successors. Notice also that we now have
bounds on all of the successors of the root, so the root's value is also
Hridaya Kandel (AI Lecturer) 105

at most 14
Example: Alpha-Beta Pruning

The second successor of D is worth 5, so again we need to keep


exploring

Hridaya Kandel (AI Lecturer) 106


Example: Alpha-Beta Pruning

[,+] A

[3,3] B [,2] C [2,2] D

3 12 8 2 14 5 2

The third successor is worth 2, so now D is worth exactly 2. MAX'S


decision at the root is to move to B, giving a value of 3
Hridaya Kandel (AI Lecturer) 107
Example: Alpha-Beta Pruning

[,+] A

[3,3] B [,2] C [2,2] D

3 12 8 2 14 5 2

The third successor is worth 2, so now D is worth exactly 2. MAX'S


decision at the root is to move to B, giving a value of 3
Hridaya Kandel (AI Lecturer) 108
Exercise

Hridaya Kandel (AI Lecturer) 109


Solution

Hridaya Kandel (AI Lecturer) 110


Steps

Hridaya Kandel (AI Lecturer) 111


Steps

Hridaya Kandel (AI Lecturer) 112


Steps

Hridaya Kandel (AI Lecturer) 113


Steps

Hridaya Kandel (AI Lecturer) 114


Steps

Hridaya Kandel (AI Lecturer) 115


Steps

Hridaya Kandel (AI Lecturer) 116


Steps

Hridaya Kandel (AI Lecturer) 117


Steps

Hridaya Kandel (AI Lecturer) 118


Steps

Hridaya Kandel (AI Lecturer) 119


Steps

Hridaya Kandel (AI Lecturer) 120


Solution

Hridaya Kandel (AI Lecturer) 121


Alpha-Beta Pruning ( prune)
Rules of Thumb
is the highest max found so far
is the lowest min value found so far

If Min is on top Alpha prune


If Max is on top Beta prune

You will only have alpha prunes at Min level


You will only have beta prunes at Max level

Hridaya Kandel (AI Lecturer) 122


Alpha-Beta Pruning
Effectiveness of Alpha-Beta
Alpha-Beta is guaranteed to compute the same Minimax value for the root node
as computed by Minimax
In the worst case Alpha-Beta does NO pruning, examining bd leaf nodes, where
each node has b children and a d-ply search is performed
In the best case, Alpha-Beta will examine only 2bd/2 leaf nodes. Hence if you
hold fixed the number of leaf nodes then you can search twice as deep as
Minimax!
The best case occurs when each player's best move is the leftmost alternative
(i.e., the first child generated). So, at MAX nodes the child with the largest value
is generated first, and at MIN nodes the child with the smallest value is generated
first. This suggest that we should order the operators carefully...

Hridaya Kandel (AI Lecturer) 123

You might also like