0% found this document useful (0 votes)
7 views

Week4 Lect1 RBReasoning Pattern Final'

This document discusses pattern matching in rule-based systems. It explains that pattern matching is used to match rules to facts in working memory. Variables in rules can be bound to values from facts during pattern matching. Different types of variables like single-field, multi-field, wildcards are described along with examples of how they match facts.

Uploaded by

Mena Safwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Week4 Lect1 RBReasoning Pattern Final'

This document discusses pattern matching in rule-based systems. It explains that pattern matching is used to match rules to facts in working memory. Variables in rules can be bound to values from facts during pattern matching. Different types of variables like single-field, multi-field, wildcards are described along with examples of how they match facts.

Uploaded by

Mena Safwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

RBS

INFERENCE
WORKING ENGINE
MEMORY

PATTERN EXECUTION
MATCHER ENGINE
RULE
BASE
AGENDA

(C) Copyright 2022 by Prof.Abeer ElKorany 2


Inference with Production
Rules
❖ There are two main issues involved in the
implementation of a rule-based system
❖ How is conflict resolution implemented?
❖ Various strategies
❖ How are facts matched to rules?
❖ Pattern matching required
❖ And many more issues need to be resolved

(C) Copyright 2022 by Prof.Abeer ElKorany 3


Inference Mechanisms
❖ Pattern matching and unification are powerful
operations to determine the similarity and
consistency of complex structures
❖ They are at the core of many rule-based and
predicate logic mechanisms
❖ Their application goes beyond rule-based systems

(C) Copyright 2022 by Prof.Abeer ElKorany 4


Variables and bindings
❖ Antecedents and consequents contain variables (?x)

❖ Variables acquire values during the matching


process

(C) Copyright 2022 by Prof.Abeer ElKorany 5


Bindings
❖ Once a variable is bound, that variable is replaced
by its binding wherever it appears in the same or
subsequently processed patterns

❖ Whenever the variables in a pattern are replaced by


their bindings, the pattern is said to be instantiated

(C) Copyright 2022 by Prof.Abeer ElKorany 6


Pattern Matching Example
❖ shapes

?????

?????

(C) Copyright 2022 by Prof.Abeer ElKorany 7


Pattern Matching Examples
❖ constants and variables

“Hans” “Franz”

“Josef” “Joseph”

?first_name “Joseph”

?last_name “Joseph”

(C) Copyright 2022 by Prof.Abeer ElKorany 8


Pattern Matching Examples
❖ terms
❖ composed of constants, variables, functions

father(X) “Joseph”

father(Y)
father(X)

mother(X)
father(X)

grandfather(X)
father(father(X)) ??

(C) Copyright 2022 by Prof.Abeer ElKorany 9


Applications of Pattern Matching
search and retrieval
❖ Precise matching
❖ strings, images, documents, objects
❖ the query and the result have to match perfectly
❖ the two should be identical
❖ all requirements specified in the query have to be met
by the result

❖ Similarity-based matching
❖ imprecise

(C) Copyright 2022 by Prof.Abeer ElKorany 10


Pattern Matching in Rule-
Based Systems
❖ Matching is the process of identifying which
combinations of facts in the WM satisfy which rules

❖ Used to match rules with appropriate facts in


working memory
❖ rules for which facts can be found are satisfied
❖ the combination of a rule with the facts that satisfy it is
used to form activation records
❖ one of the activation records is selected for execution
❖ the respective rule “fires”

(C) Copyright 2022 by Prof.Abeer ElKorany 11


Search and Retrieval(cont.)
❖ similarity-based matching
❖ requires specification of a similarity measure
❖ often domain/application/task-specific
❖ some aspects of the result are different from the query
specification
❖ but not too different

❖ partial match
❖ not all requirements specified in the query are met

(C) Copyright 2022 by Prof.Abeer ElKorany 12


Expert System Design
Pattern Matching
? ❖ single field wildcard
❖ matches anything in corresponding field of fact

❖ multi-field wildcard
$?
❖ matches zero or more fields of a fact

(C) Copyright 2022 by Prof.Abeer 13


ElKorany
Pattern Matching (cont.)

?<var>
❖ single field variable
❖ <var> is some word
❖ this symbol matches anything in the corresponding
field of a fact
❖ value matched is bound to ?<var> for scope of rule
❖ examples: ?cat ?color ?machine

