Вы находитесь на странице: 1из 28

Introduction to AI

AAPP002-4-2 Ver 1.0

Rete Algorithm
Topic & Structure of The Lesson

usages

AAPP002-4-2 Intro to AI Rete algotihm Slides 2 of 28


Learning Outcomes

• At the end of this topic, You should be


able to
• Understand different steps involved in the
execution of Rete algorithm.
• Draw a Rete graph for a problem
• Solve a searching problem using Rete
Algorithm.

AAPP002-4-2 Intro to AI Rete algotihm Slides 3 of 28


Key Terms You Must Be Able To
Use
• If you have mastered this topic, you should be able to use the
following terms correctly in your assignments and exams:

• Rete algorithm

AAPP002-4-2 Intro to AI Rete algotihm Slides 4 of 28


Rete algorithm

• A pattern matching algorithm.


• Rete is another word used for net.
• If we have r rules with an average of p
premises and f facts then r*f^p
comparisons on each cycle to find rules to
be fired.

AAPP002-4-2 Intro to AI Rete algotihm Slides 5 of 28


Rete algorithm

Rete algorithm uses a data structure that enables fast search of


applicable rules. We shall consider it in two parts:

• knowledge representation,
• knowledge management (i.e. introduction of changes into the
knowledge base).

Any rule that is reachable in the Rete graph (see below) via
nonempty relation nodes can be fired.

Rete algorithm is used in JESS (Java Expert System Shell) and


its predecessor – CLIPS (both developed in NASA.)

AAPP002-4-2 Intro to AI Rete algotihm Slides 6 of 28


• For a knowledge base with 100 rules and
an average of 3 premises and 10 facts you
will have 100,000 comparisons per cycle.

AAPP002-4-2 Intro to AI Rete algotihm Slides 7 of 28


• Rete algorithm is based on the fact that only a
few facts are added, changed or removed at
every step in the process of inference.
• Instead of doing all these comparisons every
time only new facts added can be taken into
consideration which is the appraoch taken in
Rete algorithm.
• Rete looks for changes to match in each cycle.

AAPP002-4-2 Intro to AI Rete algotihm Slides 8 of 28


Inference Engine
Facts Rules

Agenda
Pattern Matching

AAPP002-4-2 Intro to AI Rete algotihm Slides 9 of 28


• If two of the three premises of a rule are
satisfied in one cycle, there is no need to
check them again in the next cycle. Only
the third premise is of interest.
• The matching process is updated when
new facts are added or removed. This will
speed up the process of matching if the
number of new facts is small.

AAPP002-4-2 Intro to AI Rete algotihm Slides 10 of 28


• Information about the premises of each rule which are
satisfied (partial matches) must be stored.

• This information is called state information.

• Because of the need for memory space to store this


information, Rete is said to be memory intensive.

• Two networks called a pattern network and a join network are


used in Rete algorithm to store this information.

AAPP002-4-2 Intro to AI Rete algotihm Slides 11 of 28


