2 lectures 16 17-informed search algorithms ch 4.3

85
1 Dr. Tarek Helmy, ICS-KFUPM ICS-381 Principles of Artificial Intelligence Week 6 Informed Search Algorithms Chapter 4 Dr.Tarek Helmy El-Basuny

Transcript of 2 lectures 16 17-informed search algorithms ch 4.3

Page 1: 2 lectures 16 17-informed search algorithms ch 4.3

1Dr. Tarek Helmy, ICS-KFUPM

ICS-381Principles of Artificial Intelligence

Week 6

Informed Search Algorithms

Chapter 4

Dr.Tarek Helmy El-Basuny

Page 2: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 2

Last Time

We discussed:

Greedy Best-First Search Example (8-Puzzle)A* Searching algorithmProofing the optimality of A* algorithm

We are going to present

Improving the performance of A*

Iterative-Deepening A* (IDA*) Recursive Best-First Search (RBFS)Simplified Memory Bounded A* (SMA*)

Page 3: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 3

Analyzing the Heuristic Function h(n)

If h(n) = h*(n) for all n,Only nodes on optimal solution path are expandedNo unnecessary work is performed

If h(n) = 0 for all n,This heuristic is admissible, A* performs exactly as Uniform-Cost Search (UCS)

The closer h is to h*,The fewer extra nodes that will be expanded

If h1(n) <= h2(n) <= h*(n) for all n that aren't goals then h2 dominates h1h2 is a better heuristic than h1A1* using h1 expands at least as many if not more nodes than using A2* with h2.A2* is said to be better informed than A1*

Page 4: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 4

A* Search

Major drawback of A*

A* keeps all generated nodes in memory and usually runs out of space long before it runs

out of time.

It cannot venture down a single path unless it is almost continuously having success

(i.e., h is decreasing). Any failure to decrease h will almost immediately cause the

search to switch to another path.

3 important factors influencing the efficiency of algorithm A*The cost (or length) of the path foundThe number of nodes expanded in finding the pathThe computational effort required to compute

We can overcome A* space problem without sacrificing optimality or completeness, at a

small cost in execution time.

4

5

5

3

3

4

3 4

4

2 12

0

3

4

3Goal

Initial State

Page 5: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 5

Improving A*: Memory-bounded Heuristic Search

Iterative-Deepening A* (IDA*) Using f(g+h) as a cut off rather than the depth for the iteration.Cutoff value is the smallest f-cost of any node that exceeded the cutoff on the previous iteration; keep these nodes only.Space complexity O(bd)

Recursive Best-First Search (RBFS) It replaces the f-value of each node along the path with the best f-value of its children.Space complexity O(bd)

Simplified Memory Bounded A* (SMA*)Works like A* until memory is fullThen SMA* drops the node in the fringe with the largest f value and “backs up” this value to its parent.When all children of a node n have been dropped, the smallest backed up value replaces f(n)

Page 6: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 6

IDA* and ID first

IDA* is similar to Iterative depth-first:

DepthDepth--firstfirst

d = 1d = 1

d = 2d = 2

d = 3d = 3

d = 4d = 4

Expand by depthExpand by depth--layerslayers

f1f1

f2f2

f3f3

f4f4

A*A*

Expands by fExpands by f--contourscontours

Page 7: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 7

Iterative deepening A*

DepthDepth--firstfirstin each fin each f--contourcontour

Perform depth-first search LIMITED to some f-bound.

If goal found: ok.

Else: increase de f-bound and restart.

f4f4

f3f3

f2f2

f1f1

How to establish the fHow to establish the f--bounds?bounds?-- initiallyinitially: : f(f(SS))

generate all successorsgenerate all successorsrecord the record the minimalminimal f(f(succsucc)) >> f(f(SS))

Continue with Continue with minimalminimal f(f(succsucc)) instead of instead of f(f(SS))

Page 8: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 8

Iterative Deepening A* Search Algorithm

put s in OPEN, compute f(s)

start

OPEN empty ?

Remove the node of OPEN whose f(s) value is smallestand put it in CLOSE (call it n)

Expand n. calculate f(s) of successorif f(suc) < threshold then

Put successors to OPEN if pointers back to n

n = goal ?

Success

yes

yes

Threshold =min( f(s) , threshold )

set threshold as h(s)

OPEN: to keep track of the current fringe of the search.CLOSED: to record states already visited.

Page 9: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 9

8-Puzzle Example: Iterative Deepening A*

Use f(n) = g(n) + h(n) with admissible and consistent h

The cutoff used is the f-cost (g+h) rather than the depth

we again perform depth-first search, but only nodes with f-cost less than or equal to smallest f-cost of nodes expanded at last iteration.

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

