Sunteți pe pagina 1din 8

THE FORWARD CHAINING ALGORITHM

Captured from :
James P. Ignizio, Introduction To Expert System, The Development And Implemantation Of RuleBased Expert Systems., McGraw-Hill, Inc., New York, 1991,pp 157-166.
-------------------------------------------------------------------------------------------------------------------------------------We noted that an algorithm is a step-by-step procedure which ultimately converges to an optimal
solution. Further, it was noted that neither expert systems non heuristic programming methods are
considered algorithmic as they cannot, in general, guarantee optimality. As such, the reader may think
that the phrase "forward chaining algorithm" is a bit of a contradiction in terms. It is not; the algorithm
to be introduced for forward chaining merely provides a step-by-step procedure that guarantees only
the correct performance of the inference process of an expert system that employs data driven search.
Since the expert system itself is made up of heuristic rules, there is still no guarantee that the
conclusion ultimately reached by this process is itself optimal.
Fundamentally, there are two different philosophies of forward chaining. In one, we deliver all known
data to the expert system at the very onset of the consultation session. In the second approach, we
provide the expert system with only those specific elements of data that it requests during consultation.
Both approaches have their advantages and disadvantages. The first approach often works well when
the expert system is embedded in some automated process and receives its data directly from a
computer resident database-or from a set of sensors. The second approach seeks to reduce the
amount of data requested to that absolutely necessary to reach a conclusion. In the event that the
generation of data requires extensive time and cost, this particular philosophy can be particularly
appealing. Our procedure, to be described, shall use the latter approach.
Let us now consider just one algorithm by means of which the forward chaining search procedure may
be conducted. In order to more clearly understand and appreciate the algorithm, we shall present it in
such a form as to permit manual implementation. Of course, before implementation of the algorithm, it
is assumed that the rules, rule numbers, and the associated prompts (i.e., user queries) for each
attribute have been listed. Further, remember that we are still dealing with deterministic rule bases.
Algorithm for Forward Chaining: Baseline Version
1. Initialization. Establish three empty tables, the Working Memory table, the Attribute-Queue table,
and the Rule/Premise Status table. The Working Memory table will be used to record all assertions
(i.e., all the facts deduced during the consultation). The Attribute-Queue table records, in order, all
attributes for which a value has been assigned or is being sought. The attribute at the top of this
table is the attribute presently under consideration. The Rule/Premise Status table records the rule
status (i.e., active, marked, unmarked, discarded, triggered, or fired) as well as the status of each
premise clause (i.e., free, true, or false). All premise clauses are initially unspecified (i.e., free) and
all rules are initially unmarked and active. The notation employed to represent the rule and clause
states is
A = active rule
FD = fired rule
D = discarded rule
FR = free clause
U = unmarked rule
FA = false clause
M = marked rule
TU = true clause
TD = triggered rule
Printed for Expert System Shell Project
directed by
Krisna Adiyarta, Universitas Budi Luhur

