14CS720 - ARTIFICIAL INTELLIGENCE
(Slot B)
Lecture 15: Rule Based System – RETE graph
Dr. M. Suguna
Assistant Professor, CSE Dept., TCE
[email protected]
Knowledge Representation
If-Then Rules
Production Systems
Forward Chaining
Backward Chaining
Rule Based System
COs mapped: CO5 - Construct knowledge representations using logic to facilitate
inference in the given problem domain
IF… THEN Rules can be used to represent knowledge
IF it rains, then you will get wet
Rules can also be used for recommendations
IF it rains, then you should wear a coat
Production system - uses knowledge in the form of rules to provide
diagnoses or advice on the basis of input data.
Parts
Database of rules (knowledge base)
Database of facts
Inference engine which reasons about the facts using the rules
Works from a set of facts and rules towards a set of conclusions,
diagnoses or recommendations.
When a fact matches the antecedent of a rule
Rule fires
Conclusion added to facts
Use backward chaining when proving a particular conclusion
Works back from a conclusion towards the original facts.
When a conclusion matches the conclusion of a rule in the database,
the antecedents of the rule are compared with facts in the database.
Problem: more than one rule fires at once
Conflict resolution strategy decides which conclusions to use
Give rules priorities and to use the conclusion that has the highest priority
Apply the rule with the longest antecedent
Apply the rule that was most recently added to the database
IF patient has pain THEN IF patient has pain AND patient is
prescribe pain killers (priority 10) over 60
IF patient has chest pain AND patient has a history of heart
THEN treat for heart disease conditions
(priority 100) THEN take to emergency room
The rules that determine the conflict resolution strategy are called
meta rules.
Meta rules define knowledge about how the system will work.
For example, meta rules might define that knowledge from Expert A
is to be trusted more than knowledge from Expert B.
Meta rules are treated by the system like normal rules, but are given
higher priority.
Rules Facts Facts
Rules
Rule fired
1. AΛB→C 1. A triggered
2. A→D 2. B A,B,F
3. CΛD→E 3. F
4. BΛEΛF→G Goal
5. AΛE→H H
6. DΛEΛH→I
Rules Facts Facts Goals
Matching
1. AΛB→C 1. A Rules
2. A→D 2. B A,B,F
3. CΛD→E 3. F
4. BΛEΛF→G Goal
5. AΛE→H H
6. DΛEΛH→I
Expert knowledge derived from
human experts
Purpose
Diagnose illnesses
Provide recommendations
Solve other problems
Knowledge base:
database of rules (domain knowledge)
Explanation system:
explains the decisions the system makes
User Interface:
the means by which the user interacts with the expert system
Knowledge base editor:
allows the user to edit the information in the knowledge base
13
The part of an expert system that does not contain any domain
specific or case specific knowledge is the expert system shell.
A single expert system shell can be used to build a number of
different expert systems.
An example of an expert system shell is CLIPS.
INFERENCE
WORKING ENGINE
MEMORY
PATTERN EXECUTION
MATCHER ENGINE
RULE
BASE
AGENDA
RBS Inference Cycle
Match Conflict Set (CS)
Select
Act (Execute)
RBS strategy
Rule chaining direction: forward, backward
Rule selection from CS
Computational effort
Match – 80-90%
Large computational requirement to perfrom match of LHS
– determine if all instantiations of rules are satisfied by the content
of the WM (Working Memory)
O(comparison to check the satisfaction of a rule) = |WM||CE|
|WM| - no of WMEs (Working Memory Elements)
|CE| - number of condition elements in a rule
The Rete algorithm was designed by Charles Forgy of Carnegie
Mellon University - first in 1974, and later in his 1979 Ph.D. thesis and
a 1982 paper
The Rete algorithm compiles the LHS of the production rules into a
discrimination network
The network is in the form of an augmented dataflow network
Changes to WM - input to the network
The network reports changes to the CS - output of the network
A rete is a directed, acyclic, rooted graph (a tree)
A path from the root node to a leaf represents the left hand side of a rule
Each node stores details of which facts have been matched so far
As facts are changed, the changes are propagated through the tree
This makes an efficient way for expert systems to deal with environments
which change often
The net encodes the condition parts (IF-parts) of the rules
The input are the changes of the working memory (i.e. new elements
or deleted elements. Modification of elements is simulated by first
delete then add modified version)
The output is the conflict set (i.e., the applicable rules)
Find bindings for ?x & ?y so that rule applicable
conflict set
Facts
Patterns
Rules
RETE Graph
(defrule show-act WM
(a ?x) (a 1)
(b ?x ?y) (b 1 2) (b 2 3) (b 2 4)
(c ?y ?z) (c 3) (c 2)
=> …
show-act
a c select
b
a 1 b 1 2 c 3 Resulting relations
b 2 3 c 2 (alpha memories)
b 2 4
Join x
a 1 b 1 2
Intermediate partial result
(beta memories)
Join y
a 1 b 1 2 c 2
Changes to conflict set
Activity of the RETE Match during an Addition
+ a 2
a c select
b
a 1 b 1 2 c 3 Resulting relations
+ a 2 b 2 3 c 2 (alpha memories)
b 2 4
Join x
a 1 b 1 2
Intermediate partial result
(beta memories)
+ a 2 b 2 3
+ a 2 b 2 4
Join y
(defrule show-act
a 1 b 1 2 c 2 (a ?x)
(b ?x ?y)
+ a 2 b 2 3 c 3 (c ?y ?z)
=> …
Changes to conflict set
Activity of the RETE Match during a Deletion
- a 2
a c select
b
a 1 b 1 2 c 3 Resulting relations
- a 2 b 2 3 c 2 (alpha memories)
b 2 4
Join x
a 1 b 1 2
Intermediate partial result
(beta memories)
- a 2 b 2 3
- a 2 b 2 4
Join y
(defrule show-act
a 1 b 1 2 c 2 (a ?x)
(b ?x ?y)
- a 2 b 2 3 c 3 (c ?y ?z)
=> …
Changes to conflict set
Given the facts and Rules Rules:
form the RETE graph
Facts:
PROs
Sufficient state is maintained so that the comparison of 2 WMEs on two
different cycles is minimized
Similar rules will compile to similar networks
CONs
Every time a WME is removed, the same sequence that occurred during
addition must be performed and the stored state unwound
A beta-memory may contain the full Cartesian product of its two-input
memories – size combinatorially explosive
To maintain beta-memories, the join operation must be performed in a fixed
order, determined statically at compile time
Takes knowledge from experts and inputs it into the expert system
Usually choose which expert system shell to use
Responsible for entering meta-rules
If…Then Rules
Forward chaining
Backward chaining
Rule Based System (RBS)
RETE graph
Examples for RETE graph
Next lecture
Game Planning
******