6

Cutoff=4

Goal

Page 10: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 10

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=4

6

Goal

Page 11: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 11

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=4

6

5

Goal

Page 12: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 12

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=4

6

5

5

Goal

Page 13: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 13

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=4

6

5

56

Goal

Page 14: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 14

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

6

Cutoff=5

Goal

Page 15: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 15

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=5

6

Goal

Page 16: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 16

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=5

6

5

Goal

Page 17: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 17

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=5

6

5

7 Goal

Page 18: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 18

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=5

6

5

7

5

Goal

Page 19: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 19

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

4

6

Cutoff=5

6

5

7

5 5

Goal

Page 20: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 20

8-Puzzle Example: Iterative Deepening A*

f(n) = g(n) + h(n), with h(n) = number of misplaced tiles

4

5

5

3

3

4

3 4

4

2 12

0

3

4

3Goal

Initial State

With A * 16 nodes have been expanded

4

4

6

Cutoff=5

6

5

7

5 5

Goal

With IDA * Only 9 nodes expanded

Page 21: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 21

Example 2: Iterative Deepening A*

SSf=100f=100

AAf=120f=120

BBf=130f=130

CCf=120f=120

DDf=140f=140

GGf=125f=125

EEf=140f=140

FFf=125f=125

ff--limited, limited, ff--boundbound = 100= 100 ff--newnew = 120= 120

Page 22: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 22

Example 2: Iterative Deepening A*

SSf=100f=100

AAf=120f=120

BBf=130f=130

CCf=120f=120

DDf=140f=140

GGf=125f=125

EEf=140f=140

FFf=125f=125

ff--limited, limited, ff--boundbound = 120= 120 ff--newnew = 125= 125

Page 23: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 23

Example 2: Iterative Deepening A*

SSf=100f=100

AAf=120f=120

BBf=130f=130

CCf=120f=120

DDf=140f=140

GGf=125f=125

EEf=140f=140

FFf=125f=125

ff--limited, limited, ff--boundbound = 125= 125

SUCCESSSUCCESS

Page 24: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 24

Recursive Best-First Search

Keeps track of the f-value of the best-alternative path available.If current f-values exceeds this alternative f-value then backtrack to alternative path.Upon backtracking change f-value to best f-value of its children.

It takes 2 arguments:a nodean upper boundUpper bound= min (upper bound on it’s parent, current value of it’s lowest cost brother).

It explores the sub-tree below the node as long as it contains child nodes whose costs do not exceed the upper bound.If the current node exceeds this limit, the recursion unwinds back to the alternative path.As the recursion unwinds, RBFS replaces the f-value of each node along the path with the best f-value of its children.

Page 25: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 25

RBFS: Example

Path until Rumnicu Vilcea is already expanded

Above node; f-limit for every recursive call is shown on top.

Below node: f(n)

The path is followed until Pitesti which has a f-value worse than the f-limit.

Page 26: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 26

RBFS: Example

Unwind recursion and store best f-value for current best leaf Pitestiresult, f [best] ← RBFS(problem, best, min(f_limit, alternative))

best is now Fagaras. Call RBFS for new bestbest value is now 450

Page 27: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 27

RBFS: Example

Unwind recursion and store best f-value for current best leaf Fagarasresult, f [best] ← RBFS(problem, best, min(f_limit, alternative))

best is now Rimnicu Viclea (again). Call RBFS for new bestSubtree is again expanded.Best alternative subtree is now through Timisoara.

Solution is found since because 447 > 417.

Page 28: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 28

Simple Recursive Best-First Search (SRBFS)

ImplementationIf one of successors of node n has the smallest f-value over all OPENnodes, it is expanded in turn, and so on.When other OPEN node, n’, (not a successor of n) has the lowest value of f-value

backtracks to the lowest common ancestor, node kkn: successor of node k on the path to nRBFS removes the sub-tree rooted at kn, from OPENkn becomes an OPEN node with f-value value (its backed-up value)Search continues below that OPEN node with the lowest f-value.

Page 29: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 29

SRBFS -The Algorithm

SRBFS ( node: N ,bound B)IF f( N) > B RETURN f(n)IF N is a goal, EXIT algorithmIF N has no children, RETURN infinityFOR each child Ni of N, F[i] := f(Ni)

sort Ni and F[i] in increasing order of F[i] IF only one child, F[2] = infinityWHILE (F[1] ≤ B and f[1] < infinity)

F[1] := SRBFS (N1, MIN(B, F[2]))insert N1 and F[1] in sorted order

RETURN F[1]

Page 30: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 30

RBFS and IDA* Comparison

RBFS is somewhat more efficient than IDA*But still suffers from excessive node regeneration.

