0% found this document useful (0 votes)
29 views102 pages

AI UNIT 1 BEC Part 2

This document discusses various search strategies used to solve problems, including uninformed and informed search approaches. It describes breadth-first search, uniform cost search, depth-first search, and bidirectional search as uninformed strategies. Informed strategies discussed include greedy best-first search and A* search, which uses heuristics. The document provides examples of applying uniform cost search to find optimal paths between cities in Romania.

Uploaded by

Naga sai Challa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views102 pages

AI UNIT 1 BEC Part 2

This document discusses various search strategies used to solve problems, including uninformed and informed search approaches. It describes breadth-first search, uniform cost search, depth-first search, and bidirectional search as uninformed strategies. Informed strategies discussed include greedy best-first search and A* search, which uses heuristics. The document provides examples of applying uniform cost search to find optimal paths between cities in Romania.

Uploaded by

Naga sai Challa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

UNIT-1 continuation-Part-2

Solving Problems by Searching : Problem-Solving Agents, Searching


for Solutions,
Uninformed Search Strategies: Breadth First Search, Uniform Cost
Search, Depth First Search, iterative Deepening DFS and Bi-directional
Search.
Informed (Heuristic) Search Strategies: Greedy BFS, A* Algorithm,
Heuristics functions.
Beyond Classical Search: Local Search Algorithms and Optimization
Problems-Hill Climbing, Simulated Annealing,
Problem-Solving Agents

• Problem solving agents are goal-directed agents:


1. Goal Formulation: Set of one or more (desirable) world
states (e.g. checkmate in chess).
2. Problem formulation: What actions and states to
consider given a goal and an initial state.
3. Search for solution: Given the problem, search for a
solution --- a sequence of actions to achieve the goal
starting from the initial state.
4. Execution of the solution
 To illustrate these ideas, we use a very simple example—the vacuum-cleaner world
shown in Figure 2.2.
Finds sequence of actions

Updates sequences of actions


 Our agent has now adopted the goal of driving to Bucharest, and is
considering where to go from Arad. There are three roads out of
Arad, one toward Sibiu, one to Timisoara, and one to Zerind.
Well-defined problems and solutions

A problem can be defined formally by four components:


1) initial state
2) actions
3) goal test
4) path cost

 The initial state that the agent starts in. For example, the initial
state for our agent in Romania might be described as In(Arad).
 A description of the possible actions available to the agent. The most common
formulation uses a successor function.

Given a particular state x, SUCCESSOR-FN(x) returns a set of (action, successor)


ordered pairs.

For example, from the state In(Arad), the successor function for the Romania
problem would return.

{ (G o(Sibzu), In(Sibiu)), (Go(Timisoara), In( Tzmisoara)), (Go(Zerind), In(Zerind)))


Definition  The state space is the set of all states reachable from the
initial state.

 The goal test, which determines whether a given state is a goal


state.

 Sometimes there is an explicit set of possible goal states, and


the test simply checks whether the given state is one of them.

 The agent's goal in Romania is the singleton set


{In(Bucharest)).

 For example, in chess, the goal is to reach a state called


"checkmate," where the opponent's king is under attack and can't
escape.
 A path cost function that assigns a numeric cost to each path.

The problem-solving agent chooses a cost function that reflects its own
performance measure.

For the agent trying to get to Bucharest, time is of the essence, so the
cost of a path might be its length in kilometres.

The cost of a path can be described as the sum of the costs of the
individual actions along the path.

The step cost of taking action a to go from state x to state y is denoted


by c(x, a, y).

The step costs for Romania are shown in Figure 3.2 as route
distances.
 A solution to a problem is a path from the initial state to a goal
state.

 Solution quality is measured by the path cost function, and an


optimal solution has the lowest path cost among all solutions.

solution path  the path from Arad to Sibiu to Rimnicu Vilcea to


Pitesti to Bucharest.( 418 kilometers)
Formulate goal: Example: Path Finding problem
– be in Bucharest
(Romania)

Formulate problem:
– action: drive between
pair of connected cities
(direct road) Initial
– State
– state: be in a city
(20 world states)

Find solution:
– sequence of cities
leading from start to
goal state, e.g., Arad,
Sibiu, Fagaras,
Bucharest
– Goal
State
Execution
– drive from Arad to
Bucharest according to Environment: fully observable (map),
the solution
deterministic, and the agent knows effects
of each action. Is this really the case?
Searching for Solutions
 Having formulated some problems, we now need to solve them.