$?<var>
❖ multi-field variable
❖ matches zero or more fields of a fact
❖ the value(s) of the matched fields is bound to $?<var> for
the scope of the rule

(C) Copyright 2022 by Prof.Abeer 14


ElKorany
Examples
Single field wild cards

LHS Condition Fact in Fact Base Match?


(? ?) (data red)
Yes
(data ?) (data red)
Yes
(data ?) (data red green)
NO!
(data ? ?) (data red green)
Yes
(data red ?) (data red green)
Yes
(data ? green) (data green)
NO!

(C) Copyright 2022 by Prof.Abeer 15


ElKorany
Examples
Single Field Variables

LHS Condition Fact in Fact Base Match?


(data red ?x) (data red green) ?x=green
(data red ?x) (data red “green”) ?x=“green”
(data red ?x) (data red 17.4) ?x=17.4
(data ?x ?x) (data red red) ?x=red
(data ?x ?x) (data red green) NO!
(data ?x ?y) (data red green) ?x=red
?y=green
(data ?x ?y) (data red red) ?x=red
?y=red

(C) Copyright 2022 by Prof.Abeer 16


ElKorany
Examples
Multi-field wild cards

LHS Condition Fact in Fact Base Match?


($?) (data red)
Yes
(data $?) (data red)
Yes
(data red $?) (data red)
Yes
(data $?) (data red green)
Yes
(data red green $?) (data red green)
Yes
($? green) (data red green)
Yes
($? red $?) (data red green)
Yes
(data red $?) (data green red)
NO!
(data $? red $?) (data green red)
Yes
($? $?) (data red)
Yes

(C) Copyright 2022 by Prof.Abeer 17


ElKorany
Examples
Multi-Field Variables

LHS Condition Fact in Fact Base Match?


(data red $?x) (data red) $?x=()
(data red $?x) (data red green) $?x=(green)
(data red $?x) (data red one two) $?x=(one two)

(data $?x $?x) (data red red) $?x=(red)


(data $?x $?y) (data red green) Multiple
matches:

$?x=() $?y=(red green)


$?x=(red) $?y=(green)
$?x=(red green) $?y=()

(C) Copyright 2022 by Prof.Abeer 18


ElKorany
Rule-Based
Pattern Matching
❖ Go through the list of rules, and check the
antecedent (LHS) of each rule against the facts in
working memory
❖ create an activation record for each rule with a
matching set of facts
❖ repeat after each rule firing

❖ Very inefficient
❖ roughly (number of rules) * (number of facts)
❖ the actual performance depends on the formulation
of the rules and the contents of the working memory

(C) Copyright 2022 by Prof.Abeer ElKorany 19


Disadvantages of Rule Systems
❖ Require exact matching
IF The motor is hot
THEN Shut the motor down
What about
❖ The motor is running hot.
❖ The motor's temperature is hot.

❖ Have opaque rule relationship(difficult to


debug for a large rule base)
❖ IF C THEN D
❖ IF B THEN C
❖ IF A THEN B

❖ Can be slow (May need to scan the whole rule


set several times)

(C) Copyright 2022 by Prof.Abeer


ElKorany 20
Problems with Inference cycle
1. Complete scan of the working memory (facts list)
during each cycle of execution

2. More rules will result in more rules that apply and more
facts being derived that both apply and do not apply

❖ Suppose you have the following situation:


❖ – r rules in KB with average of p conditions
❖ – f facts in the fact base

❖ – Then our inference engine will have to perform


roughly r*f p comparisons each cycle

(C) Copyright 2022 by Prof.Abeer ElKorany 21


Match step

❖ Disadvantage of RBS – large computational


requirement to perform 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

(C) Copyright 2022 by Prof.Abeer ElKorany 22


Rule example
(deftemplate student (deffacts start
(slot name) (student (name Mary) (gender F))
(student (name Peter) (gender M))
(slot gender) (special_consideration yes))
(slot placed_in (default nil)) (room (number 221) (capacity 1) (vacancies 1))
(slot special_considerations (room (number 346) (capacity 2) (vacancies 1)))
(default no))