If h(n) is admissible, RBFS is optimal.Like A*, optimal if h(n) is admissible

Space complexity is O(bd).IDA* keeps only one single number (the current f-cost limit)

Time complexity difficult to characterizeDepends on accuracy if h(n) and how often best path changes.Difficult to characterize

Both IDA* and RBFS are subject to potential exponential increasein complexity associated with searching on Graph, because they cannot check for repeated states other than those on the currentpath.Thus, they may explore the same state many times.

Page 31: 2 lectures 16 17-informed search algorithms ch 4.3

31Dr. Tarek Helmy, ICS-KFUPM

ICS-381Principles of Artificial Intelligence

Week 6.2

Informed Search Algorithms

Chapter 4

Dr.Tarek Helmy El-Basuny

Page 32: 2 lectures 16 17-informed search algorithms ch 4.3

32Dr. Tarek Helmy, ICS-KFUPM

Major 1 Reminder

Time: 5 – 7:00 PM,

Date: Tomorrow (Sat., Nov. 3),

Place: Building 24-121

Materials: Every thing we presented up to the last class

Page 33: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 33

Simplified Memory-Bounded A* (SMA*)

IdeaExpand the best leaf (just like A* ) until memory is fullWhen memory is full, drop the worst leaf node (the one with the highest f-value) to accommodate new node.

If all leaf nodes have the same f-value, SMA* deletes the oldestworst leaf and expanding the newest best leaf.

Avoids re-computation of already explored areaKeeps information about the best path of a “forgotten” subtree in its ancestor.

Complete if there is enough memory for the shortest solution pathOften better than A* and IDA*

Trade-off between time and space requirements

Page 34: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 34

Simplified Memory-bounded A*

Optimizes A* to work within reduced memory.

Key idea:

SS

AA BB

CC

1313

1515 1313

memory of 3 nodes onlymemory of 3 nodes only

If memory is full and we need to generate an extra node (C):

Remove the highest f-value leaffrom QUEUE (A).Remember the f-value of the best ‘forgotten’ child in each parent node (15 in S).

(15)(15)

1818

BB

Page 35: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 35

Generate Successor 1 by 1

SS

AA BB

1313

First add A, later BFirst add A, later B

AA BB

When expanding a node (S), only add its successor 1 at a time to QUEUE.

we use left-to-right

Avoids memory overflow and allows monitoring of whether we need to delete another node

Page 36: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 36

Adjust f-values

SS

AA BB

1313

1515 2424

better estimate for better estimate for f(f(SS))

1515 If all children M of a node N have been explored and for all M:

f(S...M) > f(S...N)

Then reset:f(S…N) = min { f(S…M) | Mchild of N}

A path through N needs to go through 1 of its children !

Page 37: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 37

Too long path: give up

SS

BB

CC

1313

1313

memory of 3 nodes onlymemory of 3 nodes only

DD

1818∞

BB

CC

If extending a node would produce a path longer than memory: give up on this path (C).

Set the f-value of the node (C) to ∞(to remember that we can’t find a path here)

Page 38: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 38

SMA*: an example

SS

AA BB

CC G1G1 DD G2G2

EE G3G3 G4G4 FF

0+12=120+12=12

8+5=138+5=1310+5=1510+5=15

24+0=2424+0=2416+2=1816+2=1820+0=2020+0=2020+5=2520+5=25

30+5=3530+5=35 30+0=3030+0=30 24+0=2424+0=24 24+5=2924+5=29

1010 88

1010 101088 1616

1010 1010 88 88

SS1212

SS1212

AA1515

SS1212

AA1515 BB

1313

SS1313

BB1313

AA1515

AA1515

DD 1818

(15)(15)

13131212

Page 39: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 39

Example: continued

SS1313

BB

DD ∞

1313

(15)(15)13131515

SS

AA BB

CC G1G1 DD G2G2

EE G3G3 G4G4 FF

0+12=120+12=12

8+5=138+5=1310+5=1510+5=15

24+0=2424+0=2416+2=1816+2=1820+0=2020+0=2020+5=2520+5=25

30+5=3530+5=35 30+0=3030+0=30 24+0=2424+0=24 24+5=2924+5=29

1010 88

1010 101088 1616

1010 1010 88 88

SS13 (15)13 (15)

BB1313

DD ∞

DD ∞

