2. Rule Based Systems

MITM 613
Intelligent System
Chapter 2: Rule-based
Systems
Abdul Rahim Ahmad
2
Contents
 Rules and facts
 A rule-based system for boiler control
 Rule examination and rule firing
 Maintaining consistency
 The closed-world assumption
 Use of variables within rules
 Forward-chaining (a data-driven strategy)
 Conflict resolution
 Backward-chaining (a goal-driven strategy)
 A hybrid strategy
 Explanation facilities
Abdul
Rahim
Ahmad
3
Rules and facts
 Rule-based system is a knowledge-based system where
 The knowledge base is represented in the form of a set, or sets, of
rules.
 Rules are an elegant, expressive, straightforward, and flexible means
of expressing knowledge.
 Simplest type of rule is the production rule
IF <condition> THEN <conclusion>
 Example of a production rule is
IF the tap is open THEN water flows
 Rules need facts to be applied, for a rule-based system to be of use.
 Facts are unconditional statements which are correct at the time
that they are used.
Abdul
Rahim
Ahmad
4
Facts

Facts can be:
 looked up from a database;
 already stored in computer memory;
 determined from sensors connected to the computer;
 obtained by prompting the user for information;
 derived by applying rules to other facts.

The collection of facts which are known to the system at any given time is
called the fact base.

In the rule
IF the tap is open THEN water flows
Abdul
Rahim
Ahmad

the tap is open is a fact. Therefore, the fact could also be written as the
rule: IF TRUE THEN the tap is open

Facts can be thought of as special rules, where the condition part is
always true.
5
Facts
 Given the rule
 IF the tap is open THEN water flows
 and the fact
 the tap is open
 the derived fact water flows can be generated.
 The new fact
 is stored in computer memory
 and can be used to satisfy the conditions of other
rules, thereby leading to further derived facts.
Abdul
Rahim
Ahmad
6
Rule Writing
Abdul
Rahim
Ahmad

Rule-writing is a type of declarative programming because rules
represent knowledge that can be used by the computer, without specifying
how and when to apply that knowledge.

The ordering of rules in a program should ideally be unimportant, and it
should be possible to add new rules or modify existing ones without fear of
side effects. (however, cannot always take it for granted).

For the declared rules and facts to be useful, an inference engine for
interpreting and applying them is required .

Inference engines are incorporated into a range of software tools, such as

expert system shells

artificial intelligence toolkits

software libraries

Prolog language.
7
Example rule-based system
A rule-based system to monitor the state of a power station boiler and to
advise appropriate actions.
Sensors are fitted to the drum in order to monitor:
• the temperature of the steam in the drum;
• the voltage output from a transducer, which in turn monitors the level
of water in the drum;
• the status of pressure release valve (i.e., open or closed);
• the rate of flow of water through the control valve.
Steam is taken from the drum,
passed through the super
heater and applied to the
turbine that turns the
generator
Water is heated in the boiler tubes to
produce a steam and water mixture that
rises to the steam drum, which is a
cylindrical vessel mounted horizontally
near the top of the boiler.
Drum separate
the steam from
the water.
Abdul
Rahim
Ahmad
Boiler produce steam to drive
a turbine and generator.
8
Power Station Boiler Control
The following rules have been written for controlling the boiler:
/* Rule 2.1 */
Rules 2.1, 2.2 2.3 consists of
IF water level low THEN open control valve
recommendations to the boiler operators
/* Rule 2.2 */
IF temperature high AND water level low THEN open control valve AND shut down boiler tubes
/* Rule 2.3 */
IF steam outlet blocked THEN replace outlet pipe
/* Rule 2.4 */
IF release valve stuck THEN steam outlet blocked
/* Rule 2.5 */
Rules 2.4 and the rest …. all
IF pressure high AND release valve closed THEN release valve stuck
involve taking a low-level
/* Rule 2.6 */
fact, such as a transducer
IF steam escaping THEN steam outlet blocked
reading, and deriving a
/* Rule 2.7 */
higher-level fact
IF temperature high AND NOT(water level low) THEN pressure high
/* Rule 2.8 */
IF transducer output low THEN water level low
/* Rule 2.9 */
IF release valve open AND flow rate high THEN steam escaping
Low-level vs high-level rules
/* Rule 2.10 */
Deep vs shallow rules
IF flow rate low THEN control valve closed
Abdul
Rahim
Ahmad
shallow rules - rules specific to one particular arrangement. Deep rule - expresses a fundamental rule - valid
under any circumstances. Low-level rules - dependent on a reading. Represent the first stages toward reaching a
conclusion. High-level rules use more abstract information. Closest to providing a solution to a problem.
9
Example rule-based system
Rules for controlling the boiler:
/* Rule 2.1 */
IF water level low THEN open control valve
/* Rule 2.2 */
IF temperature high AND water level low THEN open
control valve AND shut down boiler tubes
/* Rule 2.3 */
IF steam outlet blocked THEN replace outlet pipe
/* Rule 2.4 */
IF release valve stuck THEN steam outlet blocked
/* Rule 2.5 */
IF pressure high AND release valve closed THEN
release valve stuck
/* Rule 2.6 */
IF steam escaping THEN steam outlet blocked
/* Rule 2.7 */
IF temperature high AND NOT(water level low) THEN
pressure high
/* Rule 2.8 */
IF transducer output low THEN water level low
/* Rule 2.9 */
IF release valve open AND flow rate high THEN
steam escaping
/* Rule 2.10 */
IF flow rate low THEN control valve closed
Abdul
Rahim
Ahmad
10
Rule examination and rule firing
 The task of interpreting and applying the rules is done by
