Conditional or selective rule using Clips -


i hope doing well? beginner in regards clips. have graph of several nodes (start nodes (input) , of end nodes (output). want create rule in case have number of input equal output, rule must me choose between several combinations (start , end nodes) shortest path each combination.

another rule can added same rule before, if have number of input greater number of output or opposite. after selecting between combination necessary take consideration input or output , derive towards nearest point?

(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 " " ?end " " ?path " cost of  " ?cost  crlf) (assert (info (start ?start) (end ?end) (path $?path) (cost ?cost))) ) 

the combination result if have 3 startnode , 2 endnode

how many node start 3 enter nbr node start   start node name 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 node end value -5  path  (a,a,m,i) cost of  3.5  path  c (c,c,d,n,m,i) cost of  9  path  e (e,n,m,i) cost of  4  path  b (a,a,b,b) cost of  7  path  c b (c,c,b,b) cost of  5  path  e b (e,n,k,g,b,b) cost of  5.8 

and final result hope get

path  (a,a,m,i) cost of  3.5 */ because low cost between 3 first combinaison */ path  e b (e,n,k,g,b,b) cost of  5.8 */ because low cost between 3 second combinaison */ path  c b (c,c,b,b) cost of  5  */ because low cost between (c b) , (c i) */ 

i hope clear ?? thank

try combining rules info , varvar following:

(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 " " ?end " " ?path " cost of  " ?cost  crlf)    (assert (info (start ?start) (end ?end) (path $?path) (cost ?cost)))) 

the results you'll are:

 path  (a,a,m,i) cost of  3.5  path  e (e,n,m,i) cost of  4  path  c b (c,c,b,b) cost of  5 

it's not clear why you're expecting path e b selected since path e has lower cost.


Comments