((∞)

G2G22424

13132424

SS1515

BB2424((∞)

(15)(15)

G2G22424

(15)(15)

G2G22424

((∞)AA

1515

SS1515

AA1515

BB2424

BB2424

(24)(24)

CC 2525 ∞

SS15 (24)15 (24)

AA

CC ∞

1515

CC ∞

(∞)

G1G1 2020

151520 20

151520 20

Page 40: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 40

SMA*: Properties:

Complete: If available memory allows to store the shortest path.

Optimal: If available memory allows to store the best path.

Otherwise: returns the best path that fits in memory.

Memory: Uses whatever memory available.

Speed: If enough memory to store entire tree: same as A*

Thrashing

SMA* switches back and forth continually between a set of candidate

solution paths, only a small subset of which can fit in memory. More

computation for switching rather than searching.

Page 41: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 41

Performance Measure

PerformanceHow search algorithm focus on goal rather than walk off in irrelevant directions.P = L / T

L : depth of goalT : Total number of node expanded

Effective Branching Factor (B)B + B2 + B3 + ..... + BL = T

Page 42: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 42

Partial Searching

The searches covered so far are characterized as partial searches. Why?Partial Searching:

Means, it looks through a set of nodes for shortest path to the goal state using a heuristic function.The heuristic function is an estimate, based on domain-specific information, of how close we are to a goal.Nodes: state descriptions, partial solutionsEdges: action that changes state for some costSolution: sequence of actions that change from the start to the goal stateBFS, IDS, UCS, Greedy, A*, etc.Ok for small search spaces that are often "toy world" problemsNot ok for Hard problems requiring exponential time to find the optimal solution, i.e. Traveling Salesperson Problem (TSP)

Page 43: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 43

Local Search and Optimization

Previous searches:keep paths in memory, and remember alternatives so search can backtrack. Solution is a path to a goal.Path may be irrelevant, if the final configuration only is needed (8-queens, IC design, network optimization, …)

Local Search:Use a single current state and move only to neighbors. Use little spaceCan find reasonable solutions in large or infinite (continuous) state spaces for which the other algorithms are not suitable

Optimization:Local search is often suitable for optimization problems.Search for best state by optimizing an objective function.

Page 44: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 44

Another Approach: Complete Searching

Typically used for problems where finding a goal state is more important than finding the shortest path to it. Examples: airline scheduling, VLSI layout.A problem is called NP (nondeterministic polynomial time) class if it is solvable in polynomial time.How can hard NP problems be solved in a reasonable (i.e. polynomial) time? by using either:

Approximate model: find an exact solution to a simpler version of the problemApproximate solution: find a non-optimal solution of the original hard problem

Next we'll explore means to search through complete solution space for a solution that is near optimal.Complete Searching

Look through solution space for better solutionNodes: complete solutionEdge: operator changes to another solutionCan stop at any time and have a solution

Technique suited for:Optimization problemsHard problems, e.g. Traveling Salesman Problem (TSP):

Page 45: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 45

Traveling Salesperson Problem (TSP)

A salesman wants to visit a list of citiesStopping in each city only onceReturning to the first cityTraveling the shortest distance

Nodes are cities

Arcs are labeled with distances between cities

5 Cities TSP

5

9

8

4

A

ED

B C

5

6

7

5 32

A B C D E

A 0 5 8 9 7

B 5 0 6 5 5

C 8 6 0 2 3

D 9 5 2 0 4

E 7 5 3 4 0

Page 46: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 46

Traveling Salesperson Problem (TSP)

5

9

8

4

A

ED

B C

5

6

7

5 32

A solution is a permutation of cities, called a toure.g. A – B – C – D – E – ALength 24

How many solutions exist?(n-1)!/2 where n = # of citiesn = 5 results in 12 toursn = 10 results in 181440 toursn = 20 results in ~6*1016 tours

A B C D EA 0 5 8 9 7B 5 0 6 5 5C 8 6 0 2 3D 9 5 2 0 4E 7 5 3 4 0

5 City TSP

Page 47: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 47

Another Approach: Complete Searching

An operator is needed to transform one solution to another.What operators work for TSP?

Two-swap (common)Take two cities and swap their location in the toure.g. A-B-C-D-E swap(A,D) yields D-B-C-A-E

Two-interchangeReverse the path between two cities e.g. A-B-C-D-E interchange (A,D) yields D-C-B-A-E. Both work since graph is fully connected

Solutions that can be reached with one application of an operator are in the current solution's neighborhood .Local searches only consider solutions in the neighborhood.In general, the neighborhood should be much smaller than the size of the search space.

Hill-ClimbingBeam SearchSimulated AnnealingGenetic Algorithms, other

An evaluation function f(n) is used to map each solution to a number corresponding to the quality of that solution.

Page 48: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 48

Hill-Climbing Search

“Continuously moves in the direction of increasing value”It terminates when a peak is reached.

Looks one step ahead to determine if any successor is better than the current state; if there is, move to the best successor.

If there exists a successor s for the current state n such that

h(s) < h(n)

h(s) <= h(t) for all the successors t of n,

Then move from n to s. Otherwise, halt at n.

Similar to Greedy search in that it uses h, but does not allow backtracking or jumping to an alternative path since it doesn’t “remember” where it has been.

Not complete since the search will terminate at "local minima," "plateaus," and "ridges."

Page 49: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 49

Hill Climbing: Example-1

SS

DD

AA EE

BB FF

GG

SS

AA 8.98.910.410.4

10.410.4 6.96.9

6.76.7 3.03.0

Perform depth-first, BUT: instead of left-to-right selection,

FIRST select the child with the best heuristic value

1. Pick a random point in the search space

2. Consider all the neighbors of the current state

3. Choose the neighbor with the best quality and move to that state

4. Repeat 2 thru 4 until all the neighboring states are of lower quality

5. Return the current state as the solution state

Page 50: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 50

Hill Climbing Search: Implementation

1. QUEUE <-- path only containing the root;2. WHILE QUEUE is not empty AND goal is not reached

DO remove the first path from the QUEUE;create new paths (to all children);reject the new paths with loops;sort new paths (HEURISTIC) ;add the new paths to front of QUEUE;

3. IF goal reachedTHEN success;ELSE failure;

function HILL-CLIMBING( problem) return a state that is a local maximuminput: problem, a problemlocal variables: current, a node.

neighbor, a node.current ← MAKE-NODE(INITIAL-STATE[problem])loop do

neighbor ← a highest valued successor of currentif VALUE [neighbor] ≤ VALUE[current] then return STATE[current]current ← neighbor

Page 51: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 51

Hill Climbing: Example-2

547

2

10

12

4

8

Optimal goal (this might not be reached)

Suboptimal goal

start state (candidate solution)

6

3

These numbers measurevalue, not path length

Neighbor solutions

8

Page 52: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 52

8 Queens Example: Hill Climbing

N N N Y N N N YConflicts

2212312

N N N N N Y N YConflicts

N N N N N N N NConflicts

The numbers give the number of conflicts. Choose a move withthe lowest number of conflicts. Randomly break ties. Hill descending.

Move the queenin this column

Move the queenin this column

33

23230

Done!

Page 53: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 53

Example-3: Hill Climbing

2 8 31 6 47 5

2 8 31 47 6 5

2 31 8 47 6 5

1 38 4

7 6 5

2

31 8 47 6 5

2

1 38 47 6 5

2start goal

5

h = 3

h = 3

h = 2

h = 1

h = 0h = 4

5

4

43

2

f(n) = (number of tiles out of place)

Page 54: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 54

Example-3: Hill Climbing

1 2 34 57 8 6

1 2 34 5

7 8 6

1 34 2 57 8 6

1 24 5 37 8 6

1 2 34 5 67 8

1 2 34 57 8 6

1 2 34 8 5

7 6

1 2 34 8 57 6

1 2 34 8 57 6

1 24 8 37 6 5

1 2 34 87 6 5

5

6 4

3

4 2

1 3 3

0 2

h(n)We can use heuristics to guide “hill climbing”search.

In this example, the Manhattan Distance heuristic helps us quickly find a solution to the 8-puzzle.

Page 55: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 55

Drawbacks of Hill Climbing

Problems:Local Maxima: peaks that aren’t the highest point in the space: No progressPlateaus: the space has a broad flat region that gives the search algorithm no direction (random selection to solve)Ridges: flat like a plateau, but with drop-offs to the sides; Search may oscillate from side to side, making little progress

Remedy: Introduce randomness

Global Maximum

Page 56: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 56

Problems of Hill Climbing

1 2 34 5 86 7

1 2 34 56 7 8

1 2 34 5 86 7

1 2 34 56 7 8

1 24 5 36 7 8

6

7 5

6 6

h(n)In this example, hill climbing does not work!

All the nodes on the fringe are taking a step “backwards”(local minima)

Page 57: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 57

Local Beam Search

The idea is that you just keep around those states that are relatively good, and just forget the rest.Local beam search: somewhat similar to Hill Climbing:

Start from N initial states.Expand all N states and keep k best successors.

This can avoid some local optima, but not always. It is most useful when the search space is big and the local optima aren't too common. Local Beam Search Algorithm Keep track of k states instead of one

Initially: k random states

Next: determine all successors of k statesExtend all paths one stepReject all paths with loopsSort all paths in queue by estimated distance to goal

If any of successors is goal → finished

Else select k best from successors and repeat.

Page 58: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 58

Local Beam Search

Concentrates on promising paths

Page 59: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 59

Local Beam Search

Assume a pre-fixed WIDTH i.e. 2

Perform breadth-first, BUT:

Only keep the WIDTH best new nodes depending on heuristic at each new level.

SS

AA DD10.410.4 8.98.9

Depth 1)Depth 1)

SS

BB DD AA EE

AA DD

SS

6.76.7 8.98.9 10.410.4 6.96.9

Depth 2)Depth 2)

