details of rule based reasoning

73
Details of Rule Based Reasoning Chapter 5 The Engineering of Knowledge Based Systems

Upload: zea

Post on 20-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Details of Rule Based Reasoning. Chapter 5 The Engineering of Knowledge Based Systems. Introduction. Traditionally pattern matching systems have been associated with forward reasoning while backward reasoning systems typically have been implemented as inference networks. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Details of Rule Based Reasoning

Details of Rule Based Reasoning

Chapter 5

The Engineering of Knowledge Based Systems

Page 2: Details of Rule Based Reasoning

Introduction

• Traditionally pattern matching systems have been associated with forward reasoning while backward reasoning systems typically have been implemented as inference networks.

• This chapter presents examples to forward and backward reasoning and examines how these systems can be implemented.

Page 3: Details of Rule Based Reasoning

Forward Reasoning

• Forward reasoning is the process of working from a set of data to toward the conclusions that can be drawn from this data.

• A rule is eligible for execution when its premises are satisfied.

Page 4: Details of Rule Based Reasoning

Forward Reasoning

• Monitoring and diagnosing real-time process control systems where data are continuously being acquired, modified and updated are suitable for forward reasoning.

• Other suitable applications can be listed as:– design– planning– scheduling

The important characteristics of these applications are:1. The need for a quick response to changes in data2. Few predetermined relations existing between the input data and

the conclusion drawn.

Page 5: Details of Rule Based Reasoning

Forward Reasoning

Implemented as Pattern matching• Where synthesis of new facts based on the rules’

conclusions occurs.• In these applications there are many potential

solutions that can be derived from the input data. The rules express knowledge as general patterns and the precise connections (inference chain) between these rules can not be predetermined.

• As a result these systems use forward reasoning pattern matching systems due to the implicit flexibility of the pattern matching approach.

Page 6: Details of Rule Based Reasoning

Example 1 of Forward Reasoning

Problem Statement.• It is discovered that the retirement village is under

the threat of flooding of Suwannee River. An expert system to give alert when a flood warning or evacuation order should be given is required to be developed.

• 10 rules and 18 parameters are set to solve this problem

Page 7: Details of Rule Based Reasoning

Example 1 of Forward Reasoning

• Parameters– month any month of the year– upstream precipitation none, light, heavy– weather forecast sunny, cloudy, stormy– river height measurement in feet– season dry, wet– local rain none, light rain, heavy rain– river change none, lower, higher– flood warning yes, no– evacuation order yes, no

Page 8: Details of Rule Based Reasoning

Example 1 of Forward Reasoning

R1 IF month = may … oct THEN season = wet

R2 IF month = nov … april THEN season = dry

R3 IF upstream = none AND season = dry

THEN change = lower

R4 IF upstream = none AND season = wet

THEN change = none

R5 IF upstream = light THEN change = none

R6 IF upstream = heavy THEN change = higher

R7 IF level = low THEN flood = no AND

evac = noR8 IF change = none | lower

AND level = normal | low THEN flood = no AND

evac = noR9 IF change = higher AND

level = normal AND rain = heavy

THEN flood = yes (CF = 0.4) AND evac = no

Page 9: Details of Rule Based Reasoning

Example 1 of Forward ReasoningR10 IF change = higher AND

level = normal AND rain = light

THEN flood = no AND evac = no

R11 IF change = higher AND level = high AND rain = none | light

THEN flood = yes (CF=0.5) AND evac = yes (CF=0.2)

R12 IF change = higher AND level = high AND rain = heavy

THEN flood = yes AND evac = yes (CF=0.8)

R13 IF heigt <10 THEN level = low

R14 IF heiht >=10 AND <=16 THEN level = normal

R15 IF height > 16 THEN level = high

R16 IF forecast = sunnyTHEN rain = none

R17 IF forecast = cloudy THEN rain = light

R18 IF forecast = stormyTHEN rain = heavy

Page 10: Details of Rule Based Reasoning

Inference Network

Intermediate conclusions

Input parameters

Final conclusions

Page 11: Details of Rule Based Reasoning

• The data used by this system can be gathered from a set of sensors, requiring no interaction with the users of the system.

• The system must continually monitor the data collected by the sensors to determine if a warning should be given.

Page 12: Details of Rule Based Reasoning

Implementation

