CLIPS-规则操作块移动实例

一、

1、设A、B、C、D、E、F块,分为二堆放置。一堆为:A在B块上,B在C块上。另一堆为:D在E块上,E在F块上。

2、上层有块时,下层块不能移动,如移动B块,必须先移动A块。

3、move-to-floor把上层的块移到空地上

clear-upper-block清除要移动的块上层的块

clear-lower-block清除将要放置块的上层的块

move-directly在两个块上层均无块的情况下,直接移动

4、完成将C块移到E块之上

 

二、操作

1、cc.clp内容如下:

(deftemplate MAIN::goal
(slot move)
(slot on-top-of))

(deftemplate MAIN::on-top-of
(slot upper)
(slot lower))

(deffacts MAIN::initial-block
(block A)
(block B)
(block C)
(block D)
(block E)
(block F)
(on-top-of (upper NOTHING)(lower A))
(on-top-of (upper A) (lowerB))
(on-top-of (upper B) (lowerC))
(on-top-of (upper C) (lowerFLOOR))
(on-top-of (upper NOTHING)(lower D))
(on-top-of (upper D) (lowerE))
(on-top-of (upper E) (lowerF))
(on-top-of (upper F) (lowerFLOOR))
(goal (move C) (on-top-ofE)))

(defrule MAIN::move-to-floor
?goal <- (goal(move ?block1) (on-top-of FLOOR))
(block ?block1)
(on-top-of (upper NOTHING)(lower ?block1))
?stack <-(on-top-of (upper ?block1) (lower ?block2))
=>
(retract ?goal ?stack)
(assert (on-top-of (upper?block1) (lower FLOOR)))
(assert (on-top-of (upperNOTHING) (lower ?block2)))
(printout t ?block1 " move ontop of floor." crlf))

(defrule MAIN::clear-upper-block
(goal (move ?block1))
(block ?block1)
(on-top-of (upper ?block2)(lower ?block1))
(block ?block2)
=>
(assert (goal (move ?block2)(on-top-of FLOOR))))

(defrule MAIN::clear-lower-block
(goal (on-top-of?block1))
(block ?block1)
(on-top-of (upper ?block2)(lower ?block1))
(block ?block2)
=>
(assert (goal (move ?block2)(on-top-of FLOOR))))

(defrule MAIN::move-directly
?goal <- (goal(move ?block1) (on-top-of ?block2))
(block ?block1)
(block ?block2)
(on-top-of (upper NOTHING)(lower ?block1))
?stack-1 <-(on-top-of (upper ?block1) (lower ?block3))
?stack-2 <-(on-top-of (upper NOTHING) (lower ?block2))
=>
(retract ?goal ?stack-1?stack-2)
(assert (goal (move ?block2)(on-top-of FLOOR)))
(assert (on-top-of (upperNOTHING) (lower ?block3)))
(printout t ?block1 " move ontop of " ?block2 "." crlf))

2、

CLIPS> (clear)
CLIPS> (reset)
CLIPS> (load "E:\cc.clp")
Defining deftemplate: goal
Defining deftemplate: on-top-of
Defining deffacts: initial-block
Defining defrule: move-to-floor +j+j+j+j+j
Defining defrule: clear-upper-block +j+j+j+j+j
Defining defrule: clear-lower-block =j+j+j+j+j
Defining defrule: move-directly =j=j+j+j+j+j+j
TRUE
CLIPS> (reset)
CLIPS> (run)
A move on top of floor.
B move on top of floor.
D move on top of floor.
C move on top of E.
CLIPS> (reset)
CLIPS> (run)
A move on top of floor.
B move on top of floor.
D move on top of floor.
C move on top of E.
CLIPS> (reset)
CLIPS> (watch all)
CLIPS> (run)
FIRE 1clear-upper-block: f-15,f-3,f-9,f-2
==>f-16 (goal(move B) (on-top-of FLOOR))
==> Activation0clear-upper-block: f-16,f-2,f-8,f-1
FIRE 2clear-upper-block: f-16,f-2,f-8,f-1
==>f-17 (goal(move A) (on-top-of FLOOR))
==> Activation0move-to-floor: f-17,f-1,f-7,f-8
FIRE 3move-to-floor: f-17,f-1,f-7,f-8
<==f-17 (goal(move A) (on-top-of FLOOR))
<==f-8(on-top-of (upper A) (lower B))
==>f-18(on-top-of (upper A) (lower FLOOR))
==>f-19(on-top-of (upper NOTHING) (lower B))
==> Activation0move-to-floor: f-16,f-2,f-19,f-9
A move on top of floor.
FIRE 4move-to-floor: f-16,f-2,f-19,f-9
<==f-16 (goal(move B) (on-top-of FLOOR))
<==f-9(on-top-of (upper B) (lower C))
==>f-20(on-top-of (upper B) (lower FLOOR))
==>f-21(on-top-of (upper NOTHING) (lower C))
B move on top of floor.
FIRE 5clear-lower-block: f-15,f-5,f-12,f-4
==>f-22 (goal(move D) (on-top-of FLOOR))
==> Activation0move-to-floor: f-22,f-4,f-11,f-12
FIRE 6move-to-floor: f-22,f-4,f-11,f-12
<==f-22 (goal(move D) (on-top-of FLOOR))
<==f-12(on-top-of (upper D) (lower E))
==>f-23(on-top-of (upper D) (lower FLOOR))
==>f-24(on-top-of (upper NOTHING) (lower E))
==> Activation0move-directly: f-15,f-3,f-5,f-21,f-10,f-24
D move on top of floor.
FIRE 7move-directly: f-15,f-3,f-5,f-21,f-10,f-24
<==f-15 (goal(move C) (on-top-of E))
<==f-10(on-top-of (upper C) (lower FLOOR))
<==f-24(on-top-of (upper NOTHING) (lower E))
==>f-25 (goal(move E) (on-top-of FLOOR))
==>f-26(on-top-of (upper NOTHING) (lower FLOOR))
C move on top of E.
<== Focus MAIN
7 rulesfiredRun time is 0.230000000000018 seconds.
30.4347826086932 rules per second.
18 mean number of facts (19 maximum).
1 mean number of instances (1 maximum).
1 mean number of activations (2 maximum).
CLIPS>

你可能感兴趣的:(C++,c,F#,C#,J#)