1 / 79

Heuristic Search

Heuristic Search. CIS 479/579 Bruce R. Maxim UM-Dearborn. State Space Search. Many AI problems can be conceptualized as searching a well defined set of related problem states Puzzle solving Game playing Generate and test Genetic algorithms Scheduling algorithms. Water Jug Problem.

joshua
Download Presentation

Heuristic Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heuristic Search CIS 479/579 Bruce R. Maxim UM-Dearborn

  2. State Space Search • Many AI problems can be conceptualized as searching a well defined set of related problem states • Puzzle solving • Game playing • Generate and test • Genetic algorithms • Scheduling algorithms

  3. Water Jug Problem • Problem • You have a four gallon jug and a three gallon jug and the goal is to come up with exactly two gallons of water. • Operations • Dump bucket contents on the ground • Dump bucket contents in other bucket • Fill bucket to top with water

  4. Tree Representation (0,0) (4,0) (0,3)   (0,3) (0,0) (4,3) (1,3) (3,0) (4,3) (0,0)

  5. Digraph Representation (0,0) (4,0) (0,3) (1,3) (4,3) (3,0)

  6. Adjacency List • (0 0)  ((4 0) (0 3)) • (4 0)  (( 4 3) (0 0) (1 3) (0 3)) • (0 3)  ((4 3) (3 0) (0 0)) • (1 3)  ((1 0) (4 3) (4 0) (0 3))

  7. Good Knowledge Representations • Important things made clear /explicit • Expose natural constraints • Must be complete • Are concise • Transparent (easily understood) • Information can be retrieved & stored quickly • Detail suppressed (can be found as needed) • Computable using existing procedures

  8. River Puzzle • Problem • There are four items a farmer, wolf, goose, and corn. The farmer can only take one item across the river at a time. • Constraints • Wolf will eat the goose if left alone with it • Goose will eat the corn if left alone with it

  9. Problem Analysis • Each of the 4 problem elements can be considered a Boolean variable based on its bank location • There are 24 or 16 possible states • 6 of these states fail the “no eat” test • That leaves 10 states to consider which could be linked n C(10,2) = 45 ways • However only 10 of these links can really be used to connect states

  10. F=Farmer W=Wolf G=Goose C=Corn ~=River W F ~ W F G F W F G ~ G F ~ W C W C C ~ G F G ~ C F ~ W C F ~ C F W W G ~ G G ~ G C C C F C W ~ G W

  11. Solution • Once graph is constructed finding solution is easy (simply find a path) • AI programs would rarely construct the entire graph explicitly before searching • AI programs would generate nodes as needed and restrict the path search to the nodes generated • May use forward reasoning (initial to goal) or backward reasoning (goal to initial)

  12. Rules • Many time these state transitions are written as rules: If ((Wolf Corn) (Farmer Goose)) Then ((Wolf Corn Farmer) (Goose)) • Depending on which direction you are reasoning you match either the left (if) or right (then) part

  13. Potential Problems • If you have more than one state to move to then you need a procedure to choose one • Exact matches often do not occur in the real world so you may need to measure how “close” you are to an exact match

  14. Control Strategies • A good control strategy causes motion (ideally toward the goal state) • The control strategy should be systematic and not allow repeated use of the same rule sequences (to avoid infinite loops)

  15. Heuristics • AI programming often relies on the use of heuristics • Heuristics are techniques that improves efficiency by trading “speed” for “completeness”

  16. Search Considerations • Can the search algorithm work for the problem space? • Is the search algorithm efficient? • Is it easy to implement? • Is search the best approach or is more knowledge better?

  17. Example Graph A F S B C

  18. Adjacency List (setf (get 's 'children) '(a b)) (setf (get 'a 'children) '(s b f)) (setf (get 'b 'children) '(s a c)) (setf (get 'c 'children) '(b f)) (setf (get 'f 'children) '(a c))

  19. “Any Path” Search Algorithms • Depth First Search • Hill Climbing • Best First Search • Breadth First Search • Beam Search

  20. Depth First Search Add root to queue of partial paths Until queue is empty or goal is attained If first queue element equals the goal then do nothing Else remove the first queue element add its children to the front of the queue of the partial paths If goal is attained then announce success

  21. Depth First Output > (depth 's 'f) ((s)) ((a s) (b s)) ((b a s) (f a s) (b s)) ((c b a s) (f a s) (b s)) ((f c b a s) (f a s) (b s)) (s a b c f)

  22. Depth First Weakness • Depth first is not good for “tall” trees when it is possible to over commit to a “bad” path early • In our example depth first missed a complete solution because it focused on checking the first partial path and did not test the others in the queue

  23. Depth First Search (defun depth (start finish) (depth1 (list (list start)) finish))(defun depth1 (queue finish) (cond ((null queue) nil) ((equal finish (caar queue)) (print queue) (reverse (car queue))) (t (print queue) (depth1 (append (expand (car queue)) (cdr queue)) finish))))

  24. expand (defun expand (path) ;kills cycles (remove-if #’(lambda (path) (member (car path) (cdr path)) ) (mapcar #'(lambda (child)(cons child path)) (get (car path) 'children) ) ) ) > (expand '(a s)) ((b a s) (f a s))

  25. Recursive Depth First (defun simple-depth (tree goal) (cond ((= (car tree) goal) tree) ((atom tree) nil) (t (cons (car tree) (or (simple-depth (cadr tree) goal) (simple-depth (caadr tree) goal) ) ) ) ) )

  26. Breadth First Search Add root to queue of partial paths Until queue is empty or goal is attained If first queue element equals the goal then do nothing Else remove the first queue element add its children to the rear of the queue of the partial paths If goal is attained then announce success

  27. Breadth First Output > (breadth 's 'f) ((s)) ((a s) (b s)) ((b s) (b a s) (f a s)) ((b a s) (f a s) (a b s) (c b s)) ((f a s) (a b s) (c b s) (c b a s)) (s a f)

  28. Breadth First Weakness • Breadth first is not good for “fat” trees where the nodes have high branching factors • Can also be bad choice when several partial paths lead to the same node several levels down (bottleneck) • Not always fast but it will never miss a complete solution

  29. Breadth First Search (defun breadth (start finish) (breadth1 (list (list start)) finish)) (defun breadth1 (queue finish) (cond ((null queue) nil) ((equal finish (caar queue)) (print queue) (reverse (car queue))) (t (print queue) (breadth1 (append (cdr queue) (expand (car queue))) finish))))

  30. Improving Search • We will need to add knowledge to our algorithms to make them perform better • We will need to add some distance estimates, even using “best guesses” would help • We will need to begin sorting part of the the queue of partial paths

  31. Insertion Sort (defun insertion-sort (s predicate) ;insertion sort for lists (cond ((null s) nil) (t (splice-in (car s) (insertion-sort (cdr s) predicate) predicate)))

  32. Insertion Sort Helper (defun splice-in (element s predicate) ;insert in order (cond ((null s) (list element)) ;end of s? ((funcall predicate element (car s)) (cons element s)) ;add here? (t (cons (car s) (splice-in element (cdr s) predicate))) ;try further down ))

  33. sort • Common Lisp has a built in “sort” function that allows you to sort any list using a predicate capable of comparing two list elements • Examples > (sort '(1 2 3 4 5) '>) (5 4 3 2 1) > (sort '(5 4 3 2 1) '<) (1 2 3 4 5)

  34. closerp • When could define a predicate function that either retrieves or computes the distance to the goal for the nodes (defun closerp (x y) (< (get (car x) 'distance) (get (car y) 'distance) ) )

  35. Example Graph 1 A 0 2 F S B C 2 1

  36. Using closerp > (setf (get 's 'distance) 2) > (setf (get 'a 'distance) 1) > (setf (get 'b 'distance) 2) > (setf (get 'c 'distance) 1) > (setf (get 'f 'distance) 0) > (sort '((b s) (b a s) (f a s)) 'closerp) ((f a s) (b s) (b a s))

  37. Euclidean Distance • Distance could be computed based on properties of the node instead • Another possibility (defun distance (n1 n2) (sqrt (+ (expt (- (get n1 ‘x) (get n2 ‘x)) 2) (expt (- (get n1 ‘y) (get n2 ‘y)) 2) ) ) )

  38. Hill Climbing • An improved depth first search • Requires the ability to guess the distance to the goal using an ordinal scale (does not require exact distances) • Children are sorted before being added to the front of the queue of partial paths

  39. Uses for Hill Climbing • You must have adjustable (and reversible) operations to control progress • Adjusting thermostat without numbers • Adjusting fuzzy TV signal • Mountain climbing in the fog using an altimeter

  40. Hill Climbing Add root to queue of partial paths Until queue is empty or goal is attained If first queue element equals the goal then do nothing Else remove the first queue element sort its children based on distance to goal add children to the front of the queue of the partial paths If goal is attained then announce success

  41. Hill Climbing Output > (hill 's 'f) ((s)) ((a s) (b s)) ((f a s) (b a s) (b s)) (s a f)

  42. Hill Climbing (defun hill (start finish) (hill1 (list (list start)) finish)) (defun hill1 (queue finish) (cond ((null queue) nil) ((equal finish (caar queue)) (print queue) (reverse (car queue))) (t (print queue) (hill1 (append (sort (expand (car queue)) 'closerp) (cdr queue) ) finish))))

  43. Hill Climbing Weaknesses • Foothill problem • Local peaks attract procedure’s attention away from trying to reach the “top” • Plateau problem • Area flat so there is very little to attract procedure to one path over another • Ridge problem • Every step is down though not at a local minimum (e.g. at the Northpole every step is south)

  44. Best First Search • An improved hill climbing or depth first search • Requires the ability to guess the distance to the goal using an ordinal scale (does not require exact distances) • Entire queue of partial path is sorted after it is modified

  45. Best First Search Add root to queue of partial paths Until queue is empty or goal is attained If first queue element equals the goal then do nothing Else remove the first queue element add its children to the front of the queue of the partial paths sort the queue of partial paths If goal is attained then announce success

  46. Best First Output > (best 's 'f) ((s)) ((a s) (b s)) ((f a s) (b a s) (b s)) (s a f)

  47. Best First Search (defun best (start finish) (best1 (list (list start)) finish)) (defun best1 (queue finish) (cond ((null queue) nil) ((equal finish (caar queue)) (print queue) (reverse (car queue))) (t (print queue) (best1 (sort (append (expand (car queue)) (cdr queue)) 'closerp) finish))))

  48. Best First Weaknesses • Still requires the use of a “natural” distance measure • Sorting takes time • Tends to produce shorter paths than other depth first search algorithms, but is not guaranteed to produce the shortest path

  49. Beam First Search • Improved version of breadth first search • Good for “fat” trees • Does not guarantee it will find shortest path • Trades speed for completeness

  50. Beam First Search Add root to queue of partial paths Until queue is empty or goal is attained If first queue element equals the goal then do nothing Else If length (queue) > then then take first queue elements as new queue remove the first queue element add its sorted children to the rear of the queue of the partial paths If goal is attained then announce success

More Related