Lecture-07
The inference engine can take two basic
approaches to search for an answer.
These are:
Forward chaining
Backward chaining
In forward chaining the inference engine
begins with some initial information and
work forward, attempting to match that
information with a rule. i.e.
 in forward chaining, the rule interpreter or
inference engine attempts to match a fact
or statement in the working memory to the
situation stated in the left-hand of IF part of
the rule.
 Once a fact in the working memory has been
matched to the IF part of the rule, the rule is
then fired.
 The action or consequent stated could
produce new knowledge or new fact that is
stored in the working memory.
 This new fact may then be used to search
out the next appropriate rule.
 This search and matching process continues
until a final conclusion rule is fired.
To understand the forward chaining
process let us consider an example.
The rule base (knowledge base) consisting
of seven rules and the working memory
contains the initially known facts F and L.
INITIAL CASE FACTS
F
L
INFERRED FACTS
IF K & L THEN J
IF F & J THEN M
IF F THEN I
IF L & I THEN N
IF N THEN G
IF M THEN G
IF G THEN H
 The Inference Engine starts with the first rule and
matches its premises with the initial case facts.
 There are two premises in the first rule i.e. K and L.
 The rule cannot be fired because L is available in the
working memory but K is not.
 For a rule to be fired, all of its premises must be
matched.
 The inference engine then takes another rule in the
sequence and matches its premises with the
available facts.
 Rule 2 also cannot be fired because F is
matched and J is not.
 Then it takes rule 3 and check its premises.
Rule 3 is fired, because there is only one
premise i.e. F, which is available in the
working memory.
 Therefore, the consequence is executed and I
is added as an inferred fact into the working
memory.
 Rule 4 is also fired because all its premises
are matched and N is added into the working
memory.
Rule 5 is then fired and G is added into
inferred facts.
Rule 6 cannot be fired, because its
premise is not available.
 The last rule is fired and H is added into
the working memory.
INITIAL CASE FACTS
F
L
INFERRED FACTS
1) IF K & L THEN J
2) IF F & J THEN M
3) IF F THEN I
4) IF L & I THEN N
5) IF N HEN G
6) IF M THEN G
7) IF G THEN H
I
G
N
H
F I
L
N G H
Rule 3
Rule 4
Rule 5 Rule 7
AND
1. Add facts to working memory (WM)
2. Take each rule in turn and check to see if
any of its premises match the facts in the
WM
3. When matches found for all premises of
a rule, place the conclusion of the rule in
WM.
4. Repeat this process until no more facts
can be added. Each repetition of the
process is called a pass.
 Let’s look at how a doctor goes about
diagnosing a patient.
 He asks the patient for symptoms and then
infers diagnosis from symptoms.
 Forward chaining is based on the same idea.
It is an “inference strategy that begins with a
set of known facts, derives new facts using
rules whose premises match the known facts,
and continues this process until no further
rules have premises that match the known or
derived facts.
Knowledge Base
Rule 1
IF The patient has deep cough
AND We suspect an infection
THEN The patient has Pneumonia
Rule 2
IF The patient’s temperature is above
100
THEN Patient has fever
Rule 3
IF The patient has been sick for over a
fortnight AND The patient has a fever
THEN We suspect an infection
Working Memory
Case facts
• Patients temperature= 103
• Patient has been sick for over a
month
• Patient has violent coughing fits
Rule, premise Status Working Memory
1, 1
Deep cough
True Temp= 103
Sick for a month
Coughing fits
1, 2
Suspect infection
Unknown Temp= 103
Sick for a month
Coughing fits
2, 1
Temperature>100
True, fire rule Temp= 103
Sick for a month
Coughing fits
Patient has fever
Rule, premise Status Working Memory
1, 1 Deep cough True Temp= 103
Sick for a month
Coughing fits
Patient has fever
1, 2
Suspect infection
Unknown Temp= 103
Sick for a month
Coughing fits
Patient has fever
3, 1
Sick for over fortnight
True Temp= 103
Sick for a month
Coughing fits
Patient has fever
3, 2
Patient has fever
True, fire Temp= 103
Sick for a month
Coughing fits
Patient has fever
Infection
Rule, premise Status Working Memory
1, 1
Deep cough
True Temp= 103
Sick for a month
Coughing fits
Patient has fever
Infection
1, 2
Suspect infection
True, fire Temp= 103
Sick for a month
Coughing fits
Patient has fever
Infection
Pneumonia
Now, no more facts can be added to the
WM. Diagnosis: Patient has Pneumonia.
 Another approach of inference engine is backward
