|
| 1 | + |
| 2 | +;;;====================================================== |
| 3 | +;;; Cannibals and Missionaries Problem |
| 4 | +;;; |
| 5 | +;;; Another classic AI problem. The point is |
| 6 | +;;; to get three cannibals and three missionaries |
| 7 | +;;; across a stream with a boat that can only |
| 8 | +;;; hold two people. If the cannibals outnumber |
| 9 | +;;; the missionaries on either side of the stream, |
| 10 | +;; then the cannibals will eat the missionaries. |
| 11 | +;;; |
| 12 | +;;; CLIPS Version 6.01 Example |
| 13 | +;;; |
| 14 | +;;; To execute, merely load, reset and run. |
| 15 | +;;;====================================================== |
| 16 | + |
| 17 | +;;;*********************** |
| 18 | +;;;* ARBITRARY VARIABLES * |
| 19 | +;;;*********************** |
| 20 | + |
| 21 | +(defglobal ?*n-missionaries* = 3) |
| 22 | +(defglobal ?*n-cannibals* = 3) |
| 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) |
| 33 | + (slot parent) |
| 34 | + (slot shore-1-missionaries) |
| 35 | + (slot shore-1-cannibals) |
| 36 | + (slot shore-2-missionaries) |
| 37 | + (slot shore-2-cannibals) |
| 38 | + (slot boat-location) |
| 39 | + (slot last-move)) |
| 40 | + |
| 41 | +;;;***************** |
| 42 | +;;;* INITIAL STATE * |
| 43 | +;;;***************** |
| 44 | + |
| 45 | +(deffacts MAIN::initial-positions |
| 46 | + (status (search-depth 1) |
| 47 | + (parent no-parent) |
| 48 | + (shore-1-missionaries ?*n-missionaries*) |
| 49 | + (shore-2-missionaries 0) |
| 50 | + (shore-1-cannibals ?*n-cannibals*) |
| 51 | + (shore-2-cannibals 0) |
| 52 | + (boat-location shore-1) |
| 53 | + (last-move "No move."))) |
| 54 | + |
| 55 | +(deffacts MAIN::boat-information |
| 56 | + (boat-can-hold 2)) |
| 57 | + |
| 58 | +;;;**************************************** |
| 59 | +;;;* FUNCTION FOR MOVE DESCRIPTION STRING * |
| 60 | +;;;**************************************** |
| 61 | + |
| 62 | +(deffunction MAIN::move-string (?missionaries ?cannibals ?shore) |
| 63 | + (if (eq ?missionaries 0) then |
| 64 | + (if (eq ?cannibals 1) |
| 65 | + then (format nil "Move 1 cannibal to %s.%n" ?shore) |
| 66 | + else (format nil "Move %d cannibals to %s.%n" ?cannibals ?shore)) |
| 67 | + else |
| 68 | + (if (eq ?missionaries 1) then |
| 69 | + (if (eq ?cannibals 0) then |
| 70 | + (format nil "Move 1 missionary to %s.%n" ?shore) else |
| 71 | + (if (eq ?cannibals 1) then |
| 72 | + (format nil "Move 1 missionary and 1 cannibal to %s.%n" ?shore) |
| 73 | + else |
| 74 | + (format nil "Move 1 missionary and %d cannibals to %s.%n" |
| 75 | + ?cannibals ?shore))) |
| 76 | + else |
| 77 | + (if (eq ?cannibals 0) then |
| 78 | + (format nil "Move %d missionaries to %s.%n" ?missionaries ?shore) |
| 79 | + else |
| 80 | + (if (eq ?cannibals 1) then |
| 81 | + (format nil "Move %d missionaries and 1 cannibal to %s.%n" |
| 82 | + ?missionaries ?shore) |
| 83 | + else |
| 84 | + (format nil "Move %d missionary and %d cannibals to %s.%n" |
| 85 | + ?missionaries ?cannibals ?shore)))))) |
| 86 | + |
| 87 | +;;;*********************** |
| 88 | +;;;* GENERATE PATH RULES * |
| 89 | +;;;*********************** |
| 90 | + |
| 91 | +(defrule MAIN::shore-1-move |
| 92 | + ?node <- (status (search-depth ?num) |
| 93 | + (boat-location shore-1) |
| 94 | + (shore-1-missionaries ?s1m) |
| 95 | + (shore-1-cannibals ?s1c) |
| 96 | + (shore-2-missionaries ?s2m) |
| 97 | + (shore-2-cannibals ?s2c)) |
| 98 | + (boat-can-hold ?limit) |
| 99 | + => |
| 100 | + (bind ?max-missionaries (min ?s1m ?limit)) |
| 101 | + (bind ?missionaries 0) |
| 102 | + (while (<= ?missionaries ?max-missionaries) |
| 103 | + (bind ?min-cannibals (max 0 (- 1 ?missionaries))) |
| 104 | + (bind ?max-cannibals (min ?s1c (- ?limit ?missionaries))) |
| 105 | + (bind ?cannibals ?min-cannibals) |
| 106 | + (while (<= ?cannibals ?max-cannibals) |
| 107 | + (duplicate ?node (search-depth (+ 1 ?num)) |
| 108 | + (parent ?node) |
| 109 | + (shore-1-missionaries (- ?s1m ?missionaries)) |
| 110 | + (shore-1-cannibals (- ?s1c ?cannibals)) |
| 111 | + (shore-2-missionaries (+ ?s2m ?missionaries)) |
| 112 | + (shore-2-cannibals (+ ?s2c ?cannibals)) |
| 113 | + (boat-location shore-2) |
| 114 | + (last-move (MAIN::move-string ?missionaries ?cannibals shore-2))) |
| 115 | + (++ ?cannibals)) |
| 116 | + (++ ?missionaries))) |
| 117 | + |
| 118 | +(defrule MAIN::shore-2-move |
| 119 | + ?node <- (status (search-depth ?num) |
| 120 | + (boat-location shore-2) |
| 121 | + (shore-1-missionaries ?s1m) |
| 122 | + (shore-1-cannibals ?s1c) |
| 123 | + (shore-2-missionaries ?s2m) |
| 124 | + (shore-2-cannibals ?s2c)) |
| 125 | + (boat-can-hold ?limit) |
| 126 | + => |
| 127 | + (bind ?max-missionaries (min ?s2m ?limit)) |
| 128 | + (bind ?missionaries 0) |
| 129 | + (while (<= ?missionaries ?max-missionaries) |
| 130 | + (bind ?min-cannibals (max 0 (- 1 ?missionaries))) |
| 131 | + (bind ?max-cannibals (min ?s2c (- ?limit ?missionaries))) |
| 132 | + (bind ?cannibals ?min-cannibals) |
| 133 | + (while (<= ?cannibals ?max-cannibals) |
| 134 | + (duplicate ?node (search-depth (+ 1 ?num)) |
| 135 | + (parent ?node) |
| 136 | + (shore-1-missionaries (+ ?s1m ?missionaries)) |
| 137 | + (shore-1-cannibals (+ ?s1c ?cannibals)) |
| 138 | + (shore-2-missionaries (- ?s2m ?missionaries)) |
| 139 | + (shore-2-cannibals (- ?s2c ?cannibals)) |
| 140 | + (boat-location shore-1) |
| 141 | + (last-move (MAIN::move-string ?missionaries ?cannibals shore-1))) |
| 142 | + (++ ?cannibals)) |
| 143 | + (++ ?missionaries))) |
| 144 | + |
| 145 | +;;;****************************** |
| 146 | +;;;* CONSTRAINT VIOLATION RULES * |
| 147 | +;;;****************************** |
| 148 | + |
| 149 | +(defmodule CONSTRAINTS) |
| 150 | + |
| 151 | +(defrule CONSTRAINTS::cannibals-eat-missionaries |
| 152 | + (declare (auto-focus TRUE)) |
| 153 | + ?node <- (status (shore-1-missionaries ?s1m) |
| 154 | + (shore-1-cannibals ?s1c) |
| 155 | + (shore-2-missionaries ?s2m) |
| 156 | + (shore-2-cannibals ?s2c)) |
| 157 | + (test (or (and (> ?s2c ?s2m) (<> ?s2m 0)) |
| 158 | + (and (> ?s1c ?s1m) (<> ?s1m 0)))) |
| 159 | + => |
| 160 | + (retract ?node)) |
| 161 | + |
| 162 | +(defrule CONSTRAINTS::circular-path |
| 163 | + (declare (auto-focus TRUE)) |
| 164 | + (status (search-depth ?sd1) |
| 165 | + (boat-location ?bl) |
| 166 | + (shore-1-missionaries ?s1m) |
| 167 | + (shore-1-cannibals ?s1c) |
| 168 | + (shore-2-missionaries ?s2m) |
| 169 | + (shore-2-cannibals ?s2c)) |
| 170 | + ?node <- (status (search-depth ?sd2&:(< ?sd1 ?sd2)) |
| 171 | + (boat-location ?bl) |
| 172 | + (shore-1-missionaries ?s1m) |
| 173 | + (shore-1-cannibals ?s1c) |
| 174 | + (shore-2-missionaries ?s2m) |
| 175 | + (shore-2-cannibals ?s2c)) |
| 176 | + => |
| 177 | + (retract ?node)) |
| 178 | + |
| 179 | +;;;********************************* |
| 180 | +;;;* FIND AND PRINT SOLUTION RULES * |
| 181 | +;;;********************************* |
| 182 | + |
| 183 | +(defmodule SOLUTION) |
| 184 | + |
| 185 | +(deftemplate SOLUTION::moves |
| 186 | + (slot id) |
| 187 | + (multislot moves-list)) |
| 188 | + |
| 189 | +(defrule SOLUTION::recognize-solution |
| 190 | + (declare (auto-focus TRUE)) |
| 191 | + ?node <- (status (parent ?parent) |
| 192 | + (shore-2-missionaries ?m&:(= ?m ?*n-missionaries*)) |
| 193 | + (shore-2-cannibals ?c&:(= ?c ?*n-cannibals*)) |
| 194 | + (last-move ?move)) |
| 195 | + => |
| 196 | + (retract ?node) |
| 197 | + (assert (moves (id ?parent) (moves-list ?move)))) |
| 198 | + |
| 199 | +(defrule SOLUTION::further-solution |
| 200 | + ?node <- (status (parent ?parent) |
| 201 | + (last-move ?move)) |
| 202 | + ?mv <- (moves (id ?node) (moves-list $?rest)) |
| 203 | + => |
| 204 | + (modify ?mv (id ?parent) (moves-list ?move ?rest))) |
| 205 | + |
| 206 | +(defrule SOLUTION::print-solution |
| 207 | + ?mv <- (moves (id no-parent) (moves-list "No move." $?m)) |
| 208 | + => |
| 209 | + (retract ?mv) |
| 210 | + (printout t crlf "Solution found: " crlf crlf) |
| 211 | + (bind ?length (length$ ?m)) |
| 212 | + (bind ?i 1) |
| 213 | + (while (<= ?i ?length) |
| 214 | + (bind ?move (nth$ ?i ?m)) |
| 215 | + (printout t ?move) |
| 216 | + (bind ?i (+ 1 ?i)))) |
| 217 | + |
| 218 | +(reset) |
| 219 | +(run) |
0 commit comments