Upload
dwayne-patrick
View
220
Download
0
Embed Size (px)
Citation preview
Rule-based Systems:Mechanisms & Efficiency
Artificial Intelligence
CMSC 25000
January 10, 2002
Agenda
• Rule-based systems– Backward-chaining II:
• Variables, unification, and search
– RETE method:• Relational model: Databases
• Efficient rule-based reasoning
• Conclusions
Rule-based Systems
• Represent knowledge as:– Assertions: (mother-of ?x ?y), (female ?x)– Rules:
• If (parent-of ?x ?y) Antecedent – (female ?x)
• then (mother-of ?x) Consequent
• Manipulate knowledge with:– Rule interpreter:
• Forward-Chain: Rules & assertions => New assertions
• Backward-Chain: Hypothesis (consequent)=>Antecedents
Rule Manipulation w/Variables
• Forward-chaining:– Match antecedents with variables to assertions
WITHOUT variables– Match (bind) variables to constants
• Backward-chaining:– Match hypothesis to consequent
• May contain variables: e.g. (grandparent A ?x)
– Match variables to constants AND variables!– Requires general unification
Backward Chaining with Variables
R1: if (parent ?x ?y) (parent ?y ?z) then (grandparent ?x ?z)
R2: if (father ?v1 ?w1) then (parent ?v1 ?w1)
R3: if (mother ?v2 ?w2) then (parent ?v2 ?w2)
Assertions:(father A B) (mother B C)
Hypothesis: (grandparent A ?m)
(grandparent ?x ?z) ?x:A?z:?m
(parent ?x ?y) (parent ?y ?z)
(parent ?v1 ?w1) (parent ?v2 ?w2)
?x:A?z:?m?y:??
?x:A?z:?m?y:?w1:B
(father ?v1 ?w1)
?x:A:?v1 ?z:?m ?y:??:?w1
(mother ?v2 ?w2)
?x:A:?v1 ?z:?m:?w2 ?y:?w1:?v2:B
(father A B)?x:A:?v1 ?z:?m ?y:?w1:B (mother B C)
?x:A:?v1 ?z:?m:?w2:C ?y:w1:?v2:B
Unification: Variable Renaming
• Matching: (grandparent A ?m) (grandparent ?x ?z)– No problem: bind ?x=A, ?z=?m– If match hypothesis (grandparent A ?y)
• (grandparent ?x ?z) => (parent ?y ?y): Problem!!!
• Just an artefact, though, so
• Without loss of generality, rename variables– if (parent ?x1 ?y1)
• (parent ?y1 ?z1)
– then (grandparent ?x1 ?z1)
Unification: Occurs Check
• How do we unify:
(?z likes ?z) (?y likes (house-of ?x))
Need (?z = ?y) and (?z = (house-of ?x)),
Implies (?y = (house-of ?x)): FINE• Now, how do we unify:
(?z likes ?z) (?x likes (house-of ?x))• Need (?z = ?x) and (?z = (house-of ?x))
– Implies (?x = (house-of ?x)) : problematic.
• In unification, a variable cannot be assigned to an expression where that variable occurs (called the “occurs check”).
RETE Method
• Rule chaining problem: Efficiency!
• For each rule,– For each antecedent,
• For each assignment of variables to assertions
• Assert consequent
– E.g. for grandparent example,• k=2 antecedents, n=4 assertions: 16 steps
– Worst case: n^k!!!!
• Different representation & implementation
(parent ?x ?y) (parent ?y ?z) (grand-parent ?x ?z) (x:A y:B) (y:A z:B) conflict in y (y:A z:C) conflict in y (y:B z:D) (x:A y:B z:D) (y:C z:E) conflict in y
(x:A y:C) (y:A z:B) conflict in y (y:A z:C) conflict in y (y:B z:D) conflict in y (y:C z:E) (x:A y:C z:E)
(x:B y:D) (y:A z:B) conflict in y (y:A z:C) conflict in y (y:B z:D) conflict in y (y:C z:E) conflict in y
(x:C y:E) (y:A z:B) conflict in y (y:A z:C) conflict in y (y:B z:D) conflict in y (y:C z:E) conflict in y
Forward Chaining with variables IF (parent ?x ?y) (parent A B) (parent ?y ?z) (parent A C) THEN (grand-parent ?x ?z) (parent B D) (parent C E)
Rule System as Relational DB
• Working memory: relations in database table
• Rules and rule interpretations as relational database operations
R1: if (parent ?x ?y) (parent ?y ?z) (is ?x female) then (grandmother ?x ?z)
(parent A B)(parent A C)(parent B D)(parent C E)(is A female)
Working Memory as Database TableFirst Second Third
Parent A B
Parent A C
Parent B D
Parent C E
Is A female
Columns: Fields (Header: field name); Rows: records: assertionsRelation: Database Table “data”
Pattern Matching as Relational DB Ops
• Which rules triggered? – Which antecedents match? What bindings apply?
• “Select” matching assertions
• “Project” new bindings
• “Join” bindings across antecedents
Selecting Assertions
First Second Third
Parent A B
Parent A C
Parent B D
Parent C E
(parent ?x ?y) => Select data with First = parent
Projecting Bindings
X Y
A B
A C
B D
C E
A1 = Project result over Second and Third
Projecting Bindings
Y Z
A B
A C
B D
C E
A2 = Project [Select Data with First=parent] over Second and Third
Joining for Consistency
X Y Z
A B D
A C E
Join tables for consistency: B1= Join A1 and A2 with Y=Y
Produce Consequent
X ZA DA E
XA
A3 =Project [Select Data with First=Is and Third=Female] over Second
B2= Project [Join B1 and A3 with X = X] over X and Z
(Grandmother A D)(Grandmother A E)
Analysis
• Relational database representation performs forward-chaining– Finds all new assertions based on rules, assertions
• But, still expensive:– n antecedents =>– n selects +n projects + n-1 joins + n-1 projects– PER RULE! – Also Joins very expensive - others linear in table
Incremental Efficiency
• Solution: Compile out rules+interpreter into– RETE: Network to incrementally process each
assertion• At any time, operate on SUBSET of relations
• Steps:– Each assertion/relation passed thru Select filters– Project to alpha node = store in table– Joins represented by beta nodes
• Only small # of comparisons at each stage
Compiled Out ReteAssertions:
Parent A BFirst 2nd 3rd Select new with First=Parent
Select new with First=Parent
Select new with First=is and 3rd=female
A1 A2 A3X YA B
Y ZA B
XA
B1B2
Compiled Out ReteAssertions:
Parent A C First 2nd 3rd Select new with First=Parent
Select new with First=Parent
Select new with First=is and 3rd=female
A1 A2 A3X YA BA C
Y ZA BA C
XA
B1B2
Compiled Out ReteAssertions:
Parent B DFirst 2nd 3rd Select new with First=Parent
Select new with First=Parent
Select new with First=is and 3rd=female
A1 A2 A3X YA BA CB D
Y ZA BA CB D
XA
B1B2
X Y ZA B D
X ZA D ETC..
Summary
• Rule-based systems: Mechanisms
• Unification supports matching patterns to constants AND variables– Variable renaming preserves generality
• RETE method– Relational databases and operations– Incremental processing
• Efficient rule-based reasoning