AI Unit-2 Notes
AI Unit-2 Notes
Unit-2
Introduction to Search: Searching for solutions, uninformed search strategies, informed
search strategies, Local search algorithms and optimistic problems, Adversarial Search,
Search for games. , Alpha - Beta pruning.
Example Problem
Toy problems
The first example we will examine is the vacuum world. This can be formulated as a problem
as follows:
States: The agent is in one of two locations, each of which might or might not contain
dirt. Thus there are 2 x 22 = 8 possible world states.
Initial state: Any state can be designated as the initial state.
Successor function: This generates the legal states that result from trying the three
actions (Left, Right, and Suck). The complete state space is shown in Figure 3.3.
Goal test: This checks whether all the squares are clean.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
The 8-puzzle consists of a 3 x 3 board with eight numbered tiles and a blank space. A tile
adjacent to the blank space can slide into the space. The object is to reach a specified goal
state, such as the one shown on the right of the figure. The standard formulation is as follows:
States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
Initial state: Any state can be designated as the initial state. Note that any given goal
can be reached from exactly half of the possible initial states.
Successor function: This generates the legal states that result from trying the four
actions (blank moves Left, Right, Up, or Down).
Goal test: This checks whether the state matches the goal configuration.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
Real-world problems
Consider a simplified example of an airline travel problem specified as follows:
States: Each is represented by a location (e.g., an airport) and the current time.
Initial state: This is specified by the problem.
Successor function: This returns the states resulting from taking any scheduled flight
(perhaps further specified by seat class and location), leaving later than the current
time plus the within-airport transit time, from the current airport to another.
Goal test: Are we at the destination by some pre specified time?
Path cost: This depends on monetary cost, waiting time, flight time, customs and
immigration procedures, seat quality, time of day, type of airplane, frequent-flyer
mileage awards, and so on.
need to represent the collection of nodes that have been generated but not yet expandedthis collection is called the fringe or frontier. Each element of the fringe is a leaf node, that
is, a node with no successors in the tree.
We will assume that the collection of nodes is implemented as a queue. The operations on a
queue are as follows:
Breadth-first search
Breadth-first search is a simple strategy in which the root node is expanded first, then all
the successors of the root node are expanded next, then their successors, and so on. In
general, all the nodes are expanded at a given depth in the search tree before any nodes at the
next level are expanded.
Breadth-first search can be implemented by calling TREE-SEARCH with an empty fringe
that is a first-in-first-out (FIFO) queue, assuring that the nodes that are visited first will be
expanded first. The FIFO queue puts all newly generated successors at the end of the queue,
which means that shallow nodes are expanded before deeper nodes. First lesson is that, the
memory requirements are a bigger problem for breadth9rst search than is the execution time.
The second lesson is that the time requirements are still a major factor. In general,
exponential-complexity search problems cannot be solved by uninformed methods for any but
the smallest instances.
Uniform-cost search
Breadth-first search is optimal when all step costs are equal, because it always expands the
shallowest unexpanded node. By a simple extension, we can find an algorithm that is optimal
with any step cost function. Instead of expanding the shallowest node, uniform-cost search
expands the node n with the lowest path cost. Note that if all step costs are equal, this is
identical to breadth-first search.
Uniform-cost search does not care about the number of steps a path has, but only about their
total cost. Therefore, it will get stuck in an infinite loop if it ever expands a node that has a
zero-cost action leading back to the same state
Depth-first search
Depth-first search always expands the deepest node in the current fringe of the search tree.
The search proceeds immediately to the deepest level of the search tree, where the nodes have
no successors. As those nodes are expanded, they are dropped from the fringe, so then the
search "backs up" to the next shallowest node that still has unexplored successors.
This strategy can be implemented by TREE-SEARCH with a last-in-first-out (LIFO) queue,
also known as a stack. As an alternative to the TREE-SEARCH implementation, it is
common to implement depth-first search with a recursive function that calls itself on each of
its children in turn.
Depth-first search has very modest memory requirements. It needs to store only a single path
from the root to a leaf node, along with the remaining unexpanded sibling nodes for each
node on the path. Once a node has been expanded, it can be removed from memory as soon
as all its descendants have been fully explored.
The drawback of depth-first search is that it can make a wrong choice and get stuck going
down a very long (or even infinite) path when a different choice would lead to a solution near
the root of the search tree.
10
11
Depth-limited search
The problem of unbounded trees can be alleviated by supplying depth-first search with a
predetermined depth limit l. That is, nodes at depth l are treated as if they have no successors.
This approach is called depth-limited search. The depth limit solves the infinite-path
problem.
12
Bidirectional search
The idea behind bidirectional search is to run two simultaneous searches-one forward from
the initial state and the other backward from the goal, stopping when the two searches
meeting the middle.
13
It suffers from the same defects as depth-first search-it is not optimal, and it is incomplete
(because it can start down an infinite path and never return to try other possibilities). The
worst-case time and space complexity is O(bm), where m is the maximum depth of the search
space. With a good heuristic function, however, the complexity can be reduced substantially.
14
A* search
The most widely-known form of best-first search is called A* search (pronounced "A-star
Search"). It evaluates nodes by combining g (n), the cost to reach the node, and h (n.), the
cost to get from the node to the goal:
f (n)=g (n) + h (n)
g (n) = the path cost from the start node to node n,
h (n) = the estimated cost of the cheapest path from n to the goal,
f (n) = the estimated cost of the cheapest solution through n.
A* search is both complete and optimal. In this case, A* is optimal if h(n) is an admissible
heuristic-that is, provided that h(n) never overestimates the cost to reach the goal.
Admissible heuristics are by nature optimistic, because they think the cost of solving the
problem is less than it actually is. Since g(n) is the exact cost to reach n, we have as
immediate consequence that f (n) never overestimates the true cost of a solution through n.
15
16
Suppose a suboptimal goal node G2 appears on the fringe, and let the cost of the optimal
solution be C*. Then, because G2 is suboptimal and because h (G2=) 0 (true for any goal
node), we know
f (G2) = g(G2) + h(G2) = g(G2) > C*
If h (n) does not overestimate the cost of completing the solution path, then we know that
f (n) = g(n) + h(n)<=C* .
Now we have shown that f (n) <=C* < f (G2) so G2 will not be expanded and A* must
return an optimal solution.
A heuristic h (n) is consistent if, for every node n and every successor n' of n generated by
any action a, the estimated cost of reaching the goal from n is no greater than the step cost of
getting to n' plus the estimated cost of reaching the goal from n':
h (n)<=c(n, a , n') + h(n).
Another important consequence of consistency is the following: if h (n) is consistent, then the
values off (n) along any path are nondecreasing. The proof follows directly from the
definition of consistency. Suppose n' is a successor of n; then g(n')= g(n)+ c(n,a , n') for some
a, and we have
It follows that the sequence of nodes expanded by A* using GRAPH-SEARCH is in non
decreasing order of f (n). Hence, the first goal node selected for expansion must be an optimal
Solution, since all later nodes will be at least as expensive.
LOCAL SEARCH ALGORITHMS AND OPTIMIZATION PROBLEMS
Local search algorithms operate using a single current node (rather than multiple paths) and
generally move only to neighbours of that node. Typically, the paths followed by the search
are not retained. Although local search algorithms are not systematic, they have two key
advantages: (1) they use very little memoryusually a constant amount; and (2) they can
often find reasonable solutions in large or infinite (continuous) state spaces for which
systematic algorithms are unsuitable. In addition to finding goals, local search algorithms are
useful for solving pure optimization problems, in which the aim is to find the best state
according to an objective function.
17
A landscape has both "location" (defined by the state) and "elevation" (defined by the value
of the heuristic cost function or objective function). If elevation corresponds to cost, then the
aim is to find the lowest valleya global minimum; if elevation corresponds to an objective
function, then the aim is to find the highest peaka global maximum. Local search
algorithms explore this landscape. A complete local search algorithm always finds a goal if
one exists; an optimal algorithm always finds a global minimum/maximum.
Hill-climbing search
It is simply a loop that continually moves in the direction of increasing valuethat is, uphill.
It terminates when it reaches a "peak" where no neighbour has a higher value. The algorithm
does not maintain a search tree, so the data structure for the current node need only record the
state and the value of the objective function. Hill climbing does not look ahead beyond the
immediate neighbours of the current state.
Unfortunately, hill climbing often gets stuck for the following reasons:
Local maxima: a local maximum is a peak that is higher than each of its
neighbouring states but lower than the global maximum. Hill-climbing algorithms that
reach the vicinity of a local maximum will be drawn upward toward the peak but will
then be stuck with nowhere else to go.
Ridges: Ridges result in a sequence of local maxima that is very difficult for greedy
algorithms to navigate
Plateaux: a plateau is a flat area of the state-space landscape. It can be a flat local
maximum, from which no uphill exit exists, or a shoulder, from which progress is
possible.
18
Simulated annealing
A hill-climbing algorithm that never makes "downhill" moves toward states with lower value
(or higher cost) is guaranteed to be incomplete, because it can get stuck on a local maxi-mum.
In contrast, a purely random walkthat is, moving to a successor chosen uniformly at
random from the set of successorsis complete but extremely inefficient. Therefore, it seems
reasonable to try to combine hill climbing with a random walk in some way that yields both
efficiency and completeness. Simulated annealing is such an algorithm.
19
20
algorithm that generates all actions at once, or 0(m) for an algorithm that generates actions
one at a time.
21
ALPHABETA PRUNING
The problem with minimax search is that the number of game states it has to examine is
exponential in the depth of the tree. The trick is that it is possible to compute the correct
minimax decision without looking at every node in the game tree.
Alphabeta pruning is a search algorithm that seeks to decrease the number of nodes that are
evaluated by the minimax algorithm in its search tree. It stops completely evaluating a move
when at least one possibility has been found that proves the move to be worse than a
previously examined move. Such moves need not be evaluated further. 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.
22
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:
= the value of the best (i.e., highest-value) choice we have found so far at any
choice point along the path for MAX.
= the value of the best (i.e., lowest-value) choice we have found so far at any choice
point along the path for MIN.
Alpha-beta search updates the values of and as it goes along and prunes the remaining
branches at a node (i.e., terminates the recursive call) as soon as the value of the current node
is known to be worse than the current a or p value for MAX or MIN, respectively.