XXignoreignore

XXignoreignore

Page 60: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 60

Local Beam Search: Example 2

CC EE BB FFBB DD AA EE

SS

AA DD

AA CC GG

XX XXXX

__

__10.410.4 0.00.0

Depth 4)Depth 4)

CC EE BB FFBB DD AA EE

SS

AA DD

XX XX4.04.0 6.96.9 6.76.7 3.03.0

Depth 3)Depth 3)

XXignoreignore

__endend

Ignore leafs that are not goal nodes

Page 61: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 61

Genetic Algorithms: Basic Terminology

1. Chromosomes: Chromosome means a candidate solution to a problem and is encoded as a string of bits.

2. Genes: a chromosome can be divided into functional blocks of DNA, genes, which encode traits, such as eye color. A different settings for a trait (blue, green, brown, etc.) are called alleles. Each gene is located at a particular position, called a locus, on the chromosome. Genes are single bits or short blocks of adjacent bits.

3. Genome: the complete collection of chromosomes is called the organism’s genome.

Population: A set of Chromosomes (Collection of Solutions)1. Genotype: a set of genes contained in a genome.2. Crossover (or recombination): occurs when two chromosomes bump into one

another exchanging chunks of genetic information, resulting in an offspring. 3. Mutation: offspring is subject to mutation, in which elementary bits of DNA are