(deftemplate room
(slot number) WMEs
41 (student name Mary gender F placed_in nil
(slot capacity (type INTEGER)(default 4)) special_consideration no)
52 (student name Peter gender M placed_in nil
(slot genderes_are) special_consideration yes)
9 (room number 221 capacity 1 vacancies 1)
(slot vacancies (type INTEGER)) 12 (room number 346 capacity 2 vacancies 1)
(multislot occupants))

(C) Copyright 2022 by Prof.Abeer ElKorany 23


Rule example
(defrule assign-student-empty-room
?unplaced_student  (student (name ?stud_name)
(placed_in nil)
(gender ?gen))
?empty_room  (room (number ?room_no)
(capacity ?room_cap)
(vacancies ?free_places))
(test (= ?room_cap ?free_places))
=>
(modify ?unplaced_student (placed_in ?room_no))
(modify ?empty_room (occupants ?stud_name) (gender_are ?gen)
(vacancies (-- ?free_places))))

srxyzt name(s,x)  placed_in(s,nil)  gender(s,y) 


number(r,z)  capacity(r,t)  vacancies(r,t) →
placed_in(s,z)  ….
(C) Copyright 2022 by Prof.Abeer ElKorany 24
Making it more efficient
❖ The Rete algorithm is an efficient pattern matching
algorithm for implementing rule-based expert systems.
❖ The Rete algorithm was designed by Dr. Charles L. Forgy
of Carnegie Mellon University in 1979.
❖ the name comes from the latin word rete
❖ stands for net

❖ Rete has become the basis for many popular expert


systems, including OPS5, CLIPS, and JESS.
❖ The Rete algorithm was the first efficient solution to the
facts-rules pattern matching problem
❖ It stores information about matches in a network structure

(C) Copyright 2022 by Prof.Abeer ElKorany 25


Rete Algorithm
❖ Rete looks for changes to match in each cycle
❖ Focus on few facts that 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.
❖ Unnecessary Computations Facts Change (Keep track
of changes) Rules remain unchanged
❖ the Rete algorithm performs an improved matching of
rules and facts
❖ basis for many rule-based expert system shells

(C) Copyright 2022 by Prof.Abeer ElKorany 26


The Rete Matching Algorithm
❖ Nodes of the network correspond to individual
condition elements
❖ Conditions and conjunctions of conditions

❖ Each node has two sets associated with it


❖ The first set contains all the working memory
elements that the condition node matches
❖ The second set contains combinations of working
memory elements and the bindings which produce
a consistent match of the conditions that chain up
to the node condition

(C) Copyright 2022 by Prof.Abeer ElKorany 27


RETE algorithm
❖ Consists of a network of interconnected nodes
❖ Creates a decision tree where each node corresponds to a
pattern occurring at the left-hand side of a rule
❖ Each node has a memory of facts that satisfy the pattern
• input nodes are at the top, output nodes at the bottom
▪ join nodes have two inputs, and combine facts
▪ terminal node at the bottom of the network represent
individual rules
❖ a rule is satisfied if there is a combination of facts that
passes all the test nodes from root to a leaf.

(C) Copyright 2022 by Prof.Abeer ElKorany 28


The Rete Matching
Algorithm
❖ With this configuration repetitive testing of all
rule conditions in each cycle is avoided
❖ Only the nodes affected by a newly inserted or
modified fact are checked
❖ For example, consider the rules
❖ IF a(X,1) and b(X,Z) THEN g1(X,Z)
IF a(X,2) and b(X,Z) THEN g2(X,Z)

(C) Copyright 2022 by Prof.Abeer ElKorany 29


The Rete Matching Algorithm

Initially the working memory


is empty start

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

- There is a starting node


and a node for 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

(C) Copyright 2022 by Prof.Abeer ElKorany 30


The Rete Matching
Algorithm
Initially the working memory
is empty start

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

Y=1 Y=2
- There is a starting node
and a node for 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

(C) Copyright 2022 by Prof.Abeer ElKorany 31


The Rete Matching Algorithm

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

(C) Copyright 2022 by Prof.Abeer ElKorany 32


The Rete Matching Algorithm

Fact a(3,1)
Fact b(3,4) is now added to start
the working memory

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

(C) Copyright 2022 by Prof.Abeer ElKorany 33


