我正在尝试创建一个CLIPS程序,该程序将使用任何初始堆栈并将其重新排列到任何目标堆栈中。我是在断言,但似乎什么也没做。
(断言(A B C堆)(D E F堆)(目标D C B堆)(目标A堆)(目标F堆))
到目前为止,这是我的代码:
(defrule move-direct
;(declare (salience 10000))
?stack1 <- (stack ?block1 $?bottom1)
?stack2 <- (stack ?block2 $?bottom2)
(goal-stack ?block1 ?block2 $?goalbottom)
=>
(retract ?stack1 ?stack2)
(assert (stack ?block1 ?block2 ?bottom2))
(assert (stack $?bottom1))
(printout t ?block1 " moved on top of " ?block2 crlf))
(defrule move-on-floor
; (declare (salience 10000))
?stack1 <- (stack ?top $?blocks ?movethisblock $?bottom1)
;(goal-stack ?movethisblock $?bottom1)
(goal-stack $?blocks ?movethisblock $?bottom2)
=>
(retract ?stack1)
(assert (stack ?top))
(assert (stack $?blocks ?movethisblock $?bottom1))
(printout t ?top " moved on to the floor" crlf))
答案 0 :(得分:2)
调试代码通常涉及质疑您的期望。您希望直接移动和地板移动规则可以执行某些操作,但是为什么呢?对于直接移动规则和所做的断言,前两个模式中?block1和?block2的唯一可能值是A或D。因此,第三个模式必须匹配以AA开头的目标堆栈,AD,DA或DD。这样的目标堆栈不存在,因此该规则不匹配。
有关地板移动规则,请查看每种情况。如果(目标堆栈A)与目标堆栈模式匹配,其中?move-this-block是A而$?blocks是(),则必须有一个堆栈在A顶部有一个块(变量?top)。由于A在堆栈的顶部,因此该目标堆栈将不匹配规则。
如果(目标堆栈FE)与目标堆栈模式匹配,则序列($?blocks?move-this-block)为(FE)或F。这两个序列中的任何一个都不存在堆栈,一个单独的块放在顶部,因此该规则将不符合此顺序。
如果(目标堆栈D C B)匹配目标堆栈模式,则必须与目标堆栈相匹配的目标堆栈序列为(D C B),(DC)或(D)。同样,没有包含该序列的堆栈,只有一个块位于该序列的顶部。
在直接移动规则的逻辑中,您只想在现有堆栈与目标堆栈的底部匹配时直接移动块。对于地板移动规则,您还希望确保不会将块移出部分完成的堆栈。
CLIPS (6.31 2/3/18)
CLIPS>
(defrule move-direct
(declare (salience 10))
?stack1 <- (stack ?block1 $?bottom)
?stack2 <- (stack ?block2 $?goalbottom)
(goal-stack $? ?block1 ?block2 $?goalbottom)
=>
(retract ?stack1 ?stack2)
(assert (stack ?block1 ?block2 ?goalbottom))
(assert (stack $?bottom))
(printout t ?block1 " moved on top of " ?block2 crlf))
CLIPS>
(defrule move-on-floor
(goal-stack $? ?next $?goalbottom)
(not (stack $? ?next $?goalbottom))
?stack <- (stack $?top ?bottom)
(test (member$ ?next ?top))
=>
(retract ?stack)
(assert (stack (nth$ 1 ?top)))
(assert (stack (rest$ ?top) ?bottom))
(printout t (nth$ 1 ?top) " moved on to the floor" crlf))
CLIPS>
(assert (stack A B C)
(stack D E F)
(goal-stack D C B)
(goal-stack A)
(goal-stack F E))
<Fact-5>
CLIPS> (run)
D moved on to the floor
E moved on to the floor
F moved on top of E
A moved on to the floor
B moved on to the floor
C moved on top of B
D moved on top of C
CLIPS> (facts)
f-0 (initial-fact)
f-3 (goal-stack D C B)
f-4 (goal-stack A)
f-5 (goal-stack F E)
f-10 (stack F E)
f-11 (stack)
f-12 (stack A)
f-17 (stack D C B)
For a total of 8 facts.
CLIPS> (reset)
CLIPS>
(assert (stack A B C)
(goal-stack A B)
(goal-stack C))
<Fact-3>
CLIPS> (run)
A moved on to the floor
B moved on to the floor
A moved on top of B
CLIPS> (facts)
f-0 (initial-fact)
f-2 (goal-stack A B)
f-3 (goal-stack C)
f-7 (stack C)
f-8 (stack A B)
f-9 (stack)
For a total of 6 facts.
CLIPS>