• Because all the relations are predetermined an internal representation that exploits this explicitness is used. This internal representation details for each rule:1. The rule number

2. Parameters used within the premises of the rule (called upstream elements)

3. Parameters used within the conclusions of the rule (called downstream elements)

4. The set of premises

5. The set of conclusions

Page 13: Details of Rule Based Reasoning

Implementation

and for each parameter1. The name of the parameter

2. A list of rules that derive a value for this parameter (called set-by)

3. A list of rules that use this parameter in their premises (called premise-for)

4. The value(s) of this parameter (and their associated confidence factors) if known

Page 14: Details of Rule Based Reasoning

Implementation

For example, rule R1 and parameter change are described internally as

Rule: R1

Upstream-elements: month

Downstream-elements: season

Premises: month = may … oct

Conclusions: season = wet

Page 15: Details of Rule Based Reasoning

Implementation

Parameter: change

Set-by: (R3 R4 R5 R6)

Premise-for: (R8 R9 R10 R11 R12)

Values -

Two additional lists (input data and conclusions) are needed for this approach

Input data: (month upstream forecast height)

Conclusions: (flood evac)

Page 16: Details of Rule Based Reasoning

Implementation

Because all the interconnections among the rules can be stated explicitly, it is easy to determine what actions to take whenever some parameters are obtained.

The algorithm is basic breadth first search technique.

Page 17: Details of Rule Based Reasoning

Forward Reasoning Inference Network Algorithm

1. Assign values to all input nodes from the external sources providing information to the knowledge-based system.

2. Form a que containing rules that use the values of these input nodes in their premises.

3. Until there are no more rules in Q:a. Examine the first rule in Q, comparing its premises with the values of the appropriate

parameters to decide if the premises of the rules are satisfied.

b. If the premises of the rule are not satisfied, remove the rule from Q, and go back to a.

c. If the rule is matched:

» Execute the rule, setting the rule’s downstream elements to the values specified by the rule.

» Decide which rules use the downstream elements just set within their premises,

» Add these rules as the last rules within Q if they are not already in Q, even if their premises are not fully satisfied.

» Delete the original rule from the front of the Q, and return to step a.

4. Output the values of the hypotheses that have been identified as conclusion.

5. If this application involves a real time process control, go back to step 1 and start the process again.

Page 18: Details of Rule Based Reasoning

Demonstration of the approach

• Assume that the following values are obtained:month = may

upstream = light

forecast = cloudy

height = 15

• Step 2 requires that we build a queue of rulesQ = ( R1 R2 R3 R4 R5 R6 R13 R 14 R15 R16 R17 R18)

Page 19: Details of Rule Based Reasoning

Demonstration of the approach

• R1 is examined to see if its premises are satisfied. month = may is satisfied. New conclusion season = wet is derived.

• Rules with season premise are R3 and R4 which are already in the queue.

• R1 is removed from the queue

• R2 is tried. Fails (month = nov…)• R3 R4 also fail

Page 20: Details of Rule Based Reasoning

Demonstration of the approach

• R5 is tried. Its premise is satisfied deriving

change = none• The value of the parameter change is used by

(R8, R9, R10, R11, and R12).

Q becomesQ = (R6 R13 R 14 R15 R16 R17 R18 R8 R9 R10 R11 R12)

Page 21: Details of Rule Based Reasoning

Demonstration of the approach

The known facts are month = may

upstream = light

forecast = cloudy

height = 15

season = wet

change = none

Page 22: Details of Rule Based Reasoning

Demonstration of the approach

This process continues until Q is empty. At this time our database of known facts become:month = mayupstream = lightforecast = cloudyheight = 15season = wetchange = nonelevel = normalrain = lightflood = noevac = no

Page 23: Details of Rule Based Reasoning

Demonstration of the approach

• If this is a real time application, the system repeats the process with new set of acquired input parameters.

• Forward chaining systems implemented as inference networks are very good for applications involving monitoring and diagnosing real-time process control systems where data are continually being acquired, modified and updated automatically.

Page 24: Details of Rule Based Reasoning

Example 2 of Forward Reasoning

Statement of the problem:Suppose that John is changing apartments and needs

packing. Some items are much heavier than others, some occupy larger space, some are fragile and some are malleable.

