home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / Dilemma1 < prev    next >
Encoding:
Text File  |  1993-06-02  |  6.1 KB  |  198 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 fact 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. (defmodule MAIN 
  22.   (export deftemplate status))
  23.  
  24. ;;;*************
  25. ;;;* TEMPLATES *
  26. ;;;*************
  27.  
  28. ;;; The status facts hold the state  
  29. ;;; information of the search tree.
  30.  
  31. (deftemplate MAIN::status 
  32.    (slot search-depth (type INTEGER) (range 1 ?VARIABLE))
  33.    (slot parent (type FACT-ADDRESS SYMBOL) (allowed-symbols no-parent))
  34.    (slot farmer-location 
  35.       (type SYMBOL) (allowed-symbols shore-1 shore-2))
  36.    (slot fox-location
  37.       (type SYMBOL) (allowed-symbols shore-1 shore-2))
  38.    (slot goat-location
  39.       (type SYMBOL) (allowed-symbols shore-1 shore-2))
  40.    (slot cabbage-location
  41.       (type SYMBOL) (allowed-symbols shore-1 shore-2))
  42.    (slot last-move
  43.       (type SYMBOL) (allowed-symbols no-move alone fox goat cabbage)))
  44.    
  45. ;;;*****************
  46. ;;;* INITIAL STATE *
  47. ;;;*****************
  48.  
  49. (deffacts MAIN::initial-positions
  50.   (status (search-depth 1) 
  51.           (parent no-parent)
  52.           (farmer-location shore-1)
  53.           (fox-location shore-1)
  54.           (goat-location shore-1)
  55.           (cabbage-location shore-1)
  56.           (last-move no-move)))
  57.  
  58. (deffacts MAIN::opposites
  59.   (opposite-of shore-1 shore-2)
  60.   (opposite-of shore-2 shore-1))
  61.  
  62. ;;;***********************
  63. ;;;* GENERATE PATH RULES *
  64. ;;;***********************
  65.  
  66. (defrule MAIN::move-alone 
  67.   ?node <- (status (search-depth ?num) 
  68.                    (farmer-location ?fs))
  69.   (opposite-of ?fs ?ns)
  70.   =>
  71.   (duplicate ?node (search-depth =(+ 1 ?num))
  72.                    (parent ?node)
  73.                    (farmer-location ?ns)
  74.                    (last-move alone)))
  75.  
  76. (defrule MAIN::move-with-fox
  77.   ?node <- (status (search-depth ?num) 
  78.                    (farmer-location ?fs)
  79.                    (fox-location ?fs))
  80.   (opposite-of ?fs ?ns)
  81.   =>
  82.   (duplicate ?node (search-depth =(+ 1 ?num)) 
  83.                    (parent ?node)
  84.                    (farmer-location ?ns)
  85.                    (fox-location ?ns)
  86.                    (last-move fox)))
  87.  
  88. (defrule MAIN::move-with-goat 
  89.   ?node <- (status (search-depth ?num) 
  90.                    (farmer-location ?fs)
  91.                    (goat-location ?fs))
  92.   (opposite-of ?fs ?ns)
  93.   =>
  94.   (duplicate ?node (search-depth =(+ 1 ?num)) 
  95.                    (parent ?node)
  96.                    (farmer-location ?ns)
  97.                    (goat-location ?ns)
  98.                    (last-move goat)))
  99.  
  100. (defrule MAIN::move-with-cabbage
  101.   ?node <- (status (search-depth ?num)
  102.                    (farmer-location ?fs)
  103.                    (cabbage-location ?fs))
  104.   (opposite-of ?fs ?ns)
  105.   =>
  106.   (duplicate ?node (search-depth =(+ 1 ?num)) 
  107.                    (parent ?node)
  108.                    (farmer-location ?ns)
  109.                    (cabbage-location ?ns)
  110.                    (last-move cabbage)))
  111.  
  112. ;;;******************************
  113. ;;;* CONSTRAINT VIOLATION RULES *
  114. ;;;******************************
  115.  
  116. (defmodule CONSTRAINTS 
  117.   (import MAIN deftemplate status))
  118.  
  119. (defrule CONSTRAINTS::fox-eats-goat 
  120.   (declare (auto-focus TRUE))
  121.   ?node <- (status (farmer-location ?s1)
  122.                    (fox-location ?s2&~?s1)
  123.                    (goat-location ?s2))
  124.   =>
  125.   (retract ?node))
  126.  
  127. (defrule CONSTRAINTS::goat-eats-cabbage 
  128.   (declare (auto-focus TRUE))
  129.   ?node <- (status (farmer-location ?s1)
  130.                    (goat-location ?s2&~?s1)
  131.                    (cabbage-location ?s2))
  132.   =>
  133.   (retract ?node))
  134.  
  135. (defrule CONSTRAINTS::circular-path 
  136.   (declare (auto-focus TRUE))
  137.   (status (search-depth ?sd1)
  138.           (farmer-location ?fs)
  139.           (fox-location ?xs)
  140.           (goat-location ?gs)
  141.           (cabbage-location ?cs))
  142.   ?node <- (status (search-depth ?sd2&:(< ?sd1 ?sd2))
  143.                    (farmer-location ?fs)
  144.                    (fox-location ?xs)
  145.                    (goat-location ?gs)
  146.                    (cabbage-location ?cs))
  147.   =>
  148.   (retract ?node))
  149.  
  150. ;;;*********************************
  151. ;;;* FIND AND PRINT SOLUTION RULES *
  152. ;;;*********************************
  153.  
  154. (defmodule SOLUTION 
  155.   (import MAIN deftemplate status))
  156.        
  157. (deftemplate SOLUTION::moves 
  158.    (slot id (type FACT-ADDRESS SYMBOL) (allowed-symbols no-parent)) 
  159.    (multislot moves-list  
  160.       (type SYMBOL) (allowed-symbols no-move alone fox goat cabbage)))
  161.  
  162. (defrule SOLUTION::recognize-solution 
  163.   (declare (auto-focus TRUE))
  164.   ?node <- (status (parent ?parent)
  165.                    (farmer-location shore-2)
  166.                    (fox-location shore-2)
  167.                    (goat-location shore-2)
  168.                    (cabbage-location shore-2)
  169.                    (last-move ?move))
  170.   =>
  171.   (retract ?node)
  172.   (assert (moves (id ?parent) (moves-list ?move))))
  173.  
  174. (defrule SOLUTION::further-solution 
  175.   ?node <- (status (parent ?parent)
  176.                    (last-move ?move))
  177.   ?mv <- (moves (id ?node) (moves-list $?rest))
  178.   =>
  179.   (modify ?mv (id ?parent) (moves-list ?move ?rest)))
  180.  
  181. (defrule SOLUTION::print-solution 
  182.   ?mv <- (moves (id no-parent) (moves-list no-move $?m))
  183.   =>
  184.   (retract ?mv)
  185.   (printout t t  "Solution found: " t t)
  186.   (bind ?length (length ?m))
  187.   (bind ?i 1)
  188.   (bind ?shore shore-2)
  189.   (while (<= ?i ?length)
  190.      (bind ?thing (nth ?i ?m))
  191.      (if (eq ?thing alone)
  192.         then (printout t "Farmer moves alone to " ?shore "." t)
  193.         else (printout t "Farmer moves with " ?thing " to " ?shore "." t))
  194.      (if (eq ?shore shore-1)
  195.         then (bind ?shore shore-2)
  196.         else (bind ?shore shore-1))
  197.      (bind ?i (+ 1 ?i))))
  198.