chaining.
 Here the inference engine starts with a fact which is
called hypothesis.
 The inference engine then begins examining the right-
hand or THEN part of rules looking for a match. The
inference engine searches for evidence to support the
hypothesis originally stated.
 If a match is found, the working memory is updated,
recording the conditions or premises that the rule states
as necessary for supporting the matched conclusion.
The chaining process continues with
system repeatedly attempting to match the
right side of the rule against the current
system’s status.
The corresponding IF sides of the rules
matched are used to generate new
intermediate hypotheses or goal states
which are recorded in the working memory.
This backward chaining continues until the
hypothesis is proved.
Now using the same knowledge base and
working memory, we can illustrate
backward chaining.
F I
L
N G H
Rule 3
Rule 4
Rule 5 Rule 7
AND
 We begin by telling the system that we want to prove
H.
 The inference engine first looks in the working memory
on the off chance H is there, and no further processing
is necessary.
 Of course, it is not there, so the inference engine
begins looking for conclusions of rules that infer H.
 It finds rule 7. It, therefore, looks for G in the working
memory.
 It is not there, so now the rule interpreter looks for
rules with G on their right side.
 It finds rules 5 and 6. If it looks at rule 5 first, it will then
look in the working memory for N.
 N is not there so it searches for rules that
conclude N.
 It now discovers rule 4 with premises L and I. The
search continues for L and I. L is in the working
memory, so now I is looked for. I is found in rule 3.
 therefore, the rule interpreter now looks for F and
finds it in the working memory.
 At this point, F proves I, and L
 and I prove N.
 N, in turn, proves G.
 G then proves H,
 the original Hypothesis.
INITIAL CASE FACTS
F
L
1) IF K & L THEN J
2) IF F & J THEN M
3) IF F THEN I
4) IF L & I THEN N
5) IF N THEN G
6) IF M THEN G
7) IF G THEN H
Backward chaining is an inference strategy
that works backward from a hypothesis to
a proof.
You begin with a hypothesis about what
the situation might be. Then you prove it
using given facts,
 e.g. a doctor may suspect some disease
and proceed by inspection of symptoms. In
backward chaining terminology, the
hypothesis to prove is called the goal.
1. Start with the goal.
2. Goal may be in WM initially, so check and
you are done if found!
3. If not, then search for goal in the THEN part
of the rules (match conclusions, rather than
premises).
4. Check to see if the goal rule’s premises are
listed in the working memory.
5. Premises not listed become sub-goals to
prove.
6. Process continues in a recursive fashion
until the hypothesis is proved.
Rules
Rule 1
IF The patient has deep cough
AND We suspect an infection
THEN The patient has Pneumonia
Rule 2
IF The patient’s temperature is above 100
THEN Patient has fever
Rule 3
IF The patient has been sick for over a fortnight
AND The patient has fever
THEN We suspect an infection
Goal
Patient has Pneumonia
Step Description Working Memory
1 Goal: Patient has pneumonia. Not in working
memory
2 Find rules with goal in conclusion: Rule 1
3 See if rule 1, premise 1 is known, “the
patient has a deep cough
4 Find rules with this statement in conclusion.
No rule found. “The patient has a deep
cough” is a primitive. Prompt patient.
Response: Yes.
Deep cough
5 See if rule 1, premise 2 is known, “We
suspect an infection”
Deep cough
6 This is in conclusion of rule 3. See if rule 3,
premise 1 is known, “The patient has been
sick for over a fortnight”
Deep cough
7 This is a primitive. Prompt patient.
Response: Yes
Deep cough
Sick over a month
Step Description Working Memory
8 See if rule 3, premise 2 is known, “The
patient has a fever”
Deep cough
Sick over a month
9 This is conclusion of rule 2. See if rule 2,
premise 1 is known, “The patient’s
temperature is above 100”
Deep cough
Sick over a month
10 This is a primitive. Prompt patient.
Response:
Yes. Fire Rule
Deep cough
Sick over a month
Fever
11 Rule 3 fires Deep cough
Sick over a month
Fever
Infection
12 Rule 1 fires Deep cough
Sick over a month
Fever
Infection
Ai lecture 07 inference engine

