0% found this document useful (0 votes)
30 views51 pages

Unit 1 AI Part-3 Searchtechniques

Uploaded by

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

Unit 1 AI Part-3 Searchtechniques

Uploaded by

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

Search Techniques

Uninformed search algorithms or Brute-force algorithms or


Exhaustive algorithms, search through the search space all
possible candidates for the solution checking whether each
candidate satisfies the problem’s statement.

Informed search algorithms or Heuristic Search Techniques use


heuristic functions that are specific to the problem, apply them to
guide the search through the search space to try to reduce the
amount of time spent in searching.
Breadth-First Search
• BFS expands all the states one step away from start state ,then

two steps and then three steps….till goal state is reached.

• It gives Optimal Solution.

• The Search is implemented using OPEN and CLOSED Lists.

• OPEN list contains the states that are to be expanded.

• CLOSED Keeps the track of states already expanded.

• OPEN is maintained as a Queue.

• CLOSED is maintained as a stack.


Algorithm (BFS)
Input: START and GOAL states
Local variables : OPEN, CLOSED, STATE-X, SUCCs, FOUND;
Output: Yes or No
Method:
• Initialize OPEN list with START and CLOSED=ϕ
• Found=False;
• While(OPEN!=ϕ and FOUND=false) do
• {
• Remove the first state from OPEN and call it STATE-X;
• Put STATE-X in the front of CLOSED List :
• If STATE-X=GOAL then FOUND=true else
{
Perform EXPAND Operation on STATE-X. Producing List of SUCCs:
Remove from successors those states .if any that are in the CLOSED
List:
Append SUCCs at the end of OPEN List:
} }
If FOUND=true then return Yes else return No
Stop
State space search tree for water jug problem
using BFS
DEPTH FIRST SEARCH

• In DFS ,we go as far down as possible into search tree before


backing up and trying alternatives.
• It works always by generating a descendent of the most recently
expanded node until some depth cut off is reached.
• Then backtracks to next most recently expanded node and
generate one of its descendants.
• DFS is memory efficient.

• OPEN and CLOSE both are stacks.


Algorithm: DFS
The path obtained is

(0,0) 🡪 (5,0) 🡪
(5,3)🡪(0,3)🡪(3,0)🡪(3,3)🡪(5,1)🡪(0,1)🡪(1,0)🡪(1,3) 🡪
(4,0)
Comparisons
• BFS is effective when search tree has low branching factor

• BFS can work even in trees that are infinitely deep.

• BFS Requires more memory.

• BFS gives Optimal Solution.

• DFS is effective when there are less no. of subtrees

• DFS is best when goal exist in the lower left portion of the tree.

• DFS is memory efficient

• DFS may not give optimal solution.

• DFS is dangerous when path closer to START and farther from

GOAL is chosen.
Heuristic Search Techniques
Heuristics
✔ A heuristic is a method that improves the efficiency of the
search process.
✔ Some heuristics help in the search process without
sacrificing any claims and occasionally cause an excellent
path to be overlooked.
✔ Heuristics may not find the best solution every time but
guarantee that they find a good solution in a reasonable
time.
✔ These are particularly useful in solving tough and complex
problems, solutions of which would require infinite time.
Heuristic search
‘A heuristic function maps a problem state descriptions to a measures of
desirability(represented as numbers).
Heuristic search methods use knowledge about the problem domain,
choose promising operators and heuristic functions to evaluate the next
state towards the goal state.
For finding a solution, steps involved:
✔ Add domain—specific information to select what is the best path to
continue searching along.
✔ Define a heuristic function h(n) that estimates the ‘goodness’ of a
node. Specifically, h(n) = estimated cost(or distance) of minimal cost
path from n to a goal state.
Example :
❖ Finding a route from one city to another city.
Heuristic search techniques
• A framework for describing search methods is
provided and several general purpose
search techniques are discussed.
• All are varieties of Heuristic Search:
– Generate and test
– Hill Climbing
– Best First Search
– A* Algorithm
Generate-and-Test

Algorithm
1. Generate a possible solution.
2. Test to see if this is actually a solution.
3. Quit if a solution has been found.
Otherwise, return to step 1.

17
Generate-and-Test

• Acceptable for simple problems.


• Inefficient for problems with large space.

18
Generate-and-Test

