Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Rule Based Programming

3rd year, 2nd semester

The RETE algorithm


CLIPS agenda
Rule salience
Fact templates
The RETE algorithm
Introduced by Charles L. Forgy (1975) - CMU

The original paper

RETE: anatomical network of blood vessels and nerve


fibers

The fastest forward-checking algorithm for RBP.


Root node

Alpha network
f1 = one f1 = two
(defrule first
(one ?x 1 ? ?)
f3 = 1 f2 = f3 f3 = 1
(two ?x ?x)
=>
… f2 = 2
)
(defrule second
(one 2 1 ?x ?y)
(two ?y 1)
=>

)
Root node

Beta network
f1 = one f1 = two
(defrule first
(one ?x 1 ? ?)
f3 = 1 f2 = f3 f3 = 1
(two ?x ?x)
=>
… f2 = 2
)
(defrule second
(one 2 1 ?x ?y)
left f2 = right f2 left f5 = right f2
(two ?y 1)
=>
… “first” activated “second”
) activated
Root node

Working memory n1 n2
f1 = one f1 = two
Fact 1: (one 1 1 2 2)
Fact 2: (one 1 1 2 1)
Fact 3: (one 2 1 3 2) n3 n4 n5
Fact 4: (one 3 1 3 2) f3 = 1 f2 = f3 f3 = 1
Fact 5: (two 1 1)
Fact 6: (two 2 2)
n6
f2 = 2
n1: Facts 1, 2, 3, 4
n2: Facts 5, 6
n3: Facts 1, 2, 3, 4
n4: Facts 5, 6 n7 n8
n5: Fact 5 left f2 = right f2 left f5 = right f2
n6: Fact 3
n7: Facts (1,5), (2,5), (3, 5), (4, 5)
n8: - “first” activated “second”
stored: 4, 6, 10, 12, 13, 14, 4x2 activated
Consideration for writing rules
● Place stricter patterns first (less chance to get to check the others)
● Place patterns over fewer facts first (fewer checks to get to the others)
● Place patterns over emerging facts last (adding new facts will trigger less checks)

(defrule good
(alpha ?x ?y&:(> ?y ?x) 5)
(beta ? $?)
)

(defrule bad
(beta ? $?)
(alpha ?x ?y&:(> ?y ?x) 5)
)
The Agenda
Agenda window OR (agenda)

0 first: f-1
0 second: f-2

The list of all activations (rule name : facts satisfying their patterns). The same rule can
appear multiple times in the agenda if it’s conditions are satisfied in multiple ways (by the
same or other facts).
What happens when we (run)?
How are the two activations executed?
● Sequential? What happens if the first rule removes facts needed by the second?
● Parallel? How do you solve the possible conflicts between the merging facts?
● What about this case:

(deffacts f (alpha 2) (beta 1))


(defrule first
(alpha ?) => (printout t “good”)))
(defrule second
(beta ?) => (assert (alpha 1)))

How many times is “good” printed?


What happens when we (run)?
Step 1: RETE computes the agenda.
Step 2: If the agenda has at least one activation, go to step 3. Else go to step 5.
Step 3: The first activation is executed - the right side of the rule is applied.
Step 4: Back to step 1.
Step 5: Stop.

Important CLIPS tip: the agenda is computed again after each rule execution.
Important CLIPS tip: once executed, an activation will never be added again.
Use the other activations for debugging. Is the rule you expected to run activated? What
activations are before it?
To run just one execution, use (run 1).
Rule saliences (I)
(defrule first 0 first: ^
(not (apple)) => (printout t “missing”))) 0 second: ^
(defrule second
(not (apple)) => (assert (apple)))

Is “missing” printed?

(defrule first
(not (apple)) => (printout t “missing”))) 10 second: ^
(defrule second (declare (salience 10)) 0 first: ^
(not (apple)) => (assert (apple)))
Rule saliences (II)
Use salience values in multiples of 10 or 100. You may decide later to insert
another salience between existing ones.

Using a different salience for each rule skips most of the inference engine (which is
bad!).