This is done by a search through the state space.

Figure 3.6 shows some of the expansions in the search tree for
finding a route from Arad to Bucharest. The root of the search tree
is a search node corresponding to the initial state, In(Arad).

expanding the current state  that is, applying the successor


function to the current state, there by generating a new set of
states. In this case, we get three new states: In(Sibiu),
In(Timisoara), and In(Zerind).

 The choice of which state to expand is determined by the search


strategy.
The set of all leaf nodes available for expansion at any given point is
called the frontier.

Figure 3.7 An informal description of the general tree-search and graph search
algorithms .
Difference between state and node

• A state is a --- representation of --- a physical configuration.

• A node is a data structure constituting part of a search tree


includes state, tree parent node, action (applied to parent), path
cost (initial state to node), depth.

•A frontier , which is a data structure used to store all the


possible states (nodes) that you can go from the
current states.
• A node is having five components:
• STATE: which state it is in the state space
• PARENT-NODE: from which node it is generated
• ACTION: which action applied to its parent-node to generate it
• PATH-COST: the cost, g(n), from initial state to the node n itself
• DEPTH: number of steps along the path from the initial state

Figure 3.8  Nodes are the data structures from which the search tree is
constructed. Each has a parent, a state, and various bookkeeping fields. Arrows
point from child to parent.
 The frontier needs to be stored in such a way that the search algorithm
can easily choose the next node to expand according to its preferred
strategy. The appropriate data structure for this is a queue. The
operations on a queue are as follows:

• EMPTY?(queue) returns true only if there are no more elements in the


queue.
• POP(queue) removes the first element of the queue and returns it.
• INSERT(element , queue) inserts an element and returns the resulting
queue.
Measuring problem-solving performance

• The evaluation of a search strategy

• Completeness:
• is the strategy guaranteed to find a solution when there is one?
• Optimality:
• does the strategy find the highest-quality solution when there are
several different solutions?
• Time complexity:
• how long does it take to find a solution?
• Space complexity:
• how much memory is needed to perform the search?
Uninformed Search Strategies
• Uninformed search (also called blind search).
• no information about the number of steps or the path cost from
the current state to the goal.
• search the state space blindly
• Informed search, or heuristic search
• a cleverer strategy that searches toward the goal, based on the
information from the current state so far.

Uninformed search strategies are :


1) Breadth-first search
2) Uniform-cost search
3) Depth-first search
4) iterative Deepening DFS
5) Bidirectional search
 The FIFO queue puts all newly generated successors at the end of the queue, which
means that shallow nodes are expanded before deeper nodes. Figure 3.12 shows the
progress of the search on a simple binary tree.
Breadth-first search
S

A D

B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
Optimal soln. found?
Yes (if all step costs are identical)
b: maximum branching factor of the search tree
d: depth of the least-cost solution
Uniform-cost search

 Uniform cost search, modifies breadth-first strategy by always


expanding the lowest-cost node.

 Uniform cost search is useful for finding shortest path.

 Note that if all step costs are equal, this is identical to breadth-first
search.

Unsuitable for operators with negative cost

 Uniform-cost search does not care about the number of steps a path
has, but only about their total cost.
Priority Queue is an abstract data type that is similar to a queue,
and every element has some priority value associated with it. The
priority of the elements in a priority queue determines the order in
which elements are served (i.e., the order in which they are
removed). If in any case the elements have same priority, they are
served as per their ordering in the queue.
Example of UNIFORM COST

Figure 3.15, where 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
Uniform-cost search is guided by path costs rather than depths, so its
complexity cannot easily be characterized in terms of b and d.

Instead, let C* be the cost of the optimal solution, and assume that every
action costs at least ε.
Then the algorithm's worst-case time and space complexity is
O(b(C*/ ε)).

Let C* be the cost of optimal solution.


ε is every action cost
Depth-first search

 Depth-first search always expands the deepest node in the current


frontier of the search tree.

The progress of the search is illustrated in Figure 3.16.

 This strategy can be implemented by TREE-SEARCH


with a last-in-first-out (LIFO) queue, also known as a
stack.
 The properties of depth-first search depend strongly on whether the
graph-search or tree-search version is used.

The graph-search version, which avoids repeated states and redundant


