首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在议程上从来没有这样的片段

在议程上从来没有这样的片段
EN

Stack Overflow用户
提问于 2019-01-17 17:42:36
回答 1查看 154关注 0票数 0

我想要建立一个专家系统,在有几层楼的大楼发生紧急情况时(它需要为任何楼层工作),电梯应该把人们带到地面。问题是,把电梯送到任何楼层的缺陷从来没有出现在议事日程上,所以系统什么也不做。正确的行动应该是发射规则,然后是另一个规则,把人们从地板上带走。

破坏规则的代码如下:

代码语言:javascript
复制
(defrule move_to_floor        "elevator moves to any floor "
      ?i <- (elevator is_at floor ?x has ?y adults and ?z minors)
      (floor ?fl&~?x has ?n adult and ?m minor people)
      (test (> (+ ?n ?m) 0))
      => 
      (retract ?i)
      (assert (elevator is_at floor ?fl has ?y adults and ?z minors))
)

在上述另一个缺陷中从用户初始化的事实如下:

代码语言:javascript
复制
f-0     (initial-fact)
f-1     (elevator is_at 0 has 0 adults and 0 minors)
f-3     (capacity 4)
f-4     (floors 3)
f-5     (initCanEnter 0) ;At 0 this prevents from entering the init_defrule again
f-6     (floor 3 has 2 adult and 1 minor people)
f-7     (floor 2 has 4 adult and 5 minor people)
f-8     (floor 1 has 1 adult and 2 minor people)

我似乎找不到解决办法。另外,我使用的是deffacts,而不是脱节模板,因为我已经看到很多人在互联网上使用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-17 18:16:04

您可以使用patterns命令查看规则中的哪些模式是匹配的。

代码语言:javascript
复制
         CLIPS (6.31 2/3/18)
CLIPS> 
(defrule move_to_floor "elevator moves to any floor "
   ?i <- (elevator is_at floor ?x has ?y adults and ?z minors)
   (floor ?fl&~?x has ?n adult and ?m minor people)
   (test (> (+ ?n ?m) 0))
   => 
   (retract ?i)
   (assert (elevator is_at floor ?fl has ?y adults and ?z minors)))
CLIPS> 
(deffacts initial
   (elevator is_at 0 has 0 adults and 0 minors)
   (capacity 4)
   (floors 3)
   (initCanEnter 0) ;At 0 this prevents from entering the init_defrule again
   (floor 3 has 2 adult and 1 minor people)
   (floor 2 has 4 adult and 5 minor people)
   (floor 1 has 1 adult and 2 minor people))
CLIPS> (reset)
CLIPS> (matches move_to_floor)
Matches for Pattern 1
 None
Matches for Pattern 2
f-5
f-6
f-7
Partial matches for CEs 1 - 2
 None
Activations
 None
(3 0 0)
CLIPS> 

在这种情况下,第一个模式不匹配。这是因为您的模式需要is_at ?x,但是您的事实包含is_at 0 (在事实中缺少符号)。如果你纠正这个问题,规则将被列入议程。

代码语言:javascript
复制
CLIPS>    
(deffacts initial
   (elevator is_at floor 0 has 0 adults and 0 minors)
   (capacity 4)
   (floors 3)
   (initCanEnter 0) ;At 0 this prevents from entering the init_defrule again
   (floor 3 has 2 adult and 1 minor people)
   (floor 2 has 4 adult and 5 minor people)
   (floor 1 has 1 adult and 2 minor people))
CLIPS> (reset)
CLIPS> (agenda)
0      move_to_floor: f-1,f-7
0      move_to_floor: f-1,f-6
0      move_to_floor: f-1,f-5
For a total of 3 activations.
CLIPS>

如果此时发出一个(运行)命令,规则将在从一层到另一层的循环中不断地触发,所以接下来需要解决的问题。

如果您使用的是缺陷模板事实而不是有序事实,那么如果您拼写错了槽名,那么如果您有多个属性的事实,最好使用这些事实。

代码语言:javascript
复制
CLIPS> (clear)
CLIPS>   
(deftemplate elevator 
  (slot at_floor (type INTEGER))
  (slot adults (type INTEGER))
  (slot minors (type INTEGER)))
CLIPS>   
(deftemplate floor
   (slot # (type INTEGER))
   (slot adults (type INTEGER))
   (slot minors (type INTEGER)))
CLIPS>    
(deffacts initial
   (elevator (at_floor 0))
   (capacity 4)
   (floors 3)
   (initCanEnter 0) 
   (floor (# 3) (adults 2) (minors 1))
   (floor (# 2) (adults 4) (minors 5))
   (floor (# 1) (adults 1) (minors 2)))
CLIPS>    
 (defrule move_to_floor 
   ?i <- (elevator (at_floor ?x))
   (floor (# ?fl&~?x) (adults ?n) (minors ?m))
   (test (> (+ ?n ?m) 0))
   => 
   (modify ?i (at_floor ?fl)))
CLIPS> (reset)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (elevator (at_floor 0) (adults 0) (minors 0))
f-2     (capacity 4)
f-3     (floors 3)
f-4     (initCanEnter 0)
f-5     (floor (# 3) (adults 2) (minors 1))
f-6     (floor (# 2) (adults 4) (minors 5))
f-7     (floor (# 1) (adults 1) (minors 2))
For a total of 8 facts.
CLIPS> (agenda)
0      move_to_floor: f-1,f-7
0      move_to_floor: f-1,f-6
0      move_to_floor: f-1,f-5
For a total of 3 activations.
CLIPS>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54241466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档