Ainn Unit 2
Ainn Unit 2
Problem solving agents, searching for solutions; uninformed search strategies: breadth first
search, depth first search, depth limited search, bidirectional search, comparing uninform
search strategies. Heuristic search strategies Greedy best-first search, A* search, AO* search,
memory bounded heuristic search: local search algorithms & optimization problems: Hill
climbing search, simulated annealing search, local beam search.
PROBLEM-SOLVING AGENTS
Intelligent agents are supposed to maximize their performance measure.
Problem formulation is the process of deciding what actions and states to consider, given a
goal.
Search
Execute
a)Initial state– It is the starting point of an agent. i.e. in (Agent X).The starting state which
agent knows itself.
UNIT 2 Page 1
b)Successor Function -The set of possible actions available to the agent. The term operator
is used to denote the description of an action in terms of which state will be reached by
carrying out the action in a particular state.
For a successor function S, given a particular state x, S(x) returns the set of states reachable
from x by any single action.
Set of all states reachable from initial state is known as state space search.
c) The goal test –In which the agent can apply to a single state description to determine if it
is a goal state. Sometimes there is an explicit set of possible goal states, and the test simply
Checks to see if we have reached one of them. Sometimes the goal is specified by an abstract
property rather than an explicitly enumerated set of states.
For example, in chess, the goal is to reach a state called "checkmate," where the opponent's
king can be captured on the next move no matter what the opponent does.
d) Path cost– A path cost function is a function that assigns a cost to a path. In all cases we
will consider, the cost of a path is the sum of the costs of the individual actions along the
path. The pathcost function is often denoted by g.
Path: A path in the state space is a sequence of states connected by a sequence of actions.
State Space– the state space forms a graph in which the nodes are states and arcs between
nodes are actions.
UNIT 2 Page 2
Figure-1
Example: 1. Route finding problem. In figure-1 given map between Coimbatore and
Chennai via other places. Your task is to find the best way to reach from Coimbatore to
Chennai.
Initial State: In (Coimbatore)
Successor Function: {< Go (Pollachi), In (Pollachi)>
< Go (Erode), In (Erode)>
< Go (Palladam), In (Palladam)>
< Go (Mettupalayam), In (Mettupalayam)>}
Goal Test: In (Chennai)
Solution : i. Coimbatore Mettupalayam can’t reach goal
ii. Coimbatore Pollachi Palani Dindigul Trichy Chennai
path cost = 37 + 60+57 +97+320=571
iii. Coimbatore Erode Salem VelloreChennai
Path Cost:100 + 66 + 200 + 140 = 506
So the best solution is third one because the path cost is least.
UNIT 2 Page 3
2 8 3 1 2 3
1 6 4 4 5 6
5 7 8
7
UNIT 2 Page 4
The general TREE-SEARCH algorithm is shown below:
UNIT 2 Page 5
UNINFORMED SEARCH STRATEGIES
Uninformed search (also called blind search).
The term means that the strategies have no additional information about states
beyond that provided in the problem definition.
All they can do is generate successors and distinguish a goal state from a non-goal
state.
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.
This is achieved very simply by using a FIFO queue for the frontier.
Thus, new nodes (which are always deeper than their parents) go to the back of the
queue, and old nodes, which are shallower than the new nodes,get expanded first.
UNIT 2 Page 6
Uniform-cost search
Instead of expanding the shallowest node, uniform-cost search expands the node n
with the lowest path cost g(n).
This is done by storing the frontier as a priority queue ordered by g.
UNIT 2 Page 7
The problem is to get from Sibiu to Bucharest. The successors of Sibiu are Rimnicu Vilcea
and Fagaras, with costs 80 and 99, respectively. The least-cost node, Rimnicu Vilcea, is
expanded next, adding Pitesti with cost 80 + 97=177. The least-cost node is now Fagaras, so
it is expanded, adding Bucharest with cost 99+211=310. Now a goal node has been
generated, but uniform-cost search keeps going, choosing Pitesti for expansion and adding a
second path to Bucharest with cost 80+97+101= 278. Now the algorithm checks to see if this
new path is better than the old one; it is, so the old one is discarded. Bucharest, now with g-
cost 278, is selected for expansion and the solution is returned.
Depth-first search
Depth-first search always expands the deepest node in the current frontier 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 frontier, so then the search
“backs up” to the next deepest node that still has unexplored successors.
depth-first search uses a LIFO queue.
A LIFO queue means that the most recently generated node is chosen for expansion.
UNIT 2 Page 8
Depth-limited search
UNIT 2 Page 9
depth-limited search can terminate with two kinds of failure: the standard failure
value indicates no solution; the cutoff value indicates no solution within the depth
limit.
Bidirectional search
Bidirectional search is implemented by replacing the goal test with a check to see
whether the frontiers of the two searches intersect; if they do, a solution has been
found.
The check can be done when each node is generated or selected for expansion and,
with a hash table, will take constant time.
The idea behind bidirectional search is to run two simultaneous searches—one
forward from the initial state and the other backward from the goal—hoping that the
two searches meet in the middle
Complete — if the shallowest goal node is at some finite depth d, will eventually find it after
generating all shallower nodes
UNIT 2 Page 10
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 lead to a solution quickly.
Thus, it evaluates nodes by using just the heuristic function; that is, f(n) = h(n).
Let us see how this works for route this works for route-finding problems in Romania;
we use the straight line distance heuristic, which we will call hSLD.
UNIT 2 Page 11
Best first search algorithm:
Step 1: Place the starting node into the OPEN list.
Step 2: If the OPEN list is empty, Stop and return failure.
Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
Step 4: Expand the node n, and generate the successors of node n.
Step 5: Check each successor of node n, and find whether any node is a goal node or not. If
any successor node is goal node, then return success and terminate the search, else proceed to
Step 6.
Step 6: For each successor node, algorithm checks for evaluation function f(n), and then
check if the node has been in either OPEN or CLOSED list. If the node has not been in both
list, then add it to the OPEN list.
Step 7: Return to Step 2.
UNIT 2 Page 12
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure
and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation
function (g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list.
For each successor n', check whether n' is already in the OPEN or CLOSED list, if not
then compute evaluation function for n' and place into Open list.
UNIT 2 Page 13
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to
the back pointer which reflects the lowest g(n') value.
AO* Algorithm
In the above figure we can see an example of a simple AND-OR graph wherein, the
acquisition of speakers can be broken into sub problems/tasks that could be performed
to finish the main goal.
The sub task is to either steal speakers which will directly helps us achieve the main
goal "or" earn some money "and" buy speakers which helps us achieve the main goal.
The AND part of the graphs are represented by the AND-ARCS, referring that all the
sub problems with the AND-ARCS need to be solved for the predecessor node or
problem to be completed.
The edges without AND-ARCS are OR sub problems that can be done instead of the
sub problems with And-arcs.
It is to be noted that several edges can come from a single node as well as the
presence of multiple AND arcs and multiple OR sub problems are possible.
The AO* algorithm is a knowledge-based search technique, meaning the start state
and the goal state is already defined , and the best path is found using heuristics.
The time complexity of the algorithm is significantly reduced due to the informed
search technique.
Compared to the A* algorithm , AO* algorithm is very efficient in searching the
AND-OR trees very efficiently.
UNIT 2 Page 14
f(n) = g(n) + h(n)
where,
g(n): The actual cost of traversal from initial state to the current state.
h(n): The estimated cost of traversal from the current state to the goal state.
f(n): The actual cost of traversal from the initial state to the goal state.
AO* Algorithm
Example
Here, in the above example all numbers in brackets are the heuristic value i.e h(n). Each edge
is considered to have a value of 1 by default.
Step-1
Starting from node A, we first calculate the best path.
f(A-B) = g(B) + h(B) = 1+4= 5 , where 1 is the default cost value of travelling from A to B
and 4 is the estimated cost from B to Goal state.
UNIT 2 Page 15
f(A-C-D) = g(C) + h(C) + g(D) + h(D) = 1+2+1+3 = 7 , here we are calculating the path cost
as both C and D because they have the AND-Arc. The default cost value of travelling from
A-C is 1, and from A-D is 1, but the heuristic value given for C and D are 2 and 3
respectively hence making the cost as 7.
Step-2
Using the same formula as step-1, the path is now calculated from the B node,
f(B-E) = 1 + 6 = 7.
f(B-F) = 1 + 8 = 9
Hence, the B-E path has lesser cost. Now the heuristics have to be updated since there is a
difference between actual and heuristic value of B. The minimum cost path is chosen and is
updated as the heuristic , in our case the value is 7. And because of change in heuristic of B
there is also change in heuristic of A which is to be calculated again.
f(A-B) = g(B) + updated((h(B)) = 1+7=8
UNIT 2 Page 16
Step-3
Comparing path of f(A-B) and f(A-C-D) it is seen that f(A-C-D) is smaller. Hence f(A-C-D)
needs to be explored.
Now the current node becomes C node and the cost of the path is calculated,
f(C-G) = 1+2 = 3
f(C-H-I) = 1+0+1+0 = 2
f(C-H-I) is chosen as minimum cost path,also there is no change in heuristic since it matches
the actual cost. Heuristic of path of H and I are 0 and hence they are solved, but Path A-D
also needs to be calculated , since it has an AND-arc.
f(D-J) = 1+0 = 1, hence heuristic of D needs to be updated to 1. And finally the f(A-C-D)
needs to be updated.
f(A-C-D) = g(C) + h(C) + g(D) + updated((h(D)) = 1+2+1+1 =5.
UNIT 2 Page 17
Recursive best-first search (RBFS) is a simple recursive algorithm that attempts to
mimic the operation of standard best-first search, but using only linear space
UNIT 2 Page 18
function RECURSIVE – BEST – FIRST – SEARCH (Problem) return
RBFS (Problem, MAKE – NODE) (INITIAL – STATE [problem
function RBFS (problem, node, f - limit) return a solution, failure and
a new f – cost limit
if GOAL – TEST [problem] (state) then return node
successors ← EXPAND (node, problem)
if successors is empty then return failure, ∞
for each g in successors do
UNIT 2 Page 19
UNIT 2 Page 20
Two algorithms that do this are MA∗ (memory-bounded A∗) and SMA∗ (simplified
MA∗).
SMA∗ proceeds just like A∗, expanding the best leaf until memory is full.
At this point, it cannot add a new node to the search tree without dropping an old one.
SMA∗ always drops the worst leaf node—the one with the highest f-value.
Like RBFS, SMA∗ then backs up the value of the forgotten node to its parent.
In this way, the ancestor of a forgotten subtree knows the quality of the best path in
that subtree.
With this information, SMA∗ regenerates the subtree only when all other paths have
been shown to look worse than the path it has forgotten.
Another way of saying this is that, if all the descendants of a node n are forgotten,
then we will not know which way to go from n, but we will still have an idea of how
worthwhile it is to go anywhere from n.
UNIT 2 Page 21
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.
To understand local search, we find it useful to consider the state-space landscape.
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 valley—a global
minimum; if elevation corresponds to an objective function, then the aim is to find the
highest peak—a 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 value—that is,
uphill.
It terminates when it reaches a “peak” where no neighbor 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 neighbors of the current
state.
Hill climbing is sometimes called greedy local search because it grabs a good
neighbour state without thinking ahead about where to go next.
UNIT 2 Page 22
Hill climbing often makes rapid progress toward a solution because it is usually quite
easy to improve a bad state.
• 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 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.
1. Stochastic hill climbing chooses at random from among the uphill moves; the
probability of selection can vary with the steepness of the uphill move. This usually
converges more slowly than steepest ascent, but in some state landscapes, it finds
better solutions.
2. First-choice hill climbing implements stochastic hill climbing by generating
successors randomly until one is generated that is better than the current state. This is
a good strategy when a state has many (e.g., thousands) of successors.
3. Random-restart hill climbing adopts the well-known adage, “If at first you don’t
succeed, try, try again.” It conducts a series of hill-climbing searches from randomly
generated initial states, until a goal is found.
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 maximum.
In contrast, a purely random walk—that is, moving to a successor chosen uniformly at
random from the set of successors—is 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.
In metallurgy, annealing is the process used to temper or harden metals and glass by
heating them to a high temperature and then gradually cooling them, thus allowing the
material to reach a low energy crystalline state.
To explain simulated annealing, we switch our point of view from hill climbing to
gradient descent (i.e., minimizing cost) and imagine the task of getting a ping-pong
ball into the deepest crevice in a bumpy surface.
If we just let the ball roll, it will come to rest at a local minimum.
If we shake the surface, we can bounce the ball out of the local minimum.
The trick is to shake just hard enough to bounce the ball out of local minima but not
hard enough to dislodge it from the global minimum.
The simulated-annealing solution is to start by shaking hard (i.e., at a high
temperature) and then gradually reduce the intensity of the shaking (i.e., lower the
temperature).
UNIT 2 Page 23
Instead of picking the best move, however, it picks a 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.
The probability also decreases as the “temperature” T goes down: “bad” moves are
more likely to be allowed at the start when T is high, and they become more unlikely
as T decreases.
If the schedule lowers T slowly enough, the algorithm will find a global optimum
with probability approaching 1.
The local beam search algorithm3 keeps track of k states rather than just one.
It begins with k randomly generated states.
At each step, all the successors of all k states are generated. If any one is a goal, the
algorithm halts.
Otherwise, it selects the k best successors from the complete list and repeats.
At first sight, a local beam search with k states might seem to be nothing more than
running k random restarts in parallel instead of in sequence.
In fact, the two algorithms are quite different.
In a random-restart search, each search process runs independently of the others. In a
local beam search, useful information is passed among the parallel search threads.
In its simplest form, local beam search can suffer from a lack of diversity among the
k states—they can quickly become concentrated in a small region of the state space,
making the search little more than an expensive version of hill climbing.
UNIT 2 Page 24