• Exhaustive generate-and-test.
• Heuristic generate-and-test: not consider paths that
seem unlikely to lead to a solution.
• Plan generate-test:
C−reate a list of candidates.
A−pply generate-and-test to that list.

19
Generate-and-Test
Example: coloured blocks
“Arrange four 6-sided cubes in a row, with each side of
each cube painted one of four colours, such that on all
four sides of the row one block face of each colour is
showing.”
Heuristic: if there are more red faces than other colours
then, when placing a block with several red faces, use few
of them as possible as outside faces.

20
Generate-and-Test
It is a depth first search procedure since complete
solutions must be generated before they can be tested.
In its most systematic form, it is simply an exhaustive
search of the problem space.
Operate by generating solutions randomly. Also
called as British Museum algorithm

If a sufficient number of monkeys were placed in front of a


set of typewriters, and left alone long enough, then they would
eventually produce all the works of shakespeare.

Dendral: which infers the struture of organic compounds


using NMR spectrogram. It uses plan-generate-test.
Hill Climbing

• Searching for a goal state = Climbing to the top of a hill

• Generate-and-test + direction to move.

• Heuristic function to estimate how close a given state


is to a goal state.

22
Simple Hill Climbing
Algorithm
Input: START & GOAL states
Variables: OPEN, NODE, SUCCs, FOUND
Output: Yes / No
Method:
• Store initially the start node in a OPEN list(maintained as stack),
FOUND=False
• While ( OPEN≠empty and FOUND=false)
{
remove the top element from OPEN list and call it
NODE
if NODE is goal then FOUND = true
else
find SUCCs of NODE. If any:
sort SUCCs by estimated cost from NODE to goal state and
add them to the front of OPEN list.
}
If FOUND= true then return Yes otherwise NO 23
Simple Hill Climbing

• Evaluation function as a way to inject task-specific


knowledge into the control process.

Example: coloured blocks

Heuristic function: the sum of the number of different


colours on each of the four sides (solution = 16).

24
General trace of heuristic function
Hill Climbing : Issues

Local Maximum : It is a state that is better than all its


neighbours but not better than some other states.

Plateau : It is a flat area of the search space where all


neighbouring states has the same value.

Ridge: It is an area of search space that is higher than


surrounding areas but that cannot be traversed by
single move in any one direction.

26
Hill Climbing: Disadvantages

Local maximum
A state that is better than all of its neighbours, but not
better than some other states far away.

27
Hill Climbing: Disadvantages

Plateau
A flat area of the search space in which all neighbouring
states have the same value.

28
Hill Climbing: Disadvantages

Ridge
The orientation of the high region, compared to the
set of available moves, makes it impossible to climb
up.
However, two moves executed serially may increase
the height.

29
Hill Climbing: Disadvantages

Ways to overcome
• Backtrack to some earlier node and try going in a
different direction.
• Make a big jump to try to get in a new section.
• Moving in several directions at once.

30
Hill Climbing: Disadvantages

• Hill climbing is a local method:


Decides what to do next by looking only at the
“immediate” consequences of its choices.
• Global information might be encoded in heuristic
functions.

31
Hill Climbing: Disadvantages

Start Goal
A D
D C
C B
B A
Blocks World

32
Hill Climbing: Disadvantages

Start Goal
A D
0 4
D C
C B
B A
Blocks World
Local heuristic:
+1 for each block that is resting on the thing it is supposed to
be resting on.
−1 for each block that is resting on a wrong thing.
33
Hill Climbing: Disadvantages

0 A 2

D D
C C
B B A

34
Hill Climbing: Disadvantages

D 2
C
B A

A 0
D 0

C C D C 0
B B A B A D

35
Hill Climbing: Disadvantages

Start Goal
A D
−6 6
D C
C B
B A
Blocks World
Global heuristic:
For each block that has the correct support structure: +1 to
every block in the support structure.
For each block that has a wrong support structure: −1 to
every block in the support structure. 24
Hill Climbing: Disadvantages

D −
3
C
B A

A −6
D −2

C C D C −1
B B A B A D

37
Hill Climbing: Conclusion

• Can be very inefficient in a large, rough problem


space.

• Global heuristic may have to pay for computational


complexity.

• Often useful when combined with other methods,


getting it started right in the right general
neighbourhood.

38
A* Algorithm

• It uses heuristic function h(n), and cost to reach


