Rete 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Knowledge-Based Systems

Lecture 4

Symbol vs Knowledge Level


Symbol level:
concerned with manipulating (patterns of) symbols. not concerned with meaning of symbols in external world.

Production Rules & Chaining


Knowledge level:

1.4

Production rules Forward, Backward Chaining RETE algorithm


Book:
copyright 2000 Vrije Universiteit, sectie Kunstmatige Intelligentie slides written by Frans Voorbraak, based on slides originally written by Frank van Harmelen and adjusted by Joeri Engelfriet and Niek Wijngaards
1

concerned with particular tasks and/or goals. concerned with the meaning of the symbols. What knowledge is needed for the task? How can the knowledge be used?

NB. Some forms of representation, such as


(production) rules, are possible at both levels.
2

Symbol vs Knowledge Level (2)


How to choose a representation? Symbol level:
machine-friendly? easily implemented or not? computational efficiency; complexity

What is a (Production) Rule?


IF A1 & & An THEN B

Ai:
conditions (situation, antecedent, LHS) match against database (working memory) obtain from user, or derive from other rules

Knowledge level:
expert-friendly? user-friendly? is it easy to analyse or formalise?

B:
conclusion (action, consequent, RHS) add to working memory more general: an action, e.g. removing something from working memory (situation-action rule)
3 4

Independence: choice at knowledge level does not


dictate choice at symbol level, or vice versa.

What is a Rule? (2)



IF A1 & & An THEN B A1,, An, B often assumed to be atomic, or literal. Conclusion B:
in principle no conjunction is needed
IF THEN B1 & B2 becomes IF THEN B1 + IF THEN B2

Knowledge: Different Types


Heuristic association Causal relation Situation + action
(default rule)
IF address = hospital THEN health = bad IF fever THEN flu

(oorzaak-gevolg)

usually no disjunction allowed

IF disease-type = infection THEN temp = high IF flu THEN fever

Condition A1 & & An


in principle no disjunction needed
IF A1 A2 THEN B becomes IF A1 THEN B + IF A2 THEN B

(handeling)

Negation is complicated (CWA, NAF, De Morgan) Negation as failure: if not KB | A, then KB | A.


5

IF disease-type = infection THEN prescribe(antibiotics) IF visible(object) & in_path(object), THEN avoid(object)

Logical relation
IF temp > 37 THEN fever

(by definition)
6

Knowledge: Different Types (2)


Model knowledge type 1 knowledge type n Representation rules

Rules are not Logic


R = A1 An B (1) Logic is more general Both rules and logic allow the derivation of B from (Modus Ponens) R and A1 An Logic also allows the derivation of (A1 An) from R and B (Modus Tollens) A2 An B from R and A1 (Resolution) A3 An B from R and A1 A2 (Resolution) A1 An C from R and B C (Resolution) Price to pay for logics generality: inefficiency
7 8

Advantages
many applications using the same tools flexibility

Disadvantage
source of confusion: e.g. exception handling, abduction, left-to-right ...

PROLOG
Resolution: A V B , A
B

Rules are not Logic (2)


(2) Rules are (possibly) non-monotonic IF diseasek THEN drug1 IF diseasek & pregnant THEN drug2 Monotonicity of L: if S |L p and S S, then S | L p (3) Rules are (possibly) uncertain IF pain-in-chest THEN heart-attack (0.6) pain-in-chest (0.9) heart-attack (?0.54 = 0.6*0.9)
9 10

Unification: find substitution for resolution

PROLOG (PROgramming in LOGic) counters


inefficiency of general application of resolution by:

restricting the language: only allow Horn clauses.


Horn clause: disjunction of negated atomic formula and at most one non-negated atomic formula (equivalent to rule where conclusion and all conditions are atomic) A1 An B = = (A1 An ) B A1 An B

restricting the reasoning: answer queries, goal-driven

Production System Cycle


repeat until stop condition: - conflict set empty - goal elements in WM Match
{ Conflict set }

Using Rules
Matching of patterns
check whether a rule is applicable forward reasoning: when its conditions are satisfied backward reasoning: when its conclusion is a (sub)goal

Execution (firing, application) of rules


(select a rule if more than one is applicable)
results in new situation forward reasoning: add conclusion backward reasoning: add conditions as new (sub)goals

Conflict Resolution
Rule

Execution

Repeating both activities results in chaining of rules


11 12

Forward & Backward Chaining


Forward Chaining
start from the data (data-driven, bottom-up) apply rules with true condition conclusion is used for condition of another rule
A + IF A THEN B + IF B THEN C B

