home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / Dilemma3 < prev    next >
Encoding:
Text File  |  1993-06-02  |  6.8 KB  |  230 lines

  1.  
  2. ;;;======================================================
  3. ;;;   Farmer's Dilemma Problem
  4. ;;;
  5. ;;;     Another classic AI problem (cannibals and the 
  6. ;;;     missionary) in agricultural terms. The point is
  7. ;;;     to get the farmer, the fox the cabbage and the
  8. ;;;     goat across a stream.
  9. ;;;        But the boat only holds 2 items. If left 
  10. ;;;     alone with the goat, the fox will eat it. If
  11. ;;;     left alone with the cabbage, the goat will eat
  12. ;;;     it.
  13. ;;;        This example uses rules and object pattern  
  14. ;;;     matching to solve the problem.
  15. ;;;
  16. ;;;     CLIPS Version 6.0 Example
  17. ;;;
  18. ;;;     To execute, merely load, reset and run.
  19. ;;;======================================================
  20.  
  21. ;;;***********
  22. ;;;* CLASSES *
  23. ;;;***********
  24.  
  25. ;;; The status instances hold the state  
  26. ;;; information of the search tree.
  27.  
  28. (defclass status (is-a USER)
  29.    (role concrete)
  30.    (pattern-match reactive)
  31.    (slot search-depth
  32.      (create-accessor write)
  33.      (type INTEGER) (range 1 ?VARIABLE) (default 1)) 
  34.    (slot parent
  35.      (create-accessor write)
  36.      (type INSTANCE-ADDRESS) (default ?DERIVE))
  37.    (slot farmer-location 
  38.      (create-accessor write)
  39.      (type SYMBOL) (allowed-symbols shore-1 shore-2) (default shore-1))
  40.    (slot fox-location
  41.      (create-accessor write)
  42.      (type SYMBOL) (allowed-symbols shore-1 shore-2) (default shore-1))
  43.    (slot goat-location
  44.      (create-accessor write)
  45.      (type SYMBOL) (allowed-symbols shore-1 shore-2) (default shore-1))
  46.    (slot cabbage-location
  47.      (create-accessor write)
  48.      (type SYMBOL) (allowed-symbols shore-1 shore-2) (default shore-1))
  49.    (slot last-move
  50.      (create-accessor write)
  51.      (type SYMBOL) (allowed-symbols no-move alone fox goat cabbage)
  52.      (default no-move)))
  53.    
  54. ;;; The moves instances hold the information of all the moves
  55. ;;; made to reach a given state.
  56.        
  57. (defclass moves (is-a USER)
  58.    (role concrete)
  59.    (pattern-match reactive)
  60.    (slot id
  61.       (create-accessor write)
  62.       (type INSTANCE)) 
  63.    (multislot moves-list 
  64.       (create-accessor write)
  65.       (type SYMBOL)
  66.       (allowed-symbols no-move alone fox goat cabbage)))
  67.  
  68. (defclass opposite-of
  69.    (is-a USER)
  70.    (role concrete)
  71.    (pattern-match reactive)
  72.    (slot value (create-accessor write))
  73.    (slot opposite-value (create-accessor write)))
  74.  
  75. ;;;*****************
  76. ;;;* INITIAL STATE *
  77. ;;;*****************
  78.  
  79. (definstances startups
  80.   (of status)
  81.   (of opposite-of (value shore-1) (opposite-value shore-2))
  82.   (of opposite-of (value shore-2) (opposite-value shore-1)))
  83.  
  84. ;;;***********************
  85. ;;;* GENERATE PATH RULES *
  86. ;;;***********************
  87.  
  88. (defrule move-alone 
  89.   ?node <- (object (is-a status)
  90.                    (search-depth ?num)  
  91.                    (farmer-location ?fs))
  92.   (object (is-a opposite-of) (value ?fs) (opposite-value ?ns))
  93.   =>
  94.   (duplicate-instance ?node
  95.     (search-depth (+ 1 ?num))
  96.     (parent ?node)
  97.     (farmer-location ?ns)
  98.     (last-move alone)))
  99.  
  100. (defrule move-with-fox
  101.   ?node <- (object (is-a status)
  102.                    (search-depth ?num) 
  103.                    (farmer-location ?fs)
  104.                    (fox-location ?fs))
  105.   (object (is-a opposite-of) (value ?fs) (opposite-value ?ns))
  106.   =>
  107.   (duplicate-instance ?node
  108.     (search-depth (+ 1 ?num))
  109.     (parent ?node)
  110.     (farmer-location ?ns)
  111.     (last-move fox)
  112.     (fox-location ?ns)))
  113.  
  114. (defrule move-with-goat 
  115.   ?node <- (object (is-a status)
  116.                    (search-depth ?num) 
  117.                    (farmer-location ?fs)
  118.                    (goat-location ?fs))
  119.   (object (is-a opposite-of) (value ?fs) (opposite-value ?ns))
  120.   =>
  121.   (duplicate-instance ?node
  122.     (search-depth (+ 1 ?num))
  123.     (parent ?node)
  124.     (farmer-location ?ns)
  125.     (last-move goat)
  126.     (goat-location ?ns)))
  127.  
  128. (defrule move-with-cabbage
  129.   ?node <- (object (is-a status)
  130.                    (search-depth ?num) 
  131.                    (farmer-location ?fs)
  132.                    (cabbage-location ?fs))
  133.   (object (is-a opposite-of) (value ?fs) (opposite-value ?ns))
  134.   =>
  135.   (duplicate-instance ?node
  136.     (search-depth (+ 1 ?num))
  137.     (parent ?node)
  138.     (farmer-location ?ns)
  139.     (last-move cabbage)
  140.     (cabbage-location ?ns)))
  141.  
  142. ;;;******************************
  143. ;;;* CONSTRAINT VIOLATION RULES *
  144. ;;;******************************
  145.  
  146. (defrule fox-eats-goat 
  147.   (declare (salience 200))
  148.   ?node <- (object (is-a status)
  149.                    (farmer-location ?s1)
  150.                    (fox-location ?s2&~?s1)
  151.                    (goat-location ?s2))
  152.   =>
  153.   (unmake-instance ?node))
  154.  
  155. (defrule goat-eats-cabbage 
  156.   (declare (salience 200))
  157.   ?node <- (object (is-a status)
  158.                    (farmer-location ?s1)
  159.                    (goat-location ?s2&~?s1)
  160.                    (cabbage-location ?s2))
  161.   =>
  162.   (unmake-instance ?node))
  163.  
  164. (defrule circular-path 
  165.   (declare (salience 200))
  166.   (object (is-a status)
  167.           (search-depth ?sd1)
  168.           (farmer-location ?fs)
  169.           (fox-location ?xs)
  170.           (goat-location ?gs)
  171.           (cabbage-location ?cs))
  172.   ?node <- (object (is-a status)
  173.                    (search-depth ?sd2&:(< ?sd1 ?sd2))
  174.                    (farmer-location ?fs)
  175.                    (fox-location ?xs)
  176.                    (goat-location ?gs)
  177.                    (cabbage-location ?cs))
  178.   =>
  179.   (unmake-instance ?node))
  180.  
  181. ;;;*********************************
  182. ;;;* FIND AND PRINT SOLUTION RULES *
  183. ;;;*********************************
  184.  
  185. (defrule recognize-solution 
  186.   (declare (salience 100))
  187.   ?node <- (object (is-a status)
  188.                    (parent ?parent)
  189.                    (farmer-location shore-2)
  190.                    (fox-location shore-2)
  191.                    (goat-location shore-2)
  192.                    (cabbage-location shore-2)
  193.                    (last-move ?move))
  194.   =>
  195.   (unmake-instance ?node)
  196.   (make-instance of moves
  197.      (id ?parent) (moves-list ?move)))
  198.  
  199. (defrule further-solution 
  200.   (declare (salience 100))
  201.   ?state <- (object (is-a status)
  202.                     (parent ?parent)
  203.                     (last-move ?move))
  204.   ?mv <- (object (is-a moves)
  205.                  (id ?state)
  206.                  (moves-list $?rest))
  207.   =>
  208.   (modify-instance ?mv (id ?parent) (moves-list ?move ?rest)))
  209.  
  210. (defrule print-solution 
  211.   (declare (salience 100))
  212.   ?mv <- (object (is-a moves)
  213.                  ;(id [no-parent]) 
  214.                  (moves-list no-move $?m))
  215.   =>
  216.   (unmake-instance ?mv)
  217.   (printout t t  "Solution found: " t t)
  218.   (bind ?length (length ?m))
  219.   (bind ?i 1)
  220.   (bind ?shore shore-2)
  221.   (while (<= ?i ?length)
  222.      (bind ?thing (nth$ ?i ?m))
  223.      (if (eq ?thing alone)
  224.         then (printout t "Farmer moves alone to " ?shore "." t)
  225.         else (printout t "Farmer moves with " ?thing " to " ?shore "." t))
  226.      (if (eq ?shore shore-1)
  227.         then (bind ?shore shore-2)
  228.         else (bind ?shore shore-1))
  229.      (bind ?i (+ 1 ?i))))
  230.