The Rete Matching Algorithm
Fact a(3,1)
Fact b(3,4)
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

(C) Copyright 2022 by Prof.Abeer ElKorany 34


Rete example R1: IF x & y THEN p
R2: IF x & y & z THEN q

Pattern x? y? z?
x? y?
Network

Join Network

8 nodes
q

(C) Copyright 2022 by Prof.Abeer ElKorany 35


R1: IF x & y THEN p
Rete example R2: IF x & y & z THEN q

Pattern x? y? z?
Network

R1
Join Network

p R2

6 nodes
q

(C) Copyright 2022 by Prof.Abeer ElKorany 36


Rete example R1: IF x & y THEN p
R2: IF x & y & z THEN q

Pattern x? y? z?
Network

R1
Join Network

p R2

5 nodes
q

(C) Copyright 2022 by Prof.Abeer ElKorany 37


(defrule job-size
(message (jobs ?x) (size ?y) (status new))
(job-def (size ?y) (size-def medium))
=>
RETE match Network
(assert (job (job-name ?x) (job-size ?y))))
for the rule
Distribute WM changes

message job-def

(status new) (size-def medium)

Alpha memories
Memory support

?y
Test for consistent sizes

Beta memory
Condition relationship

Changes
(C) Copyright 2022 by Prof.Abeer ElKorany to conflict set 38
(defrule show-act WM
(a ?x) (a 1) Initial state of
(b ?x ?y) (b 1 2) (b 2 3) (b 2 4)
(c ?y ) (c 3) (c 2) RETE Network
=> …
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

(C) Copyright 2022 by Prof.Abeer ElKoranyChanges to conflict set 39


Activity of the RETE Match during an Addition
WM + a 2
(a 1)
(b 1 2) (b 2 3) (b 2 4)
(c 3) (c 2) a c select
b
(a 2)
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
=> …
Changes
(C) Copyright 2022 by Prof.Abeer ElKorany to conflict set 40
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 )
=> …
Changes
(C) Copyright 2022 by Prof.Abeer ElKorany to conflict set 41
Assert and Retract with
Rete
❖ asserting additional facts imposes some more constraints on
the network

❖ retracting facts indicates that some previously computed


activation records are not valid anymore, and should be
discarded

❖ in addition to the actual facts, tags are sent through the


networks
❖ ADD to add facts (i.e. for assert)
❖ REMOVE to remove facts (i.e. for retract)
❖ CLEAR to flush the network memories (i.e. for reset)
❖ UPDATE to populate the join nodes of newly added rules
❖ already existing join nodes can neglect these tokens

(C) Copyright 2022 by Prof.Abeer ElKorany 42


Further Optimizations
❖ sophisticated data structures to optimize the
network
❖ hash table to presort the tokens before running the
join node tests

❖ fine-tuning via parameters


❖ frequently trade-off between memory usage and time

(C) Copyright 2022 by Prof.Abeer ElKorany


43
Expert System Design
Further Optimizations
1 IF Shape=long Example: 9 IF Fruitclass=tree & Color=orange
& Seedclass=stonefruit & Diam>3
& Color=(green or yellow)
THEN Fruit=banana THEN Fruit=peach
2 IF Shape=(round or oblong) 10 IF Fruitclass=tree & Color=orange
& Diam>4 & Seedclass=multiple
THEN Fruitclass=vine THEN Fruit=orange
3 IF Shape=round & Diam<4 11 IF Fruitclass=tree & Color=red
THEN Fruitclass=tree & Seedclass=stonefruit
4 IF Seedcount=1 THEN Fruit=cherry
THEN Seedclass=stonefruit 12 IF Fruitclass=tree & Color=orange
5 IF Seedcount>1 & Seedclass=stonefruit & Diam < 3
THEN Seedclass=multiple THEN Fruit=abricot
6 IF Fruitclass=vine & Color=green 13 IF Fruitclass=tree
THEN Fruit=watermelon & Color=(red or yellow or green)
7 IF Fruitclass=vine & Seedclass=multiple
& Surface=smooth & Color=yellow THEN Fruit=apple
THEN Fruit=honeydew 14 IF Fruitclass=tree & Color=purple
8 IF Fruitclass=vine & & Seedclass=stonefruit
Surface=rough THEN Fruit=plum
& Color=tan THEN Fruit=cantaloupe
(C) Copyright 2022 by Prof.Abeer ElKorany 44
Fruit Knowledge Base Example
Shape = long Fruit = banana
= round
= oblong

