在剪辑中找到最适合的汽车
Finding the fittest car in clips
所以我们有一个项目,我们需要制作一个专家系统,通过给它一些汽车的事实,它为用户选择最合适的。
首先,在 class 中,我们学到了一些关于剪辑的知识,例如 detemplate、defacts 和 defrule。仅此而已(!!!)
所以我认为我的项目不能包含模块或函数之类的代码。
其次,根据给定的数据,代码应该是我下面写的。
事情是:
1. 除了(收回),我们没有学到其他方法。
2. 如果我是对的:
2a.我的代码有什么办法可以找到应该收回的内容而不需要专家写下不相关的事实吗?
2b.我应该写什么才能每次都有结果? (例如,如果我们给类型系列和价格高,我们将不会有 ruselt)。
2c.有没有什么办法在那之后 "Subtraction method" 我虽然,程序打印,在问题之后,剩余的事实但不是用“(事实)”方式??
谢谢你。
代码是:
(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)
))
这是撤回事实的更好方法:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ~?type))
=>
(retract ?c))
(defrule price_rule
(price ?price)
?c <- (car (price ~?price))
=>
(retract ?c))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ~?performance))
=>
(retract ?c))
(defrule match
(declare (salience -100))
(car (brand_name ?name))
=>
(printout t "The car that fits you is: " ?name crlf))
根据匹配条件的数量打印汽车列表的方法如下:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (allowed-symbols low mid high))
(multislot matches))
(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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ?type) (matches $?m))
(test (not (member$ type ?m)))
=>
(modify ?c (matches ?m type)))
(defrule price_rule
(price ?price)
?c <- (car (price ?price) (matches $?m))
(test (not (member$ price ?m)))
=>
(modify ?c (matches ?m price)))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ?performance) (matches $?m))
(test (not (member$ performance ?m)))
=>
(modify ?c (matches ?m performance)))
(defrule match-header
(declare (salience -100))
=>
(printout t "Cars matching your criteria: " crlf))
(defrule match-3
(declare (salience -200))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 3))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-2
(declare (salience -300))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 2))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-1
(declare (salience -400))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 1))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-none
(declare (salience -200))
(not (and (car (brand_name ?name) (matches $?m))
(test (> (length$ ?m) 0))))
=>
(printout t " None" crlf))
使用排序和查找所有事实函数来帮助打印结果:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (allowed-symbols low mid high))
(multislot matches))
(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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ?type) (matches $?m))
(test (not (member$ type ?m)))
=>
(modify ?c (matches ?m type)))
(defrule price_rule
(price ?price)
?c <- (car (price ?price) (matches $?m))
(test (not (member$ price ?m)))
=>
(modify ?c (matches ?m price)))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ?performance) (matches $?m))
(test (not (member$ performance ?m)))
=>
(modify ?c (matches ?m performance)))
(defrule match-header
(declare (salience -100))
=>
(printout t "Cars matching your criteria: " crlf))
(deffunction sort-cars (?f1 ?f2)
(< (length$ (fact-slot-value ?f1 matches))
(length$ (fact-slot-value ?f2 matches))))
(defrule match-some
(declare (salience -200))
(exists (and (car (matches $?m))
(test (> (length$ ?m) 0))))
=>
(bind ?cars (find-all-facts ((?c car)) (> (length$ ?c:matches) 0)))
(bind ?cars (sort sort-cars ?cars))
(progn$ (?c ?cars)
(printout t " " (fact-slot-value ?c brand_name) " : " (implode$ (fact-slot-value ?c matches)) crlf)))
(defrule match-none
(declare (salience -200))
(not (and (car (matches $?m))
(test (> (length$ ?m) 0))))
=>
(printout t " None" crlf))
当你运行程序时的最终结果:
CLIPS> (reset)
CLIPS> (run)
Give the Type you want (possible options: family city super): super
Give the price you want (possible options: low mid high): high
Give the performance you want (possible options: low mid high): mid
Cars matching your criteria:
Mercedes E200 : performance type price
Ferrari F40 : price type
Rover 25 : performance
CLIPS>
所以我们有一个项目,我们需要制作一个专家系统,通过给它一些汽车的事实,它为用户选择最合适的。
首先,在 class 中,我们学到了一些关于剪辑的知识,例如 detemplate、defacts 和 defrule。仅此而已(!!!) 所以我认为我的项目不能包含模块或函数之类的代码。
其次,根据给定的数据,代码应该是我下面写的。
事情是: 1. 除了(收回),我们没有学到其他方法。 2. 如果我是对的: 2a.我的代码有什么办法可以找到应该收回的内容而不需要专家写下不相关的事实吗? 2b.我应该写什么才能每次都有结果? (例如,如果我们给类型系列和价格高,我们将不会有 ruselt)。 2c.有没有什么办法在那之后 "Subtraction method" 我虽然,程序打印,在问题之后,剩余的事实但不是用“(事实)”方式??
谢谢你。
代码是:
(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)
))
这是撤回事实的更好方法:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ~?type))
=>
(retract ?c))
(defrule price_rule
(price ?price)
?c <- (car (price ~?price))
=>
(retract ?c))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ~?performance))
=>
(retract ?c))
(defrule match
(declare (salience -100))
(car (brand_name ?name))
=>
(printout t "The car that fits you is: " ?name crlf))
根据匹配条件的数量打印汽车列表的方法如下:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (allowed-symbols low mid high))
(multislot matches))
(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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ?type) (matches $?m))
(test (not (member$ type ?m)))
=>
(modify ?c (matches ?m type)))
(defrule price_rule
(price ?price)
?c <- (car (price ?price) (matches $?m))
(test (not (member$ price ?m)))
=>
(modify ?c (matches ?m price)))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ?performance) (matches $?m))
(test (not (member$ performance ?m)))
=>
(modify ?c (matches ?m performance)))
(defrule match-header
(declare (salience -100))
=>
(printout t "Cars matching your criteria: " crlf))
(defrule match-3
(declare (salience -200))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 3))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-2
(declare (salience -300))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 2))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-1
(declare (salience -400))
(car (brand_name ?name) (matches $?m))
(test (= (length$ ?m) 1))
=>
(printout t " " ?name " : " (implode$ ?m) crlf))
(defrule match-none
(declare (salience -200))
(not (and (car (brand_name ?name) (matches $?m))
(test (> (length$ ?m) 0))))
=>
(printout t " None" crlf))
使用排序和查找所有事实函数来帮助打印结果:
(deftemplate car
(slot brand_name (type STRING))
(slot type (type SYMBOL)(allowed-symbols family city super))
(slot price (type SYMBOL) (allowed-symbols low mid high))
(slot performance (type SYMBOL) (allowed-symbols low mid high))
(slot equipment (type SYMBOL) (allowed-symbols low mid high))
(multislot matches))
(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 questions
=>
(printout t "Give the Type you want (possible options: family city super): ")
(assert (type (read)))
(printout t "Give the price you want (possible options: low mid high): ")
(assert (price (read)))
(printout t "Give the performance you want (possible options: low mid high): ")
(assert (performance (read))))
(defrule type_rule
(type ?type)
?c <- (car (type ?type) (matches $?m))
(test (not (member$ type ?m)))
=>
(modify ?c (matches ?m type)))
(defrule price_rule
(price ?price)
?c <- (car (price ?price) (matches $?m))
(test (not (member$ price ?m)))
=>
(modify ?c (matches ?m price)))
(defrule performance_rule
(performance ?performance)
?c <- (car (performance ?performance) (matches $?m))
(test (not (member$ performance ?m)))
=>
(modify ?c (matches ?m performance)))
(defrule match-header
(declare (salience -100))
=>
(printout t "Cars matching your criteria: " crlf))
(deffunction sort-cars (?f1 ?f2)
(< (length$ (fact-slot-value ?f1 matches))
(length$ (fact-slot-value ?f2 matches))))
(defrule match-some
(declare (salience -200))
(exists (and (car (matches $?m))
(test (> (length$ ?m) 0))))
=>
(bind ?cars (find-all-facts ((?c car)) (> (length$ ?c:matches) 0)))
(bind ?cars (sort sort-cars ?cars))
(progn$ (?c ?cars)
(printout t " " (fact-slot-value ?c brand_name) " : " (implode$ (fact-slot-value ?c matches)) crlf)))
(defrule match-none
(declare (salience -200))
(not (and (car (matches $?m))
(test (> (length$ ?m) 0))))
=>
(printout t " None" crlf))
当你运行程序时的最终结果:
CLIPS> (reset)
CLIPS> (run)
Give the Type you want (possible options: family city super): super
Give the price you want (possible options: low mid high): high
Give the performance you want (possible options: low mid high): mid
Cars matching your criteria:
Mercedes E200 : performance type price
Ferrari F40 : price type
Rover 25 : performance
CLIPS>