AI Notes Unit II 1
AI Notes Unit II 1
AI Notes Unit II 1
ARTIFICIAL INTELLIGENCE
UNIT-II
(2) SEARCHING TECHNIQUES
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
2.3 ADVERSARIAL SEARCH
2.3.1 Games
2.3.2 Optimal Decisions in Games
2.3.3 Alpha-Beta Pruning
2.3.4 Imperfect ,Real-time Decisions
2.3.5 Games that include Element of Chance
-----------------------------------------------------------------------------------------------------------------------------
h(n) = extimated cost of the cheapest path from node n to a goal node.
For example,in Romania,one might estimate the cost of the cheapest path from Arad to Bucharest
via a straight-line distance from Arad to Bucharest(Figure 2.1).
Heuristic function are the most common form in which additional knowledge is imparted to the
search algorithm.
Greedy Best-first search
Greedy best-first search tries to expand the node that is closest to the goal,on the grounds that
this is likely to a solution quickly.
It evaluates the nodes by using the heuristic function f(n) = h(n).
Taking the example of Route-finding problems in Romania , the goal is to reach Bucharest starting
from the city Arad. We need to know the straight-line distances to Bucharest from various cities as
shown in Figure 2.1. For example, the initial state is In(Arad) ,and the straight line distance
heuristic hSLD(In(Arad)) is found to be 366.
Using the straight-line distance heuristic hSLD ,the goal state can be reached faster.
Figure 2.2 shows the progress of greedy best-first search using hSLD to find a path from Arad to
Bucharest. The first node to be expanded from Arad will be Sibiu,because it is closer to Bucharest
than either Zerind or Timisoara. The next node to be expanded will be Fagaras,because it is closest.
Fagaras in turn generates Bucharest,which is the goal.
Properties of greedy search
A* Search
A* Search is the most widely used form of best-first search. The evaluation function f(n) is
obtained by combining
(1) g(n) = the cost to reach the node,and
(2) h(n) = the cost to get from the node to the goal :
f(n) = g(n) + h(n).
A* Search is both optimal and complete. A* is optimal if h(n) is an admissible heuristic. The obvious
example of admissible heuristic is the straight-line distance hSLD. It cannot be an overestimate.
A* Search is optimal if h(n) is an admissible heuristic – that is,provided that h(n) never
overestimates the cost to reach the goal.
An obvious example of an admissible heuristic is the straight-line distance hSLD that we used in
getting to Bucharest. The progress of an A* tree search for Bucharest is shown in Figure 2.2.
The values of ‘g ‘ are computed from the step costs shown in the Romania map( figure 2.1). Also
the values of hSLD are given in Figure 2.1.
function RFBS( problem, node, f_limit) return a solution or failure and a new f-
cost limit
if GOAL-TEST[problem](STATE[node]) then return node
successors EXPAND(node, problem)
if successors is empty then return failure, ∞
for each s in successors do
f [s] max(g(s) + h(s), f [node])
repeat
best the lowest f-value node in successors
if f [best] > f_limit then return failure, f [best]
alternative the second lowest f-value among successors
result, f [best] RBFS(problem, best, min(f_limit, alternative))
if result failure then return result
Figure 2.4 The algorithm for recursive best-first search
Figure 2.5 Stages in an RBFS search for the shortest route to Bucharest. The f-limit value for each
recursive call is shown on top of each current node. (a) The path via Rimnicu Vilcea is followed
until the current best leaf (Pitesti) has a value that is worse than the best alternative path (Fagaras).
(b) The recursion unwinds and the best leaf value of the forgotten subtree (417) is backed up to
Rimnicu Vilcea;then Fagaras is expanded,revealing a best leaf value of 450.
(c) The recursion unwinds and the best leaf value of the forgotten subtree (450) is backed upto
Fagaras; then Rimni Vicea is expanded. This time because the best alternative path(through
Timisoara) costs atleast 447,the expansion continues to Bucharest
RBFS Evaluation :
RBFS is a bit more efficient than IDA*
– Still excessive node generation (mind changes)
Like A*, optimal if h(n) is admissible
Space complexity is O(bd).
– IDA* retains only one single number (the current f-cost limit)
Time complexity difficult to characterize
– Depends on accuracy if h(n) and how often best path changes.
IDA* en RBFS suffer from too little memory.
The 8-puzzle
The 8-puzzle is an example of Heuristic search problem. The object of the puzzle is to slide the tiles
horizontally or vertically into the empty space until the configuration matches the goal
configuration(Figure 2.6)
The average cost for a randomly generated 8-puzzle instance is about 22 steps. The branching factor
is about 3.(When the empty tile is in the middle,there are four possible moves;when it is in the
corner there are two;and when it is along an edge there are three). This means that an exhaustive
search to depth 22 would look at about 322 approximately = 3.1 X 1010 states.
By keeping track of repeated states,we could cut this down by a factor of about 170,000,because
there are only 9!/2 = 181,440 distinct states that are reachable. This is a manageable number ,but the
corresponding number for the 15-puzzle is roughly 1013.
If we want to find the shortest solutions by using A*,we need a heuristic function that never
overestimates the number of steps to the goal.
The two commonly used heuristic functions for the 15-puzzle are :
(1) h1 = the number of misplaced tiles.
For figure 2.6 ,all of the eight tiles are out of position,so the start state would have h1 = 8. h1 is an
admissible heuristic.
(2) h2 = the sum of the distances of the tiles from their goal positions. This is called the city
block distance or Manhattan distance.
h2 is admissible ,because all any move can do is move one tile one step closer to the goal.
Tiles 1 to 8 in start state give a Manhattan distance of
h2 = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18.
Figure 2.7 Comparison of search costs and effective branching factors for the ITERATIVE-
DEEPENING-SEARCH and A* Algorithms with h1,and h2. Data are average over 100 instances of
the 8-puzzle,for various solution lengths.
o In such cases, we can use local search algorithms. They operate using a single current
state(rather than multiple paths) and generally move only to neighbors of that state.
o The important applications of these class of problems are (a) integrated-circuit design,
(b)Factory-floor layout,(c) job-shop scheduling,(d)automatic programming,
(e)telecommunications network optimization,(f)Vehicle routing,and (g) portfolio
management.
OPTIMIZATION PROBLEMS
Inaddition 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.
Hill-climbing search
The hill-climbing search algorithm as shown in figure 2.9, is simply a loop that continually moves
in the direction of increasing value – that is,uphill. It terminates when it reaches a “peak” where no
neighbor has a higher value.
current ¬ MAKE-NODE(INITIAL-STATE[problem])
loop do
neighbor ¬ a highest valued successor of current
if VALUE [neighbor] ≤ VALUE[current] then return STATE[current]
current ¬ neighbor
Figure 2.9 The hill-climbing search algorithm (steepest ascent version),which is the most basic
local search technique. At each step the current node is replaced by the best neighbor;the neighbor
with the highest VALUE. If the heuristic cost estimate h is used,we could find the neighbor with the
lowest h.
Hill-climbing is sometimes called greedy local search because it grabs a good neighbor state
without thinking ahead about where to go next. Greedy algorithms often perform quite well.
Problems with hill-climbing
Hill-climbing often gets stuck for the following reasons :
o Local maxima : a local maximum is a peak that is higher than each of its neighboring
states,but lower than the global maximum. Hill-climbing algorithms that reach the vicinity
of a local maximum will be drawn upwards towards the peak,but will then be stuck with
nowhere else to go
o Ridges : A ridge is shown in Figure 2.10. Ridges results in a sequence of local maxima that
is very difficult for greedy algorithms to navigate.
o Plateaux : A plateau is an area of the state space landscape where the evaluation function is
flat. It can be a flat local maximum,from which no uphill exit exists,or a shoulder,from
which it is possible to make progress.
Figure 2.10 Illustration of why ridges cause difficulties for hill-climbing. The grid of states(dark
circles) is superimposed on a ridge rising from left to right,creating a sequence of local maxima that
are not directly connected to each other. From each local maximum,all th available options point
downhill.
Hill-climbing variations
Stochastic hill-climbing
o Random selection among the uphill moves.
o The selection probability can vary with the steepness of the uphill move.
First-choice hill-climbing
o cfr. stochastic hill climbing by generating successors randomly until a better one is
found.
Random-restart hill-climbing
o Tries to avoid getting stuck in local maxima.
Simulated annealing search
A hill-climbing algorithm that never makes “downhill” moves towards states with lower value(or
higher cost) is guaranteed to be incomplete,because it can stuck on a local maximum.In contrast,a
purely random walk –that is,moving to a successor choosen uniformly at random from the set of
successors – is complete,but extremely inefficient.
Simulated annealing is an algorithm that combines hill-climbing with a random walk in someway
that yields both efficiency and completeness.
Figure 2.11 shows simulated annealing algorithm. It is quite similar to hill climbing. Instead of
picking the best move,however,it picks the random move. If the move improves the situation,it is
always accepted. Otherwise,the algorithm accepts the move with some probability less than 1. The
probability decreases exponentially with the “badness” of the move – the amount E by which the
evaluation is worsened.
Simulated annealing was first used extensively to solve VLSI layout problems in the early 1980s. It
has been applied widely to factory scheduling and other large-scale optimization tasks.
Figure 2.11 The simulated annealing search algorithm,a version of stochastic hill climbing where
some downhill moves are allowed.
Genetic algorithms
A Genetic algorithm(or GA) is a variant of stochastic beam search in which successor states are
generated by combining two parent states,rather than by modifying a single state.
Like beam search,Gas begin with a set of k randomly generated states,called the population. Each
state,or individual,is represented as a string over a finite alphabet – most commonly,a string of 0s
and 1s. For example,an 8 8-quuens state must specify the positions of 8 queens,each in acolumn of
8 squares,and so requires 8 x log2 8 = 24 bits.
Figure 2.12 The genetic algorithm. The initial population in (a) is ranked by the fitness function in
(b),resulting in pairs for mating in (c). They produce offspring in (d),which are subjected to
mutation in (e).
Figure 2.12 shows a population of four 8-digit strings representing 8-queen states. The production
of the next generation of states is shown in Figure 2.12(b) to (e).
In (b) each state is rated by the evaluation function or the fitness function.
In (c),a random choice of two pairs is selected for reproduction,in accordance with the probabilities
in (b).
Figure 2.13 describes the algorithm that implements all these steps.
Figure 2.15 (a) Principle states and territories of Australia. Coloring this map can be viewed as
aconstraint satisfaction problem. The goal is to assign colors to each region so that no neighboring
regions have the same color.
Figure 2.15 (b) The map coloring problem represented as a constraint graph.
Figure 2.16 (a) Cryptarithmetic problem. Each letter stands for a distinct digit;the aim is to
find a substitution of digits for letters such that the resulting sum is arithmetically
correct,with the added restriction that no leading zeros are allowed. (b) The constraint
hypergraph for the cryptarithmetic problem,showint the Alldiff constraint as well as the
column addition constraints. Each constraint is a square box connected to the variables it
contains.
2.2.2 Backtracking Search for CSPs
The term backtracking search is used for depth-first search that chooses values for one variable at
a time and backtracks when a variable has no legal values left to assign. The algorithm is shown in
figure 2.17.
Figure 2.17 A simple backtracking algorithm for constraint satisfaction problem. The algorithm is
modeled on the recursive depth-first search
Figure 2.17(b) Part of search tree generated by simple backtracking for the map coloring problem.
k-Consistency
Local Search for CSPs
Independent Subproblems
Tree-Structured CSPs
2.4 ADVERSARIAL SEARCH
Competetive environments,in which the agent’s goals are in conflict,give rise to adversarial search
problems – often known as games.
2.4.1 Games
Mathematical Game Theory,a branch of economics,views any multiagent environment as a game
provided that the impact of each agent on the other is “significant”,regardless of whether the agents
are cooperative or competitive. In,AI,”games” are deterministic,turn-taking,two-player,zero-sum
games of perfect information. This means deterministic,fully observable environments in which
there are two agents whose actions must alternate and in which the utility values at the end of the
game are always equal and opposite. For example,if one player wins the game of chess(+1),the
other player necessarily loses(-1). It is this opposition between the agents’ utility functions that
makes the situation adversarial.
Game Tree
The initial state and legal moves for each side define the game tree for the game. Figure 2.18
shows the part of the game tree for tic-tac-toe (noughts and crosses). From the initial state,MAX has
nine possible moves. Play alternates between MAX’s placing an X and MIN’s placing a 0 until we
reach leaf nodes corresponding to the terminal states such that one player has three in a row or all
the squares are filled. He number on each leaf node indicates the utility value of the terminal state
from the point of view of MAX;high values are assumed to be good for MAX and bad for MIN. It is
the MAX’s job to use the search tree(particularly the utility of terminal states) to determine the best
move.
Figure 2.18 A partial search tree . The top node is the initial state,and MAX move first,placing an X in an
empty square.
2.4.2 Optimal Decisions in Games
In normal search problem,the optimal solution would be a sequence of move leading to a goal
state – a terminal state that is a win. In a game,on the other hand,MIN has something to say about
it,MAX therefore must find a contingent strategy,which specifies MAX’s move in the initial
state,then MAX’s moves in the states resulting from every possible response by MIN,then MAX’s
moves in the states resulting from every possible response by MIN those moves,and so on. An
optimal strategy leads to outcomes at least as good as any other strategy when one is playing an
infallible opponent.
o α : the value of the best(i.e.,highest-value) choice we have found so far at any choice point
along the path of MAX.
o β: the value of best (i.e., lowest-value) choice we have found so far at any choice point
along the path of MIN.
Alpha Beta search updates the values of α and β as it goes along and prunes the remaining branches
at anode(i.e.,terminates the recursive call) as soon as the value of the current node is known to be
worse than the current α and β value for MAX and MIN,respectively. The complete algorithm is
given in Figure 2.21.
The effectiveness of alpha-beta pruning is highly dependent on the order in which the successors
are examined. It might be worthwhile to try to examine first the successors that are likely to be the
best. In such case,it turns out that alpha-beta needs to examine only O(bd/2) nodes to pick the best
move,instead of O(bd) for minimax. This means that the effective branching factor becomes sqrt(b)
instead of b – for chess,6 instead of 35. Put anotherway alpha-beta cab look ahead roughly twice as
far as minimax in the same amount of time.
Figure 2.21 The alpha beta search algorithm. These routines are the same as the
minimax routines in figure 2.20,except for the two lines in each of MIN-VALUE and
MAX-VALUE that maintain α and β
Evaluation functions
An evaluation function returns an estimate of the expected utility of the game from a given position,just as
the heuristic function return an estimate of the distance to the goal.
Games of imperfect information
o Minimax and alpha-beta pruning require too much leaf-node evaluations.
May be impractical within a reasonable amount of time.
o SHANNON (1950):
o Cut off search earlier (replace TERMINAL-TEST by CUTOFF-TEST)
o Apply heuristic evaluation function EVAL (replacing utility function of alpha-beta)
Cutting off search
Change:
– if TERMINAL-TEST(state) then return UTILITY(state)
into
– if CUTOFF-TEST(state,depth) then return EVAL(state)
Introduces a fixed-depth limit depth
– Is selected so that the amount of time will not exceed what the rules of the game
allow.
When cuttoff occurs, the evaluation is performed.
Heuristic EVAL
Idea: produce an estimate of the expected utility of the game from a given position.
Performance depends on quality of EVAL.
Requirements:
– EVAL should order terminal-nodes in the same way as UTILITY.
– Computation may not take too long.
– For non-terminal states the EVAL should be strongly correlated with the actual
chance of winning.
Only useful for quiescent (no wild swings in value in near future) states
Weighted Linear Function
The introductory chess books give an approximate material value for each piece : each pawn is
worth 1,a knight or bishop is worth 3,a rook 3,and the queen 9. These feature values are then added
up toobtain the evaluation of the position. Mathematically,these kind of evaluation fuction is called
weighted linear function,and it can be expressed as :
• e.g., w1 = 9 with
f1(s) = (number of white queens) – (number of black queens), etc.
Figure 2.23 A typical backgammon position. The goal of the game is to move all
one’s pieces off the board. White moves clockwise toward 25,and black moves
counterclockwise toward 0. A piece can move to any position unless there are
multiple opponent pieces there; if there is one opponent ,it is captured and must
start over. In the position shown,white has rolled 6-5 and must choose among four
legal moves (5-10,5-11),(5-11,19-24),(5-10,10-16),and (5-11,11-16)
------------------------------------------------------------------------------------------------------------------------
Figure 2-24 Schematic game tree for a backgammon position.
These equations can be backed-up recursively all the way to the root of the game
tree.
------------------------------------------------------------------------------------------------------