Advertisement
SageTheWizard

cmpsc441-hw2-2

Oct 8th, 2018
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 5.91 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;                         JACOB GALLUCCI                ;;;
  3. ;;;                         jag6248@psu.edu                               ;;;
  4. ;;;                         Dr. Chang                     ;;;
  5. ;;;                         CMPSC441 - Assignment 2 - search.ss           ;;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. ;;; CMPSC441               Artificial Intelligence              Fall 2018 ;;;
  8. ;;;                                                                       ;;;
  9. ;;;                                                                       ;;;
  10. ;;; SIMPLE SEARCH PROCEDURE                                               ;;;
  11. ;;;                                                                       ;;;
  12. ;;;   - Startup Code for homework 2                                       ;;;
  13. ;;;                                                                       ;;;
  14. ;;;   - Note that you don't have use this code                            ;;;
  15. ;;;     if you really want to dig in and write your own.                  ;;;
  16. ;;;                                                                       ;;;
  17. ;;;                                                                       ;;;
  18. ;;; Report bugs to sukmoon@psu.edu                                        ;;;
  19. ;;;                                                                       ;;;
  20. ;;; THANK YOU!                                                            ;;;
  21. ;;;                                                                       ;;;
  22. ;;;                                                                       ;;;
  23. ;;; Sukmoon Chang                                      September 25, 2018 ;;;
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25.  
  26. ;;;
  27. ;;; SEARCH:
  28. ;;;   -- Non-curried version of generic search algorithm
  29. ;;;   -- Can be customized for depth-first and breadth-first search
  30. ;;;   -- You must convert it to a curried version so that
  31. ;;;      - the function accepts 1 algorithm specific parameter and returns a function
  32. ;;;      - that accepts 3 problem-specific parameters and returns a function
  33. ;;;      - that accepths 1 instance specific parameter and performs the search
  34. ;;;   -- The 5 parameters are described below
  35. ;;;
  36. ;;; Input:
  37. ;;;   merge-queue
  38. ;;;     -- algorithm specific
  39. ;;;     -- procedure that takes a list of new paths and a queue
  40. ;;;        and returns a new queue
  41. ;;;   extend
  42. ;;;     -- problem-specific
  43. ;;;     -- procedure that takes a state and a list of visited states,
  44. ;;;        and returns a list of states that are reachable in one move
  45. ;;;        from the given state
  46. ;;;   goal?
  47. ;;;     -- problem-specific
  48. ;;;     -- predicate that takes a state and returns true if the
  49. ;;;        state is a goal state, false otherwise
  50. ;;;   print-path
  51. ;;;     -- problem-specific
  52. ;;;     -- procedure that takes a state and prints out a state nicely
  53. ;;;   init-state
  54. ;;;     -- problem instance-specific
  55. ;;;     -- an initial state to start the search from
  56. ;;;
  57. ;;; OUTPUT:
  58. ;;;   -- When succeeded, a path from the initial state to a goal state
  59. ;;;   -- When failed, #f
  60. ;;;
  61.  
  62.  
  63. (module hw2 racket
  64.  
  65.   ;; only the following 2 procedures will be provided
  66.   (provide depth-first-search breadth-first-search)
  67.  
  68.   ;; generic search algorithm
  69.    
  70.    
  71.   (define search
  72.     (lambda (merge-queue)
  73.       (lambda (extend goal? print-path)
  74.         (lambda (init-state)
  75.         (letrec
  76.             ((search-helper
  77.               (lambda (queue visited)
  78.                 (cond
  79.                   ((null? queue) #f)
  80.                   ((goal? (caar queue))
  81.                    (begin
  82.                      (print-path (car queue))
  83.                      (car queue)))
  84.                   (else
  85.                    (let ((successors (extend (caar queue) visited)))
  86.                      (cond
  87.                        ((null? successors)
  88.                         (search-helper (cdr queue) visited))
  89.                        (else
  90.                         (let ((new-paths (extend-path successors (car queue))))
  91.                           (search-helper
  92.                            (merge-queue queue new-paths)
  93.                            (append successors visited)))))))))))
  94.           (search-helper
  95.            (list (list init-state))   ; initial queue
  96.            (list init-state)))))))      ; initial visited
  97.  
  98.  
  99.   (define extend-path
  100.     (lambda (successors path)
  101.       (if (null? successors)
  102.           '()
  103.           (cons (cons (car successors) path)
  104.             (extend-path (cdr successors) path)))))
  105.  
  106.  
  107.  
  108.   ;; merge new extended paths to queue for depth first search
  109.   ;; - uncomment and define your merge for depth first search
  110.  
  111.   (define depth-first-merge
  112.     (lambda (queue new-paths)
  113.       (cond
  114.         ((null? new-paths) (append '() queue))
  115.         (else (append new-paths queue)))))
  116.  
  117.  
  118.  
  119.   ;; merge new extended paths to queue for breadth first search
  120.   ;; - uncomment and define your merge for breadth first search
  121.  
  122.   (define breadth-first-merge
  123.     (lambda (queue new-paths)
  124.       (cond
  125.         ((null? new-paths) (append queue '()))
  126.         (else (append queue new-paths)))))
  127.  
  128.   ;; customize the generic search for depth first search
  129.   ;; - uncomment and define your depth-first-search in terms of your
  130.   ;;   curried version of search and depth-first-merge
  131.  
  132.   (define depth-first-search
  133.     (lambda (extend goal? print-path)
  134.       (lambda (init-state)
  135.         (((search depth-first-merge) extend goal? print-path) init-state))))
  136.  
  137.   ;; customize the generic search for breadth first search
  138.   ;; - uncomment and define your breadth-first-search in terms of your
  139.   ;;   curried version of search and breadth-first-merge
  140.  
  141.   (define breadth-first-search
  142.     (lambda (extend goal? print-path)
  143.       (lambda (init-state)
  144.         (((search breadth-first-merge) extend goal? print-path) init-state))))
  145.  
  146.  
  147.   ;;;; DO NOT REMOVE THE FOLLOWING LINE
  148.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement