26
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

Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

  • Upload
    lydat

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Lecture 2An Brief Review of

Conventional Search Algorithms

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/1

Page 2: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

State space representationMaze 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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/2

Maze game

0 1 2

2

1

0

x

y

Page 3: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

State space representation

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 2 3

8 B 4

7 6 5

2 B 3

1 8 4

7 6 5

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/3

Initial state

Goal state

Page 4: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Search graph

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

U D

U D

U D

U D

U D

R

L L

L

R

R

• 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

Page 5: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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 a 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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/5

2 B 3

1 8 4

7 6 5

2 8 3

1 B 4

7 6 5

B 2 3

1 8 4

7 6 5

2 3 B

1 8 4

7 6 5

Page 6: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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

Page 7: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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

Page 8: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/8

Point: Do not re-visit the same node!

Page 9: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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 !

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/9

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

U D

U D

U D

U D

U D

R

L L

L

R

R

Page 10: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/10

Point: Keep un-visited nodes in the open list!

Page 11: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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

Page 12: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Depth-first Search

• If we implement the open list using STACK, Algorithm-3 is a depth-first search algorithm.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/12

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

U D

U D

U D

U D

U D

R

L L

L

R

R

(0,0)

(0,1)

(1,1) (0,2)

(2,1) (1,2) (1,0)

(2,0) (2,2)

(1) Open: {(0,1)}Closed: {(0,0)}

Page 13: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Depth-first Search

• If we implement the open list using STACK, Algorithm-3 is a depth-first search algorithm.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/13

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

U D

U D

U D

U D

U D

R

L L

L

R

R

(0,0)

(0,1)

(1,1) (0,2)

(2,1) (1,2) (1,0)

(2,0) (2,2)

(2) Open: {(1,1)(0,2))}Closed: {(0,0),(0,1)}

Page 14: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Depth-first Search

• If we implement the open list using STACK, Algorithm-3 is a depth-first search algorithm.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/14

(0,0)

(0,1)

(0,2)

(1,0)

(1,1)

(1,2)

(2,0)

(2,1)

(2,2)

U D

U D

U D

U D

U D

R

L L

L

R

R

(0,0)

(0,1)

(1,1) (0,2)

(2,1) (1,2) (1,0)

(2,0) (2,2)

(3) Open: {(1,0),(2,1),(1,2),(0,2)}Closed: {(0,0),(0,1),(1,1)}

Page 15: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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/15

Page 16: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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/16

Page 17: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/17

Cost of the edge between (n,n’)

Page 18: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/18

If each node is a solution, c(ni,nj) represents the increment of cost when we replace ni with nj.

Page 19: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Example of uniform cost search

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/19

A

D

F

C

E

G H

B

1

21

1

4

2

2

1

From “artificial intelligence”, Ohmsha

Page 20: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Heuristic search

• Hill climbing search

• Best-first search

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/20An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/20

Meta-heuristics

Heuristics

Page 21: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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/21

Page 22: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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 maximize a 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/22

Page 23: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Properties of Algorithm-5

• Usually, the heuristic function ℎ(𝑛) is defined as a fitness function (e.g. recognition rate for pattern recognition).

• 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.

• Minimization problems can be solved by using – ℎ(𝑥).

• Problem: Search can become stuck in suboptimal solution.An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/23

Page 24: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

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

D

F

C

E

G H

B

1

21

1

4

2

2

1

Lec02/24An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©

Page 25: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Example of best-first search

F

B

A

EH

G

(1)

Open: {<G,5>}

Closed: {F}

(2)

Open: {<H,3>,<E,4>}

Closed: {F,G}

(3)

Open: {<E,4>}

Closed: {F,G,H}

(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}

Lec02/25An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved ©

Here, heuristic function is the predicted cost, rather than real cost. Thus, we can search more quickly, but do not guarantee the optimal solution.

Page 26: Lecture 2 An Brief Review of Conventional Search Algorithmsqf-zhao/TEACHING/MH/Lec02.pdf · Algorithm-5: Hill climbing algorithm •Step 1: n=initial node. •Step 2: Finish with

Homework

• For the example given in p. 12, complete the searchprocess by providing information related to the open list and the closed list.

• Submit your answer in hard copy before the class of next week.

An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved © Lec02/26