the inference engine
 The application of rules can be broken down as follows:
 selecting rules to examine (available rules)
 determining which are applicable (triggered rules making the
conflict set)
 selecting a rule to fire.
 Rule examination and firing can be forward-chaining
or backward-chaining.
Abdul
Rahim
Ahmad
11
Maintaining Consistency
 A key advantage of rule-based systems is their
flexibility.
 New rules can be added at will, but only if each
rule is written with care, consistent and without
assuming the behavior of other rules.
 Make each rule an accurate statement in its own right
 Rules can contain combinations of conditions and
conclusions.
Abdul
Rahim
Ahmad
12
Closed world assumption
 All propositions are assumed either TRUE or
FALSE.
 Sometimes the rule-writer intends a rule
differently.
 Measures that could be taken to avoid this
ambiguity modify the rules - so that they do not contain any
negative conditions
Abdul
Rahim
Ahmad
 modify the inference engine - ensure that a rule is
examined before another
13
Use of variables within rules
 In real-world systems, variables can be used to
make rules more general
 reducing the number of rules needed
 keeping the rule set manageable.
/* Rule 2.13 */
IF control valve 1 is open THEN
tube 1 is high /* Rule 2.14 */
IF control valve 2 is open THEN
tube 2 is high
/* Rule 2.15 */
IF control valve 3 is open THEN
tube 3 is high
/* Rule 2.16 */
IF control valve 4 is open THEN
tube 4 is high
/* Rule 2.17 */
IF control valve 5 is open THEN
tube 5 is high
Abdul
Rahim
Ahmad
flow rate in
flow rate in
flow rate in
flow rate in
flow rate in
/* Rule 2.18 */
IF control valve ?x is open THEN flow rate in
tube ?x is high
14
Forward-chaining
 Inference engine applies a
strategy for deciding which
rules to apply and when to
apply them.
 Forward-chaining - a datadriven strategy:
 rules are selected and applied
in response to the current fact
base. The fact base comprises
all facts known by the system,
whether derived by rules or
supplied directly
Abdul
Rahim
Ahmad
15
Forward-chaining
 The key points of the scheme are as follows:
 Rules are examined and fired on the basis of the current
fact base, independently of any predetermined goals.
 The set of rules available for examination may comprise all
of the rules or a subset.
 Of the available rules, those whose conditions are satisfied
are said to have been triggered. These rules make up the
conflict set, and the method of selecting a rule from the
conflict set is conflict resolution.
Abdul
Rahim
Ahmad
 Only one rule in a conflict set is fired on a given cycle.
(once a rule has fired, the stored deductions have
potentially changed, and it cannot be guaranteed that other
rules in the conflict set still have their condition satisfied).
16
Alternative form of Forwardchaining
Single instantiation of variables

Abdul
Rahim
Ahmad
conclusions may be performed
using just the first set of
instantiations that are found
Multiple instantiation of variables