paths, is complete in finite state spaces because it will eventually
expand every node.

The tree-search version, on the other hand, is not complete—for


example, in Figure 3.6 the algorithm will follow the Arad–Sibiu–
Arad–Sibiu loop forever

For example, in Figure 3.16, depth first search will explore the entire left
subtree even if node C is a goal node.

If node J were also a goal node, then depth-first search would return it as
a solution instead of C, which would be a better solution; hence, depth-
first search is not optimal.
Depth-first search
S

A D

B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
iterative deepening depth-first search

 Iterative deepening search (or iterative deepening depth-first


search) is a general strategy, often used in combination with depth-
first tree search, that finds the best depth limit.

 It does this by gradually increasing the limit—first 0, then 1, then


2, and so on—until a goal is found.

 Iterative deepening combines the benefits of depth-first


and breadth-first search.

 iterative deepening is the preferred uninformed search method


when there is a large search space and the depth of the solution
is not known.
Bidirectional search
 Run two simultaneous searches  one forward from the initial
state another backward from the goal, stop when the two
searches meet.

 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.
Bidirectional search
S
Forward
A D Backwards

B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
Bidirectional Strategy

2 frontiers queues: frontier1 and frontier2

Time and space complexity = O(bd/2)


Comparing search strategies
Informed (Heuristic) Search Strategies
 can find solutions more efficiently than an uninformed strategy.

1. Greedy best-first search

2. A* search or A* Algorithm: Minimizing the total estimated


solution cost
1. 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.

heuristic function: f (n) = h(n).

f a heuristic function, denoted h(n)

h(n) = estimated cost of the cheapest path from


the state at node n to a goal
state.

if n is a goal node, then h(n)=0.


 Let us see how this works for route-finding problems in Romania, using the
straight line distance heuristic, which we will call hSLD.

 For example, in Romania, one might estimate the cost of the cheapest
path from Arad to Bucharest via the straight-line distance from Arad to
Bucharest.

 hSLD is correlated with actual road distances and is, therefore, a useful
heuristic.

 If the goal is Bucharest, we will need to know the straight-line distances to


Bucharest, which are shown in Figure 3.22

For example, hSLD (In(Arad)=) 366. Notice that the values of hSLD cannot be computed
from the problem description itself.
Figure 3.23 shows the progress of a 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.
 It is not optimal, however: the path via Sibiu and Fagaras to
Bucharest is 32 kilometers longer than the path through Rimnicu
Vilcea and Pitesti.

 This shows why the algorithm is called “greedy”—at each step it


tries to get as close to the goal as it can.

 Greedy best-first tree search is also incomplete even in a


finite state space, much like depth-first search.

 Consider the problem of getting from Iasi to Fagaras. The


heuristic suggests that Neamt be expanded first because it is
closest to Fagaras, but it is a dead end.

The worst-case time and space complexity for the tree version is O(bm), where m
is the maximum depth of the search space.
Optimal is =
A* search: Minimizing the total estimated solution cost
The most widely-known form of best-first search is called A* search .

Evaluation function f(n) = g(n) + h(n)

g(n) = gives the path cost from the start node to node n.
h(n) = is the estimated cost of the cheapest path from n to the goal.

f (n) = estimated cost of the cheapest solution through n.

 A* search gives both complete and optimal solution path.

 The algorithm is identical to UNIFORM-COST-SEARCH except


that A∗ uses g + h instead of g.
In Figure 3.24, we show the progress of an A∗ tree search for Bucharest.
The values of g are computed from the step costs in Figure 3.2, and the
values of
hSLD are given in Figure 3.22.
 Notice in particular that Bucharest first appears on the frontier at step (e), but it is not
selected for expansion because its f-cost (450) is higher than that of Pitesti (417).
Another solution through Pitesti whose cost is as low as 417, so the algorithm will not
settle for a solution that costs 450.
Conditions for optimality: Admissibility and consistency

 The first condition we require for optimality is that h(n) be an


admissible heuristic.

 An admissible heuristic is one that never overestimates the cost to


reach the goal.

 A* is optimal if h(n) is an admissible heuristic h(n) never


overestimates the cost to reach the goal.

g(n)  is the actual cost to reach n along the current path

An obvious example of an admissible heuristic is the straight-line


distance hSLD that we used in getting to Bucharest.

 Straight-line distance is admissible because the shortest path between


