1 / 57

Search Problems

Search Problems. (read Chapters 3 and 4 of Russell and Norvig) Many (perhaps most) AI problems can be considered search problems. This can be modeled on geographical search.

viho
Télécharger la présentation

Search Problems

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. Search Problems (read Chapters 3 and 4 of Russell and Norvig) • Many (perhaps most) AI problems can be considered search problems. • This can be modeled on geographical search. • Start with a list of actions that can be taken at various geographical points on the map and their consequences, formulated as conditionals of the form ((precondition & action) -> result) (proclaim '(special *planning-conditionals*)) (defun pcond (precondition action) (list '-> (list '& precondition (concatenate 'string "go to " action)) action))

  2. Search Problems (defun precond (condit) (second (second condit))) (defun action (condit) (third (second condit))) (defun result (condit) (third condit))

  3. Road Map of Romania Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  4. Find Bucharest (setf *planning-conditionals* (list (pcond "Oradea" "Zerind") (pcond "Oradea" "Sibiu") (pcond "Arad" "Sibiu") (pcond "Sibiu" ”Fagaras") (pcond "Arad" "Timisoara") (pcond "Timisoara" "Lugoj") (pcond "Lugoj" "Mehadia") (pcond "Mehadia" "Craiova") (pcond "Craiova" "Rimnicu Vilcea") (pcond "Rimnicu Vilcea" "Sibiu") (pcond "Rimnicu Vilcea" "Pitesti") (pcond "Pitesti" "Bucharest") (pcond ”Fagaras" "Bucharest") ... ))

  5. Find Bucharest • Given a starting point, a route to Bucharest will consist of a list of actions which, if performed in order, will end with our being in Bucharest. • Routes can be characterized recursively: • If there is a conditional ((town1 & action) -> town2) in *planning-conditionals* then (list action) is a route from town1 to town2. • If there is a conditional ((town1 & action) -> town) in *planning-conditionals* and route is a route from town to town2, then (cons actionroute) is a route from town1 to town2.

  6. Find Bucharest • If we allow loops, there are infinitely many routes, so let us preclude loops. • Non-circular routes that do not reuse actions in a list used-actions of actions can be characterized recursively: • If there is a conditional ((town1 & action) -> town2) in *planning-conditionals* and action is not a member of used-actions, then (list action) is a non-circular route from town1 to town2. • If there is a conditional ((town1 & action) -> town) in *planning-conditionals* and route is a non-circular route from town to town2 that does not reuse any members of (cons action used-actions) , then (cons actionroute) is a non-circular route from town1 to town2. • A non-circular route from town1 to town2 is a non-circular route that does not reuse “go to town1”. C E F B A D

  7. Find Bucharest • To avoid circularity, we must keep track of the actions already tried and not try them again. We also want to avoid going back through the starting point. (defun find-routes-from (town1 town2 &optional used-actions) (when (null used-actions) (setf used-actions (list (concatenate 'string "go to " town1)))) (let ((routes nil)) (dolist (condit *planning-conditionals*) (when (and (equal (precond condit) town1) (not (mem (action condit) used-actions))) (cond ((equal (result condit) town2) (push (list (action condit)) routes)) (t (let ((subroutes (find-routes-from (result condit) town2 (cons (action condit) used-actions)))) (dolist (subroute subroutes) (push (cons (action condit) subroute) routes))))))) routes))

  8. Find Bucharest (find-routes-from "Lugoj" "Bucharest") (("go to Timisoara" "go to Arad" "go to Sibiu" "go to Fagaras" "go to Bucharest") ("go to Timisoara" "go to Arad" "go to Sibiu" "go to Rimnicu Vilcea" "go to Craiova" "go to Pitesti" "go to Bucharest") ("go to Timisoara" "go to Arad" "go to Sibiu" "go to Rimnicu Vilcea" "go to Pitesti" "go to Bucharest") ("go to Timisoara" "go to Arad" "go to Zerind" "go to Oradea" "go to Sibiu" "go to Fagaras" "go to Bucharest") ("go to Timisoara" "go to Arad" "go to Zerind" "go to Oradea" "go to Sibiu" "go to Rimnicu Vilcea" "go to Craiova" "go to Pitesti" "go to Bucharest") ("go to Timisoara" "go to Arad" "go to Zerind" "go to Oradea" "go to Sibiu" "go to Rimnicu Vilcea" "go to Pitesti" "go to Bucharest") ("go to Mehadia" "go to Dobreta" "go to Craiova" "go to Rimnicu Vilcea" "go to Pitesti" "go to Bucharest") ("go to Mehadia" "go to Dobreta" "go to Craiova" "go to Rimnicu Vilcea" "go to Sibiu" "go to Fagaras" "go to Bucharest") ("go to Mehadia" "go to Dobreta" "go to Craiova" "go to Pitesti" "go to Rimnicu Vilcea" "go to Sibiu" "go to Fagaras" "go to Bucharest") ("go to Mehadia" "go to Dobreta" "go to Craiova" "go to Pitesti" "go to Bucharest")) • We can also write the code to display the search as it goes along. (defun indent (n) (dotimes (x n) (princ " .")))

  9. (defun find-routes-from (town1 town2 &optional used-actions (indent 0)) (when (null used-actions) (setf used-actions (list (concatenate 'string "go to " town1)))) (when (zerop indent) (terpri)) (let ((routes nil)) (dolist (condit *planning-conditionals*) (when (and(equal (precond condit) town1) (not (mem (action condit) used-actions))) (cond ((equal (result condit) town2) (push (list (action condit)) routes) (indent indent) (princ "Completing route with: ") (princ (action condit)) (terpri)) (t (indent indent) (princ "Trying: ") (princ (action condit)) (terpri) (let ((subroutes (find-routes-from (result condit) town2 (cons (action condit) used-actions) (1+ indent)))) (dolist (subroute subroutes) (push (cons (action condit) subroute) routes))))))) routes))

  10. (find-routes-from "Lugoj" "Bucharest") Trying: go to Mehadia .Trying: go to Dobreta . .Trying: go to Craiova . . .Trying: go to Rimnicu Vilcea . . . .Trying: go to Sibiu . . . . .Trying: go to Fagaras . . . . . .Completing route with: go to Bucharest . . . . .Trying: go to Oradea . . . . . .Trying: go to Zerind . . . . . . .Trying: go to Arad . . . . . . . .Trying: go to Timisoara . . . . .Trying: go to Arad . . . . . .Trying: go to Zerind . . . . . . .Trying: go to Oradea . . . . . .Trying: go to Timisoara . . . .Trying: go to Pitesti . . . . .Completing route with: go to Bucharest . . .Trying: go to Pitesti . . . .Completing route with: go to Bucharest . . . .Trying: go to Rimnicu Vilcea . . . . .Trying: go to Sibiu . . . . . .Trying: go to Fagaras . . . . . . .Completing route with: go to Bucharest . . . . . .Trying: go to Oradea . . . . . . .Trying: go to Zerind . . . . . . . .Trying: go to Arad . . . . . . . . .Trying: go to Timisoara . . . . . .Trying: go to Arad . . . . . . .Trying: go to Zerind . . . . . . . .Trying: go to Oradea . . . . . . .Trying: go to Timisoara

  11. Find Bucharest • Sometimes we just want to find a single route (defun find-route-from (town1 town2 &optional used-actions) (when (null used-actions) (setf used-actions (list (concatenate 'string "go to " town1)))) (dolist (condit *planning-conditionals*) (when (and (equal (precond condit) town1) (not (mem (action condit) used-actions))) (cond ((equal (result condit) town2) (return-from find-route-from (list (action condit)))) (t (let ((subroute (find-route-from (result condit) town2 (cons (action condit) used-actions)))) (when subroute (return-from find-route-from (cons (action condit) subroute)))))))))

  12. Reporting search (defun find-route-from (town1 town2 &optional used-actions (indent 0)) (when (null used-actions) (setf used-actions (list (concatenate 'string "go to " town1)))) (when (zerop indent) (terpri)) (dolist (condit *planning-conditionals*) (when (and (equal (precond condit) town1) (not (mem (action condit) used-actions))) (cond ((equal (result condit) town2) (return-from find-route-from (list (action condit)))) (t (indent indent) (princ "Trying: ") (princ (action condit)) (terpri) (let ((subroute (find-route-from (result condit) town2 (cons (action condit) used-actions) (1+ indent)))) (when subroute (return-from find-route-from (cons (action condit) subroute)))))))))

  13. Going from Urziceni to Timisoara Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  14. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  15. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  16. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  17. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  18. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  19. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  20. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  21. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  22. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  23. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  24. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  25. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  26. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  27. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  28. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  29. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  30. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  31. Going from Urziceni to Timisoarabreadth-first search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  32. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  33. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  34. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  35. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  36. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  37. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  38. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  39. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  40. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  41. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  42. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  43. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  44. Going from Urziceni to Timisoara depth-first search search Oradea Neamt Zerind Iasi Arad Sibiu Fagaras Vasiui Rimnicu Vilcea Timisoara Lugoj Pitesti Hirsova Mehadia Urziceni Bucharest Etorie Dobreta Cralova Giurgiu

  45. General Search • For breadth-first search, we must store all the "partial routes" of length n, then systematically extend each one by adding a single additional step in as many ways as possible, and then repeat this indefinitely until we find a route from town1 to town2. • A mechanism for doing this is to maintain a queue of partial routes. The members of the queue will be called "nodes. They are lists of actions, and it is convenient to make the first member of each list the town the route ends at. • A search strategy is an algorithm for selecting which node on the queue to try extending next. • Generally, the most efficient way of implementing search strategies is to use them to order the queue, and then always select the first member of the queue for expansion. • So we will take a strategy to be a function which, when applied to a new node and an existing queue returns a new queue which results from inserting the node into the old queue in the appropriate place.

  46. General Search • Search for route from town1 to town2 • Initialize queue to (list (list town1)) • Initialize towns-tried to (list town1) • Enter loop • If the queue is empty, return from loop. • Let node be the first element on the queue. Remove it from the queue. Let town0 be the first member (the "destination") of node. • for each planning conditional ((town0 & go-to-town) -> town) where town is not a member of towns-tried, let new-route be the result of adding go-to-town to the route in node and: • If town = town2, return new-route. • Otherwise, add town to towns-tried, and add (cons town new-route) to the queue, ordering the queue according to the search strategy.

  47. General Search (defun SEARCH-FOR-ROUTE (town1 town2 strategy) (terpri) (princ town1) (terpri) (let ((queue (list (list town1))) (towns-tried (list town1))) (loop (when (null queue) (princ "No route found") (terpri) (return)) (let ((node (first queue))) (setf queue (cdr queue)) (indent (length (cdr node))) (princ "Trying: ") (princ (car node)) (terpri) (dolist (condit *planning-conditionals*) (let ((town (result condit))) (when (and (not (mem town towns-tried)) (equal (precond condit) (car node))) (cond ((equal town town2) (return-from search-for-route (reverse (cons (action condit) (cdr node))))) (t (push town towns-tried) (setf queue (funcall strategy (cons town (cons (action condit) (cdr node))) queue)))))))))))

  48. General Search ;; for depth-first search (defun depth-first (node queue) (cons node queue)) ;; for breadth-first search (defun breadth-first (node queue) (reverse (cons node (reverse queue))))

  49. Depth-first Breadth-first Urziceni Trying: Urziceni .Trying: Bucharest . .Trying: Fagaras . . .Trying: Sibiu . . . .Trying: Rimnicu Vilcea . . . . .Trying: Craiova . . . . . .Trying: Dobreta . . . . . . .Trying: Mehadia . . . . . . . .Trying: Lugoj ("go to Bucharest" "go to Fagaras" "go to Sibiu" "go to Rimnicu Vilcea" "go to Craiova" "go to Dobreta" "go to Mehadia" "go to Lugoj" "go to Timisoara") Urziceni Trying: Urziceni .Trying: Hirsova .Trying: Vaslui .Trying: Bucharest . .Trying: Eforie . .Trying: Iasi . .Trying: Giurgiu . .Trying: Pitesti . .Trying: Fagaras . . .Trying: Neamt . . .Trying: Craiova . . .Trying: Rimnicu Vilcea . . .Trying: Sibiu . . . .Trying: Dobreta . . . .Trying: Oradea . . . .Trying: Arad ("go to Bucharest" "go to Fagaras" "go to Sibiu" "go to Arad" "go to Timisoara")

  50. General Search Using Structures (defstruct (town-node (:print-function print-town-node) (:conc-name nil)) (town-name nil) (route-to nil) (evaluation 0) (parent-node nil)) (defun print-town-node (node stream depth) (declare (ignore depth)) (princ (town-name node) stream)) defstruct automatically introduces a new function make-town-node, which is used as follows: (make-town-node :town-name name :route-to route :evaluation evaluation :parent-node node)

More Related