I was trying to solve farmer dilemma.
"The point is to get the farmer, the fox, the cabbage and the goat across a stream. But the boat only holds 2 items. If left alone with the goat, the fox will eat it. If left alone with the cabbage, the goat will eat it"
and for that, I mainly must use only functions templets and rules to solve it assume the farmer, fox, goat, and cabbage all was on the side -1 and want to move to the side 1 firstly I make state templet
(deftemplate state
(slot farmer)
(slot fox)
(slot goat)
(slot cab))
the I define a function to do the movement
(deffunction move(?f ?x ?g ?c)
(if (and (neq ?f 1) (or (and (eq ?f ?g)
(and(neq ?f ?x) (neq ?f ?c)))
(or (and (neq ?f ?c) (and(eq ?f ?x) (eq ?f ?g)))
(and (eq ?f ?c) (and(eq ?f ?x) (eq ?f ?g))))))
then
(bind ?f (* -1 ?f))
(bind ?g (* -1 ?g))
(printout t "farmer become on side" ?f "fox on side " ?x
"goat on side " ?g "cabbage on side " ?c crlf)
else
(if (or (and (neq ?f ?c) (and(eq ?c ?x) (eq ?f ?g)))
(and (neq ?f ?g) (and(eq ?f ?x) (eq ?f ?c))))
then
(bind ?f (* -1 ?f))
(printout t "farmer become on side" ?f "fox on side " ?x
"goat on side " ?g "cabbage on side " ?c crlf)
else
(if (and (neq ?f ?g) (and(eq ?f ?x) (eq ?f ?c)))
then
(bind ?f (* -1 ?f))
(bind ?x (* -1 ?x))
(printout t "farmer become on side" ?f "fox on side " ?x
"goat on side " ?g "cabbage on side " ?c crlf)
else
(if (and (neq ?f ?x) (and(eq ?f ?c) (eq ?f ?g)))
then
(bind ?f (* -1 ?f))
(bind ?c (* -1 ?c))
(printout t "farmer become on side" ?f "fox on side " ?x
"goat on side " ?g "cabbage on side " ?c crlf)
))))))
I also define the rule
(defrule move-to-opp
(state (farmer ?f) (fox ?x) (goat ?g) (cab ?c))
(or (or (neq ?f 1) (neq ?g 1)) (or (neq ?x 1) (neq ?c 1)))
=>
(printout t "firstly farmer on side" ?f "fox on side " ?x
"goat on side " ?g "cabbage on side " ?c crlf)
(move ?f ?x ?g ?c))
Then I have defined facts inside the templet to be all on the first side -1
(deffacts initial-state
(state (farmer -1)
(fox -1)
(goat -1)
(cab -1)))
after running the rules no output appears, but it makes the first step for me when I remove the looping condition from the rule
(or (or (neq ?f 1) (neq ?g 1)) (or (neq ?x 1) (neq ?c 1)))