John wants to ensure that all the items survive the move unbroken and is not interested in finding the optimal scheme for packing all the items.

Page 25: Details of Rule Based Reasoning

Items to be Packed by the Boxer SystemITEM COUNT WEIGHT SIZE FRAGILE PLIABLE

Books 7 heavy 2 no no

Lamp 2 medium 8 yes no

Dishes 1 set medium 5 yes no

Coat 2 light 3 no yes

VCR 1 medium 4 no no

TV 1 heavy 10 yes no

Glasses 1 set medium 4 yes no

Jeans 4 light 2 no yes

Shirts 8 light 1 no yes

Page 26: Details of Rule Based Reasoning

Boxer System

• This problem is one of synthesis, since there are many possible combinations to use in packing.

• The process also requires a pattern matcher, since the objects and the boxes can be described by a list of values that detail the object’s (box’s)properties.

Page 27: Details of Rule Based Reasoning

Boxer System

John develops four primary steps in his BOXER system.1. Box the fragile items first, taking care to pad each of these items.

2. Box the heavy weight items, ensuring that these items are not placed with the fragile ones.

3. Box the medium weight items, again ensuring that these items are not placed with the fragile ones.

4. Box the light weight items, putting them wherever there is room.

John has collected enough boxes all of the same size (10 space units).

Page 28: Details of Rule Based Reasoning

Boxer SystemR1 IF step is box-fragile items

there is a fragile item to boxthere is an empty box or a box

containing only fragile itemsthe fragile item has not been padded

THEN pad the fragile itemR2 IF step is box-fragile items

there is a fragile item to boxthere is an empty box or a box

containing only fragile itemsthe fragile item has been paddedthe box’s free space >= the fragile item’s size

THEN put the fragile item in the boxdecrease the box’s free space by the fragile item’s size

Page 29: Details of Rule Based Reasoning

Boxer SystemR3 IF step is box-fragile items

there is a fragile item to box

THEN start a fresh box

R4 IF step is box-fragile items

THEN discontinue box fragile items start box heavy items

Page 30: Details of Rule Based Reasoning

Boxer SystemR5 IF step is box-heavy-items

there is a heavy item there is an empty box or a box that does not

contain fragile items or a box that contains <4 heavy items

the box’s free space >= the item’s size THEN put the heavy item in the box

decrease the box’s free space by the heavy item’s size

R6 IF step is box-heavy-items

there is a heavy item THEN start a fresh box

R7 IF step is box-heavy items

THEN discontinue box-heavy-items start box-medium-items

Page 31: Details of Rule Based Reasoning

Boxer SystemR8 IF step is box-medium-items

there is a medium item there is an empty box or a box that does not

contain fragile items or the box’s free space >= the medium item’s size

THEN put the medium item in the box decrease the box’s free space by the medium item’s size

R9 IF step is box-medium-items

there is a medium item THEN start a fresh box

R10 IF step is box-medium items

THEN discontinue box-medium-items start box-light-items

Page 32: Details of Rule Based Reasoning

Boxer SystemR11 IF step is box-light-items

there is a light item there is a box whose free space >= the light item’s size

THEN put the light item in the box decrease the box’s free space by the light item’s size

R12 IF step is box-light-items

there is a light item THEN start a fresh box

R13 IF step is box-light items

THEN discontinue box-light-items halt

Page 33: Details of Rule Based Reasoning

Boxer SystemImportant characteristics of these rules are:

• Each rule checks the currently active processing step.

• By keeping track of the steps the system subdivides knowledge into smaller groups.

• Only when all the objects of a certain group have been processed the system moves on to the next group.

• The premises of some rules within subgroups are also premises of other rules. This means some conflict resolution must be employed to determine which actual rule to be executed.

Page 34: Details of Rule Based Reasoning

Boxer System – Conflict Resolution

The selection is based on two ordered criteria:1. Always select the rule instantiated with the largest

number of premises

2. Should multiple instantiations still exist, select the rule instantiated with the data closest to the top of the database.

Page 35: Details of Rule Based Reasoning

Boxer System – Conflict Resolution

State : Box fragile items

Unboxed Items Number Weight Size Fragile? Pliable?

Book 1 heavy 2 no no

Book 2 heavy 2 No no

Lamp 1 medium 8 yes no

Dishes 1 medium 5 yes no