the node n from the start state g(n).
• A* search algorithm finds the shortest path
through the search space using the heuristic
function.
• This search algorithm expands less search tree
and provides optimal result faster.
• A* algorithm uses g(n)+h(n).
• The A* (pronounced "A star") algorithm is a widely used
pathfinding and graph traversal algorithm that efficiently
finds the shortest path between two nodes in a weighted
graph.
• It is an extension of Dijkstra's algorithm, but with an added
heuristic to improve its efficiency by guiding the search
towards the goal.
• The algorithm maintains two main lists:
• Open List: This list contains the nodes that are to be
explored. Initially, it contains the starting node.
• Closed List: This list contains the nodes that have already
been visited and whose neighbors have been explored.
A* uses a combination of two values for each node during
the search:
1.g(n): The actual cost of the path from the start node to
node n. It represents the cost of the path found so far.
2.h(n): The heuristic cost (or the estimated cost) from
node n to the goal node. This is the key difference from
Dijkstra's algorithm and is what makes A* "informed" or
"guided" in its search. The heuristic should be
admissible, meaning it should never overestimate the
true cost to reach the goal from node n.
The total cost for a node is given by f(n) = g(n) + h(n).
The A* algorithm proceeds as follows:

Initialize the open list with the starting node, and set its
g(n) and h(n) values accordingly.

While the open list is not empty:


a. Extract the node with the lowest f(n) value from the
open list. This will be the next node to explore.
b. If the extracted node is the goal node, the path has
been found, and the algorithm terminates.
c. Otherwise, move the node from the open list to the
closed list.
d. For each neighbor of the current node:
i. If the neighbor is in the closed list or an obstacle (unreachable
node), skip it.
ii. Calculate the tentative g(n) value for the neighbor.
iii. If the neighbor is not in the open list or the new g(n) value is lower
than the previously recorded value, update the neighbor's g(n),
h(n), and f(n) values, and set the current node as its parent.
iv. Add the neighbor to the open list if it's not already there.
The algorithm will either find the shortest path to the goal node or
determine that there is no path to the goal.

A* is widely used in various applications, such as pathfinding in


video games, robotics, and route planning in maps and navigation
systems, thanks to its efficiency and ability to find the shortest path
while considering a heuristic to guide the search.
A* Algorithm
make an openlist containing only the starting node
make an empty closedlist
while (the destination node has not been reached):
consider the node with the lowest f score in the openlist
if (this node is our destination node) : finished
else:
put the current_node in the closedlist and look at all of its neighbors
for (each neighbor of the current_node):
if (neighbor has lower g value than current and is in the closedlist) :
replace the neighbor with the new, lower, g value
current_node is now the neighbor's parent

else if (current g value is lower and this neighbor is in the


openlist ) :
replace the neighbor with the new, lower, g value
change the neighbor's parent to our current_node

else if this neighbor is not in both lists:


Solving Eight Puzzle Problem using A* Algorithm
The Evaluation function f(X) is as follows
f(x)=g(x)+h(x) where
h(x)=the number of tiles not in their goal position in a give state X
g(X)=depth of node X in the search tree
Another possible heuristic function for 8 Puzzle problem
is

h(x)=the sum of the distances of the tiles (1 to 8) from


their goal position in a given state X.

h(Start_state)=1+0+1+0+1+0+2+0=5
Optimal Solution by A* Algorithm
A* algorithm finds Optimal solution if heuristic function is carefully
designed and is underestimated.

A* gives an optimal solution if h never overestimates the actual value


from current to goal state.

Underestimation: By underestimating the heuristic value,some effort is


wasted but sill there is chance to go back and try another path and will
find optimal path.
Overestimation; Overestimation of h can not be guaranteed to
give shortest path.
Admissibility of A*
A search algorithm is admissible ,if for any graph, it always terminates
in an optimal path from start state to Goal state,if path exists.

if h is underestimating then heuristic function is called admissible


heuristic function.

MONOTONIC FUNCTION
A heuristic function h is monotone if

1.For all states Xi and Xj such that Xj is successor of Xi


h(Xi) - h(Xj) <= Cost(Xi,Xj) i.e actual cost of going from Xi to Xj

2.h(Goal)=0
Each monotonic heuristic function is admissible.

● A Cost function f is monotone if f(N)<=f(succ(N))

● for any admissible cost function f,we can construct a monotonic


admissible function.
Thank you

You might also like