A maximum of 5 levels is recommended. Use saliences for critical rules and to


organize your code (group rules by purpose, not by priority).
CLIPS inference engine
● Activated rules are placed above all rules of lower salience and below all rules
of higher salience.
● Among rules of equal salience, the current conflict resolution strategy is
used to determine the placement.
● If the above steps are unable to specify an ordering, then an activation is
arbitrarily (not randomly) ordered in relation to the others.

CLIPS provides seven conflict resolution strategies: depth, breadth, simplicity,


complexity, lex, mea, and random. The default strategy is depth. The current
strategy can be set by using (set-strategy …) - which will reorder the
agenda based upon the new strategy.
Conflict resolution strategies (I)
Depth Strategy: Newly activated rules are placed above all rules of the same
salience.
Breadth Strategy: Newly activated rules are placed below all rules of the same
salience.
Simplicity Strategy: Among rules of the same salience, newly activated rules are
placed above all activations of rules with equal or higher specificity. The specificity
of a rule is determined by the number of comparisons that must be performed on
the LHS of the rule. Each comparison to a constant or previously bound variable
adds one to the specificity. Each function call made on the LHS of a rule as part of
the :, =, or test conditional element adds one to the specificity. The boolean
functions and, or, and not do not add to the specificity of a rule, but their arguments
do.
Conflict resolution strategies (II)
Complexity Strategy: Among rules of the same salience, newly activated rules are
placed above all activations of rules with equal or lower specificity.
LEX&MEA Strategies: adapted from OPS5 (to be discussed in course 9), based
also on timestamps of first activation. Used mostly for backwards-compatibility
with OPS5.
Random Strategy: each activation is assigned a random number that is used to
determine its placement among activations of equal salience. This random number
is preserved when the strategy is changed so that the same ordering is reproduced
when the random strategy is selected again. Can be used to check whether the order
of the rules with the same salience matters.
Using complex facts
(student Ionel 4 5 3 4 2 2 2 10 20 52)

(defrule add_1_to_project
?a<-(student ?x1 ?x2 ?x3 ?x4 ?x5 ?x6 ?x7 ?x8 ?x $?x9)
=>
(retract ?a)
(assert (student ?x1 ?x2 ?x3 ?x4 ?x5 ?x6 ?x7 ?x8 (+ ?x 1)
$?x9)
)
CLIPS templates (I)
(deftemplate student
(slot name)
(multislot labs)
(slot project)
(slot exam)
(slot total)
)

(student (name Ionel) (labs 4 5 3 4 2 2 2) (project 10) (exam


20) (total 52))
CLIPS templates (II)
(defrule add_1_to_project
?a<-(student (project ?x))
=>
(modify ?a (project (+ ?x 1)))
)

Caution! Both example rules will enter an infinite loop. Make sure that the newly created
fact doesn’t fulfill the LHS conditions by using flags in templates or by removing facts
checked in the LHS.
Forall and exists equivalents
(forall (list $? ?x $?) (numbers $? ?x $?))

(not (and (list $? ?x $?) (not (numbers $? ?x $?))))

(exists (list $? ?x $?) (numbers $? ?x $?))

(not (not (and (list $? ?x $?) (numbers $? ?x $?))))


Logical patterns
(defrule associate Creates associations between facts matched
(logical (student (name ?x) to LHS patterns and those created in the
(total ?y:&(< ?y 50))) RHS.
=> If the LHS facts are removed, so do the RHS
(assert (restanta ?x)) facts.
) LHS facts are permanently checked after
each step of the inference engine!
Using logical supplements
implementing additional rules to maintain
the coherence of the facts (if no longer a then
no longer b).
More CLIPS
http://clipsrules.sourceforge.net/documentation/v630/ug.pdf

https://www.csie.ntu.edu.tw/~sylee/courses/clips/toc.htm

http://www.cs.umanitoba.ca/~comp4200/Notes.html

https://www.youtube.com/watch?v=WwGU6AU3f-A

https://kcir.pwr.edu.pl/~witold/ai/CLIPS_tutorial/

You might also like