Coat 1 light 3 no yes

TV 1 heavy 10 yes no

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number: Contains: Free Space:

Page 36: Details of Rule Based Reasoning

Boxer System – Conflict Resolution

The system starts with box-fragile-items.

R3 and R4 are instantiated.

R3 has the larger number of premises and three instantiations (lamp, dishes, TV)

R3 for lamp 1

R3for dishes 1

R3for TV 1

R4

Page 37: Details of Rule Based Reasoning

Database after execution of R3 with Lamp 1

State : Box fragile items

Unboxed Items Number Weight Size Fragile? Pliable?

Book 1 heavy 2 no no

Book 2 heavy 2 No no

Lamp 1 medium 8 yes no

Dishes 1 medium 5 yes no

Coat 1 light 3 no yes

TV 1 heavy 10 yes no

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number:

1

Contains:

-

Free Space:

10

Page 38: Details of Rule Based Reasoning

Boxer System – Conflict Resolution

R1 for lamp 1 and box 1

R1 for dishes 1 and box 1

R1 for TV 1 1 and box 1

R3 for lamp 1

R3 for dishes 1

R3 for TV 1

R4

Page 39: Details of Rule Based Reasoning

Database after execution of R1 with Lamp 1 and Box 1

State : Box fragile items

Unboxed Items Number Weight Size Fragile? Pliable?

Book 1 heavy 2 no no

Book 2 heavy 2 No no

Lamp(padded) 1 medium 8 yes no

Dishes 1 medium 5 yes no

Coat 1 light 3 no yes

TV 1 heavy 10 yes no

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number:

1

Contains:

-

Free Space:

10

Page 40: Details of Rule Based Reasoning

Boxer System – Conflict Resolution

R1 for dishes 1 and box 1

R1 for TV 1 1 and box 1

R2 for lamp 1 and box 1

R3 for dishes 1

R3 for TV 1

R4

Page 41: Details of Rule Based Reasoning

Database after execution of R2 for Lamp 1 and Box 1

State : Box fragile items

Unboxed Items Number Weight Size Fragile? Pliable?

Book 1 heavy 2 no no

Book 2 heavy 2 No no

Dishes 1 medium 5 yes no

Coat 1 light 3 no yes

TV 1 heavy 10 yes no

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number:

1

Contains:

Lamp(padded)

Free Space:

2

Page 42: Details of Rule Based Reasoning

Database after Boxing all Fragile Items

State : Box heavy items

Unboxed Items Number Weight Size Fragile? Pliable?

Book 1 heavy 2 no no

Book 2 heavy 2 No no

Coat 1 light 3 no yes

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number:

1

2

3

Contains:

Lamp(padded) 1

Dishes (padded) 1

TV (padded) 1

Free Space:

2

5

0

Page 43: Details of Rule Based Reasoning

Database after Boxing all Heavy Items

State : Box heavy items

Unboxed Items Number Weight Size Fragile? Pliable?

Coat 1 light 3 no yes

pants 1 light 2 no yes

pants 2 light 2 no yes

Shirt 1 light 1 no Yes

Shirt 2 light 1 no Yes

Box Number:

1

2

3

4

Contains:

Lamp(padded) 1

Dishes (padded) 1

TV (padded) 1

Book 1, Book2

Free Space:

2

5

0

6

Page 44: Details of Rule Based Reasoning

Database after Boxing all light Items

State : Box light items

Unboxed Items Number Weight Size Fragile? Pliable?

Box Number:

1

2

3

4

Contains:

Lamp(padded) 1, Pants 1

Dishes (padded) 1, coat 1, pants 2

TV (padded) 1

Book 1, Book2, shirt 1, shirt 2

Free Space:

0

0

0

4

Page 45: Details of Rule Based Reasoning

The Rete Algorithm• As seen in the example, a pattern matcher, forward

reasoning system is extremely inefficient.

• The match, conflict resolution, and execute cycle implies that all of the rules are compared to all of the facts in the fact base to decide which rules belong in the conflict set.

If we have – r rules in our knowledge base

– f facts in our database

– An average of p premises in each rule

we will perform r*f * * p comparisons to our fact base on every cycle to determine which rules can be executed.

Page 46: Details of Rule Based Reasoning

The Rete Algorithm• The rules that can potentially execute on any cycle

