【问题标题】:Sort a set of facts CLIPS对一组事实 CLIPS 进行排序
【发布时间】:2019-09-20 10:35:11
【问题描述】:

我正在尝试根据基于两个字段的比较器对 CLIPS 中的事实集合进行排序... 不幸的是,如果传递了第一个字段相同的两个事实,我无法理解为什么比较器(显然是正确的)会打印 2。

我的比较器:

(deffunction MAIN::rating-sort (?f1 ?f2)
   (printout t ?f1 crlf)
   (printout t ?f2 crlf)
   (printout t "f1-SC " (fact-slot-value ?f1 sum-certainties) crlf)
   (printout t "f2-SC " (fact-slot-value ?f2 sum-certainties) crlf)
   (printout t "f1-TP " (fact-slot-value ?f1 total-price) crlf)
   (printout t "f2-TP " (fact-slot-value ?f2 total-price) crlf)
   (if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then (printout t "1" crlf) return TRUE
   else (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then (printout t "2" crlf) return FALSE
        else (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then (printout t "3" crlf) return TRUE
             else (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then (printout t "4" crlf) return FALSE
                  else (printout t "5" crlf) return FALSE)))))

WM 中的事实:

f-64    (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 1 0 0 0) (total-price 75.0) (certainty 14.0 -0.001 -0.001 -0.001) (sum-certainties 13.997) (flag TRUE))
f-66    (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 1 0 0) (total-price 100.0) (certainty -0.001 14.0 -0.001 -0.001) (sum-certainties 13.997) (flag TRUE))
f-68    (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 1 0) (total-price 75.0) (certainty -0.001 -0.001 14.0 -0.001) (sum-certainties 13.997) (flag TRUE))
f-70    (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 0 1) (total-price 100.0) (certainty -0.001 -0.001 -0.001 14.0) (sum-certainties 13.997) (flag TRUE))

我的比较器输出:

<Fact-64>
<Fact-66>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-68>
<Fact-70>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-64>
<Fact-68>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 75.0
2
<Fact-66>
<Fact-68>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 75.0
2

我不明白 13.997 怎么会大于 13.997。 提前谢谢大家。

【问题讨论】:

    标签: clips


    【解决方案1】:

    您需要在 deffunction 中使用 (return TRUE)(return FALSE) 而不是 return TRUEreturn FALSE,但即使进行此更改,代码也可以正常工作。比较器从不打印 2。

             CLIPS (6.31 6/12/19)
    CLIPS> 
    (deffunction rating-sort (?f1 ?f2)
       (printout t ?f1 crlf)
       (printout t ?f2 crlf)
       (printout t "f1-SC " (fact-slot-value ?f1 sum-certainties) crlf)
       (printout t "f2-SC " (fact-slot-value ?f2 sum-certainties) crlf)
       (printout t "f1-TP " (fact-slot-value ?f1 total-price) crlf)
       (printout t "f2-TP " (fact-slot-value ?f2 total-price) crlf)
       (if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) 
          then 
          (printout t "1" crlf) return TRUE
          else 
          (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) 
              then (printout t "2" crlf) return FALSE
              else 
              (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) 
                  then (printout t "3" crlf) return TRUE
                  else 
                  (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) 
                      then (printout t "4" crlf) return FALSE
                      else (printout t "5" crlf) return FALSE)))))
    CLIPS> 
    
    (deftemplate alternative
       (multislot hotels)
       (multislot times)
       (slot total-price)
       (multislot certainty)
       (slot sum-certainties)
       (slot flag))            
    CLIPS> 
    (deffacts alternatives
       (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 1 0 0 0) 
                    (total-price 75.0) (certainty 14.0 -0.001 -0.001 -0.001) 
                    (sum-certainties 13.997) (flag TRUE))
       (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 1 0 0) 
                    (total-price 100.0) (certainty -0.001 14.0 -0.001 -0.001) 
                    (sum-certainties 13.997) (flag TRUE))
       (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 1 0) 
                    (total-price 75.0) (certainty -0.001 -0.001 14.0 -0.001) 
                    (sum-certainties 13.997) (flag TRUE))
       (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 0 1) 
                    (total-price 100.0) (certainty -0.001 -0.001 -0.001 14.0) 
                    (sum-certainties 13.997) (flag TRUE)))
    CLIPS> (reset)
    CLIPS> (sort rating-sort (find-all-facts ((?f alternative)) TRUE))
    <Fact-1>
    <Fact-2>
    f1-SC 13.997
    f2-SC 13.997
    f1-TP 75.0
    f2-TP 100.0
    4
    <Fact-3>
    <Fact-4>
    f1-SC 13.997
    f2-SC 13.997
    f1-TP 75.0
    f2-TP 100.0
    4
    <Fact-1>
    <Fact-3>
    f1-SC 13.997
    f2-SC 13.997
    f1-TP 75.0
    f2-TP 75.0
    5
    <Fact-2>
    <Fact-3>
    f1-SC 13.997
    f2-SC 13.997
    f1-TP 100.0
    f2-TP 75.0
    3
    <Fact-2>
    <Fact-4>
    f1-SC 13.997
    f2-SC 13.997
    f1-TP 100.0
    f2-TP 100.0
    5
    (<Fact-1> <Fact-3> <Fact-2> <Fact-4>)
    CLIPS>
    

    【讨论】:

    • 我也觉得打印 2 很荒谬……但事实就是如此……无论如何,感谢您为我提供的耐心
    • 如果我使用精确到千分之一,比较器不能正常工作,但如果我增加或减少精度,那么比较器会给我正确的结果。黑魔法!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2018-08-02
    相关资源
    最近更新 更多