any two points is a straight line. so the straight line cannot be an
overestimate.
 A second, slightly stronger condition called consistency (or sometimes
monotonicity) is required only for applications of A∗ to graph search.

 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′.
Optimality of A*
A∗ is optimal if h(n) is admissible, while the graph-search version is
optimal if h(n) is consistent.

The value of f is non-decreasing along the path

By definition of consistency  g (n’) = g(n) + c(n, a, n’))


Contour= outline, or figure
Heuristic Function
The 8-puzzle was one of the earliest heuristic search problems.
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 3.28).

To find a good heuristic function  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.
h1 = the number of misplaced tiles. For Figure 3.28, all of the eight tiles are out of
position, so the start state would have h1 = 8.
h2 = the sum of the distances of the tiles from their goal positions. Because tiles
cannot move along diagonals, the distance we will count is the sum of the
horizontal and vertical distances. This is sometimes called the city block distance
or Manhattan distance. h2 is also admissible because all any move can do is move
one tile one step closer to the goal.
As expected,
Tiles 1 to 8 in the start state give a Manhattan distanceneither
of of these overestimates th
true solution cost, which is 26.
LOCAL SEARCH ALGORITHMS AND OPTIMIZATION PROBLEMS
Introduction

Local search algorithms operate using a single current state (rather


than multiple paths) and generally move only to neighbours of that state.

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 memory-usually a constant amount; and

(2) they can often find reasonable solutions in large or infinite


(continuous) state spaces for which systematic algorithms are unsuitable.
•X-axis: denotes the state
state space land scape space ie states.
•Y-axis: denotes the values of
objective function
corresponding to a particular
state.

Figure 4.1 A one-dimensional state space landscape in which elevation


corresponds to the objective function. The aim is to find the global maximum.
Hill-climbing search modifies the current state to try to improve it, as shown
by the arrow. The various topographic features are defined in the text.
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.

Problem: depending on initial state, can get stuck in local optimum (here
maximum)
LOCAL SEARCH ALGORITHMS AND OPTIMIZATION PROBLEMS

1) Hill-climbing search

2) Simulated annealing search


1) 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.
 To illustrate hill-climbing, we will use the 8-queens problem.

 Local-search algorithms typically use a complete-state formulation, where each


state has 8 queens on the board, one per column.

 The successor function returns all possible states generated by moving a single
queen to another square in the same column (so each state has 8 x 7 = 56
successors).

 The heuristic cost function h is the number of pairs of queens that


are attacking each other, either directly or indirectly.

 The global minimum of this function is zero, which occurs only


at perfect solutions.

 Hill climbing is sometimes called greedy local search because it


grabs a good neighbor state without thinking ahead about where to
go next.
Figure 4.3 (a) An 8-queens state with heuristic cost estimate h=17, showing the value
of h for each possible successor obtained by moving a queen within its column. The
best moves are marked. (b) A local minimum in the 8-queens state space; the state
has h=1 but every successor has a higher cost.
Unfortunately, hill climbing often gets stuck for the following reasons:

1) Local maxima:

2) Ridges:

3) Plateaux:
1) Local maxima: a local maximum is a peak that is higher than each
of its neighboring states, but lower than the global maximum.
Ridges: a ridge is shown in Figure 4.4.

Ridges result in a sequence of local maxima that is very difficult for


greedy algorithms to navigate.

Figure 4.4
Ridge = a long, narrow hilltop, mountain range.
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. (See Figure 4.10.)
A hill-climbing search might be unable to find its way off the
plateau.
A plateau is a flat, elevated landform that rises sharply above the surrounding
area on at least one side
In stochastic hill climbing  1) it does not examine all its neighbour.
2) select one neighbour node at random and decides
whether to choose it as current state.

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 of successors.
Simulated annealing search

drawback of hill climbing

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 get stuck on a local maximum.

 Simulated annealing algorithm  combine hill climbing with a


random walk in some way that yields both efficiency and
completeness.

 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 coalesce into a
low-energy crystalline state.

Coalesce = meld.
If the move improves the situation, it is always accepted. Otherwise, the algorithm
accepts the move with some probability less than 1.

e
If the move improves the situation, it is always accepted. Otherwise, the algorithm
accepts the move with some probability less than 1.

probability

Figure 4.14 The simulated annealing search algorithm


Temperature
Solution for 8-Queen problem
Solutions to 8-queen problems.

You might also like