changed from parent to offspring. In GAs, crossover and mutation are the two most widely used operators.

4. Fitness/Evaluation Function: the probability that the sates will live to reproduce.

Page 62: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 62

Definitions

01 0 01 01 0 1 0 01 1 01 10 10 01 0 10 1 0 0 1 1 01 10 10 0 1 0 10 1 0 0 1 1 0 1 1

0 10 0 10 10 1 00 1 1 0 11

01 0 0 1 01 01 0 01 1 0 1 1

01 0 0 1 01 01 0 0 1 10 1 10 10 0 10 10 1 00 1 1 0 11

0 1 0 01 01 0 1 0 01 1 01 1

Population

0 1 0 0 1 0 1 0 1 0 0 1 1 0 1 1Chromosome:

1Gene:

Page 63: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 63

Crossover

Merging 2 chromosomes to create new chromosomes

0 1 0 0 1 0 1 0 1 0 0 1 1 0 1 1 0 1 0 0 1 0 1 0 1 0 0 1 1 0 1 1

Page 64: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 64

Mutation

Random changing of a gene in a chromosome

0 1 0 0 1 0 1 0 1 0 01 1 1 0 1 1

Mutation can help for escaping from Local Maximums in our state space.

Page 65: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 65

Genetic Algorithms: Crossover

Reproduction(Crossover)

y x

-

tan

sin

+

log

y

pow

x

Parent state A +

cos

y

/

x

Parent state B

5

y x

-

tan

sin

+

cos

y

/

x

Child of A and B

10011101

1110100010011000

Parent state A

Parent state BChild of A and B

Page 66: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 66

Genetic Algorithms: Mutation

5

+

tan

y

/

x

Child of A

5

+

cos

y

/

x

Mutation

Parent state A

C A E F BD A

C A E D BF A

Parent state A

Child of A

10011101 10011111

Parent state A

Child of A

Mutation

Page 67: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 67

GA Operators

A genetic algorithm maintains a population of candidate solutions for the problem at hand, and makes it evolve by iteratively applying a set of stochastic operators:Selection replicates the most successful solutions found in a population at a rate proportional to their relative quality.Crossover: decomposes two distinct solutions and then randomly mixes their parts to form novel solutions.Mutation: Randomly converts some of the bits in a chromosome. For example, if mutation occurs at the second bit in chromosome 11000001, the result is 10000001.

1 0 1 0 1 1 1

1 1 0 0 0 1 1

Parent 1

Parent 2

1 0 1 0 0 1 1

1 1 0 0 1 1 0

Child 1

Child 2 Mutation

Page 68: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 68

A simple Genetic Algorithm

The outline of a simple genetic algorithm is the following:1. Start with the randomly generated population of “n” j-bit chromosomes.2. Evaluate the fitness of each chromosome.3. Repeat the following steps until n offspring have been created:

a. Select a pair of parent chromosomes from the current population based on their fitness.

b. With the probability pc, called the crossover rate, crossover the pair at a randomly chosen point to form two offspring.

