【发布时间】:2017-05-15 17:16:38
【问题描述】:
所以我们有一个项目,我们需要创建一个专家系统,通过给它一些汽车的事实,它为用户选择最合适的。
首先,在课堂上我们学到了一些关于剪辑的东西,比如 deftemplate、deffacts 和 defrule。仅此而已(!!!) 所以我认为我的项目不能包含模块或函数之类的代码。
其次,根据给定的数据,代码应该和我写的一样。
问题是: 1. 除了(撤回),我们没有学到其他方法。 2. 如果我是对的: 2a.有什么方法可以让我的代码找到应该撤回的内容并且不需要专家写下不相关的事实? 2b。我应该写什么,这样每次都能有结果? (例如,如果我们给类型家庭和价格高我们不会有一个 ruselt)。 2c。有没有什么办法可以在“减法”之后,程序打印,在问题之后,剩下的事实而不是“(事实)”方式??
谢谢你。
代码是:
(deftemplate car_template
(slot brand_name (type STRING))
(slot type (type STRING)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
;;; low = 0-10000 mid = 10001-20000 high = 20001-10000
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SUMBOL) (allowed-symbols low mid high))
)
(deffacts car_facts
(car (brand_name "Opel Astra")(type family)(price mid)(performance low)
(car (brand_name "Peugeot 106a")(type city)(price low)(performance high)
(car (brand_name "Mercedes E200")(type super)(price high)(performance mid)
(car (brand_name "Rover 25")(type family)(price mid)(performance mid)
(car (brand_name "Ferrari F40")(type super)(price high)(performance high)
)
(defrule type_rule
(initial-fact)
=>
(printout t "Give the Type you want (possible options: family city super): "
(bind ?response (read))
(if (eq ?response family) then
;;;retracting city
(retract 2)
;;;rectarting super
(retract 3 5)
else (if (eq ?response city) then
;;;retracting family
(retract 1 4)
;;;rectarting super
(retract 3 5)
else
;;;retracting city
(retract 2)
;;;retracting family
(retract 1 4)
))))
(defrule price_rule
(initial-fact)
=>
(printout t "Give the price you want (possible options: low mid high): "
(bind ?response (read))
(if (eq ?response low) then
;;;retracting mid
(retract 1 4)
;;;rectarting high
(retract 3 5)
else (if (eq ?response mid) then
;;;retracting low
(retract 2)
;;;rectarting high
(retract 3 5)
else
;;;retracting mid
(retract 1 4)
;;;retracting low
(retract 2)
))))
(defrule performance_rule
(initial-fact)
=>
(printout t "Give the performance you want (possible options: low mid high): "
(bind ?response (read))
(if (eq ?response low) then
;;;retracting mid
(retract 3 4)
;;;rectarting high
(retract 2 5)
else (if (eq ?response mid) then
;;;retracting low
(retract 1)
;;;rectarting high
(retract 2 5)
else
;;;retracting mid
(retract 3 4)
;;;retracting low
(retract 1)
))))
(defrule print_facts
(initial-fact)
=>
(printout t "The car that fits you is: "
(facts)
))
【问题讨论】:
-
不需要在没有其他条件的情况下将初始事实添加到规则中;它会在 6.3 版之前的 CLIPS 版本中自动添加。最初的事实功能在 6.3 版本中已被弃用;它仍然由复位断言,但没有条件的规则不再依赖它。在 6.4 版本中,initial-fact 不再被断言,因此明确匹配该事实的规则将不再被激活。
标签: clips expert-system