31
Abdul Rahim Ahmad MITM 613 Intelligent System Chapter 2: Rule-based Systems

Abdul Rahim Ahmad MITM 613 Intelligent System Chapter 2: Rule-based Systems

Embed Size (px)

Citation preview

Abdul Rahim Ahmad

MITM 613Intelligent System

Chapter 2: Rule-based Systems

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 facilitiesAbdul Rahim Ahmad

2

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 isIF 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

3

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

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.

Abdul Rahim Ahmad

4

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

5

Rule Writing

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.Abdul Rahim Ahmad

6

Example rule-based system

Abdul Rahim Ahmad

7

A rule-based system to monitor the state of a power station boiler and to advise appropriate actions.

Boiler produce steam to drive a turbine and generator.

Drum separate the steam from the water.

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.

Steam is taken from the drum, passed through the super heater and applied to the turbine that turns the generator

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.

Power Station Boiler Control

Abdul Rahim Ahmad

8

The following rules have been written 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

Rules 2.4 and the rest …. all involve taking a low-level fact, such as a transducer reading, and deriving a higher-level fact

Rules 2.1, 2.2 2.3 consists of recommendations to the boiler operators

Low-level vs high-level rules

Deep vs shallow rules

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.

Example rule-based system

Abdul Rahim Ahmad

9

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

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

10

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

11

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

modify the inference engine - ensure that a rule is examined before anotherAbdul

Rahim Ahmad

12

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.

Abdul Rahim Ahmad

13

/* Rule 2.18 */IF control valve ?x is open THEN flow rate in tube ?x is high

/* Rule 2.13 */IF control valve 1 is open THEN flow rate in tube 1 is high /* Rule 2.14 */IF control valve 2 is open THEN flow rate in tube 2 is high/* Rule 2.15 */IF control valve 3 is open THEN flow rate in tube 3 is high/* Rule 2.16 */IF control valve 4 is open THEN flow rate in tube 4 is high/* Rule 2.17 */IF control valve 5 is open THEN flow rate in tube 5 is high

Forward-chaining

Inference engine applies a strategy for deciding which rules to apply and when to apply them.

Forward-chaining - a data-driven 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

14

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.

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).Abdul

Rahim Ahmad

15

Alternative form of Forward-chaining Single instantiation of variables

conclusions may be performed using just the first set of instantiations that are found

Abdul Rahim Ahmad

16

Multiple instantiation of variables

conclusions may be performed repeatedly using all possible instantiations

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

Abdul Rahim Ahmad

17

Suppose that we start with two facts:control valve 1 is opencontrol valve 2 is open

In multiple instantiation, each rule would fire once, generating conclusions in the following order:

flow rate in tube 1 is highflow rate in tube 2 is highclose control valve 1close control valve 2

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 thefollowing order:

flow rate in tube 1 is high close control valve 1flow rate in tube 2 is high close control valve 2

single instantiation is a depth-first process

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

18

Example of Rete Algorithm

Abdul Rahim Ahmad

19

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.

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:

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.

Use Metarules (knowledge about how that knowledge should be applied)

Abdul Rahim Ahmad

20

/* Rule 2.1a */ IF water level low THEN open control valve PRIORITY 4.0 /* Rule 2.2a */IF temperature high and water level low THENopen control valve AND shut down boiler tubes PRIORITY 9.0

/* Metarule 2.23 */ PREFER rules about shut-down TO rules about control valves

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

Searching for what rules to apply next can use a depth-first or breadth-first strategy using a search tree where the nodes of the search tree are rules.

Abdul Rahim Ahmad

21

Depth First Search

Depth First Backward chaining

Abdul Rahim Ahmad

22

/* 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

One of the relevant rules is selected for examination The system searches the rule base for a rule which can satisfy this goal. If fails the system backtracks to the last place where a choice between possible rules was made and will try an alternative. If information is not yet known, it becomes the new goal. This process continues until a goal is satisfied by the information in the fact base. When this happens, the original goal is fulfilled, and the chosen path through the rules is the solution. If all possible ways of achieving the overall goal have been explored and failed, then the overall goal fails.

Breadth First Backward chaining

Abdul Rahim Ahmad

23

/* 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

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 joint-shortest) 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.

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

24

Implementation of backward-chaining

Abdul Rahim Ahmad

25

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.

Recursive Definition of Backward Chaining

Abdul Rahim Ahmad

26

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; /* ‘result’ is the value returned by the function */ /* ‘backwardchain’ */enddefine;

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 depth-first 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

27

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

28

Rule dependance network

Abdul Rahim Ahmad

29

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

Abdul Rahim Ahmad

30

Replace outlet pipe BECAUSE (Rule 2.3) steam outlet blockedsteam outlet blocked BECAUSE (Rule 2.4) release valve stuckrelease valve stuck BECAUSE (Rule 2.5) pressure high AND release valve closedpressure 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

END

Abdul Rahim Ahmad

31