Professional Documents
Culture Documents
Techniques
Searching
• Searching is the process of looking for
the solution to a goal state through a set
of possibilities.
C 2
B 9
2
3 F goal state
start state A D E
4
3 4
Searching for a solution
C 2
B 9
2
3 F goal state
start state A D
3
Search tree
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
state = C, state = F,
cost = 5 cost = 12
goal state!
state = A,
cost = 7
search tree nodes and states are not the same thing!
Full search tree
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
goal state!
state = A,
state = F,
cost = 7
cost = 11
goal state!
state = B, state = D,
.. cost = 10
.. cost = 10
. .
Changing the goal:
want to visit all vertices on the graph
C 2
B 9
2
3 F
A E
D 4
3 4
state = F, {A, B}
state = C, {A, B} state = E, {A, D}
cost = 12
cost = 5 cost = 7
state = A, {B, C}
state = F, {A, D, E}
cost = 7
cost = 11
state = D, {A, B, C}
state = B, {A, C}
cost = 10
What would happen if the
.. cost = 10
.. goal were to visit every
. . location twice?
Key concepts in search
• Set of states that we can be in
• Including an initial state…
• … and goal states (equivalently, a goal test)
• For every state, a set of actions that we can take
• Each action results in a new state
• Typically defined by successor function
• Given a state, produces all states that can be reached from it
• Cost function that determines the cost of each action
(or path = sequence of actions)
• Solution: path from initial state to a goal state
• Optimal solution: solution with minimal cost
8-puzzle
1 2 1 2 3
4 5 3 4 5 6
7 8 6 7 8
goal state
8-puzzle
1 2
4 5 3
7 8 6
1 2 1 2 1 5 2
4 5 3 4 5 3 4 3
7 8 6 7 8 6 7 8 6
.. ..
. .
Generic search algorithm
• Fringe = set of nodes generated but not expanded
S
• Select a child
• convention: left-to-right
A • Repeatedly go to next
child, as long as possible.
B
C E • Return to left-over
alternatives (higher-up)
D F only when needed.
G
Depth-first search
Implementing depth-first search
• Fringe can be maintained as a Last-In-First-Out (LIFO) structure
(aka. a stack)
• Also easy to implement recursively:
• DFS(node)
• If goal(node) return solution(node);
• For each successor of node
• Return DFS(successor) unless it is failure;
• Return failure;
Depth-first pseudocode:
1. QUEUE <-- path only containing the root;
3. IF goal reached
THEN success;
ELSE failure;
Example:
• Consider the following map:
A 4 B 4 C
3
S 5 5 G
4 D E F 3
2 4
Advantage
1. Completeness- Depth first is Complete for FINITE number of
nodes.
Disadvantages
1.Speed- IF the search space contains very deep branches
without solution, THEN Depth-first may waste much time in
them.
S • Move
downwards,
A D level by level,
until goal is
B A
reached.
D E
C E E B B F
D F B F C E A C G
G C G F
G
Breadth-first search
Properties of breadth-first search
• Nodes are expanded in the same order in which they are
generated
• Fringe can be maintained as a First-In-First-Out (FIFO) queue
• BFS is complete: if a solution exists, one will be found
• BFS finds a shallowest solution
• Not necessarily an optimal solution
• If every node has b successors (the branching factor), first
solution is at depth d, then fringe size will be at least bd at some
point
• This much space (and time) required
2. Breadth-first: Pseudo code
A 4 B 4 C
3
S 5 5 G
4 D E F 3
2 4
Limitations
1 .Memory demanding : it Is Very demanding on
memory.
3. IF goal reached
THEN success;
ELSE failure;
4. Iterative deepening Search
•Restrict a depth-first search to a
fixed depth.
Goal
Start
Bi-directional pseudocode:
1. QUEUE1 <-- path only containing the root;
QUEUE2 <-- path only containing the goal;
S
3 4 • At each step,
select the node
4
4 A 3 5 D with the lowest
5 2 accumulated
cost.
B 7 D 8 A9 E 6
4 5 5 4
5 4
C 11 E 12 E 13 B 13 B 11 F 10
3
D F B F C E A C G 13
G C G F
G
Uniform cost search
• Consider the following problem.
•
•
• Solution: SBG
Tutorial 1.
• Consider the following map
Uniform cost search exercise
Use the following to search the
path from A to p.
•1 uniform cost search method
•2. depth first
•3. breadth first
•4. bi-directional search
•3. iterative depth limited
Exercise
A 4 B 4 C
3
S 5 5 G
4 D E F 3
2 4
•Use the following blind search methods
•To find the path from S to G in the above map.
•1. uniform cost search method
•2. bi-directional search method
•3. iterative depth limited search
Informed Searching
Techniques
Concept of informed (heuristic) search
• Involves exploring the node that is most likely to be nearest to
a goal state.
• There is no guarantee that the heuristic provided will get you
closer to a goal state than any other.
• Also called informed search
heuristics Example
• An office (goal) in a city building
• Knowledge
• Building name e.g. KICC, NSSF?
• Wing,
• floor no
• Street etc
• Example:
Patient visit the doctor and specify symptoms.
• Knowledge : fever, nausea, headache, …
Knowledge (heuristics) example
• Example:
• Climbing a hill in thick Fog
• Heuristic function checks the change in altitude in 4 directions;
• the strongest increase is the direction in which to move next.
Heuristic (informed) Searches - Characteristics
1. They require some domain knowledge (heuristics)
2. Usually more efficient than blind searches
3. There is no guarantee that the next node to be expanded
is the best node
Heuristic Searches - Why Use?
Advantage:
They are efficient in terms of both time and space.
Unlike blind search methods that are too resource intensive
(inefficient)
Heuristic search
Disadvantages:
1. There is no guarantee that the heuristic function will return a
value that ensures that a particular node is closer to a goal
state than any other node.
2. heuristics may not be available
Heuristic functions
• Heuristic function is a function that tells us how close we are
to a goal state
• That is, the heuristic function give an estimate of how close
the current state is to the goal state.
Uniform cost vs heuristic functions
• Uniform cost method calculates the cheapest cost so far
(accumulated cost).
• This cost is denoted by g(n)
• On the other hand, heuristic function tries to estimate how
close we are from the current state to the goal state
• This estimate is denoted by h(n)
Heuristic Functions Example
• We can develop a heuristic function that helps us to find a
solution to the Romanian route finding problem.
• One possible heuristic is based on the straight line distance (as
the crow flies) between two cities
Heuristic Functions Example
Heuristic Functions Example
• The route to your final destination may take a winding road so,
although the Straight Line Distance is shorter, the actual
distance to travel is longer.
• E.g SLD for arad to sibiu =140
• But the actual distance might be longer than 140.
• This is because you might follow a wind road to Sibiu
A 4 B 4 C
3
S 5 5 G
4 D E F 3
2 4
• Define f(T) = the straight-line distance from T
to G A 10.4 B 6.7 C 4
11 The estimate
S
G can be wrong!
8.9
D E 6.9 F 3
Heuristic Search Methods
• Methods that use a heuristic function to provide specific
knowledge about the problem:
• Examples:
• Hill climbing method
• Greedy search method
• A* search Method
Hill climbing
• This method tries to find a point in the search space that is
better than all the others.
• That is a solution state of better quality than all the others.
• The search Moves in direction of best value
• The best value can be the highest or the lowest value.
• It depends on the search problem
Hill climbing
• Hill climbing is a combination of depth-first and heuristic
functions
Hill climbing algorithm
1. Pick a random point in the search space.
2. Consider all the neighbours of the current state.
3. Choose the neighbour with the best quality and move to that
state.
4. Repeat 2 thru 4 until all the neighbouring states are of lower
quality.
5. Return the current state as the solution state.
Hill climbing example1
10.4
A 6.9
E
6.7 B F 3.0
G
Problems of hill climbing
• Local maxima
• Plateau
• Ridges
local maxima problem
• When we reach a position where there are no better
neighbours, it is not a guarantee that we have found the best
solution state. It might be a small hillock instead of at the top
of the mountain rang
Foothills:
Local
maximum
local maxima problem
• This small hillock (peak) is called local maxima.
• It has no better neighbours but there are better solution states
elsewhere in the search space.
Hill climbing algorithm problems
local maxima problem
• One solution to this problem is to conduct a multi-start hill-
climb
• This consists of a series of hill climbing searches, each one
starting at a random point in the search space.
• The best result is chosen from all the searches and is returned
as the solution.
2. Plateau
• This is an area where the search space is flat so that all
neighbours return the same evaluation.
• The solution of this problem is to conduct a random walk or
jump until you find an area that starts to give better quality
solutions
2. Plateau
• If there are no neighbours of better quality it is assumed that
we are on the top of table top mountain
Plateaus
Ridges:
• When the search has reached on top of a ridge ,all directions fixed
in advance move downwards from the current position surface.
The solution of this problem is to set new rules, which are more directly
targeted to the goal, irrespective of the value returned .
Ridges
Heuristic Searches - Greedy search
• So named because it takes the biggest “bite” it can out of the
problem.
It expands the node that is judged to be closest to the goal
state.
• To make this judgement it uses the heuristic function, h(n).
in other words,
• Always expand the heuristically best nodes first.
Greedy search, or heuristic best-first search:
S 30
10 40
1 20 • At each step, select
3
the node with the
best (in this case:
lowest) heuristic
35 15 value.
2 27 18
25 45 The ‘open’
nodes
Greedy search algorithm:
S
3 4
4
4 A 3 5 D • At each step,
5 2 select the node
B 7 D 8 A 9 E 6 with the lowest
4 5 5 4 accumulated
5 4
cost.
C 11 E 12 E 13 B 13 B 11 F 10
3
D F B F C E A C G 13
G C G F
G
Now incorporate heuristic estimates
• Replace the ‘accumulated cost’ in the ‘Uniform cost search’ by a function:
fn g(n) h(n)
• where:
cost(path) .g(n) = the accumulated cost of the
partial path
h(T) = a heuristic estimate of the cost
remaining
from T to a goal node
• Combines the cost so far and the heuristic estimated cost to the
goal. That is fn = g(n) + h(n)
This gives us estimated cost of the cheapest solution through path n
4 4
3 A B C
S 5 5
G
4 D 2 E F 3
4
• Define h(n) = the straight-line distance from
node to G A 10.4 B 6.7 C 4
11
S
G
8.9
D E 6.9 F 3
S
3 + 10.4 = 13.4 A D 4 + 8.9 = 12.9
S
A 13.4 D
9 + 10.4 = 19.4 A E 6 + 6.9 = 12.9
X
S
A 13.4 D
A E
X B F 10 + 3.0 = 13.0
11 + 6.7 = 17.7
S
A 13.4 D
A
X
E STOP!
17.7 B F
G 13 + 0.0 = 13.0
Summary
• Blind Search is very expensive
• A Search with info (heuristics) is more efficient
END