2. Start inference. Assign a value to a specific premise attribute, where this attribute must not appear
in any conclusion clause 1. Record this attribute at the top of the Attribute-Queue table. Also record
this attribute, and its associated value, at the bottom of the Working Memory table.
3. Rule scan and check for convergence. Examine the Rule/Premise Status table. If no rules are
active, STOP Otherwise, scan the active rule-set premise clauses for all occurrences of the
attribute on the top of the Attribute-Queue table, and record any changes in status of the premise
clauses of the active rule set.
(a) If the premise2 of any rule is false then mark the associated rule as being discarded. Repeat this
for all rules having a false premise. When complete, proceed to step 3b.
(b) If the premise3 of any rule is true then mark the associated rule as being triggered and place its
conclusion attribute (and rule number) at the bottom of the Attribute-Queue table. Repeat this for
all rules having a true premise. When complete, proceed to step 3c.
(c) If no rules are presently in the triggered state (i.e., via a check of the Rule Status table), go to
step 5. Otherwise (i.e., if one or more rules are triggered), go to step 4.
4. Rule firing. Cross out the topmost attribute on the Attribute-Queue table. Change the status of the
rule associated with the new topmost attribute from triggered to fired. Place the conclusion
associated with the fired rule at the bottom of the Working Memory table. Return to step 3.
5. Queue status. Cross out the topmost attribute on the Attribute-Queue table and proceed to step 6.
6. Convergence check and rule marking. Scan the active rule set for any unmarked, active rule. If no such
rules can be found, STOP Otherwise, mark the first such rule found (i.e., in the Rule/Premise Status table)
and go to step 7.
7. Query. For the most recently marked rule, query the user for the value of an attribute in any of the rule's
free premise clauses. If the user has a response then go to step 8. Otherwise (i.e., if the user either has no
response or if the attribute has no associated prompt), continue this step for all remaining free premise
clauses of the marked rule. If all such clauses have been examined without a user response, return to step
6.
8. Rule unmarking. Place the associated attribute (and rule number) on the top of the Attribute-Queue table.
Also place this attribute, plus its value, at the bottom of the Working Memory table. Unmark the most
recently marked rule and return to step 3.
------------------------------------------------------------------------------------------------The choice, as far as this algorithm is concerned, of the premise attribute selected is arbitrary. We simply
must volunteer at least one piece of data before the inference process may be started.
2 By "premise," we are referring to the entire premise of the rule, which may be composed of several premise
clauses.
3 Same comment as for footnote 2, above.
4 We shall use this relatively inefficient rule set (recall Fig. 5.3) simply to illustrate the algorithm.
1

Printed for Expert System Shell Project


directed by
Krisna Adiyarta, Universitas Budi Luhur

Example 6.1 The forward chaining algorithm is best understood by means of illustration. To demonstrate its
implementation, let us first consider the rule set from Chap. 5, Fig. 5.2. The rules associated with this figure
are repeated below4:
Rule 1: If engine type is prop Then plane is C130
Rule 2: If engine type is jet
and wing position is low Then plane is B747
Rule 3: If engine type is jet
and wing position is high and bulges are none Then plane is C5A
Rule 4: If engine type is jet
and wing position is high and bulges are aft of wing Then plane is C141
We also need to associate prompts for all attributes that do not appear in a conclusion clause. These prompts
are listed below:
Engine type: "Are the aircraft engines jet or propellor driven?"
Wing position: "Are the wings positioned high or low on the fuselage?"
Bulges: "Where, on the fuselage, are there pronounced bulges?"
There are thus four rules with one, two, three, and three premise clauses per rule, respectively. The initial
tables associated with this production rule set are shown in Table 6.1.
Table 6.1
Initial tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U
A, U

(3)

A, U

(4)

A, U

Attribute-Queue table (attribute listing)

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR
FR
FR
FR
FR
FR
FR
FR
FR

Working Memory table (attribute = value)

As may be seen, all rules are active (A) and unmarked (U) while all premise clauses are unspecified or
free (FR); and thus we have completed step 1 of the forward chaining algorithm. Moving to step 2, we
must initiate the inference process by assigning a value to a specific premise attribute, where the
attribute selected does not appear in any conclusion clause. Step 2 is somewhat akin to turning the
key in a car's ignition; it serves to start the inference process. Examining the rule set, we see that the
attributes engine type, wing position, and bulges are all possibilities. Let us arbitrarily select engine
type to begin the process. We must then assign a value to the attribute engine type. Only two values
exist, jet or propellor. To determine which value to assign, we must assume a specific aircraft type. Let
Printed for Expert System Shell Project
directed by
Krisna Adiyarta, Universitas Budi Luhur

us assume that the user has sighted a C5A. In that case, the value for engine type will be jet. Thus, we
place the attribute engine type at the top of the Attribute-Queue table and also place this attribute, plus
the value jet, on the Working Memory table. The results are shown in Table 6.2.
We now proceed to step 3. Since there are active rules (all of them are in fact active) we cannot
terminate the algorithm. Thus we scan the premise clauses of the active rule set (rules 1 to 4) looking
for all occurrences of the attribute engine type. This attribute appears in the premise clauses of all
rules wherein, for rule 1, the first premise clause is now false and, for the remainder of the rules, their
first premise clause is true. We enter this information in the premise clause columns of the
Rule/Premise Status table. Moving to step 3a, we see that rule 1 is false
Table 6.2
Second set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U
A, U

