Advertisement
SageTheWizard

hw2-cmpsc441-1

Oct 8th, 2018
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 7.92 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;   JACOB GALLUCCI                                                        ;;
  3. ;;   [email protected]                                                       ;;
  4. ;;   CMPSC441 - Assignment 2                                               ;;
  5. ;;   farmers.ss                                                            ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;; CMPSC441               Artificial Intelligence              Fall 2018 ;;;
  9. ;;;                                                                       ;;;
  10. ;;;                                                                       ;;;
  11. ;;; PROBLEM-SPECIFIC CODE FOR FARMER'S PROBLEM                            ;;;
  12. ;;;                                                                       ;;;
  13. ;;;   - Write your code for farmer's problem here                         ;;;
  14. ;;;                                                                       ;;;
  15. ;;; Report bugs to [email protected]                                        ;;;
  16. ;;;                                                                       ;;;
  17. ;;; THANK YOU!                                                            ;;;
  18. ;;;                                                                       ;;;
  19. ;;;                                                                       ;;;
  20. ;;; Sukmoon Chang                                      September 25, 2018 ;;;
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22.  
  23.  
  24. ;;; DO NOT MODIFY NEXT 2 LINES
  25. #lang mzscheme
  26. (require "search.ss")
  27.  
  28.  
  29. ;;; Problem specific code for solving the farmer's problem
  30. ;;; using the curried version of the simple search procedure.
  31. ;;; The farmer's problem was discussed in the lecture.
  32. ;;;
  33. ;;; To solve the problem, load this file and run
  34. ;;;    (farmer-depth-first)
  35. ;;; or
  36. ;;;    (farmer-breadth-first)
  37. ;;;
  38. ;;; Here, a state is represented as follows:
  39. ;;;       (farmer (grain chicken fox))
  40. ;;; where #t represents the left bay
  41. ;;;       #f represents the right bay
  42. ;;; For example
  43. ;;;         (#f (#t #f #t))
  44. ;;; says
  45. ;;;         grain and fox are on the left bay
  46. ;;;         farmer and chicken are on the right bay
  47. ;;;
  48. ;;; So, the initial state is
  49. ;;;         (#t (#t #t #t))
  50. ;;; and the final state is
  51. ;;;         (#f (#f #f #f))
  52.  
  53.  
  54.  
  55. ;;; For consistency, 2 procedures for outputting
  56. ;;; the current path are provided below
  57.  
  58.  
  59. ;; pretty-print a path
  60. (define pretty-print-path
  61.   (lambda (path)
  62.     (letrec
  63.         ((helper
  64.            (lambda (path)
  65.              (cond
  66.                ((null? path) (printf "~%"))
  67.                (else
  68.                  (begin
  69.                    (pretty-print-state (car path))
  70.                    (printf "~%")
  71.                    (helper (cdr path))))))))
  72.       (helper (reverse path)))))
  73.  
  74.  
  75. (define pretty-print-state
  76.   (lambda (state)
  77.     (let ((farmer (car state))
  78.           (grain   (car (cadr state)))
  79.           (chicken (cadr (cadr state)))
  80.           (fox     (caddr (cadr state))))
  81.       (if farmer
  82.           (printf " Farmer |=>    |~%")
  83.           (printf "        |    <=| Farmer~%"))
  84.       (if grain
  85.           (printf " Grain  |      |~%")
  86.           (printf "        |      | Grain~%"))
  87.       (if chicken
  88.           (printf "Chicken |      |~%")
  89.           (printf "        |      | Chicken~%"))
  90.       (if fox
  91.           (printf "    Fox |      |~%")
  92.           (printf "        |      | Fox~%")))))
  93.  
  94.  
  95. ;;; Rest of your code goes here
  96.  
  97. ;; initial state
  98. (define init-state '(#t (#t #t #t)))
  99.  
  100. ;; determine if a state is invalid
  101. ; returns #f if an element is on the same side as another element that will eat it w/o farmer
  102. ;            or if the state has already been visited, Otherwise returns #t
  103. (define good-config?
  104.   (lambda (config visited)
  105.     (cond
  106.       ((visited? visited config) #f)
  107.       ((and (eq? (car (cadr config)) (cadr (cadr config)))
  108.             (not (eq? (car (cadr config)) (car config)))) #f)
  109.       ((and (eq? (cadr (cadr config)) (caddr (cadr config)))
  110.             (not (eq? (cadr (cadr config)) (car config)))) #f)
  111.       (else #t))))
  112.  
  113. ;; Flip functions [used in cross functs] - used to easily flip the value for extend
  114. ; changes value of G, C or F to #t -> #f or visa versa
  115. (define flip-grain
  116.   (lambda (grain-chicken-fox)
  117.     (cons (not (car grain-chicken-fox)) (cdr grain-chicken-fox))))
  118. (define flip-chicken
  119.   (lambda (grain-chicken-fox)
  120.     (cons (car grain-chicken-fox)
  121.           (cons (not (cadr grain-chicken-fox))
  122.                 (cddr grain-chicken-fox)))))
  123. (define flip-fox
  124.   (lambda (grain-chicken-fox)
  125.     (reverse (flip-grain (reverse grain-chicken-fox)))))
  126.  
  127. ;; Same Side [used in cross functs] - Checks if the farmer is on the same side of river as G,C or F
  128. ; returns #t or #f depending on if the farmer is on the correct side
  129. (define same-side-grain?
  130.   (lambda (state)
  131.     (cond
  132.       ((eq? (car (cadr state)) (car state)) #t)
  133.       (else #f))))
  134. (define same-side-chicken?
  135.   (lambda (state)
  136.     (cond
  137.       ((eq? (cadr (cadr state)) (car state)) #t)
  138.       (else #f))))
  139. (define same-side-fox?
  140.   (lambda (state)
  141.     (cond
  142.       ((eq? (caddr (cadr state )) (car state)) #t)
  143.       (else #f))))
  144.  
  145. ;; Cross [used in extend] - Makes state of farmer moving across river (with or without element)
  146. ; Crosses the farmer and the related element (switches #t -> #f or visa versa) and returns state
  147. ; If the farmer is not on the same side as the related element, returns empty list as state
  148. (define cross-grain
  149.   (lambda (state)
  150.     (cond
  151.       ((same-side-grain? state)
  152.        (cons (not (car state)) (list (flip-grain (cadr state)))))
  153.       (else '()))))
  154. (define cross-chicken
  155.   (lambda (state)
  156.     (cond
  157.       ((same-side-chicken? state)
  158.        (cons (not (car state)) (list (flip-chicken (cadr state)))))
  159.       (else '()))))
  160. (define cross-fox
  161.   (lambda (state)
  162.     (cond
  163.       ((same-side-fox? state)
  164.        (cons (not (car state)) (list (flip-fox (cadr state)))))
  165.       (else '()))))
  166. (define cross-farmer
  167.   (lambda (state)
  168.     (cons (not (car state)) (cdr state))))
  169.  
  170. ;; Extend - gets children of a state
  171. ; Makes a list of all possible movements then filters out Invalid movements
  172. ; (Empty lists made by cross functs OR movements where something gets eaten)
  173. ; Then it returns the filtered list (valid children of the current state)
  174. (define extend
  175.   (lambda (state visited)
  176.     (letrec ([filter-pre
  177.              (lambda (pre-extend visited)
  178.                (cond
  179.                  ((null? pre-extend) '())
  180.                  ((null? (car pre-extend)) (filter-pre (cdr pre-extend) visited))
  181.                  ((good-config? (car pre-extend) visited)
  182.                   (cons (car pre-extend) (filter-pre (cdr pre-extend) visited)))
  183.                  (else (filter-pre (cdr pre-extend) visited))))])
  184.       (define pre-extend
  185.         (cons (cross-grain state)
  186.               (cons (cross-chicken state)
  187.                     (cons (cross-fox state)
  188.                           (list (cross-farmer state))))))
  189.       (filter-pre pre-extend visited))))
  190.  
  191. ;; Goal - Checks if the current state is a goal, returns #t if goal
  192. (define goal?
  193.   (lambda (state)
  194.     (cond
  195.       ((null? state) #t)
  196.       ((list? (car state)) (goal? (car state)))
  197.       ((not (car state)) (goal? (cdr state)))
  198.       (else #f))))
  199.  
  200. ;; Visited [used in good-config]- determine if a state is visited
  201. (define visited?
  202.   (lambda (visited current-move)
  203.     (cond
  204.       ((null? visited) #f)
  205.       ((equal? (car visited) current-move) #t)
  206.       (else (visited? (cdr visited) current-move)))))
  207.  
  208. ;; Searches - Depth first and Breadth first - run searches using these
  209. (define farmer-depth-first
  210.   (lambda ()
  211.     ((depth-first-search extend goal? pretty-print-path) init-state)))
  212. (define farmer-breadth-first
  213.   (lambda ()
  214.     ((breadth-first-search extend goal? pretty-print-path) init-state)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement