【问题标题】:Conditional or selective rule using Clips使用剪辑的条件或选择性规则
【发布时间】:2017-08-31 18:14:55
【问题描述】:

我希望你做得很好?我是 CLIPS 的初学者。我有几个节点(开始节点(输入)和结束节点(输出)的图。我想在输入的数量等于输出的情况下创建一个规则,该规则必须帮助我在几个之间进行选择组合(开始和结束节点)每个组合的最短路径。

如果我输入的数量大于输出的数量或相反,可以添加另一个规则与之前相同的规则。在组合之间进行选择后,是否还需要考虑输入或输出,并将其推向最近的点?

(deftemplate path
(slot start) 
(slot end)
(multislot path) 
(slot cost(type NUMBER)))

(deftemplate info
(slot start) 
(slot end)
(multislot path) 
(slot cost))




(deffacts variable_table

(path (start A)(end B)(path A,a,b,B)         (cost  7))
(path (start A)(end C)(path A,a,b,c,C)       (cost   10))
(path (start A)(end D)(path A,a,m,n,d,D)     (cost 8.5))
(path (start A)(end E)(path A,a,m,n,E)       (cost 5.5))
(path (start A)(end F)(path A,a,m,n,k,g,f,F) (cost  9.4))
(path (start A)(end CS) (path A,a,m,n,k,CS)  (cost  6.7))
(path (start A)(end G)(path A,a,m,n,k,g,G)   (cost 8))
(path (start A)(end H)(path A,a,m,n,d,e,H)   (cost 10.3))
(path (start A)(end I)(path A,a,m,I)         (cost 3.5))

(path (start B)(end A) (path B,b,a,A)        (cost   7  ))
(path (start B)(end C) (path B,b,c,C)        (cost  4))
(path (start B)(end D) (path B,b,c,d,D)      (cost 7 ))
(path (start B)(end E) (path B,b,g,k,n,E)    (cost 5.8  ))
(path (start B)(end F) (path B,b,g,f,F)      (cost  4.7))
(path (start B)(end CS)(path B,b,g,k,CS)     (cost  4.6))
(path (start B)(end G) (path NG2,b,g,G)      (cost  3.3))
(path (start B)(end H) (path NG2,b,g,f,e,H)  (cost 6.5 ))
(path (start B)(end I) (path NG2,b,g,k,n,m,I)(cost 7.8 ))

(path (start C)(end A) (path C,c,b,a,A)      (cost   10))
(path (start C)(end B) (path C,c,b,B)        (cost   5))
(path (start C)(end D) (path C,c,d,D)        (cost   4))
(path (start C)(end E) (path C,c,d,n,E)      (cost   5.3))
(path (start C)(end F) (path C,c,d,e,f,F)    (cost   7.6))
(path (start C)(end CS)(path C,c,b,g,k,CS)   (cost   7.6))
(path (start C)(end G) (path C,c,b,g,G)      (cost  6.3))
(path (start C)(end H) (path C,c,d,e,H)      (cost   5.8))
(path (start C)(end I) (path C,c,d,n,m,I)    (cost  9))

(path (start D)(end A) (path D,d,n,m,a,A)    (cost  8.5))
(path (start D)(end B) (path D,d,c,b,B)      (cost  7))
(path (start D)(end C) (path D,d,c,C)        (cost  4))
(path (start D)(end E) (path D,d,n,E)        (cost  5))
(path (start D)(end F) (path D,d,e,f,F)      (cost  5.6))
(path (start D)(end CS)(path D,d,n,k,CS)     (cost  6.2))
(path (start D)(end G) (path D,d,e,f,g,G)    (cost  6))
(path (start D)(end H) (path D,d,e,H)        (cost  3.8))
(path (start D)(end I) (path D,d,n,m,I)      (cost  7))

(path (start E)(end A) (path E,n,m,a,A)      (cost  5.5))
(path (start E)(end B) (path E,n,k,g,b,B)    (cost  5.8))
(path (start E)(end C) (path E,n,d,c,C)      (cost  7))
(path (start E)(end D) (path E,n,d,D)        (cost  5))
(path (start E)(end F) (path E,n,k,g,f,F)    (cost  5.9))
(path (start E)(end CS)(path E,n,k,CS)       (cost  3.2))
(path (start E)(end G) (path E,n,k,g,G)      (cost  4.5))
(path (start E)(end H) (path E,n,d,e,H)      (cost  6.8))
(path (start E)(end I) (path E,n,m,I)        (cost  4))

(path (start F)(end A) (path F,f,g,k,n,m,a,A)(cost   9.4))
(path (start F)(end B) (path F,f,g,b,B)      (cost   4.7))
(path (start F)(end C) (path F,f,e,d,c,C)    (cost  7.6))
(path (start F)(end D) (path F,f,e,d,D)      (cost  5.6))
(path (start F)(end E) (path F,f,g,k,n,E)    (cost  5.9))
(path (start F)(end CS)(path F,f,g,k,CS)     (cost   4.7))
(path (start F)(end G) (path F,f,g,G)        (cost   3.4))
(path (start F)(end H) (path F,f,e,H)        (cost   3.8))
(path (start F)(end I) (path F,f,g,k,n,m,I)  (cost  7.9))


(path (start CS)(end A) (path CS,k,n,m,a,A)  (cost  6.7))
(path (start CS)(end B) (path CS,k,g,b,B)    (cost  4.6))
(path (start CS)(end C) (path CS,K,g,b,c,C)  (cost  7.6))
(path (start CS)(end D) (path CS,k,n,d,D)    (cost  6.2))
(path (start CS)(end E) (path CS,K,N,E)      (cost  3.2))
(path (start CS)(end F) (path CS,k,g,f,F)    (cost  4.7))
(path (start CS)(end G) (path CS,k,g,G)      (cost  3.3))
(path (start CS)(end H) (path CS,k,g,f,e,H)  (cost  6.5))
(path (start CS)(end I)(path CS,k,n,m,I)     (cost  5.2))

(path (start G)(end A) (path G,g,k,n,m,a,A)  (cost       8))
(path (start G)(end B) (path G,g,b,B)        (cost  3.3))
(path (start G)(end C) (path G,g,b,c,C)      (cost  6.3))
(path (start G)(end D) (path G,g,f,e,d,D)    (cost  6))
(path (start G)(end E) (path G,g,k,n,E)      (cost  4.5))
(path (start G)(end F) (path G,g,f,F)        (cost  3.4))
(path (start G)(end CS)(path G,g,k,CS)       (cost  3.3))
(path (start G)(end H) (path G,g,f,e,H)      (cost  5.2))
(path (start G)(end I) (path G,g,k,n,m,I)    (cost  6.5))

(path (start H)(end A) (path H,e,d,n,m,a,A)  (cost 10.3))
(path (start H)(end B) (path H,e,f,g,B)      (cost  6.5))
(path (start H)(end C) (path H,e,d,C)        (cost  3.8))
(path (start H)(end D) (path H,e,d,c,D)      (cost  5.8))
(path (start H)(end E) (path H,e,d,n,E)      (cost  6.8))
(path (start H)(end F) (path H,e,f,F)        (cost  3.8))
(path (start H)(end CS)(path H,e,f,g,k,CS)   (cost  6.5))
(path (start H)(end H) (path H,e,f,g,H)      (cost  5.2))
(path (start H)(end I) (path H,e,d,n,m,I)    (cost  8.8))

(path (start I)(end A) (path I,m,a,A)        (cost  3.5))
(path (start I)(end B) (path I,m,n,k,g,b,B)  (cost 7.8))
(path (start I)(end C) (path I,m,n,d,c,C)    (cost  9))
(path (start I)(end D) (path I,m,n,d,D)      (cost  7))
(path (start I)(end E) (path I,m,n,E)        (cost  4))
(path (start I)(end F) (path I,m,n,k,g,f,F)  (cost  7.9))
(path (start I)(end CS)(path I,m,m,k,CS)     (cost  5.2))
(path (start I)(end G) (path I,m,n,k,g,G)    (cost  6.5))
(path (start I)(end H) (path I,m,n,d,e,I)    (cost 8.8)))

 (defrule lancesaisiepoint
?f1 <-  (debut node)
=>
    (retract ?f1)
    (printout t "how many node start")
    (bind ?x (read))
    (assert (startnode ?x))
 )

(defrule saisiepoint
?f <-   (startnode ?a)
    (test (> ?a 0))
=>
 (printout t "Enter nbr node start  " crlf)
(printout t "start node name ")
(bind ?start (read))
(printout t "node start value ")
(bind ?y1 (read))
(assert (startnode ?start ?y1))
(retract ?f)
(assert (startnode (- ?a 1)))
)

(defrule lancesaisiemoins
?f1 <-  (startnode 0)
=>
(retract ?f1)
(printout t "how many node end ")
(bind ?x (read))
(assert (endnode ?x))
 )

(defrule saisiemoins
?f <-   (endnode ?a)
(test (> ?a 0))
=>
(printout t "Enter nbr node end" crlf)
(printout t "end node name ")
(bind ?end (read))
(printout t "node end value ")
(bind ?y2 (read))
(assert (endnode ?end ?y2))
(retract ?f)
(assert (endnode (- ?a 1))))


(defrule supprnbrpoint ;compter
?x<-    (startnode 0)
=>
    (retract ?x)
    )

(defrule supprnbrmoins
?x<-    (endnode 0)
=>
    (retract ?x)
    )

(defrule info

 ?M <- (startnode ?start ?y1)
 ?K <- (endnode ?end ?y2)
  =>
(assert (start ?start end ?end)) 
)

(defrule varvar   

?L<- (start ?start end ?end)

(path (start ?start)(end ?end)(path $?path)(cost ?cost))
  =>
(retract ?L)
(printout t " PATH  " ?start " to " ?end " by " ?path " with a cost of  " ?cost  crlf)
(assert (info (start ?start) (end ?end) (path $?path)
(cost ?cost)))
)

如果我有 3 个 startnode 和 2 个 endnode 的组合结果

how many node start 3
Enter nbr node start  
start node name A
node start value 3
Enter nbr node start  
start node name C
node start value 5
Enter nbr node start  
start node name E
node start value 5
how many node end 2
Enter nbr node end
end node name B
node end value -5
Enter nbr node end
end node name I
node end value -5
 PATH  A to I by (A,a,m,I) with a cost of  3.5
 PATH  C to I by (C,c,d,n,m,I) with a cost of  9
 PATH  E to I by (E,n,m,I) with a cost of  4
 PATH  A to B by (A,a,b,B) with a cost of  7
 PATH  C to B by (C,c,b,B) with a cost of  5
 PATH  E to B by (E,n,k,g,b,B) with a cost of  5.8

然后作为最终结果我希望得到

PATH  A to I by (A,a,m,I) with a cost of  3.5 */ because it is the low cost between the 3 first combinaison */
PATH  E to B by (E,n,k,g,b,B) with a cost of  5.8 */ because it is the low cost between the 3 second combinaison */
PATH  C to B by (C,c,b,B) with a cost of  5  */ because it is the low cost between (C to B) and (C to I) */

我希望它是清楚的?? 谢谢你的帮助

【问题讨论】:

    标签: clips


    【解决方案1】:

    尝试将规则信息和 varvar 组合到以下内容:

    (defrule varvar   
       (startnode ?start ?)
       (endnode ?end ?)
       (path (start ?start) (end ?end) (path $?path) (cost ?cost))
       (not (and (endnode ?end2 ?) 
                 (path (start ?start) (end ?end2) (cost ?cost2&:(< ?cost2 ?cost)))))
       =>
       (printout t " PATH  " ?start " to " ?end " by " ?path " with a cost of  " ?cost  crlf)
       (assert (info (start ?start) (end ?end) (path $?path) (cost ?cost))))
    

    你会得到的结果是:

     PATH  A to I by (A,a,m,I) with a cost of  3.5
     PATH  E to I by (E,n,m,I) with a cost of  4
     PATH  C to B by (C,c,b,B) with a cost of  5
    

    不清楚为什么您期望选择从 E 到 B 的路径,因为从 E 到 I 的路径成本较低。

    【讨论】:

    • 是的,你说得对,加里先生,E 到我的路径成本更低,我正在考虑另一种推理,在你的帮助下,我发现你的 s 更好。非常非常感谢。
    • 您好 @Gary 先生,我将在几个场景中测试该规则,但它仅适用于后者,您能解释一下规则,以及如何扩展它。例如,如果我有 4 个开始和 3 个结束(通常我将有 4 个成本最低的组合,对于 5 个开始,6 .... 或 n,如果我有相反的 3 或 4 ...或 n start 和 n+1 end( endnode 的数量比 startnode 贪婪....我尝试单独执行规则但它不起作用非常感谢。
    • 规则中没有明确限制开始或结束节点的数量。
    • 谢谢你,但我的结果仍然有问题,因为我每次都必须用不同的输入(开始节点和结束节点)测试它,无论如何谢谢你的帮助。对不起我的英语。
    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2023-03-30
    • 2013-11-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多