conclusions may be performed
repeatedly using all possible
instantiations
17
Example of Forward-chaining
Rules
/* Rule 2.20 */ IF control valve ?x is open THEN flow rate in tube ?x is high
/* Rule 2.21 */ IF flow rate in tube ?x is high THEN close control valve ?x
Suppose that we start with two facts:
control valve 1 is open
control valve 2 is open
In multiple instantiation,
each rule would fire once,
generating conclusions in
the following order:
flow rate in tube 1 is high
flow rate in tube 2 is high
close control valve 1
close control valve 2
Abdul
Rahim
Ahmad
multiple instantiation is a
breadth-first process
Under single instantiation, each cycle of the
inference engine would result in a rule firing on
a single instantiation of the variable x. After
four cycles, conclusions would have been
generated in the
following order:
flow rate in tube 1 is high close control valve 1
flow rate in tube 2 is high close control valve 2
single instantiation is a depth-first process
18
Rete (“ree-tee”) Algorithm
 In forward-chaining, one inefficiency is that once
a rule has been selected from the conflict set
and fired, the conflict set is thrown away and the
process starts all over again.
 Rete algorithm is an efficient approach that
examine only those rules whose condition is
affected by changes made to the fact base on
the previous cycle.
Abdul
Rahim
Ahmad
19
Example of Rete Algorithm
Rules
/* Rule 2.22*/ IF ?p is a pipe of bore ?b AND ?v is a valve of bore ?b
THEN ?p and ?v are compatible
Condition parts of all rules assembled into a Rete network.
Each node represents an atomic condition, i.e., contains a simple test.
Two types of nodes:
• alpha nodes can be satisfied by a single fact
• beta nodes can only be satisfied by a pair of facts.
Abdul
Rahim
Ahmad
Lets say a pipe is found in node α1 with bore
100, and so the fact would be passed on to
node β1. However, node β1 would not be
satisfied yet as it has not received info from
node α2. Imagine, as a result of firing other
rules, another pipe with bore 100 is found in
α2. The Rule 2.2 can be added to conflict
set:
20
Conflict Resolution
 Choosing one rule to fire from those that are able to fire
(conflict set).
 Can choose 3 ways to fire rule in conflict set:
 Fire immediately the first rule that qualifies – first come first
serve. (conflict set is not assembled at all, and the order in
which rules are selected for examination determines the
resolution of conflict)
 Fire according to explicitly stated priority value.
/* Rule 2.1a */ IF water level low THEN open control valve PRIORITY 4.0
/* Rule 2.2a */
IF temperature high and water level low THEN
open control valve AND shut down boiler tubes PRIORITY 9.0
 Use Metarules (knowledge about how that knowledge should
be applied)
Abdul
Rahim
Ahmad
/* Metarule 2.23 */ PREFER rules about shut-down TO rules about
control valves
21
Backward-chaining (a goal-driven
strategy)
 Inference strategy that
assumes the existence of a
goal that needs to be
established or refuted
 Initially, only those rules that
can lead directly to the
fulfillment of the goal are
selected for examination
Abdul
Rahim
Ahmad
 Searching for what rules to
apply next can use a depthfirst or breadth-first
strategy using a search tree
where the nodes of the search
tree are rules.
Depth First Search
22
Depth First Backward chaining
Abdul
Rahim
Ahmad
/* Rule 2.1 */
IF water level low THEN open control valve
/* Rule 2.2 */
IF temperature high AND water level low THEN
open control valve AND shut down boiler tubes
/* Rule 2.3 */
IF steam outlet blocked THEN replace outlet pipe
/* Rule 2.4 */
IF release valve stuck THEN steam outlet blocked
/* Rule 2.5 */
IF pressure high AND release valve closed THEN
release valve stuck
/* Rule 2.6 */
IF steam escaping THEN steam outlet blocked
/* Rule 2.7 */
One of the relevant rules is selected for
examination The system searches the rule base
IF temperature high AND NOT(water level low)
for a rule which can satisfy this goal. If fails the
THEN pressure high
system backtracks to the last place where a
/* Rule 2.8 */
choice between possible rules was made and will
IF transducer output low THEN water level low
try an alternative.
If information is not yet known, it becomes the new /* Rule 2.9 */
goal. This process continues until a goal is satisfied IF release valve open AND flow rate high THEN
by the information in the fact base. When this
steam escaping
happens, the original goal is fulfilled, and the
/* Rule 2.10 */
chosen path through the rules is the solution. If all
IF flow rate low THEN control valve closed
possible ways of achieving the overall goal have
been explored and failed, then the overall goal fails.
23
Breadth First Backward chaining
Abdul
Rahim
Ahmad
All rules at the same depth would be examined.
If all branch fails, then the system will not need
to backtrack, as it will simply carry on down
those branches which still have the potential to
fulfill the goal. The first solution to be found
will be the one with the shortest (or jointshortest) chain of rules.
Require large amounts of computer memory
to keep a record of progress along all branches of
the search tree, rather than just one branch.
/* Rule 2.1 */
IF water level low THEN open control valve
/* Rule 2.2 */
IF temperature high AND water level low THEN
open control valve AND shut down boiler tubes
/* Rule 2.3 */
IF steam outlet blocked THEN replace outlet pipe
/* Rule 2.4 */
IF release valve stuck THEN steam outlet blocked
/* Rule 2.5 */
IF pressure high AND release valve closed THEN
release valve stuck
/* Rule 2.6 */
IF steam escaping THEN steam outlet blocked
/* Rule 2.7 */
IF temperature high AND NOT(water level low)
THEN pressure high
/* Rule 2.8 */
IF transducer output low THEN water level low
/* Rule 2.9 */
IF release valve open AND flow rate high THEN
steam escaping
/* Rule 2.10 */
IF flow rate low THEN control valve closed
24
Prolog
 The inference mechanism built into the Prolog