c. If no crossover occurs, the two offspring are exact copies of their respective parents.

d. Mutate the two offspring at each locus with probability pm, called the mutation rate, and place the resulting chromosomes in the new population.

e. If n is odd, one member of the new population is discarded at random.

4. Replace the current population with the new population.5. Go to step 2.

Page 69: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 69

Example 1: Genetic Algorithm

Assume the following:Length of each chromosome = 8, Fitness function f(x) = the number of ones in the bit string, Population size n = 4,Crossover rate pc = 0.7,Mutation rate pm = 0.001

The initial, randomly generated, population is the following:

Chromosome label Chromosome string Fitness

A 00000110 2

B 11101110 6

C 00100000 1

D 00110100 3

Page 70: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 70

Example 1: Step 3a

We will use a fitness-proportionate selection, where the number of times an individual is selected for reproduction is equal to its fitness divided by the average of the finesses in the population, which is (2 + 6 + 1 + 3) / 4=3

For chromosome A, this number is 2 / 3 = 0.667 For chromosome B, this number is 6 / 3 = 2For chromosome C, this number is 1 / 3 = 0.333For chromosome D, this number is 3 / 3 = 1

(0.667 + 2 + 0.333 + 1 = 4)Step 3b Apply the crossover operator on the selected parents:

Given that B and D are selected as parents, assume they crossover after the first locus with probability pc to form two offspring:

E = 10110100 and F = 01101110.Assume that B and C do not crossover thus forming two offspring which are exact copies of B and C.

Step 3c: Apply the mutation operator on the selected parents:Each offspring is subject to mutation at each locus with probability pm.Let E is mutated after the sixth locus to form E’ = 10110000, and offspring B is mutated after the first locus to form B’ = 01101110.

Fitness Functions

Page 71: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 71

Example 1: Steps 3b and 3c

The new population now becomes:

Chromosome label Chromosome string Fitness

E’ 10110000 3

F 01101110 5

C 00100000 1

B’ 01101110 5

Note that the best string, B, with fitness 6 was lost, but the average fitness of the population increased to (3 + 5 + 1 + 5) / 4= 3.5.

Iterating this process will eventually result in a string with all ones.

Page 72: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 72

Example 2: Genetic Algorithms

Fitness function: number of non-attacking pairs of queens (min = 0, max = 8 × 7/2 = 28)

24/(24+23+20+11) = 31% 23/(24+23+20+11) = 29% etc

Page 73: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 73

Discussion of Genetic Algorithms

• Genetic Algorithms require many parameters... (population size, fraction of

the population generated by crossover; mutation rate, etc... )

• How do we set these?

• Genetic Algorithms are really just a kind of hill-climbing search, but seem

to have less problems with local maximums…

• Genetic Algorithms are very easy to parallelize...

• Applications: Protein Folding, Circuit Design, Job-Scheduling Problem,

Timetabling, designing wings for aircraft, ….

Page 74: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 74

Simulated Annealing

Motivated by the physical annealing processAnnealing: harden metals and glass by heating them to a high temperature and then gradually cooling themAt the start, make lots of moves and then gradually slow downMore formally…

Instead of picking the best move (as in Hill Climbing),Generate a random new neighbor from current state,If it’s better take it,If it’s worse then take it with some probability proportional to the temperatureand the delta between the new and old states,Probability gets smaller as time passes and by the amount of “badness” of the move,

Compared to hill climbing the main difference is that SA allows downwards steps; (moves to higher cost successors).Simulated annealing also differs from hill climbing in that a move is selected at random and then decides whether to accept it.

Page 75: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 75

Simulated Annealing

Function simulated-annealing (problem) returns a solution statecurrent = start stateFor time = 1 to forever do

T = schedule[time]If T = 0 return currentnext = a randomly chosen successor of current∆ = value(next) – value(current)If ∆ > 0 then current = next

else current = next with probability e∆/T

End for

This version usually (but not always) makes uphill moves.

Page 76: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 76

Intuition behind simulated annealing

The probability of making a downhill move decreases with time (length of the exploration path from a start state).

The choice of probability distribution for allowing downhill moves is derived from the physical process of annealing metals (cooling molten metal to solid minimal-energy state).

During the annealing process in metals, there is a probability p that a transition to a higher energy state (which is sub-optimal) occurs. Higher energy implies lower value. The probability of going to a higher energy state is e∆/T where

∆ = (energy of next state) – (energy of current state)

T is the temperature of the metal.

p is higher when T is higher, and movement to higher energy states become less likely as the temperature cools down.

For both real and simulated annealing, the rate at which a system is cooled is called the annealing schedule, or just the “schedule.”

Page 77: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 77