change very little.

• Rather than comparing rules to facts to see which rules are satisfied we should instead maintain a list of satisfied rules and determine how this satisfaction list changes due to the addition and deletion of facts.

• This approach is exploited by the Rete Algorithm.

Page 47: Details of Rule Based Reasoning

The Rete AlgorithmThe Rete Algorithm involves the development of two networks:

1. a pattern network and

2. join network.

Consider the following rule:TEST 1: IF ((cat ?c small ?h ?n1)

(dog ?c ?q medium ?n2) (cat ?c large ?h ?n3) (dog ?c ?q long ?n4) )

THEN ( . . . )

(animal-type color size hair-length name)

Page 48: Details of Rule Based Reasoning

The pattern network consists of a set of trees formed from all of the premises in all of the rules

TEST 1: IF ((cat ?c small ?h ?n1) (dog ?c ?q medium ?n2) (cat ?c large ?h ?n3)

(dog ?c ?q long ?n4) ) THEN ( . . . )

The root of each tree is the first item within each premise pattern

cat

Match value to ?c

small

Match value to ?h

Match value to ?n1

dog

Match value to ?c

medium

Match value to ?q

Match value to ?n2

Page 49: Details of Rule Based Reasoning

The algorithm uses as much of the existing network as possible.It only creates a new path within the tree when the existing nodes do not correspond with the new item from the premise

