home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / PMCLIPS.ZIP / DILEMMA.CLP < prev    next >
Text File  |  1989-03-29  |  6KB  |  209 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. ;;;
  14. ;;;     To execute, merely load, reset and run.
  15. ;;;======================================================
  16.  
  17. ;;;****************************
  18. ;;;* Farmer's Dilemma Problem *
  19. ;;;****************************
  20.  
  21. ;;; The status facts hold the state information of the search tree.
  22.  
  23. (deftemplate status
  24.    (field search-depth)
  25.    (field id)
  26.    (field parent)
  27.    (field farmer-location)
  28.    (field fox-location)
  29.    (field goat-location)
  30.    (field cabbage-location)
  31.    (field previous-move))
  32.  
  33. ;;; The moves facts hold the information of all the moves made to
  34. ;;; reach a given state.
  35.  
  36. ;;;*****************
  37. ;;;* Initial State *
  38. ;;;*****************
  39.  
  40. (deffacts initial-positions
  41.   (status (search-depth 1)
  42.           (id initial-setup)
  43.           (parent no-parent)
  44.           (farmer-location shore-1) 
  45.           (fox-location shore-1)
  46.           (goat-location shore-1)
  47.           (cabbage-location shore-1)
  48.           (previous-move no-move)))
  49.  
  50. (deffacts opposites
  51.   (opposite-of shore-1 shore-2)
  52.   (opposite-of shore-2 shore-1))
  53.  
  54. ;;;************************
  55. ;;;* Generate Paths Rules *
  56. ;;;************************
  57.  
  58. (defrule move-alone ""
  59.   (status (search-depth ?num)
  60.           (id ?name)
  61.           (farmer-location ?fs)
  62.           (fox-location ?xs)
  63.           (goat-location ?gs)
  64.           (cabbage-location ?cs))
  65.   (opposite-of ?fs ?ns)
  66.   =>
  67.   (assert (status (search-depth =(+ 1 ?num))
  68.                   (id =(gensym))
  69.                   (parent ?name)
  70.                   (farmer-location ?ns)
  71.                   (fox-location ?xs)
  72.                   (goat-location ?gs)
  73.                   (cabbage-location ?cs)
  74.                   (previous-move alone))))
  75.  
  76. (defrule move-with-fox ""
  77.   (status (search-depth ?num)
  78.           (id ?name)
  79.           (farmer-location ?fs)
  80.           (fox-location ?fs)
  81.           (goat-location ?gs)
  82.           (cabbage-location ?cs))
  83.   (opposite-of ?fs ?ns)
  84.   =>
  85.   (assert (status (search-depth =(+ 1 ?num))
  86.                   (id =(gensym))
  87.                   (parent ?name)
  88.                   (farmer-location ?ns)
  89.                   (fox-location ?ns)
  90.                   (goat-location ?gs)
  91.                   (cabbage-location ?cs)
  92.                   (previous-move fox))))
  93.  
  94. (defrule move-with-goat ""
  95.   (status (search-depth ?num)
  96.           (id ?name)
  97.           (farmer-location ?fs)
  98.           (fox-location ?xs)
  99.           (goat-location ?fs)
  100.           (cabbage-location ?cs))
  101.   (opposite-of ?fs ?ns)
  102.   =>
  103.   (assert (status (search-depth =(+ 1 ?num))
  104.                   (id =(gensym))
  105.                   (parent ?name)
  106.                   (farmer-location ?ns)
  107.                   (fox-location ?xs)
  108.                   (goat-location ?ns)
  109.                   (cabbage-location ?cs)
  110.                   (previous-move goat))))
  111.  
  112. (defrule move-with-cabbage ""
  113.   (status (search-depth ?num)
  114.           (id ?name)
  115.           (farmer-location ?fs)
  116.           (fox-location ?xs)
  117.           (goat-location ?gs)
  118.           (cabbage-location ?fs))
  119.   (opposite-of ?fs ?ns)
  120.   =>
  121.   (assert (status (search-depth =(+ 1 ?num))
  122.                   (id =(gensym))
  123.                   (parent ?name)
  124.                   (farmer-location ?ns)
  125.                   (fox-location ?xs)
  126.                   (goat-location ?gs)
  127.                   (cabbage-location ?ns)
  128.                   (previous-move cabbage))))
  129.  
  130. ;;;******************************
  131. ;;;* Constraint Violation Rules *
  132. ;;;******************************
  133.  
  134. (defrule fox-eats-goat ""
  135.   (declare (salience 10000))
  136.   ?rm <- (status (farmer-location ?s1)
  137.                  (fox-location ?s2&~?s1)
  138.                  (goat-location ?s2))
  139.   =>
  140.   (retract ?rm))
  141.  
  142. (defrule goat-eats-cabbage ""
  143.   (declare (salience 10000))
  144.   ?rm <- (status (farmer-location ?s1)
  145.                  (goat-location ?s2&~?s1)
  146.                  (cabbage-location ?s2))
  147.   =>
  148.   (retract ?rm))
  149.  
  150. (defrule circular-path ""
  151.   (declare (salience 10000))
  152.   (status (search-depth ?nm)
  153.           (farmer-location ?fs)
  154.           (fox-location ?xs)
  155.           (goat-location ?gs)
  156.           (cabbage-location ?cs))
  157.   ?rm <- (status (search-depth ?nm1&:(< ?nm ?nm1))
  158.                  (farmer-location ?fs)
  159.                  (fox-location ?xs)
  160.                  (goat-location ?gs)
  161.                  (cabbage-location ?cs))
  162.   =>
  163.   (retract ?rm))
  164.  
  165. ;;;********************************
  166. ;;;* Find and Print Solution Rule *
  167. ;;;********************************
  168.  
  169. (defrule recognize-solution ""
  170.   (declare (salience 5000))
  171.   ?rm <- (status (parent ?parent)
  172.                  (farmer-location shore-2)
  173.                  (fox-location shore-2)
  174.                  (goat-location shore-2)
  175.                  (cabbage-location shore-2)
  176.                  (previous-move ?move))
  177.   =>
  178.   (retract ?rm)
  179.   (assert (moves ?parent ?move)))
  180.  
  181. (defrule further-solution ""
  182.   (declare (salience 5000))
  183.   ?mv <- (moves ?name $?rest)
  184.   (status (id ?name)
  185.           (parent ?parent)
  186.           (previous-move ?move))
  187.   =>
  188.   (retract ?mv)
  189.   (assert (moves ?parent ?move $?rest)))
  190.  
  191. (defrule print-solution ""
  192.   (declare (salience 5000))
  193.   ?mv <- (moves no-parent no-move $?m)
  194.   =>
  195.   (retract ?mv)
  196.   (printout t t  "Solution found: " t t)
  197.   (bind ?length (length $?m))
  198.   (bind ?i 1)
  199.   (bind ?shore shore-2)
  200.   (while (<= ?i ?length)
  201.      (bind ?thing (nth ?i $?m))
  202.      (if (eq ?thing alone)
  203.         then (printout t "Farmer moves alone to " ?shore "." t)
  204.         else (printout t "Farmer moves with " ?thing " to " ?shore "." t))
  205.      (if (eq ?shore shore-1)
  206.         then (bind ?shore shore-2)
  207.         else (bind ?shore shore-1))
  208.      (bind ?i (+ 1 ?i))))
  209.