Example: Rules
1 IF Shape=long & Color=(green or yellow) THEN Fruit=banana 2 IF Shape=(round or oblong) & Diam>4 THEN Fruitclass=vine 3 IF Shape=round & Diam<4 THEN Fruitclass=tree 4 IF Seedcount=1 THEN Seedclass=stonefruit 5 IF Seedcount>1 THEN Seedclass=multiple 6 IF Fruitclass=vine & Color=green THEN Fruit=watermelon 7 IF Fruitclass=vine & Surface=smooth & Color=yellow THEN Fruit=honeydew 8 IF Fruitclass=vine & Surface=rough & Color=tan THEN Fruit=cantaloupe 9 IF Fruitclass=tree & Color=orange & Seedclass=stonefruit & Diam 3 THEN Fruit=peach 10 IF Fruitclass=tree & Color=orange & Seedclass=multiple THEN Fruit=orange 11 IF Fruitclass=tree & Color=red & Seedclass=stonefruit THEN Fruit=cherry 12 IF Fruitclass=tree & Color=orange & Seedclass=stonefruit & Diam < 3 THEN Fruit=abricot 13 IF Fruitclass=tree & Color=(red or yellow or green) & Seedclass=multiple THEN Fruit=apple 14 IF Fruitclass=tree & Color=purple & Seedclass=stonefruit THEN Fruit=plum

Backward Chaining

start from the condition to test (goal-driven, top-down) apply rules with usable conclusion condition of applied rule is used for selecting another rule
IF A THEN B + IF B THEN C + C? B? A?

13

14

Forward Reasoning Example


Facts:
Diameter = 1.5 Shape = round Color = orange Surface = smooth Seedcount = 10 3 5 10 rule fired (Fruitclass = tree) (Seedclass = multiple) (Fruit = orange) (conclusion)]
15

Backward Reasoning Example


Goal: Fruit = X {1,6,7,8,9,10,11,12,13,14} 1 (Fruit = banana?):
Shape = long? fail

9 (Fruit = apricot?):
9a: Fruitclass = tree? 3
3a: Shape = round? ok 3b: Diameter < 4? ok

6 (Fruit = watermelon?):
Fruitclass = vine? 2
2a: Shape = round? ok 2b: Diameter > 4? fail

9b: Color = orange? ok 9c: Seedclass = stonefruit?


4: Seedcount = 1? fail

