如何在CLIPS中找到具有最大值的变量的ID?

时间:2018-06-01 23:41:15

标签: max clips

我试图根据他们的考试成绩找到最好的学生。我从用户那里拿走了它们。我想要CLIPS给我最好的学生的ID。例如,student1标记70,student2标记80和学生3标记100. CLIPS应该告诉我“最好的学生是......因为他/她的观点是......”我使用全局变量但是我不确定它是否是是的,因为它不起作用。

(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))

1 个答案:

答案 0 :(得分:1)

我不会使用全局变量。我会用模板和事实。 借助规则的一个解决方案是:

         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)))
    )

因此,如果没有其他学生有更高的分数,这条规则与学生的事实匹配。