(3)

A, U

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR
FR
FR
FR
FR
FR
FR
FR
FR

Working Memory table (attribute = value)


Engine type = jet

Table 6.3
Third set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U, D
A, U

(3)

A, U

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR, FA
FR, TU
FR
FR, TU
FR
FR
FR, TU
FR
FR

Working Memory table (attribute = value)


Engine type = jet

Printed for Expert System Shell Project


directed by
Krisna Adiyarta, Universitas Budi Luhur

and may be discarded. The remaining rules are neither true nor false and thus remain -active. These results
are entered as shown in Table 6.3. Note the use of the overstrike to signify a change in status. Since no rules
are presently triggered, we proceed to step 5.
We now cross out the topmost attribute (i.e., engine type) on the AttributeQueue table and proceed to step 6.
In step 6, we first scan the active rule set for any unmarked rule. Rules 2, 3, and 4 are all active and
unmarked. We mark the first of these (i.e., rule 2) and proceed to step 7. The results of the actions involved in
steps 5 and 6 are indicated in Table 6.4.
At step 7, we must ask for input from the user. The most recently marked rule is rule 2 and the only free
premise clause is the second. Since there is a prompt for this clause, we ask the user: "Are the wings
positioned high or low on the fuselage?" Since we have assumed that the aircraft is a C5A, the user will
respond with the answer, "high." We thus move to step 8.
In step 8, we place the attribute wing position (and specify that this is from rule 2) on the top of the AttributeQueue table. We place the same attribute and its value (i.e., high) at the bottom of the Working Memory table.
We then unmark rule 2 and return to step 3. At step 3, we note that there are still active rules and thus we
proceed to scan the active rule set (rules 2 to 4) for all occurrences of the attribute wing position. Since wing
position is known to be high, we note that premise 2 of rule 2 is false while the second premise of rules 3 and
4 are true. Moving to step 3a, we see that rule 2 is false and may be discarded while the remaining two rules
Table 6.4
Fourth set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U, D
A, U, M

(3)

A, U

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR, FA
FR, TU
FR
FR, TU
FR
FR
FR, TU
FR
FR

Working Memory table (attribute = value)


Engine type = jet

are still active. The results of all of the actions in this paragraph am summarized in Table 6.5. Since no rules
are in the triggered state, we proceed to step 5.
At step 5, we cross out the topmost attribute (wing position) of the AttributeQueue table and proceed to step
6. In step 6, we scan for unmarked, active rates. Rules 3 and 4 are both active and unmarked. We mark rule 3
and proceed to step 7. The results of our actions during this go-around of steps 5 to 7 are shown in Table 6.6.
Printed for Expert System Shell Project
directed by
Krisna Adiyarta, Universitas Budi Luhur

Table 6.5
Fifth set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U, D
A, U, M, U, D

(3)

A, U

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type
Wing Position (rule 2)

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR, FA
FR, TU
FR, FA
FR, TU
FR, TU
FR
FR, TU
FR, TU
FR

Working Memory table (attribute = value)


Engine type = jet
Wing Position = high

Table 6.6
Sixth set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U, D
A, U, M, U, D

(3)

A, U, M

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type
Wing Position (rule 2)

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR, FA
FR, TU
FR, FA
FR, TU
FR, TU
FR
FR, TU
FR, TU
FR

Working Memory table (attribute = value)


Engine type = jet
Wing Position = high

We are once again at step 7. The only free premise of the marked rule (rule 3) is the third premise, wherein
the associated attribute is bulges. The prompt for bulges is, "Where on the fuselage are there pronounced
bulges?" And the answer should be "none." We next move to step S wherein we place the attribute bulges
(and its associated rule number) on the top of the Attribute-Queue table and place the same attribute, and its
value, at the bottom of the Working Memory table. Rule 3 is then unmarked. The results of the actions of
steps 7 and S are shown in Table 6.7. We return to step 3.
Printed for Expert System Shell Project
directed by
Krisna Adiyarta, Universitas Budi Luhur