Reasoning:
{3, 5} {5} {10} [{applicable rules}

10 (Fruit = orange?):
10a: Fruitclass = tree ok 10b: Color = orange? ok 10c: Seedclass = multiple?
5: Seedcount > 1? ok

7 (Fruit = honeydew?):
Fruitclass = vine? fail

8 (Fruit = cantaloupe?):
Fruitclass = vine? fail

Conclusion: Fruit = orange


16

Matching
Fact and (simple) condition match if
condition identical to fact, or condition and fact can be unified by substituting variables Example: IF bird(X) THEN fly(X) bird(Tweety) {X=Tweety}. fly(Tweety) Substitute Tweety for X, so bird(X) matches bird(Tweety),

Matching (2)
Matching complex conditions with connectives:
IF A & B THEN C applicable if A and B match with facts IF A B THEN C applicable if A or B or both match facts

Complication: different negations


A: NOT A: A not true, so A is a fact (e.g. boy not girl; green not red) A is not known, A cannot be found in working memory (Negation as Failure) (e.g. not guilty until proven; not paid )

Prolog uses resolution and unification (MGU).


bird(X) fly(X)
bird(Tweety) fly(Tweety)
{X=Tweety}.
17

Treating NOT A as meaning A is a kind of


`closed world assumption.
18

Execution
Forward
Add (substitution instances of) conclusions of applied rules as new facts. Perform the actions of applied rules. Stop if no new rule applicable.

Conflict-Resolution
Examples of conflict-resolution strategies select first applicable rule prefer rules with most conditions (most specific) prefer rules which match most recently added facts select rule with highest priority select rule with highest certainty value reject rules with certainty below some threshold or combinations
19 20

Backward
Replace goal by conditions of applied rule: new (sub)goals. Remove goals matching with facts. Backtrack if some goal cannot be replaced/removed. Stop if no goal is left.

Effects of Conflict-Resolution
Some conflict-resolution strategies only influence
the order in which rules are executed and conclusions are derived.
example: preference for most specific rules

Some Variations
(not used very often)

Activate more than 1 rule at the same time


(i.e. conflict resolution does not give exactly 1 rule)

Some conflict-resolution strategies (also) prevent


the execution of some rules and the derivation of some conclusions.
example: rejecting too uncertain rules

Change not only the set of facts


but also the set of rules (updates)

Put conflict-resolution into a separate rule-system


(= meta-rules)
21 22

Forward vs. Backward Chaining


Forward:
allows many possible conclusions can work with little initial data, but this reduces the chance of finding a useful (goal) conclusion unfocussed OR

Rules as an AND/OR Graph


AND )
IF Shape = long & Color = (green or yellow) THEN Fruit = banana IF Shape = (round or oblong) & Diam > 4 THEN Fruitclass = vine IF Fruitclass = vine & Color = green THEN Fruit = watermelon

Shape=long Shape=round Shape=oblong

Backward:
only feasible for a few possible conclusions to reach the goal, typically much data needs to be available (by searching the systems database or questioning the user) focussed
23

Fruit=banana Fruitclass=vine

Diam > 4 Color=green Color=yellow Fruit=watermelon


24

Forward vs Backward (2)


Shape of graph can give some indication:
data goals

Examples Forward / Backward


Forward: real-time process control (Monitoring)
detect possible problems on the basis of incoming observations (measurements)

Forward:

Backward: Classification
determine type/class on basis of properties

FIRST forward, THEN backward: Diagnosis


data goals

Backward:
25

FIRST: generate hypotheses on basis of available data THEN: test hypotheses on basis of acquiring data

26

Last week: Diagnosis as search


Data space
data abstraction

Problems with Rules


Cyclic rules
IF A THEN A { IF A THEN B, IF B THEN C, IF C THEN A }

generate hypothesis Hypothesis space

test hypothesis

Inconsistent rules
{ IF A THEN B, IF B THEN C, IF A & D THEN C }

hypothesis refinement

Unreachable conditions
Repair space
repair + costs
27

Unusable conclusions
28

Problems with Rules (2)


Not always easy to modify:
IF A1 THEN B1 IF A2 THEN B2 IF An THEN Bn adding (general) condition C requires changing all rules: IF A1 & C THEN B1 e.g. not abnormal IF A2 & C THEN B2 IF An & C THEN Bn

Advantages of Production Rules


flexible, expressive To some degree modular:
adding new knowledge by adding new rules

Easy to understand (transparent)


for experts for users: allows explanation (How?; Why?) for knowledge engineers

Well-understood computational paradigm


relatively long history reasonably successful several tools/programs available.
30

If I put sugar in my coffee, the coffee will taste sweet, ... provided there is no diesel in my coffee
29

Explanation Generation
U: facts: {Color=orange, Shape = round, Diam = 1.5, seedcount =10 } goal: fruit =X ? S: Fruit = Orange. U: How come? (How questions produce backwards trace.) S: by rule 10 Fruit = Orange, if Fruitclass = tree & Color=orange & seedclass=multiple, and by rule 3 Fruitclass = tree, if Shape = round and Diam < 4, and by rule 5 Seedclass = multiple, if Seedcount > 1, and Color=orange, Shape = round, Diam = 1.5, seedcount=10 are given. U: S: U: S: What kind of fruit is this? (goal: fruit = X ?) Is the shape of the fruit round? (question: Shape=round ?) Why do you ask? (Why questions produce forwards trace.) Because by rule 3 if its shape is round and its diameter is smaller then 4, then it grows on a tree, and by rule 10 if it grows on a tree and its color is orange and it produces many seeds, then it is an Orange. (linguistic output by NL generator) So, is the shape of the fruit round?
31

RETE Matching Algorithm


RETE: fast pattern matching algorithm (Forgy, 1982) applied in OPS5, CLIPS, Jess, ...
LHS RHS REPEAT 1. Match: matching LHSs in WM (conflict set) 2. Conflict Resolution: select rule with `best LHS 3. Execute: do action in RHS

Problem: many items in WM & many LHSs in rules SO: avoid iterating over WM How?
structural overlap: often the same pattern re-occurs in LHS of many rules temporal redundancy: firing a rule only changes few facts, and few rules are affected by those changes.

> represent this structure and these dependencies!


32

RETE: store in network


for each LHS pattern, store matching WM-elements update network, whenever WM changes
Changes to WM RETE Changes to Conflict Set

RETE: Example
FACTS (has-goal e1 simplify) (expression e1 0 + 3) (has-goal e2 simplify) (expression e2 0 + 5) (has-goal e3 simplify) (expression e3 0 * 2) RULES (R1 (has-goal ?x simplify) (expression ?x 0 + ?y) ==>....) (R2 (has-goal ?x simplify) (expression ?x 0 * ?y) ==>....)

nodes: process input tokens, output to other nodes


root node one-input node (alpha): for each kind of pattern in WM select values of variable in pattern (alpha-memory) two-input node (beta): for combining patterns join left, right input (beta-memory) terminal node: for each rule fire rule
33

3 one-input nodes
has-goal, expression *, expression +

2 two-input nodes
R1 R2
34

RETE: Example
Root

RETE: evaluation
Nave: match all LHSs against all facts in WM Time Complexity: O(R FP )
R = nr rules, F = nr facts in WM, P = av nr patterns per LHS

X e1 e2 e3

Has-Goal

Expression *

Y 2

Expression +

RETE: store intermediate results in network


Y 3 5

Time Complexity: O(R F P ) (linear in F) But ... High memory space requirements

R2 X Y e3 2

X Y e1 3 e2 5

R1

35

36

Next Lecture
Reasoning with uncertainty Reasoning with vagueness

Book:

6.1.1, 6.1.2 6.2.1

37

You might also like