Lecture-2

Lecture 2
An Brief Review of
Conventional Search Algorithms
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/1
State space representation
Maze game
• Initial state: (0,0)
• Goal state: (2,2)
• Rules:
–
–
–
–
Rule1 : Move upward
Rule2: Move downward
Rule3: Move left
Rule4: Move right
• The state after moving will
be different for different
current states.
• For some states, some rules
cannot be used.
y
2
1
0
1
0
2
x
Maze game
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/2
State space representation
Initial state
8-puzzle problem
• Initial state: (2,B,3,4,5,6,7,1,8)
• Goal state: (1,2,3,4,5,6,7,8,B)
• Rules:
–
–
–
–
Rule 1: Move B to North
Rule 2: Move B to South
Rule 3: Move B to East
Rule 4: Move B to West
• The state after moving will be
different for different current
states.
• For some states, some rules
cannot be used.
1
8
7
2
B
6
3
4
5
Goal state
2
1
7
B
8
6
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
3
4
5
Lec02/3
Search graph
(0,2)
U
D
(0,1)
U
(1,2)
D
(0,0)
U
R
L
D
(1,1)
U
(2,2)
D
(1,0)
U
R
D
(2,1)
L
R
L
(2,0)
• Based on the state
representation, we can
draw a search graph for
the maze game.
• The problem is to find a
path between node (0,0)
and (2,2)
• Since there are different
solutions, we should find
the best path.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/4
Search tree
• For the 8 puzzle problem,
however, finding the
solution using search graph
is not efficient because
there are too many nodes
(about 180 thousands) .
• In such cases, we can use
search tree.
• Start from the root (initial
state), we can find the
solution by expanding the
tree recursively.
• Hopefully, we can find a
solution at a certain point.
2 B 3
B 2 3
1 8 4
1 8 4
7 6 5
7 6 5
2 8 3
1 B 4
7 6 5
2 3 B
1 8 4
7 6 5
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/5
Brute-force search
•
•
•
•
•
•
Random search
Search with closed list
Search with open list
Depth-first search
Breadth-first search
Uniform-cost search
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/6
Algorithm-1: Random Search
•
•
•
•
•
Step 1: n=initial node.
Step 2: Finish if n is the goal.
Step 3: Expand n to get a set S of child-nodes.
Step 4: Select a node n’ from S at random.
Step 5: n=n’, return to 2.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/7
Algorithm-2: Search with Closed List
• Step 1: n=initial node.
• Step 2: Finish if n is the goal.
• Step 3: Expand n to get a set S of child nodes.
– If S is empty, finish with failure;
– Else put n to the closed list C.
• Step 4: Select a node n’ ∉C from S. If such n’ does
not exist, finish with failure.
• Step 5: n=n’, and return to Step 2.
Point: Do not re-visit the same node!
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/8
Properties of Algorithm-2
• If the graph is finite (has finite number of nodes), Algorithm-2
always stops in a limited number of steps.
• But, we may not get a solution.
– Example: (0,0)(0,1)(0,2)(0,1)  Closed already !
(0,2)
U
D
(0,1)
U
(1,2)
D
(0,0)
U
R
L
D
(1,1)
U
(2,2)
D
(1,0)
U
R
D
(2,1)
L
R
(2,0)
L
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/9
Algorithm-3: Search with Open List
• Step 1: Put the initial node to the open list O.
• Step 2: Select a node n from O. If O is empty, finish
with failure; else, if n is the goal, finish with success.
• Step 3: Expand n to get a set S of child nodes, and
put n to the closed list C.
• Step 4: Select from S a node ∉C, link it to n, and
put it to O.
• Step 5: Return to Step 2.
Point: Keep un-visited nodes in the open list!
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/10
Features of Algorithm-3
• Algorithm-3 can find the solution if the graph is finite
and the solution exists.
• If the path from the initial node to the goal is the
solution we want, we can get the solution by tracing
back from the goal to the initial node, using the links
found during search.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/11
Depth-first Search
Open: {(1,1)(0,2))}
• If we implement the (2)
open
list using
Closed: {(0,0),(0,1)}
STACK, Algorithm-3 is a depth-first
search algorithm.
(0,0)
(3) Open: {(1,0),(2,1),(1,2),(0,2)}
Closed: {(0,0),(0,1),(1,1)}
(0,2)
U
U
D
(0,1)
U
(1,2)
D
(0,0)
R
L
U
D
(1,0)
(0,1)
(2,2)
D
(1,1)
(1) Open: {(0,1)}
Closed: {(0,0)}
U
R
D
(1,1)
(0,2)
(2,1)
L
R
(2,0)
(1,0)
(2,1)
(2,0)
(2,2)
(1,2)
L
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/12
Breadth-first Search
• If we implement the open list using a queue,
Algorithm-3 is a breadth-first search algorithm.
• In breadth-first search, all nodes in the same level
are first visited before going down.
• We can find the solution if the solution exists, even
when the tree is infinite (number of branches is
limited).
• For depth-first search, we may not be able to find
the solution if the tree is infinitely large.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/13
To find the best solution
• When there are many solutions, we would like to
find the one with the minimum cost.
• Examples include: the cheapest or faster route for
traveling around the world.
• The algorithms studied so far may not be able to find
the best solution.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/14
Algorithm-4: Uniform-cost Search
• Step 1: Put the initial node ni and its cost g(ni)=0 to the open
list O.
• Step 2: Take a node n from the head of O. If O is empty, finish
with failure; else if n is the goal, finish with success.
• Step 3: Expand n to get a set S of child nodes, and put n into
the closed list C.
• Step 4: Select a node n’ ∉C from S, and link n’ to n.
– Find g(n’)=g(n)+c(n,n’), and put n’ and its cost into O.
– If n’ is included in O already, and the new cost is smaller, replace the
old one with n’, along with its cost and link.
– Sort O in non-decreasing order according to the cost.
• Step 5: Return to Step 2.
Cost of the edge between (n,n’)
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/15
Properties of Algorithm-4
• Uniform-cost search finds the lowest cost path from
ni to n when n is expanded. That is, g(n)=g*(n).
• Even if the graph is infinite, the algorithm stops in
limited number of steps.
• If the cost of each edge c(ni,nj) is 1, the algorithm is
reduced to breadth-first search.
If each node is a solution, c(ni,nj) represents the
increment of cost when we replace ni with nj.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/16
Example of uniform cost search
A
B
2
1
C
4
2
1
E
1
F
1
G
D
H
2
From “artificial intelligence”, Ohmsha
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/17
Heuristic search
• Hill climbing search
• Best-first search
Meta-heuristics
Heuristics
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/18
Why heuristic search?
• Heuristics are some knowledge that cannot be
described explicitly using natural languages, but are
useful for finding solutions more efficiently.
• In fact, for most problems related to artificial
intelligence, if we try to find the best solution using
brute-force approaches, we cannot get the answer in
time.
• We human being actually solve this problem very
wisely using different heuristics. In some sense, a
person (e.g. detective and doctor) is more clever
than another if his heuristics are better.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/19
Algorithm-5: Hill climbing algorithm
• Step 1: n=initial node.
• Step 2: Finish with success if n is the
goal.
• Step 3: Expand n to get a set S of
child nodes. If S is empty, finish with
failure.
• Step 4: Select from S a node n’ to
minimize the heuristic function
h(n’). If h(n)<h(n’), finish with
failure.
• Step 5: n=n’, and return to Step 2.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/20
Properties of Algorithm-5
• Usually, the heuristic function h(n) is defined
as the distance between n and the goal; or
some fitness measure defined by the user.
• In Algorithm-5, search direction is nothing but
the one that can maximize the performance in
each step, in a similar way as climbing a
mountain.
• Problem: Search can become stuck in
suboptimal solution.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/21
Algorithm-6: Best-first Search
• For the example shown in the right
figure, if n=G, and if we select H as the
next node, search will finish with a
failure.
• This problem can be solved if we
introduce the open list and the closed
list, and perform uniform cost search.
• In this case, we sort the open list
based on the heuristic function, rather
than the cost function.
• This is called the best-first search.
A
B
2
1
4
C
2
1
E
1
F
1
G
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
D
2
H
Lec02/22
Example of best-first search
F
G
H
(3)
Open: {<E,4>}
Closed: {F,G,H}
(1)
Open: {<G,5>}
Closed: {F}
(2)
Open: {<H,3>,<E,4>}
Closed: {F,G}
E
A
B
Here, heuristic function is the
predicted cost, rather than real
cost. Thus, we can search more
quickly, but do not guarantee the
optimal solution.
(4)
Open: {<A,2>,<C,3><D,3>}
Closed: {F,G,H,E}
(5)
Open: {<B,0>,<C,3><D,3>}
Closed: {F,G,H,E,A}
(6)
Open: {<C,3><D,3>}
Closed: {F,G,H,E,A}
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/23
Homework
• For the example given in p. 12, complete the search
process by providing information related to the open
list and the closed list.
• Complete the search tree for the breadth-first search
for the same problem as in p. 12.
An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©
Lec02/24