如何在 CLIPS 中找到具有最大值的变量 ID?
How can I find the ID of the variable that has the maximum value in CLIPS?
我试图根据他们的考试成绩找到最好的学生。我从用户那里拿走了它们。我想让 CLIPS 给我最好的学生的 ID。例如,student1 分数 70,student2 分数 80,student 3 分数 100。CLIPS 应该告诉我 "The best student is ... because his/her point is ..." 我使用了全局变量,但我不确定它是否正确,因为它不起作用。
(defglobal ?*student1mark* = 0)
(defglobal ?*student2mark* = 0)
(defglobal ?*student3mark* = 0)
(defrule get-marks
=>
(printout t "What is the exam mark of student1?" crlf)
(bind ?*student1mark* (read))
(assert (stu1mark ?*student1mark*))
(printout t "What is the exam mark of student2?" crlf)
(bind ?*student2mark* (read))
(assert (stu2mark ?*student2mark*))
(printout t "What is the exam mark of student3?" crlf)
(bind ?*student3mark* (read))
(assert (stu3mark ?*student3mark*))
(build (str-cat
"(deffacts students (student student1 " ?*student1mark* " student student2 " ?*student2mark* " student student3 " ?*student3mark* "))")))
(defrule whichstudent
(student ?ID = (max ?*student1mark*" ?*student2mark*" ?*student3mark*))
=>
(printout t "The best student is " ?ID crlf))
我不会使用全局变量。我会选择模板和事实。
借助规则的一种解决方案是:
CLIPS (6.30 3/17/15)
CLIPS> (deftemplate student
(slot id (type INTEGER) (default ?NONE))
(slot mark (type INTEGER) (default ?NONE))
)
CLIPS> (deffacts students
(student (id 1) (mark 80))
(student (id 2) (mark 79))
(student (id 4) (mark 60))
(student (id 3) (mark 90))
)
CLIPS> (defrule best-mark
(compare-students)
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
=>
(printout t "The best student is student no. " ?id crlf)
)
CLIPS> (reset)
CLIPS> (assert (compare-students))
<Fact-5>
CLIPS> (run)
The best student is student no. 3
关键部分是
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
如果没有其他学生得分更高,则此规则与学生事实匹配。
我试图根据他们的考试成绩找到最好的学生。我从用户那里拿走了它们。我想让 CLIPS 给我最好的学生的 ID。例如,student1 分数 70,student2 分数 80,student 3 分数 100。CLIPS 应该告诉我 "The best student is ... because his/her point is ..." 我使用了全局变量,但我不确定它是否正确,因为它不起作用。
(defglobal ?*student1mark* = 0)
(defglobal ?*student2mark* = 0)
(defglobal ?*student3mark* = 0)
(defrule get-marks
=>
(printout t "What is the exam mark of student1?" crlf)
(bind ?*student1mark* (read))
(assert (stu1mark ?*student1mark*))
(printout t "What is the exam mark of student2?" crlf)
(bind ?*student2mark* (read))
(assert (stu2mark ?*student2mark*))
(printout t "What is the exam mark of student3?" crlf)
(bind ?*student3mark* (read))
(assert (stu3mark ?*student3mark*))
(build (str-cat
"(deffacts students (student student1 " ?*student1mark* " student student2 " ?*student2mark* " student student3 " ?*student3mark* "))")))
(defrule whichstudent
(student ?ID = (max ?*student1mark*" ?*student2mark*" ?*student3mark*))
=>
(printout t "The best student is " ?ID crlf))
我不会使用全局变量。我会选择模板和事实。 借助规则的一种解决方案是:
CLIPS (6.30 3/17/15)
CLIPS> (deftemplate student
(slot id (type INTEGER) (default ?NONE))
(slot mark (type INTEGER) (default ?NONE))
)
CLIPS> (deffacts students
(student (id 1) (mark 80))
(student (id 2) (mark 79))
(student (id 4) (mark 60))
(student (id 3) (mark 90))
)
CLIPS> (defrule best-mark
(compare-students)
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
=>
(printout t "The best student is student no. " ?id crlf)
)
CLIPS> (reset)
CLIPS> (assert (compare-students))
<Fact-5>
CLIPS> (run)
The best student is student no. 3
关键部分是
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
如果没有其他学生得分更高,则此规则与学生事实匹配。