The effect of varying ∆ for fixed T=10

∆ e ∆/10

-43 0.01

-13 0.27

0 1.00

A negative value for ∆implies a downhill step.The lower thevalue of ∆, the bigger the step wouldbe downhill, and the lower the probabilityof taking this downhillmove.

Page 78: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 78

The effect of varying T for fixed ∆ = -13

T e -13/T

1 0.000002

50 0.56

1010 0.9999…

The greater thevalue of T, the smaller the relativeimportance of ∆and the higher theprobability of choosing a downhillmove.

Page 79: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 79

Logistic (sigmoid) function for Simulated Annealing

Alternatively we could select any move (uphill or downhill)with probability 1/(1+e –∆/T). This is the logistic function, also called the sigmoid function. If we use this function, thealgorithm is called a stochastic hill climber. The logistic function:

Δ

1.0

T very high

p T near 0

0-20 20

0.5

Page 80: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 80

Example

0e T/ ≅∆−

Uphill1

e11obabilityPr ∆/T ≅

+= −Δ=13; T=1:

0.5e11obabilityPr ∆/T ≅

+= −Δ=13; T=1010: 1e T/ ≅∆−

DownhillΔ=-13; T=1: 0

e11obabilityPr ∆/T ≅

+= −

0e T/ ≅∆−

0.5e11obabilityPr ∆/T ≅

+= −Δ=-13; T=1010: 0e T/ ≅∆−

Page 81: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 81

Summary

Best-first search = general search, where the minimum-cost nodes (according to some measure) are expanded first.Greedy search = best-first with the estimated cost to reach the goal as a heuristic measure.

- Generally faster than uninformed search- Not optimal, Not complete.

A* search = best-first with measure = path cost so far + estimated path cost to goal.- Combines advantages of uniform-cost and greedy searches- Complete, optimal and optimally efficient- Space complexity still exponential

Time complexity of heuristic algorithms depend on quality of heuristic function. Good heuristics can sometimes be constructed by examining the problem definition or by generalizing from experience with the problem class.Iterative improvement algorithms keep only a single state in memory.Can get stuck in local extreme; simulated annealing provides a way to escape local extreme, and is complete and optimal given a slow enough cooling schedule.

Page 82: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 82

Summary

What if we follow some small number of paths?

• Hill Climbing1. Moves in the direction of increasing value.2. Terminates when it reaches a “peak”.3. Does not look beyond immediate neighbors

• Local beam search:1. Start with k randomly generated nodes.2. Generate k successors of each.3. Keep the best k out of the them.

• Simulated Annealing1. Generate a random new neighbor from current state.2. If it’s better take it.3. If it’s worse then take it with some probability proportional

• Genetic algorithms:1. Start with k randomly generated nodes called the population.2. Generate successors by combining pairs of nodes.3. Allow mutations.

Page 83: 2 lectures 16 17-informed search algorithms ch 4.3

83Dr. Tarek Helmy, ICS-KFUPM

Announcement

Term Projects progress

Time: During the Class

Date: (Mon. & Wed., Nov. 17, 24),

Each group will present/demonstrate the progress of

their project within 10 minutes.

Page 84: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 84

Projects Proposal Presentation Schedule-071

Team Members Time Project Title

224236 Noor Al-Sheala214539 Ali Al-Ribh235213 Husain Al-Saleh

8:00, Sat. Pebble Picking Game

222628 Sawd Al-Sadoun235091 Abdulaziz Al-Jraifni235865 Ibrahim Al-Hawwas

8:10, Sat. Speech Recognition

224792 Maitham Al-Zayer223132 Mahmoud Al-Saba222974 Reda Al-Ahmed

8:20: Sat. 4*4 game234383 Husain Al-Jaafar226574 Yousf Al-Alag224510 Abdullah Al-Safwan

8:30: Sat. Smart Registrar

224328 Khalid Al-Sufyany215789 Yousef Al-Numan

8:40, Sat. XXXXXXXXXXXXXXXXXXXXXXXX

234681 Nader Al-Marhoun234165 Ali Aoun

8:00, Mon. Fingerprint recognition

226246 Abdullah Al-Mubearik230417 Nawaf Al-Surahi 8:10, Mon. Intelligent Adviser234681 Saeed Al-Marhoon215027 Mohamed Habib 8:20, Mon.

XXXXXXXXXXXXXXXXXXXXXXXXX

207091 Mohamed Al-Shakhs 8:30, Mon. XXXXXXXXXXXXXXXXXXXXXXXXX

Page 85: 2 lectures 16 17-informed search algorithms ch 4.3

Dr. Tarek Helmy, ICS-KFUPM 85

The End!!

Thank you

Any Questions?