language is a depth-first backward-chainer.
 There are two sets of circumstances under which
backtracking takes place:
 when a goal cannot be satisfied by the set of rules
currently under consideration; or
 when a goal has been satisfied and the user wants
to investigate other ways of achieving the goal
(i.e., to find other solutions).
Abdul
Rahim
Ahmad
25
Implementation of
backward-chaining
Generalized flowchart for
backward-chaining from a
goal G1.
• We assume each rule has only one
condition.
• Length of the chain of rules cannot be
predetermined.
• A recursive definition is also available
and is important in Prolog.
• Backward-chaining is part the Prolog
language, expert system shells and
artificial intelligence toolkits.
Abdul
Rahim
Ahmad
26
Recursive Definition of Backward
Chaining
define function backwardchain(G);
/* returns a boolean (i.e., true/false) value */
/* G is the goal being validated */
variable S, X, C;
result:= false;
/* ‘:=‘ represents assignment of a value to a variable */
S:= set of rules whose conclusion part matches goal G;
If S is empty then
result:= false;
else
while (result=false) and (S is not empty) do
X:= rule selected from S;
S:= S with X removed;
C:= condition part of X;
if C is true then
result:=true
elseif C is false then
result:=false
elseif (backwardchain(C)=true) then
result:=true;
/* note the recursive call of ‘backwardchain’ */
/* C is the new goal */
endif;
endwhile;
endif;
return result;
Abdul
Rahim
Ahmad
/* ‘result’ is the value returned by the function */
/* ‘backwardchain’ */
enddefine;
27
Variations of backward-chaining
 Use depth-first or breadth-first search for rules;
 Decide whether or not to pursue other solutions
 Different ways of choosing between branches to explore (in depthfirst search only)
 Deciding upon the order in which to examine rules (in breadth-first
search only)
 Whether to fire the sequence of rules or simply to conclude that the
goal is proven (after having found a succession of enabling rules
whose lowest-level conditions are satisfied).
Abdul
Rahim
Ahmad
28
A hybrid strategy
 Makes use of an inference engine that can be seen of as
part forward-chaining and part backward-chaining.
 Example :
 ARBS (Algorithmic and Rule-based Blackboard System).
 In hybrid strategy, a rule dependence network is built
prior to running the system.
 For each rule, the network shows which other rules may enable it
(its antecedents), and which rules it may enable (its dependents)
Abdul
Rahim
Ahmad
29
Rule dependance network
Abdul
Rahim
Ahmad
30
Explanation facilities
 Can be divided into two categories:
 how a conclusion has been derived;
 why a particular line of reasoning is being followed.
 Example explanation for a recommendation to
replace the outlet pipe
Replace outlet pipe BECAUSE (Rule 2.3) steam outlet blocked
steam outlet blocked BECAUSE (Rule 2.4) release valve stuck
release valve stuck BECAUSE (Rule 2.5) pressure high AND release valve closed
pressure high BECAUSE (Rule 2.7) temperature high AND NOT(water level low)
NOT(water level low) BECAUSE (Rule 2.8) NOT(transducer output low)
Release valve closed, temperature high and NOT(transducer output low)
are supplied facts
Abdul
Rahim
Ahmad
31
END
Abdul
Rahim
Ahmad