In clips I have the try implement a binary tree structure, i know how implement binary tree in other language but i can't representation my Knowledge .
I am beginner in CLIPS.
my try:
(deftemplate root
(slot lchild)
(slot rchild)
(slot data))
(deftemplate node
(slot lchild)
(slot rchild)
(slot data)
)
;------------------------------------------
(deffacts initial-facts
(emptyyy)
(root (lchild niil) (rchild niil) (data niil))
)
;-----first insert
(defrule insert-root-1
(initial-fact)
?emp <-(emptyyy)
?ro <-(root (lchild ?lr)(rchild ?rr)(data ?dr))
=>
(retract ?emp )
(assert (notemptyyy))
(printout t "enter your data: " )
(bind ?r (read))
(modify ?ro (lchild niil)(rchild niil)(data ?r))
(printout t "---->root = " ?r "----> leftchild= " ?lr "---> rightchild= " ?rr crlf)
)
;................
(defrule insert-node
(notemptyyy)
?ro <-(root (lchild ?lr)(rchild ?rr)(data ?dr))
=>
(printout t "enter your node data: " )
(bind ?dn (read))
(if(<= ?dr ?dn )
then
(modify ?ro (lchild ?lr)(rchild ?dn)(data ?dr))
(printout t "---->node = " ?dr "----> leftchild= " ?lr "---> rightchild= " ?dn crlf)
else
(modify ?ro (lchild ?dn)(rchild ?rr)(data ?dr))
(printout t "---->node = " ?dr "----> leftchild= " ?dn "---> rightchild= " ?rr crlf)
)
)
i dont know how continue,this code just work for root node!!
Just as you would do in a procedural language, you have to traverse the tree to determine where to insert new nodes. You'll need to use facts to keep track of where you are in the tree traversal.
(defglobal ?*next-id* = 0)
(deffunction next-id()
(bind ?*next-id* (+ ?*next-id* 1)))
(deftemplate node
(slot id)
(slot parent (default none))
(slot left-child (default none))
(slot right-child (default none))
(slot data))
(deftemplate process
(slot data))
(deftemplate traverse
(slot id))
(defrule get-data
(not (process))
=>
(printout t "Enter data: ")
(assert (process (data (read)))))
(defrule root-node
?f <- (process (data ?value))
(test (numberp ?value))
(not (node))
=>
(retract ?f)
(assert (node (id 0)
(data ?value))))
(defrule already-exists
?f <- (process (data ?value))
(test (numberp ?value))
(node (data ?value))
=>
(retract ?f)
(printout t "Node already exists" crlf))
(defrule start-traverse
(process (data ?value))
(test (numberp ?value))
(not (node (data ?value)))
(node (id 0))
=>
(assert (traverse (id 0))))
(defrule add-left
?p <- (process (data ?value))
?t <- (traverse (id ?id))
?n <- (node (id ?id) (data ?ovalue) (left-child none))
(test (< ?value ?ovalue))
=>
(retract ?p ?t)
(bind ?nid (next-id))
(modify ?n (left-child ?nid))
(assert (node (id ?nid) (data ?value) (parent ?id))))
(defrule add-right
?p <- (process (data ?value))
?t <- (traverse (id ?id))
?n <- (node (id ?id) (data ?ovalue) (right-child none))
(test (> ?value ?ovalue))
=>
(retract ?p ?t)
(bind ?nid (next-id))
(modify ?n (right-child ?nid))
(assert (node (id ?nid) (data ?value) (parent ?id))))
(defrule traverse-left
(process (data ?value))
?t <- (traverse (id ?id))
(node (id ?id) (data ?ovalue) (left-child ?left&~none))
(test (< ?value ?ovalue))
=>
(modify ?t (id ?left)))
(defrule traverse-right
(process (data ?value))
?t <- (traverse (id ?id))
(node (id ?id) (data ?ovalue) (right-child ?right&~none))
(test (> ?value ?ovalue))
=>
(modify ?t (id ?right)))
I added the following code to your code:
(defrule r-child
?t<- (rchild-node ?value)
(node (id ?id) (data ?value) (right-child ?r))
(node (id ?r) (data ?ovalue) )
=>
(retract ?t)
(printout t "right child node<--- " ?value " --> is:= " ?ovalue crlf )
)
(defrule l-child
?t<- (lchild-node ?value)
(node (id ?id) (data ?value) (left-child ?l))
(node (id ?l) (data ?ovalue) )
=>
(retract ?t)
(printout t "left child node<--- " ?value " --> is:= " ?ovalue crlf )
)
(defrule leave
?t<- (leaf)
(node (id ?id) (data ?value) (left-child none) (right-child none) )
=>
(printout t "-leaf- " ?value crlf )
)
(defrule parent
?t<- (parent-node ?value)
(node (id ?id) (data ?value)(parent ?p ))
(node (id ?p) (data ?ovalue) )
=>
(retract ?t)
(printout t "parent<--- " ?value " --> is:= " ?ovalue crlf )
)
Related
I am trying to solve a planning assignment with pddl and i wrote the following domain
(define (domain Monster)
(:requirements :strips)
(:predicates (player ?p) (location ?x) (monster ?m) (treasure ?tr) (trap ?tp) (weapon ?w) (flyer ?f) ;entities
(playerat ?player ?location) (monsterat ?monster ?location) (trapat ?trap ?location) (treasureat ?trasure ?location) (weaponat ?weapon ?location) (flyerat ?flyer ?location); relations
(gameOver ?p) (holdsw ?p) (holdsf ?p) (holdst ?p) (go ?A ?B) (close ?A ?B)
)
(:action Move ;Move from location A to location B
:parameters (?P ?A ?B ?M) ; P->Player A->Location_1 B->Location_2
:precondition (and(player ?P) (location ?A) (location ?B) (monster ?M) (playerat ?P ?A) (go ?A ?B) (not(monsterat ?M ?B)) )
:effect (and(playerat ?P ?B) (not(playerat ?P ?A)) )
)
(:action PickWeapon ; picking up weapon
:parameters (?P ?L ?W) ;P->player L->location W->Weapon
:precondition (and(player ?P) (location ?L) (weapon ?W) (playerat ?P ?L) (weaponat ?W ?L) )
:effect (and(holdsw ?P) (not(weaponat ?W ?L)))
)
(:action PickFlyer ;picking up flyer
:parameters (?P ?L ?F) ;P->player L->location F->flyer
:precondition (and(player ?P) (location ?L) (flyer ?F) (playerat ?P ?L) (flyerat ?F ?L) )
:effect (and(holdsf ?P) (not(flyerat ?F ?L)) )
)
(:action PickTreasure ;picking up treasure
:parameters (?P ?L ?T) ;P->player L->location T->treasure
:precondition (and(player ?P) (location ?L) (treasure ?T) (playerat ?P ?L) (treasureat ?T ?L) )
:effect (and(holdst ?P) (not(treasureat ?T ?L)) )
)
(:action PlayerKilled ;player killed
:parameters (?P ?L ?M) ;P->player L->location M->monster
:precondition (and(player ?P) (location ?L) (monster ?M) (playerat ?P ?L) (monsterat ?M ?L) (not(holdsw ?P)) (not(holdsf ?P)) )
:effect (and(gameOver ?P) (not(playerat ?P ?L)) ); Game Over
)
(:action PlayerTraped ;Player traped
:parameters (?P ?L ?TR) ;P->player L->location TR->trap
:precondition (and(player ?P) (location ?L) (trap ?TR) (playerat ?P ?L) (trapat ?TR ?L) (not(holdsf ?P)) )
:effect (and(gameOver ?P) (not(playerat ?P ?L)) )
)
(:action Kill ;Killing Monster
:parameters (?P ?A ?M ?B) ;P->player L->location M->monster
:precondition (and(player ?P) (location ?B) (location ?A) (monster ?M) (playerat ?P ?A) (monsterat ?M ?B) (holdsw ?P) (close ?A ?B) (go ?A ?B) )
:effect (and(playerat ?P ?B) (not(monsterat ?M ?B)) (not(holdsw ?P)) )
)
(:action FlyOverMonster
:parameters (?P ?F ?L ?A ?M) ;P->player L->location F->flyer M->monster
:precondition (and(player ?P) (location ?L) (location ?A) (monster ?M) (playerat ?P ?A) (monsterat ?M ?L) (holdsf ?P) (close ?A ?L) (go ?A ?L) )
:effect (and(playerat ?P ?L) (not(holdsf ?P)) )
)
(:action FlyOverTrap
:parameters (?P ?F ?L ?A ?TR) ;P->player L->location F->flyer TR->trap
:precondition (and(player ?P) (location ?L) (location ?A) (trap ?TR) (playerat ?P ?A) (trapat ?TR ?L) (holdsf ?P) (close ?A ?L) (go ?A ?L) )
:effect (and(playerat ?P ?L) (not(holdsf ?P)) )
)
)
problem
i defined the problem to be planned as follows
(define (problem Monster2)
(:domain Monster)
(:objects a b c d e f g h i pl mo tp fl tr wp
)
(:init
(location a) (location b) (location c) (location d) (location e) (location f) (location g) (location h) (location i)
(player pl) (monster mo) (trap tp) (flyer fl) (treasure tr) (weapon wp)
(go a b) (go b a) (go b c) (go c b) (go c d) (go d c) (go a e) (go e b) (go e f) (go f d) (go e g) (go e h) (go h e) (go h i) (go i h)
(close b c) (close c d) (close e f)
(playerat pl a) (monsterat mo c) (treasureat tr d) (trapat tp f) (weaponat wp h) (flyerat fl i)
)
(:goal (and
(holdst pl)
(playerat pl a)
)
)
)
Trying to solve the problem i Changed my code but now I am facing another problem. The agent after killing the monster does not go to the next room but he returns back and then jambs to d room collects the treasure and stay at d. The strange thing is that the planer says that the agent is now at room c and in next state it says he is at room b.
Planner result
Planer Kill Monster agent at room c
Planner result
Planner Move next supposed to be room d and it says room b state which is wrong
Actually there are several mistakes in the PDDL file. I tested it with FF.
In the domain file you missed some AND and few parenthesis in different action definitions, e.g.
(:action Kill
:parameters (?P ?L ?M ?A) ;P->player L->location M->monster
:precondition (and(player ?P) (location ?L) (location ?A) (monster ?M) (atp ?P ?A) (atm ?M ?L) (holdsw ?P ?W) (close ?A ?L) )
:effect (and (not(atm ?M ?L)) (not(holdsw ?P ?W)) (atp ?P ?L))
)
Then you forgot to declare some predicates used in domain definition, e.g. at1...at6, located
Then in actions FlyOverMonster and FlyOverTrap you forgot to declare variable ?A, variable ?W in actions Kill and PlayerKilled.
In action PlayerKilled you have a predicate hold that doesn't appear anywhere else.
I don't continue, but I hope that the corrections are clear. Then, the planner should point out the kind of error it encounters.
Check your predicates name, I am not sure numbers are supported in their names, and in their parameter names.
In your problem, I can see occurrences of at1, at2, etc... These predicates are named strangely, and I am suspicious it is meant to work.
Moreover, they are not declared in your domain.
Trying to figure out this PDDL problem of getting robots to clean tiles but I'm stuck with what I'm doing wrong as far as I understand, I have the movements actions and clean actions only for up and down yet it still doesnt compile. It just gives me an error that says could not parse domain file.
This is the domain:
(define (domain floor-tile)
(:requirements :typing)
;; We have two types: robots and the tiles, both are objects
(:types robot tile - object)
;; define all the predicates as they are used in the probem files
(:predicates
;; described what tile a robot is at
(robot-at ?r - robot ?x - tile)
;; indicates that tile ?x is above tile ?y
(up ?x - tile ?y - tile)
;; indicates that tile ?x is below tile ?y
(down ?x - tile ?y - tile)
;; indicates that tile ?x is right of tile ?y
(right ?x - tile ?y - tile)
;; indicates that tile ?x is left of tile ?y
(left ?x - tile ?y - tile)
;; indicates that a tile is clear (robot can move there)
(clear ?x - tile)
;; indicates that a tile is cleaned
(cleaned ?x - tile)
)
(:action clean-up
:parameters (?r - robot ?x - tile ?y - tile )
:precondition (and (up ?y ?x) (robot-at ?r ?x) (clear ?y))
:effect (and (not (clear ?y)) (cleaned ?y)
)
(:action clean-down
:parameters (?r - robot?x - tile ?y - tile)
:precondition (and (down ?y ?x) (robot-at ?r ?x) (clear ?y))
:effect (and (not (clear ?y)) (cleaned ?y)
)
(:action up
:parameters (?r - robot?x - tile ?y - tile)
:precondition (and (up ?y ?x) (robot-at ?r ?x) (clear ?y))
:effect (and (robot-at ?r ?y) (not(robot-at ?r ?x)))
)
(:action down
:parameters (?r - robot?x - tile ?y - tile)
:precondition (and (down ?y ?x) (robot-at ?r ?x)(clear ?y))
:effect (and (robot-at ?r ?y)
(not(robot-at ?r ?x)))
)
(:action right
:parameters (?r - robot?x - tile ?y - tile)
:precondition (and (right ?y ?x) (robot-at ?r ?x) (clear ?y))
:effect (and (robot-at ?r ?y)
(not(robot-at ?r ?x)))
)
(:action left
:parameters (?r - robot?x - tile ?y - tile)
:precondition (and (left ?y ?x) (robot-at ?r ?x) (clear ?y))
:effect (and (robot-at ?r ?y )
(not(robot-at ?r ?x)))
)
)
Here is the problem:
(define (problem prob001)
(:domain floor-tile)
(:objects tile_0-1 tile_0-2
tile_1-1 tile_1-2
tile_2-1 tile_2-2 - tile
robot1 - robot
)
(:init
(robot-at robot1 tile_1-1)
(clear tile_0-1)
(clear tile_0-2)
(clear tile_1-2)
(clear tile_2-1)
(clear tile_2-2)
(up tile_1-1 tile_0-1)
(up tile_1-2 tile_0-2)
(up tile_2-1 tile_1-1)
(up tile_2-2 tile_1-2)
(down tile_0-1 tile_1-1)
(down tile_0-2 tile_1-2)
(down tile_1-1 tile_2-1)
(down tile_1-2 tile_2-2)
(right tile_0-2 tile_0-1)
(right tile_1-2 tile_1-1)
(right tile_2-2 tile_2-1)
(left tile_0-1 tile_0-2)
(left tile_1-1 tile_1-2)
(left tile_2-1 tile_2-2)
)
(:goal (and
(cleaned tile_0-1)
(cleaned tile_0-2)
(cleaned tile_1-1)
(cleaned tile_2-2)
))
)
Brackets are missing on the :effect of both clean-up and clean-down. After adding those brackets, the online editor's solver indicates that the goal trivially can't be reached, but at least it parses.
I am studying SPARQL and there is this example on the documentation page:
PREFIX : <http://people.example/>
PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
:alice :knows ?y .
{
SELECT ?y (MIN(?name) AS ?minName)
WHERE {
?y :name ?name .
} GROUP BY ?y
}
}
From this dabase:
#prefix : <http://people.example/> .
:alice :name "Alice", "Alice Foo", "A. Foo" .
:alice :knows :bob, :carol .
:bob :name "Bob", "Bob Bar", "B. Bar" .
:carol :name "Carol", "Carol Baz", "C. Baz" .
There is says that this query:
"Return a name (the one with the lowest sort order) for all the people that know Alice and have a name."
and the output is:
y minName
:bob "B. Bar"
:carol "C. Baz"
But I can't understand how "B. Bar" is smaller than "Bob", for example. I cannot understand what the docs means with "lowest sort order".
For me the output should be:
y minName
:bob "Bob"
:carol "Carol"
Thank you for your help.
My data which simply denotes two paths from :a to :v and k respectively is as follows:
#prefix : <http://m/questions/19587520/sparql-path-between-two-instance/> .
:a :z :d.
:d :p :e .
:e :g :f .
:f :l :g .
:g :m :h .
:h :p :k .
:k :p :c .
:c :p :v .
:a :p :x.
:x :q :y.
:y :z :c.
:a :l :g .
:g :m :h .
:h :p :k .
The SPARQL queries that I tried are
String querygraph=
"PREFIX : <http://m/questions/19587520/sparql-path-between-two-instance/>" +
"SELECT ?start ?end (count(?mid) as ?length)" +
"WHERE {" +
" values (?start ?end) { (:a :c) " +
" } " +
" ?start (: |!:)+ ?mid . " +
" ?mid (: | !:)* ?end . " +
" } " +
" group by ?start ?end " +
" ORDER BY ASC(?length)";
String querygraph1=
"PREFIX : <http://monika/questions/19587520/sparql-path-between-two-instance/>" +
"select (count(?m) as ?length) " +
"WHERE {" +
" values (?s ?d) { (:a :c) " +
" } " +
"?s (:|!:)+ ?m ."+
" ?m (: | !:)* ?d . " +
"}" ;
In this I need to print and calculate length of path but the problem is there are two paths from :a to :c. My code is not able to differentiate between them, it calculates it as one. Please help me to print both paths separately with their length.
This is not possible in the general case using SPARQL. But if you you know of some intermediate node that needs to be visited you can add this part as part of the triple pattern.
Another option that may work is if you put the two paths in different named graphs:
#prefix : <http://m/questions/19587520/sparql-path-between-two-instance/> .
GRAPH :path1 {
:a :z :d.
:d :p :e .
:e :g :f .
:f :l :g .
:g :m :h .
:h :p :k .
:k :p :c .
:c :p :v .
}
GRAPH :path2 {
:a :p :x.
:x :q :y.
:y :z :c.
:a :l :g .
:g :m :h .
:h :p :k .
}
Then you could express a query against the first graph as:
PREFIX : <http://m/questions/19587520/sparql-path-between-two-instance/>
SELECT DISTINCT ?midI ?p ?midJ
FROM :path1
where {
VALUES (?begin ?end) { (:a :k) }
?begin !:* ?midI .
?midI ?p ?midJ .
?midJ !:* ?end .
}
This way you can avoid having the two paths cross each other. However, property paths in SPARQL cannot guarantee a shortest path (or all paths for that matter), only that there is one. You can count the length as well. There is a useful question here on stackoverflow that is related to this problem that I would recommend having a look at: Finding all steps in property path.
I've got a problem using clips.
I'm trying to do a detection-fails system for a vehicle.
I have this function that let me ask a question with predefined answers and save the answer into a variable.
My problem is, I enter in the first rule, get asked the first question but, whatever I do, can't make it to enter in the second rule (tipo-transmision) so I could continue with the sequence, help pls.
;;----DEFFUNCTION----------
(deffunction pregunta (?pregunta $?respuestas-posibles)
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member ?respuesta ?respuestas-posibles)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
;; ----DEFTEMPLATE--------
(deftemplate carro
(slot falla (type SYMBOL)
(allowed-values electrica mecanica)
(default mecanica)
)
(slot transmision (type SYMBOL)
(allowed-values automatica manual)
(default automatica)
)
(multislot sintoma)
(multislot compostura)
)
;;----------DEFFACTS--------
(deffacts inicia
(carro))
;;---------DEFRULES---------
(defrule tipo-falla ""
?A <- (carro (compostura) )
=>
(bind ?resp (pregunta "Tipo de Falla (electrica/mecanica)? " electrica mecanica)
)
(if (eq ?resp electrica)
then (modify ?A (falla electrica)
(transmision automatica)
(compostura)
(sintoma)
)
else (modify ?A (falla mecanica)
(transmision automatica)
(compostura)
(sintoma))
)
)
;;-------------------------------------------------------
(defrule tipo-transmision ""
?A <- (carro (transmision automatica) (compostura) (sintoma))
=>
(bind ?resp (pregunta
"Tipo de Transmision (automatica/manual)? " automatica manual)
)
(if (eq ?resp automatica)
then (modify ?A (transmision automatica)
(falla electrica)
(sintoma cortocircuito)
)
else (modify ?A (transmision manual)
)
)
)
;;--------------------------------------------------------
(defrule tipo-sintoma ""
?A <- (carro (falla electrica) (transmision automatica) (sintoma cortocircuito)(compostura) )
=>
(bind ?resp (pregunta "Tipo de Sintoma (desajustesensor/cortocircuito/interruptorposicionralentia? " desajustesensor cortocircuito interruptorposicionralentia) )
(modify ?A (sintoma ?resp ))
)
;;----------------------------------
(defrule diagnostico1 ""
?A <- (carro (falla mecanica) (transmision automatica) (sintoma desajustesensor cortocircuito interruptorposicionralentia)(compostura) )
=>
(modify ?A (falla mecanica)
(transmision automatica)
(sintoma desajustesensor cortocircuito interruptorposicionralentia)
(compostura "DAÑADO EL SENSOR DE LA MARIPOSA DE GASES")
)
)
;;---------------------------------
(defrule nada ""
(declare (salience -10))
?A <- (carro (compostura))
=>
(modify ?A (compostura "Llevalo al mecanico.")))
;;----------RESULTADOS--------------------
(defrule inicia
(declare (salience 1))
=>
(printout t crlf crlf)
(printout t "Sistema de Detección de Fallas de Un Vehiculo")
(printout t crlf crlf))
(defrule resultados
(declare (salience -1))
(carro (falla ?A) (transmision ?T) (sintoma ?S) (compostura ?C))
=>
(printout t "La falla es " ?A crlf)
(printout t "La transmision es " ?T crlf)
(printout t "El sintoma es " ?S crlf)
(printout t "Se sugiere de reparacion que:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?C))
Your strategy for assigning values to the carro fact is unclear. You default the transmision slot value to automatica and the falla slot to mechanica. Both the tipo-falla and tipo-transmision rules assign values to the transmision and falla slots, so even if the tipo-falla rule was not looping by itself you'd have issues with these two rules interacting.
If you assign the slots a value indicating that they are unspecified, you can use this value in your rules to determine if a value has been assigned and prevent a rule from retriggering.
(deftemplate carro
(slot falla
(type SYMBOL)
(allowed-values electrica mecanica nespecificat)
(default nespecificat))
(slot transmision
(type SYMBOL)
(allowed-values automatica manual nespecificat)
(default nespecificat))
(multislot sintoma)
(multislot compostura))
(defrule tipo-falla ""
?A <- (carro (falla nespecificat))
=>
(bind ?resp (pregunta "Tipo de Falla (electrica/mecanica)? " electrica mecanica))
(modify ?A (falla ?resp)))