Ai lecture 07 inference engine

  • 1.
  • 3.
    The inference enginecan take two basic approaches to search for an answer. These are: Forward chaining Backward chaining
  • 4.
    In forward chainingthe inference engine begins with some initial information and work forward, attempting to match that information with a rule. i.e.  in forward chaining, the rule interpreter or inference engine attempts to match a fact or statement in the working memory to the situation stated in the left-hand of IF part of the rule.
  • 5.
     Once afact in the working memory has been matched to the IF part of the rule, the rule is then fired.  The action or consequent stated could produce new knowledge or new fact that is stored in the working memory.  This new fact may then be used to search out the next appropriate rule.  This search and matching process continues until a final conclusion rule is fired.
  • 6.
    To understand theforward chaining process let us consider an example. The rule base (knowledge base) consisting of seven rules and the working memory contains the initially known facts F and L.
  • 7.
    INITIAL CASE FACTS F L INFERREDFACTS IF K & L THEN J IF F & J THEN M IF F THEN I IF L & I THEN N IF N THEN G IF M THEN G IF G THEN H
  • 8.
     The InferenceEngine starts with the first rule and matches its premises with the initial case facts.  There are two premises in the first rule i.e. K and L.  The rule cannot be fired because L is available in the working memory but K is not.  For a rule to be fired, all of its premises must be matched.  The inference engine then takes another rule in the sequence and matches its premises with the available facts.
  • 9.
     Rule 2also cannot be fired because F is matched and J is not.  Then it takes rule 3 and check its premises. Rule 3 is fired, because there is only one premise i.e. F, which is available in the working memory.  Therefore, the consequence is executed and I is added as an inferred fact into the working memory.  Rule 4 is also fired because all its premises are matched and N is added into the working memory.
  • 10.
    Rule 5 isthen fired and G is added into inferred facts. Rule 6 cannot be fired, because its premise is not available.  The last rule is fired and H is added into the working memory.
  • 11.
    INITIAL CASE FACTS F L INFERREDFACTS 1) IF K & L THEN J 2) IF F & J THEN M 3) IF F THEN I 4) IF L & I THEN N 5) IF N HEN G 6) IF M THEN G 7) IF G THEN H I G N H
  • 12.
    F I L N GH Rule 3 Rule 4 Rule 5 Rule 7 AND
  • 13.
    1. Add factsto working memory (WM) 2. Take each rule in turn and check to see if any of its premises match the facts in the WM 3. When matches found for all premises of a rule, place the conclusion of the rule in WM. 4. Repeat this process until no more facts can be added. Each repetition of the process is called a pass.
  • 14.
     Let’s lookat how a doctor goes about diagnosing a patient.  He asks the patient for symptoms and then infers diagnosis from symptoms.  Forward chaining is based on the same idea. It is an “inference strategy that begins with a set of known facts, derives new facts using rules whose premises match the known facts, and continues this process until no further rules have premises that match the known or derived facts.
  • 15.
    Knowledge Base Rule 1 IFThe patient has deep cough AND We suspect an infection THEN The patient has Pneumonia Rule 2 IF The patient’s temperature is above 100 THEN Patient has fever Rule 3 IF The patient has been sick for over a fortnight AND The patient has a fever THEN We suspect an infection Working Memory Case facts • Patients temperature= 103 • Patient has been sick for over a month • Patient has violent coughing fits
  • 16.
    Rule, premise StatusWorking Memory 1, 1 Deep cough True Temp= 103 Sick for a month Coughing fits 1, 2 Suspect infection Unknown Temp= 103 Sick for a month Coughing fits 2, 1 Temperature>100 True, fire rule Temp= 103 Sick for a month Coughing fits Patient has fever
  • 17.
    Rule, premise StatusWorking Memory 1, 1 Deep cough True Temp= 103 Sick for a month Coughing fits Patient has fever 1, 2 Suspect infection Unknown Temp= 103 Sick for a month Coughing fits Patient has fever 3, 1 Sick for over fortnight True Temp= 103 Sick for a month Coughing fits Patient has fever 3, 2 Patient has fever True, fire Temp= 103 Sick for a month Coughing fits Patient has fever Infection
  • 18.
    Rule, premise StatusWorking Memory 1, 1 Deep cough True Temp= 103 Sick for a month Coughing fits Patient has fever Infection 1, 2 Suspect infection True, fire Temp= 103 Sick for a month Coughing fits Patient has fever Infection Pneumonia
  • 19.
    Now, no morefacts can be added to the WM. Diagnosis: Patient has Pneumonia.
  • 21.
     Another approachof inference engine is backward chaining.  Here the inference engine starts with a fact which is called hypothesis.  The inference engine then begins examining the right- hand or THEN part of rules looking for a match. The inference engine searches for evidence to support the hypothesis originally stated.  If a match is found, the working memory is updated, recording the conditions or premises that the rule states as necessary for supporting the matched conclusion.
  • 22.
    The chaining processcontinues with system repeatedly attempting to match the right side of the rule against the current system’s status.
  • 23.
    The corresponding IFsides of the rules matched are used to generate new intermediate hypotheses or goal states which are recorded in the working memory. This backward chaining continues until the hypothesis is proved.
  • 24.
    Now using thesame knowledge base and working memory, we can illustrate backward chaining. F I L N G H Rule 3 Rule 4 Rule 5 Rule 7 AND
  • 25.
     We beginby telling the system that we want to prove H.  The inference engine first looks in the working memory on the off chance H is there, and no further processing is necessary.  Of course, it is not there, so the inference engine begins looking for conclusions of rules that infer H.  It finds rule 7. It, therefore, looks for G in the working memory.  It is not there, so now the rule interpreter looks for rules with G on their right side.  It finds rules 5 and 6. If it looks at rule 5 first, it will then look in the working memory for N.
  • 26.
     N isnot there so it searches for rules that conclude N.  It now discovers rule 4 with premises L and I. The search continues for L and I. L is in the working memory, so now I is looked for. I is found in rule 3.  therefore, the rule interpreter now looks for F and finds it in the working memory.  At this point, F proves I, and L  and I prove N.  N, in turn, proves G.  G then proves H,  the original Hypothesis.
  • 27.
    INITIAL CASE FACTS F L 1)IF K & L THEN J 2) IF F & J THEN M 3) IF F THEN I 4) IF L & I THEN N 5) IF N THEN G 6) IF M THEN G 7) IF G THEN H
  • 28.
    Backward chaining isan inference strategy that works backward from a hypothesis to a proof. You begin with a hypothesis about what the situation might be. Then you prove it using given facts,  e.g. a doctor may suspect some disease and proceed by inspection of symptoms. In backward chaining terminology, the hypothesis to prove is called the goal.
  • 29.
    1. Start withthe goal. 2. Goal may be in WM initially, so check and you are done if found! 3. If not, then search for goal in the THEN part of the rules (match conclusions, rather than premises). 4. Check to see if the goal rule’s premises are listed in the working memory. 5. Premises not listed become sub-goals to prove. 6. Process continues in a recursive fashion until the hypothesis is proved.
  • 30.
    Rules Rule 1 IF Thepatient has deep cough AND We suspect an infection THEN The patient has Pneumonia Rule 2 IF The patient’s temperature is above 100 THEN Patient has fever Rule 3 IF The patient has been sick for over a fortnight AND The patient has fever THEN We suspect an infection Goal Patient has Pneumonia
  • 31.
    Step Description WorkingMemory 1 Goal: Patient has pneumonia. Not in working memory 2 Find rules with goal in conclusion: Rule 1 3 See if rule 1, premise 1 is known, “the patient has a deep cough 4 Find rules with this statement in conclusion. No rule found. “The patient has a deep cough” is a primitive. Prompt patient. Response: Yes. Deep cough 5 See if rule 1, premise 2 is known, “We suspect an infection” Deep cough 6 This is in conclusion of rule 3. See if rule 3, premise 1 is known, “The patient has been sick for over a fortnight” Deep cough 7 This is a primitive. Prompt patient. Response: Yes Deep cough Sick over a month
  • 32.
    Step Description WorkingMemory 8 See if rule 3, premise 2 is known, “The patient has a fever” Deep cough Sick over a month 9 This is conclusion of rule 2. See if rule 2, premise 1 is known, “The patient’s temperature is above 100” Deep cough Sick over a month 10 This is a primitive. Prompt patient. Response: Yes. Fire Rule Deep cough Sick over a month Fever 11 Rule 3 fires Deep cough Sick over a month Fever Infection 12 Rule 1 fires Deep cough Sick over a month Fever Infection