Rulebased Systems Mechanisms Efficiency Artificial Intelligence CMSC 25000
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 R 1: if (parent ? x ? y) (parent ? y ? z) then (grandparent ? x ? z) R 2: if (father ? v 1 ? w 1) then (parent ? v 1 ? w 1) R 3: if (mother ? v 2 ? w 2) then (parent ? v 2 ? w 2) Hypothesis: (grandparent A ? m) (grandparent ? x ? z) ? x: A ? z: ? m ? y: ? ? (parent ? x ? y) ? x: A ? z: ? m ? y: ? w 1: B (parent ? y ? z) (parent ? v 1 ? w 1) (parent ? v 2 ? w 2) ? x: A: ? v 1 ? z: ? m: ? w 2 ? y: ? w 1: ? v 2: B ? y: ? ? : ? w 1 Assertions: (father ? v 1 ? w 1) (mother ? v 2 ? w 2) ? x: A: ? v 1 ? z: ? m: ? w 2: C (father A B) ? x: A: ? v 1 ? z: ? m (father A B) ? y: w 1: ? v 2: B (mother B C) ? y: ? w 1: 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): Problem!!! • Just an artefact, though, so • Without loss of generality, rename variables – if (parent ? x 1 ? y 1) • (parent ? y 1 ? z 1) – then (grandparent ? x 1 ? z 1)
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
Forward Chaining with variables IF (parent ? x ? y) (parent ? y ? z) THEN (grand-parent ? x ? z) (parent A A B C B) C) D) E) (parent ? x ? y) (parent ? y ? z) (grand-parent ? x ? z) (x: A y: B) (y: A z: C) (y: B z: D) (y: C z: E) conflict (x: A y: B conflict in y z: D) in y (x: A y: C) (y: A z: B) (y: A z: C) (y: B z: D) (y: C z: E) conflict (x: A y: C in y z: E) (x: B y: D) (y: A (y: B (y: C z: B) z: C) z: D) z: E) conflict in in y y (x: C y: E) (y: A (y: B (y: C z: B) z: C) z: D) z: E) conflict in in y y
Rule System as Relational DB • Working memory: relations in database table • Rules and rule interpretations as relational database operations R 1: 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 Table Columns: Fields (Header: field name); Rows: records: assertions Relation: 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 (parent ? x ? y) => Select data with First = parent
Projecting Bindings A 1 = Project result over Second and Third
Projecting Bindings A 2 = Project [Select Data with First=parent] over Second and Third
Joining for Consistency Join tables for consistency: B 1= Join A 1 and A 2 with Y=Y
Produce Consequent X A A 3 =Project [Select Data with First=Is and Third=Female] over Second B 2= Project [Join B 1 and A 3 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 Rete Assertions: First 2 nd 3 rd Parent A B A 1 X A Y B B 1 Select new with First=Parent Select new with First=is and 3 rd=female A 2 Y A Z B B 2 A 3 X A
Compiled Out Rete Assertions: First 2 nd 3 rd Parent A C A 1 X A A Y B C B 1 Select new with First=Parent Select new with First=is and 3 rd=female A 2 Y A A Z B C B 2 A 3 X A
Compiled Out Rete Assertions: First 2 nd 3 rd Parent B D A 1 Select new with First=Parent Select new with First=is and 3 rd=female X Y A B A C B D B 1 X A Y B A 2 Z D Y Z A B A C B D B 2 A 3 X A Z D X A 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
- Slides: 23