Consider the following rule
Format
of (animal-type color size hair-length name)
premises
Rule 1: If ((cat ?c small ?h ?n1)
(dog ?c ?q medium ?n2)
(cat ?c large ?h ?n3)
(dog ?c ?q long ?n4)
Then …
The pattern network is a set of trees formed from all of the
premises of all the rules.
cat dog

match value to ?c match value to ?c

small match value to ?q

match value to ?h medium

match value to ?n1 match value to ?n2


AAPP002-4-2 Intro to AI Rete algotihm Slides 12 of 28
When the partial tree for the When the partial tree for the
third premise is constructed, fourth premise is constructed,
the algorithm realizes that a the algorithm realizes that a
tree with cat exists. tree starting with dog exists.
cat dog

match match
value to ?c value to ?c

Small large match


value to ?q
match match
value to ?h value to ?h medium long

match match match match


value to ?n1 value to ?n3 value to ?n2 value to ?n4
Now the join network is constructed
AAPP002-4-2 Intro to AI Rete by
algotihmthe algorithm. Slides 13 of 28
cat dog The join
network joins
match match the various
value to ?c value to ?c leaves of the
tree and
Small match compares
large
similarly named
value to ?q
variables.
match match
value to ?h value to ?h medium long

match match match match


value to ?n1 value to ?n3 value to ?n2 value to ?n4

join
variable
?c Complete pattern and
join variables join networks
?c and ?h
join
variables ?c
AAPP002-4-2 Intro to AI Rete algotihm Slides 14 of 28
If we had the following set of facts:
1. (cat yellow large short rebel)
2. (cat calico large short rubble)
3. (cat calico small short kitty)
4. (dog brown medium long charlie)
5. (cat brown small medium prince)
6. (dog brown small short sam)
7. (dog calico medium medium butcher)
8. (dog brown large medium star)
9. (dog calico medium long tramp)
Each of these facts is first parsed through the network. The network will then look as in the
handout.

Everytime a fact is added or removed the network is updated.

Using the network, rules that can execute can be determined quickly in a forward reasoning
system using Rete.

AAPP002-4-2 Intro to AI Rete algotihm Slides 15 of 28


Because sometimes several rules can execute, they will all
have to be put on the agenda. Conflict resolution is then
necessary to determine the rule to fire (execute).
Several conflict resolution methods are possible.
Using the order of the rule
Select
 lowest numbered rule
 first applicable rule
Using the complexity of rules
A rule with a larger number of premises is more specific
and complex. Select a
 less complex rule to find a general solution first
 more complex rule to find a more specific solution
Using order of the data
Select rules matching
 Older data
 Newer data
AAPP002-4-2 Intro to AI Rete algotihm Slides 16 of 28
Example

Knowledge includes:
1. facts, e.g.
(goal e1 simplify), (goal e2 simplify), (goal e3 simplify),
(expr e1 0 + 3), (expr e2 0 + 5), (expr e3 0 * 2),...
2. patterns, e.g.
(goal ?x simplify)
(expr ?y 0 ?op ?a2)
(parent ?x ?y)
...
3. and rules, e.g.
(R1 (goal ?x simplify) (expr ?x 0 + ?y) => (expr ?x ?y))
(R2 (goal ?x simplify) (expr ?x 0 * ?y) => (expr ?x 0))
...

AAPP002-4-2 Intro to AI Rete algotihm Slides 17 of 28


Example

Knowledge is represented in the form of an acyclic graph. It is for


the presented example as follows:
root
goal expr
x goal expr * y expr +y ...
e1 2 3
e2 5
e3

x y x y
*** ***
e3 2 e1 3
e2 5
R2 R1

AAPP002-4-2 Intro to AI Rete algotihm Slides 18 of 28


Example

The overall structure of the Rete graph is the following:

root

predicate names layer

patterns layer - alpha nodes (with one input)

beta-nodes
(with two inputs)

rules layer - one node for every rule

AAPP002-4-2 Intro to AI Rete algotihm Slides 19 of 28


Adding facts to Rete graph

When a fact arrives then

1. Select the predicate


2. Select the pattern
3. For every relation depending on the selected pattern
update the relation (add a new line to the relation).

AAPP002-4-2 Intro to AI Rete algotihm Slides 20 of 28


Example

Initially the working memory


is empty start

a(X,Y) b(X,Z)

- There is a starting
node and a node for Y=1 Y=2
each of the rule
conditions and
conjunctions of
conditions.
- Arcs are labeled with a(X,1),b(X,Z) a(X,2),b(X,Z)
variable bindings

AAPP002-4-2 Intro to AI Rete algotihm Slides 21 of 28


Example

Fact a(3,1) is added to


the working memory start

a(X,Y) a(3,1) b(X,Z)

a(3,1) is deposited
in the node labeled Y=1 Y=2
a(X,Y) and will
propagate through
the arc labeled Y=1 a(3,1)

a(X,1),b(X,Z) a(X,2),b(X,Z)

Rule doesn’t match


AAPP002-4-2 Intro to AI Rete algotihm Slides 22 of 28
Example

Fact b(3,4) is added to


the working memory start

a(X,Y) a(3,1) b(3,4) b(X,Z)

b(3,4) is deposited
in the node labeled Y=1 Y=2
b(Y,Z) and will
propagate through
the arcs labeled Y=1 a(3,1),b(3,4) b(3,4)
and Y=2
a(X,1),b(X,Z) a(X,2),b(X,Z)

Rule matches Rule doesn’t match


AAPP002-4-2 Intro to AI Rete algotihm Slides 23 of 28
Example

Fact a(3,2) is added to


the working memory start

a(X,Y) a(3,1),a(3,2) b(3,4) b(X,Z)

a(3,2) is deposited
in the node labeled Y=1 Y=2
a(X,Y) and will
propagate through
the arc labeled Y=2 a(3,1),b(3,4) a(3,2),b(3,4)

a(X,1),b(X,Z) a(X,2),b(X,Z)

Rule matches Rule matches


AAPP002-4-2 Intro to AI Rete algotihm Slides 24 of 28
Quick Review Question

AAPP002-4-2 Intro to AI Rete algotihm Slides 25 of 28


Summary of Main Teaching Points

• Recency
A conflict resolution strategy used in forward chaining
systems. The system fires the rule which depends on the
most recently added facts in working memory.

• Refractoriness
A conflict resolution strategy used in forward chaining
systems. The system does not fire a rule which has already
been fired (unless a change has occurred in working
memory).

AAPP002-4-2 Intro to AI Rete algotihm Slides 26 of 28


Question and Answer Session

Q&A

AAPP002-4-2 Intro to AI Rete algotihm Slides 27 of 28


What we will cover next

• Automated Systems

AAPP002-4-2 Intro to AI Rete algotihm Slides 28 of 28

Вам также может понравиться