Rules 3 and 4 are still active so we cannot yet terminate the search. We then scan the premise clauses of
rules 3 and 4 for the attribute bulges, which may be found in both rules. From this scan, we see that the third
premise of rule 3 is now true while the third premise of rule 4 is false. Further, we may discard rule 4 and
trigger rule 3. In triggering rule 3, we place its conclusion attribute (note that we are now at step 3b) and rule
number on the bottom of the Attribute-Queue table.
Table 6.7
Seventh set of tables for Example 6.1
Rule number

Rule Status

(1)
(2)

A, U, D
A, U, M, U, D

(3)

A, U, M, U

(4)

A, U

Attribute-Queue table (attribute listing)


Engine type
Wing Position (rule 2)
Bulges (rule 3)

Rule/Premise table
Premise clause
Number
(1)-1
(2)-1
(2)-2
(3)-1
(3)-2
(3)-3
(4)-1
(4)-2
(4)-3

Premise Clause
Status
FR, FA
FR, TU
FR, FA
FR, TU
FR, TU
FR
FR, TU
FR, TU
FR

Working Memory table (attribute = value)


Engine type = jet
Wing Position = high
Bulges = none

The results are shown in Table 6.8. From 3c, we note that since we have a rule in the triggered state, we
proceed to step 4.
This is the first time that we have reached step 4. Proceeding per directions, we cross out the topmost
attribute (i.e., bulges) on the Attribute-Queue table and change the status of the rule associated with the new
topmost attribute (i.e., plane and rule 3) from triggered to fired. We then place the conclusion of the fired rule
(i.e., that the plane is a C5A) on the bottom of the Working Memory table. We then return to step 3. Further,
since no rules are now active, we may terminate the search-with the results summarized in Table 6.9.
The primary results of interest, from Table 6.9, are those shown in the Working Memory table, and particularly
the last entry in that table. That is, we have finally determined that the plane sighted was a C5A. In addition,
we have a collection of all the facts concerning this sighting in the Working Memory table (i.e., the engine type
is jet, the wing position is high, there are no bulges on the fuselage, and the plane is a C5A), whether
provided by the user or developed by the inference process.

Printed for Expert System Shell Project


directed by
Krisna Adiyarta, Universitas Budi Luhur

Table 6.8
Eighth set of tables for Example 6.1
Rule number
(1)
(2)
(3)
(4)

Rule/Premise table
Rule Status
Premise clause
Number
A, U, D
(1)-1
A, U, M, U, D
(2)-1
(2)-2
A, U, M, U, TD
(3)-1
(3)-2
(3)-3
A, U, D
(4)-1
(4)-2
(4)-3

Attribute-Queue table (attribute listing)


Engine type
Wing Position (rule 2)
Bulges (rule 3)
Plane (rule 3)

Premise Clause
Status
FR, FA
FR, TU
FR, FA
FR, TU
FR, TU
FR, TU
FR, TU
FR, TU
FR, FA

Working Memory table (attribute = value)


Engine type = jet
Wing Position = high
Bulges = none

Table 6.9
Final set of tables for Example 6.1
Rule number
(1)
(2)
(3)
(4)

Rule/Premise table
Premise clause
Number
A, U, D
(1)-1
A, U, M, U, D
(2)-1
(2)-2
A, U, M, U, TD, FD
(3)-1
(3)-2
(3)-3
A, U, D
(4)-1
(4)-2
(4)-3
Rule Status

Attribute-Queue table (attribute listing)


Engine type
Wing Position (rule 2)
Bulges (rule 3)
Plane (rule 3)

Premise Clause
Status
FR, FA
FR, TU
FR, FA
FR, TU
FR, TU
FR, TU
FR, TU
FR, TU
FR, FA

Working Memory table (attribute = value)


Engine type = jet
Wing Position = high
Bulges = none
Plane = C5A

Printed for Expert System Shell Project


directed by
Krisna Adiyarta, Universitas Budi Luhur

S-ar putea să vă placă și