TEST 1: IF ((cat ?c small ?h ?n1) (dog ?c ?q

medium ?n2) (cat ?c large ?h ?n3) (dog ?c ?q long ?n4) ) THEN ( . . .

Page 50: Details of Rule Based Reasoning

The join network connects the various leaf nodes of the trees together (in the order in which they occur as clauses) and compares similarly named values to ensure that they have the same values.

TEST 1: IF ((cat ?c small ?h ?n1) (dog ?c ?q medium ?n2) (cat ?c large ?h ?n3)

(dog ?c ?q long ?n4) ) THEN ( . . .

The Complete Joint and Pattern Network

Page 51: Details of Rule Based Reasoning

Rete Algorithm - ExampleConsider the following database facts:

1. (cat yellow large short rebel)2. (cat calico large short rubble)3. (cat calico small short kitty)4. (dog brown medium long charlie)5. (cat brown small medium prince)6. (dog brown small short sam)7. (dog calico medium medium butch)8. (dog brown large medium star)9. (dog calico medium long tramp)

Each of these facts is first parsed through the pattern network. The 6th fact can not be completely parsed down the tree.

Page 52: Details of Rule Based Reasoning

If similar patterns exist with several rules, the algorithm reuses as much of the existing networks as is possible.

Facts parsed through the pattern network

Page 53: Details of Rule Based Reasoning

• When a rule is executed, its actions can add, delete or modify facts on the fact list. Each of these actions changes the parsed and joined facts within the pattern and join networks. By merely updating these facts within the network, a forward reasoning system using the Rete Algorithm can quickly determine all rules that can execute.

Page 54: Details of Rule Based Reasoning

TEST 1: IF ((cat ?c small ?h ?n1) (dog ?c ?q medium ?n2) (cat ?c large ?h ?n3)

(dog ?c ?q long ?n4) ) THEN ( . . .

Results of passing facts through the joint network

Page 55: Details of Rule Based Reasoning

Conflict Resolution Schemes

Conflict resolution schemes can be divided into four broad categories based on the following criteria:

1. Number of rules to execute2. Order of the rules

i. The lowest numbered ruleii. The first applicable rule following the last fired

3. Complexity of the rulesi. Refined rules (with many premises)ii. General rules (with few premises)

4. Order of the datai. The rule that matches the oldest dataii. The rule that matches the newest data

Page 56: Details of Rule Based Reasoning

Coding Forward Reasoning• Here, simple and concise routines implementing forward

reasoning are presented. • These routines are more similar to the pattern matching

algorithm because the relationships between the rules and the facts are not known beforehand.

• Internal representation format for facts and rules are as follows:fact ::= (is attribute value)

(numeric-op attribute value)

numeric-op ::= = | > | < | <= | >=

data base ::= (fact *)

rule ::= (name if (fact *) then (fact *))

knowledge base ::= (rule +)

Page 57: Details of Rule Based Reasoning

Coding Forward Reasoning• Forward reasoning consists of sequentially examining

each rule in a knowledge base to find the first one capable of deriving new facts that are currently not known.

• Every time such a rule is found it is executed immediately and the cycle starts again.

• Because this code stops searching for a rule to execute when it encounters the first satisfied rule that has not executed yet, there is never more than one eligible rule on the agenda hence there is no need for conflict resolution.

• Execution price paid in return is all rules must be examined on each rule execution cycle.

Page 58: Details of Rule Based Reasoning

Coding Forward Reasoning• The Inference process is performed by the following five

functionsforward initiates the forward reasoning process

execute-rule searches through the knowledge base looking for a rule that derives a new fact

eval-rule-f examines an individual rule if it executes (all premises are satisfied and calling derive-new-fact?)

derive-new-fact? examines the rule’s conclusions to see if there is a conclusion not in the database

display-results prints a message every time a new fact is derived.

Page 59: Details of Rule Based Reasoning

Coding Forward Reasoningforward initiates the forward reasoning process. It loops as

long as new facts are being derived and returns T if any facts are derived.

( defun forward ( )

( let (derived-fact? )

(loop

(cond ( ( not (execute-rule) )

(return derived-fact?) )

(t (setq derived-fact? T) ) ) ) ) )

loop {form}

Controls no variables, simply executes its body repeatedly

Page 60: Details of Rule Based Reasoning

Coding Forward Reasoningexecute-rule searches through the knowledge base looking

for a rule that derives a new fact

(do ((var1 init_value (iteration_form)) var1 init_value (iteration_form

var3….)((termination test) value_to_be_returned)

body)( defun execute-rule ( ) ( do ( ( rules rule-list (cdr rules) ) )

( (null rules) nil) (if (eval-rule-f (car rules ) ) (return t) ) ) )

Page 61: Details of Rule Based Reasoning

Coding Forward Reasoningeval-rule-f examines an individual rule if it executes (all premises are

satisfied and calling derive-new-fact?)

(defun eval-rule-f (rule)(do ( ( ifs (caddr rule )

( if (member (car ifs) fact-list :test #’equal )

( cdr ifs) (return nil) ) ) ;; there is a premise not satisfied ( thens (caddr (cddr rule )) ) ) ;; then list of the rule ( ( null ifs) (derive-new-fact? thens ) ) ) ) ;; if all the premises are

;; satisfied look if new fact is going to be derived

Page 62: Details of Rule Based Reasoning

Coding Forward Reasoningderive-new-fact? examines the rule’s conclusions to see if there is a conclusion not in the database.

( defun derive-new-fact? (facts) (do ( (new-fact? nil) ;; initial value for new-fact is nil (fact-arg facts (cdr facts-arg) ) ) ;; initiate fact-arg to

; ; fact and iterate over cdr of fact-arg ( ( null facts-arg) new-fact?) ( cond ( ( member (car facts-arg) fact-list :test #’equal) return nill) ( t (display-results (car facts-arg) ) ( setq new-fact? t) ( setq fact-list (cons (car facts-arg ) fact_list) ) ) ) ) )

Page 63: Details of Rule Based Reasoning

Coding Forward Reasoningdisplay-results prints a message every time a new fact is derived

(defun display-results (derived-fact ) (terpri) ;; print new line

(princ “The value of “)(princ (cadr derived-fact) ) ;; second element of a list

(princ “ is “ ) (princ (cadr drived-fact) ) )

Page 64: Details of Rule Based Reasoning

Coding Forward Reasoning(setq rule-list

‘((R1a IF (( is shape long) (is color green ))

THEN ((fruit = banana)) )

(R1b IF (( is shape long) (is color yellow))

THEN ( (is fruit banana)) )

(R2a IF ((is shape round) (> diameter 4)) THEN ((is fruitclass vine)) )

(R2b IF ((shape = oblong ) (> diameter 4))

THEN ((is fruitclass vine)) )

R3 IF ((is shape round ) (< diameter 4 )) THEN ((is fruitclass tree)) )

(R4 IF (( = seedcount 1)) THEN ((is seedclass stonefruit)) )

(R5 IF (( > seedcount 1)) THEN ((is seedclass multiple)) )

(R6 IF ((is fruitclass vine ) (is color green ))

THEN ( is fruit watermelon)) )

(R7 IF ((is fruitclass vine) (is surface smooth)

(is color yellow )) THEN ((fruit = honeydew)) )

……………………………. ) )

Page 65: Details of Rule Based Reasoning

Consider the following fact base:(setq fact-list

‘( ( is color red)( is shape round)( = seedcount 1)( < diameter 4)

) )

This fact base causes the following execution results to be displayed:

The value of FRUITCLASS is TREE

The value of SEEDCLASS is STONEFRUIT

The value of FRUIT is CHERRY

T

Page 66: Details of Rule Based Reasoning

Backward Reasoning

• Backward chaining is more suited for applications having many more inputs than possible conclusions. The ability to trace the logic backwards from the few conclusions to the many inputs make it more efficient than forward chaining.

• Diagnosis and classification problems are well suited for backward chaining.

Page 67: Details of Rule Based Reasoning

Steps in Backward Reasoning

1. Form a temporary stack initially set to the top level goals defined in the system.

2. Set the goal to be traced to the top of the stack. If the stack is empty halt and announce completion.

3. Gather all rules satisfying this goal.4. Consider each of these rules in turn

a) If all premises are satisfied, then fire this rule to derive its conclusions. Do not consider any more rules for this goal. It’s value is now given by the current rule’s conclusion. If the goal being currently traced is a top level goal, remove it from the stack and go to step 2. If it is a sub goal remove it from the stack and continue with the suspended goal.

b) If a value in the database conflicts with the premise value fail the rule.c) If a premise is not satisfied with the facts in the database look if it can

be derived. If it can, consider this parameter as a sub-goal and go back to step 2.

Page 68: Details of Rule Based Reasoning

Steps in Backward Reasoning

d) If step 4c fails then ask the user for its value and add it to the database; go to step 4a and consider the next premise of the rule.

5. If all rules that can satisfy the current goal have been attempted and all have failed to derive a value, then this goal remains undetermined. Remove it from the stack and go back to step 2.

Page 69: Details of Rule Based Reasoning

Example - Backward ReasoningConsider the following knowledge base that helps us select beverage and main course for a meal.

R1 IF guest-age < 21THEN alcohol-indicated = no

R2 IF guest-age >= 21THEN alcohol-indicated = yes

R3 IF alcohol-indicated=yes, meal = formal THEN drink = wine

R4 IF alcohol-indicated=yes, guest= boss THEN drink = wine

R5 IF alcohol-indicated=yes, guest= neighbor THEN drink = beer

R6 IF drink = wine, dinner = fish THEN wine-type = white

R7 IF drink = wine, dinner = red-meat THEN wine-type = red

R8 IF guest = boss day = friday

THEN dinner = fishR9 IF guest = boss

day <> friday THEN dinner = red-meat

R10 IF guest-age < 21THEN dinner = pizza

R11 IF guest-age >=21THEN dinner = fish

R12 IF alcohol-indicated = no THEN drink = soda

Page 70: Details of Rule Based Reasoning

Example - Backward Reasoning

The parameters and their acceptable values are:guest-age: positive integer between 15 – 100

alcohol-indicated: yes/no

meal: formal/informal

drink: wine/beer/soda

guest: boss/neighbor/friend

dinner: fish/veal/red-meat/poultry/pizza

day: monday/Tuesday/ . . . /Sunday

Page 71: Details of Rule Based Reasoning

Example - Backward Reasoning

In this approach the system takes the responsibility of questioning the user about the facts when they are needed for the reasoning process.

The system starts with no data attached to any of the parameters:

Known Fact Base: ( )

A list of goals in the order the developer feels is best to pursue should be provided to guide the system.

Goals: (drink wine-type dinner)

Page 72: Details of Rule Based Reasoning

Example - Backward Reasoning

Suppose that it is Tuesday and we have invited our 30-year-old neighbor for a casual meal.

If the knowledge base is traced the database will include the following values:

Page 73: Details of Rule Based Reasoning

Example - Backward Reasoning

Database:( ( dinner = fish ) ( drink = beer) ( guest = neighbor) ( meal = casual) ( alcohol-indicated = yes) (guest-age = 30) )

Note that the system is unable to derive a value for the parameter wine type. Also note that the system never asks a value for a day, since this premise is only used when the guest is determined to be the boss.