Diameter > 4”
Fruit = watermelon
< 4” Fruitclass = vine = honeydew
= cantalope
Surface = smooth
= rough
= apple
= apricot
Color = green
= cherry
= yellow
= peach
= tan
= plum
= orange
= orange
= red
= purple Fruitclass = tree

Seedcount > 1 Seedclass = multiple


=1 = stonefruit

(C) Copyright 2022 by Prof.Abeer 45


ElKorany
Sample Facts
(deffact list
What is the result
(color is red) from execution?
(shape is round)
The value of fruitclass is tree
(= seedcount 1) The value of seedclass is stonefruit
The value of fruit is cherry
(< diameter 4)

))

(C) Copyright 2022 by Prof.Abeer ElKorany 46


Fruit Knowledge Base
Shape = long Fruit = banana
= round
= oblong

Diameter > 4”
Fruit = watermelon
< 4” Fruitclass = vine = honeydew
= cantalope
Surface = smooth
= rough
= apple
= apricot
Color = green
= cherry
= yellow
= peach
= tan
= plum
= orange
= orange
= red
= purple Fruitclass = tree

Seedcount > 1 Seedclass = multiple


=1 = stonefruit

(C) Copyright 2022 by Prof.Abeer 47


ElKorany
Fruit Knowledge Base
Shape = long Fruit = banana
= round
= oblong

Diameter > 4”
Fruit = watermelon
< 4” Fruitclass = vine = honeydew
= cantalope
Surface = smooth
= rough
= apple
= apricot
Color = green
= cherry
= yellow
= peach
= tan
= plum
= orange
= orange
= red
= purple Fruitclass = tree

Seedcount > 1 Seedclass = multiple


=1 = stonefruit

(C) Copyright 2022 by Prof.Abeer 48


ElKorany
Fruit Knowledge Base
Shape = long Fruit = banana
= round
= oblong

Diameter > 4”
Fruit = watermelon
< 4” Fruitclass = vine = honeydew
= cantalope
Surface = smooth
= rough
= apple
= apricot
Color = green
= cherry
= yellow
= peach
= tan
= plum
= orange
= orange
= red
= purple Fruitclass = tree

Seedcount > 1 Seedclass = multiple


=1 = stonefruit

(C) Copyright 2022 by Prof.Abeer 49


ElKorany
Designing efficient rule-based systems

❖ Knowledge Engineer
❖ Tasked with working with the expert to extract
expertise and codify in a set of rules
❖ Has training in the development of expert systems, but not
necessarily in the application domain
❖ Know the capabilities of technology and knows how to apply it

❖ The most specific pattern should be placed toward the


front of the left-hand side of the rule
❖ A specific pattern will generally have the smallest
number of matching facts in the facts list and will have
the largest number of variable bindings which constrain
other patterns.

(C) Copyright 2022 by Prof.Abeer ElKorany 50


Most specific pattern goes first

❖ R1: (item ?x) (item ?y) (item ?z) (match ?x ?y ?z) )-> (buy ?x)

❖ R2 : (match ?x ?y ?z) (item ?x) (item ?y) (item ?z) ) -> (buy ?x)

The most specific pattern should be placed toward the front of the
left-hand side of the rule

A specific pattern will generally have the smallest number of matching


facts in the facts list and will have the largest number of variable
bindings which constrain other patterns.

(C) Copyright 2022 by Prof.Abeer ElKorany 51


Control: Meta-Rules
❖ Meta-Rules
Use Meta-Rules to divide rules into
classes. Choose one class over another at
a given point.
This implements domain-dependent
knowledge about which set of rules to use
during reasoning.
CLIPS provides a Module-construct with
similar effects.

(C) Copyright 2022 by Prof.Abeer ElKorany 52


Categories of Rules
❖ Salience values are arbitrary; often what we want
is that a certain class of rules are considered
before others.

❖ This can be built into the rules themselves using


a kind of 'tag'

❖ Another rule can be implemented to change


status to the next group of rules.

(C) Copyright 2022 by Prof.Abeer ElKorany 53

You might also like