Sunteți pe pagina 1din 605

Expert Systems:

Principles and
Programming,
Fourth Edition

Dr. Joseph Giarratano


Chapter 1:
Introduction to
Expert Systems

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn the meaning of an expert system


• Understand the problem domain and knowledge
domain
• Learn the advantages of an expert system
• Understand the stages in the development of an
expert system
• Examine the general characteristics of an expert
system

3
Objectives
• Examine earlier expert systems which have given
rise to today’s knowledge-based systems
• Explore the applications of expert systems in use
today
• Examine the structure of a rule-based expert
system
• Learn the difference between procedural and
nonprocedural paradigms
• What are the characteristics of artificial neural
systems

4
What is an expert system?

“An expert system is a computer system that


emulates, or acts in all respects, with the
decision-making capabilities of a human expert.”

Professor Edward Feigenbaum


Stanford University

5
Fig 1.1 Areas of Artificial
Intelligence

6
Expert system technology
may include:
• Special expert system languages – CLIPS

• Programs

• Hardware designed to facilitate the


implementation of those systems

7
Expert System Main Components

• Knowledge base – obtainable from books,


magazines, knowledgeable persons, etc.

• Inference engine – draws conclusions from the


knowledge base

8
Figure 1.2 Basic Functions
of Expert Systems

9
Problem Domain vs. Knowledge
Domain
• An expert’s knowledge is specific to one problem
domain – medicine, finance, science,
engineering, etc.
• The expert’s knowledge about solving specific
problems is called the knowledge domain.
• The problem domain is always a superset of the
knowledge domain.

10
Figure 1.3 Problem and
Knowledge Domain Relationship

11
Advantages of Expert Systems

• Increased availability
• Reduced cost
• Reduced danger
• Performance
• Multiple expertise
• Increased reliability

12
Advantages Continued

• Explanation

• Fast response

• Steady, unemotional, and complete responses at


all times

• Intelligent tutor

• Intelligent database

13
Representing the Knowledge

The knowledge of an expert system can be


represented in a number of ways, including IF-
THEN rules:

IF you are hungry THEN eat

14
Knowledge Engineering

The process of building an expert system:

1. The knowledge engineer establishes a dialog


with the human expert to elicit knowledge.
2. The knowledge engineer codes the knowledge
explicitly in the knowledge base.
3. The expert evaluates the expert system and
gives a critique to the knowledge engineer.

15
Development of an Expert System

16
The Role of AI

• An algorithm is an ideal solution guaranteed to


yield a solution in a finite amount of time.
• When an algorithm is not available or is
insufficient, we rely on artificial intelligence
(AI).
• Expert system relies on inference – we accept a
“reasonable solution.”

17
Uncertainty

• Both human experts and expert systems must be


able to deal with uncertainty.
• It is easier to program expert systems with
shallow knowledge than with deep knowledge.
• Shallow knowledge – based on empirical and
heuristic knowledge.
• Deep knowledge – based on basic structure,
function, and behavior of objects.

18
Limitations of Expert Systems

• Typical expert systems cannot generalize through


analogy to reason about new situations in the way
people can.

• A knowledge acquisition bottleneck results from


the time-consuming and labor intensive task of
building an expert system.

19
Early Expert Systems

• DENDRAL – used in chemical mass


spectroscopy to identify chemical constituents
• MYCIN – medical diagnosis of illness
• DIPMETER – geological data analysis for oil
• PROSPECTOR – geological data analysis for
minerals
• XCON/R1 – configuring computer systems

20
Table 1.3 Broad Classes
of Expert Systems

21
Problems with Algorithmic
Solutions
• Conventional computer programs generally solve
problems having algorithmic solutions.

• Algorithmic languages include C, Java, and C#.

• Classical AI languages include LISP and


PROLOG.

22
Considerations for Building
Expert Systems
• Can the problem be solved effectively by
conventional programming?
• Is there a need and a desire for an expert system?
• Is there at least one human expert who is willing
to cooperate?
• Can the expert explain the knowledge to the
knowledge engineer can understand it.
• Is the problem-solving knowledge mainly
heuristic and uncertain?

23
Languages, Shells, and Tools

• Expert system languages are post-third


generation.
• Procedural languages (e.g., C) focus on
techniques to represent data.
• More modern languages (e.g., Java) focus on data
abstraction.
• Expert system languages (e.g. CLIPS) focus on
ways to represent knowledge.

24
Expert systems Vs
conventional programs I

25
Expert systems Vs
conventional programs II

26
Expert systems Vs
conventional programs III

27
Elements of an Expert System

• User interface – mechanism by which user and


system communicate.
• Exploration facility – explains reasoning of
expert system to user.
• Working memory – global database of facts used
by rules.
• Inference engine – makes inferences deciding
which rules are satisfied and prioritizing.

28
Elements Continued

• Agenda – a prioritized list of rules created by the


inference engine, whose patterns are satisfied by
facts or objects in working memory.
• Knowledge acquisition facility – automatic way
for the user to enter knowledge in the system
bypassing the explicit coding by knowledge
engineer.
• Knowledge Base – includes the rules of the
expert system

29
Production Rules

• Knowledge base is also called production


memory.

• Production rules can be expressed in IF-THEN


pseudocode format.

• In rule-based systems, the inference engine


determines which rule antecedents are satisfied
by the facts.

30
Figure 1.6 Structure of a
Rule-Based Expert System

31
Rule-Based ES

32
Example Rules

33
Inference Engine Cycle

34
Foundation of Expert Systems

35
General Methods of Inferencing

• Forward chaining (data-driven)– reasoning from


facts to the conclusions resulting from those facts
– best for prognosis, monitoring, and control.
– Examples: CLIPS, OPS5
• Backward chaining (query/Goal driven)–
reasoning in reverse from a hypothesis, a
potential conclusion to be proved to the facts that
support the hypothesis – best for diagnosis
problems.
– Examples: MYCIN

36
Production Systems

• Rule-based expert systems – most popular type


today.
• Knowledge is represented as multiple rules that
specify what should/not be concluded from
different situations.
• Forward chaining – start w/facts and use rules do
draw conclusions/take actions.
• Backward chaining – start w/hypothesis and look
for rules that allow hypothesis to be proven true.

37
Post Production System

• Basic idea – any mathematical / logical system is


simply a set of rules specifying how to change
one string of symbols into another string of
symbols.
• these rules are also known as rewrite rules
• simple syntactic string manipulation
• no understanding or interpretation is required\also used to
define grammars of languages
– e.g BNF grammars of programming languages.

• Basic limitation – lack of control mechanism to


guide the application of the rules.
38
Markov Algorithm

• An ordered group of productions applied in order


or priority to an input string.
• If the highest priority rule is not applicable, we
apply the next, and so on.
• inefficient algorithm for systems with many
rules.
• Termination on (1) last production not applicable
to a string, or (2) production ending with period
applied
• Can be applied to substrings, beginning at left
39
Markov Algorithm

40
Rete Algorithm
• Markov: too inefficient to be used with many rules
• Functions like a net – holding a lot of information.
• Much faster response times and rule firings can occur
compared to a large group of IF-THEN rules which
would have to be checked one-by-one in conventional
program.
• Takes advantage of temporal redundancy and structural
similarity.
• Looks only for changes in matches (ignores static data)
• Drawback is high memory space requirements.

41
Procedural Paradigms

• Algorithm – method of solving a problem in a


finite number of steps.

• Procedural programs are also called sequential


programs.

• The programmer specifies exactly how a problem


solution must be coded.

42
Figure 1.8 Procedural
Languages

43
Imperative Programming

• Also known as statement-oriented


• During execution, program makes
transition from the initial state to the final
state by passing through series of
intermediate states.
• Provide rigid control and top-down-design.
• Not efficient for directly implementing
expert systems.
44
Functional Programming

• Function-based (association, domain, co-


domain); f: S T
• Not much control
• Bottom-up combine simple functions to yield
more powerful functions.
• Mathematically a function is an association or
rule that maps members of one set, the domain,
into another set, the codomain.
• e.g. LISP and Prolog

45
Nonprocedural Paradigms

• Do not depend on the programmer giving exact


details how the program is to be solved.
• Declarative programming – goal is separated
from the method to achieve it.
• Object-oriented programming – partly imperative
and partly declarative – uses objects and methods
that act on those objects.
• Inheritance – (OOP) subclasses derived from
parent classes.

46
Figure 1.9 Nonprocedural
Languages

47
What are Expert Systems?

Can be considered declarative languages:

• Programmer does not specify how to achieve a


goal at the algorithm level.

• Induction-based programming – the program


learns by generalizing from a sample.

48
Artificial Neural Systems

In the 1980s, a new development in programming


paradigms appeared called artificial neural
systems (ANS).
• Based on the way the brain processes
information.
• Models solutions by training simulated neurons
connected in a network.
• ANS are found in face recognition, medical
diagnosis, games, and speech recognition.

49
ANS Characteristics

• A complex pattern recognition problem –


computing the shortest route through a given list
of cities.
• ANS is similar to an analog computer using
simple processing elements connected in a highly
parallel manner.
• Processing elements perform Boolean /
arithmetic functions in the inputs
• Key feature is associating weights w/each
element.
50
Table 1.13 Traveling
Salesman Problem

51
Advantages of ANS

• Storage is fault tolerant


• Quality of stored image degrades gracefully in
proportion to the amount of net removed.
• Nets can extrapolate (extend) and interpolate
(insert/estimate) from their stored information.
• Nets have plasticity.
• Excellent when functionality is needed long-term
w/o repair in hostile environment – low
maintenance.

52
Disadvantage of ANS

• ANS are not well suited for number crunching or


problems requiring optimum solution.

53
Figure 1.10 Neuron
Processing Element

54
Sigmoid Function

55
Figure 1.11 A
Back-Propagation Net

56
Figure 1.12 Hopfield
Artificial Neural Net

57
MACIE

• An inference engine called MACIE (Matrix


Controlled Inference Engine) uses ANS
knowledge base.
• Designed to classify disease from symptoms into
one of the known diseases the system has been
trained on.
• MACIE uses forward chaining to make
inferences and backward chaining to query user
for additional data to reach conclusions.

58
Summary

• During the 20th Century various definitions of AI


were proposed.
• In the 1960s, a special type of AI called expert
systems dealt with complex problems in a narrow
domain, e.g., medical disease diagnosis.
• Today, expert systems are used in a variety of
fields.
• Expert systems solve problems for which there
are no known algorithms.

59
Summary Continued

• Expert systems are knowledge-based – effective


for solving real-world problems.
• Expert systems are not suited for all applications.
• Future advances in expert systems will hinge on
the new quantum computers and those with
massive computational abilities in conjunction
with computers on the Internet.

60
Chapter 2:
The Representation of
Knowledge
Objectives

• Introduce the study of logic


• Learn the difference between formal logic and
informal logic
• Learn the meaning of knowledge and how it can
be represented
• Learn about semantic nets
• Learn about object-attribute-value triples

62
Objectives Continued

• See how semantic nets can be translated into


Prolog
• Explore the limitations of semantic nets
• Learn about schemas
• Learn about frames and their limitations
• Learn how to use logic and set symbols to
represent knowledge

63
Objectives Continued

• Learn about propositional and first order


predicate logic

• Learn about quantifiers

• Explore the limitations of propositional and


predicate logic

64
What is the study of logic?

• Logic is the study of making inferences – given a


set of facts, we attempt to reach a true
conclusion.
• An example of informal logic is a courtroom
setting where lawyers make a series of inferences
hoping to convince a jury / judge .
• Formal logic (symbolic logic) is a more rigorous
approach to proving a conclusion to be true /
false.

65
Why is Logic Important

• We use logic in our everyday lives – “should I


buy this car”, “should I seek medical attention”.
• People are not very good at reasoning because
they often fail to separate word meanings with
the reasoning process itself.
• Semantics refers to the meanings we give to
symbols.

66
The Goal of Expert Systems

• We need to be able to separate the actual


meanings of words with the reasoning process
itself.
• We need to make inferences w/o relying on
semantics.
• We need to reach valid conclusions based on
facts only.

67
Knowledge in Expert Systems

• Knowledge representation is key to the success of


expert systems.
• Expert systems are designed for knowledge
representation based on rules of logic called
inferences.
• Knowledge affects the development, efficiency,
speed, and maintenance of the system.

68
Definitions of Knowledge
a)
(1) the fact or condition of knowing something with familiarity
gained through experience or association (2)acquaintance with or
understanding of a science, art, or technique
b)
(1) the fact or condition of being aware of something
(2) the range of one's information or understanding

c) the circumstance or condition of apprehending truth or fact


through reasoning : cognition

d) the fact or condition of having information or of being learned

69
Epistemology

• Epistemology is the formal study of knowledge .

• Concerned with nature, structure, and origins of


knowledge.

70
Categories of Epistemology

•Philosophy •A priori

•A posteriori •Procedural

•Declarative •Tacit

71
A Priori Knowledge

• Also called “theoretical knowledge”


• “That which precedes”
• Independent of the senses
• Universally true
• Cannot be denied without contradiction
• e.g., coin flips will give 50% heads and 50%
tails

72
A Posteriori Knowledge
• Also called “empirical knowledge”
• “That which follows”
• Derived from the senses
• Now always reliable
• Deniable on the basis of new knowledge w/o
the necessity of contradiction
• E.g., 100 coin flips give only 39 heads – what
can you conclude?

73
Procedural Knowledge

Knowing how to do something:

• Fix a watch
• Install a window
• Brush your teeth
• Ride a bicycle

74
Declarative Knowledge

• Knowledge that something is true or false

• Usually associated with declarative statements

• E.g., “Don’t touch that hot wire.”

75
Tacit Knowledge

• Unconscious knowledge

• Cannot be expressed by language

• E.g., knowing how to walk, breath, etc.

76
Knowledge in Rule-Based
Systems
• Knowledge is part of a hierarchy.

• Knowledge refers to rules that are activated by


facts or other rules.

• Activated rules produce new facts or conclusions.

• Conclusions are the end-product of inferences


when done according to formal rules.

77
Knowledge in Rule-Based Systems II

78
Expert Systems vs. ANS

• ANS does not make inferences but searches for


underlying patterns.

• Expert systems
o Draw inferences using facts
o Separate data from noise
o Transform data into information
o Transform information into knowledge

79
Metaknowledge
 Metaknowledge is knowledge about knowledge
and expertise.
 Most successful expert systems are restricted to as
small a domain as possible.
 In an expert system, an ontology is the
metaknowledge that describes everything known
about the problem domain.
 Wisdom is the metaknowledge of determining the
best goals of life and how to obtain them.

80
Figure 2.2 The Pyramid
of Knowledge

81
Knowledge Representation Methods

A number of knowledge-representation techniques


have been devised:
• Production Rules
• Semantic nets
• Frames
• Scripts
• Logic
• Conceptual graphs

82
Production Rules
• Frequently used to formulate the knowledge in
expert systems.
• A formal variation is Backus-Naur form (BNF)
– metalanguage for the definition of language
syntax
– a grammar is a complete, unambiguous set of
production rules for a specific language
– a parse tree is a graphic representation of a
sentence in that language
– provides only a syntactic description of the
language 83
• not all sentences make sense
Example: Production Rules

84
Example: Parse Tree
of a Sentence

85
86
87
Advantages and Disadvantages of
Production Rules
Advantages:
• simple and easy to understand
• straightforward implementation
• formal foundations for some variants
Disadvantages:
• simple implementations are very inefficient
• some types of knowledge are not easily expressed
in such rules
• large sets of rules become difficult to understand
and maintain

88
Semantic Nets
• A classic representation technique for propositional
information (sometimes called propositional net)
• Propositions – a form of declarative knowledge, stating
facts (true/false)
• Propositions are called “atoms” – cannot be further
subdivided.
• Semantic nets consist of nodes (objects, concepts,
situations) and arcs or links (relationships between them).
• For nodes
– Labels indicate the name
– Nodes can be instances (individual objects) or classes (generic
nodes)

89
Links-Semantic Nets

Links represent relationships


– The relationships contain the structural
information of the knowledge to be
represented
– The label indicates the type of the relationship
Common types of links:
– IS-A – relates an instance or individual to a
generic class
– A-KIND-OF – relates generic nodes to generic
nodes 90
91
Semantic Net Example

92
93
94
95
96
97
Object-Attribute-Value Triple

• One problem with semantic nets is lack of


standard definitions for link names (IS-A, AKO,
etc.).
• The OAV triplet can be used to characterize all
the knowledge in a semantic net.

98
99
Problems with Semantic Nets
Disadvantages of semantic nets could be classified as:
• Expressiveness
– no internal structure of nodes
– relationships between multiple nodes
– no easy way to represent heuristic information
– extensions are possible, but cumbersome
– best suited for binary relationships
• Efficiency
– may result in large sets of nodes and links
– search may lead to combinatorial explosion
• especially for queries with negative results
• Usability
– lack of standards for link types
– naming of nodes
• classes, instances

100
101
102
103
104
105
106
107
Schemata

• Knowledge Structure – an ordered collection of


knowledge – not just data.
• Semantic Nets – are shallow knowledge
structures – all knowledge is contained in nodes
and links.
• Schema is a more complex knowledge structure
than a semantic net.
• In a schema, a node is like a record which may
contain data, records, and/or pointers to nodes.
108
Frames

• One type of schema is a frame (or script – time-


ordered sequence of frames).
• Frames are useful for simulating commonsense
knowledge.
• Semantic nets provide 2-dimensional knowledge;
frames provide 3-dimensional.
• Frames represent related knowledge about
narrow subjects having much default knowledge.

109
Frames (Cont.)

• A frame is a group of slots and fillers that defines


a stereotypical object that is used to represent
generic as well as specific knowledge.
• Commonsense knowledge is knowledge that is
generally known.
• Prototypes are objects possessing all typical
characteristics of whatever is being modeled.

110
Frames (Cont.)

111
Simple Frame Example

112
Figure 2.8 A Car Frame

113
Frame Structure

114
Slots

115
Usage of Frame

116
Restaurant Frame Example

117
Generic RESTAURANT Frame

Specialization-of: Business-Establishment
Types:
range: Generic Restaurant Frame
(Cafeteria, Fast-Food, Seat-Yourself, Wait-To-Be-Seated)
default: Seat-Yourself
if-needed: IF plastic-orange-counter THEN Fast-Food,
IF stack-of-trays THEN Cafeteria,
IF wait-for-waitress-sign or reservations-made THEN Wait-To-Be-Seated,
OTHERWISE Seat-Yourself.
Location:
range: an ADDRESS
if-needed: (Look at the MENU)
Name:
if-needed: (Look at the MENU)
Food-Style:
range: (Burgers, Chinese, American, Seafood, French)
default: American
if-added: (Update Alternatives of Restaurant)
Times-of-Operation:
range: a Time-of-Day
default: open evenings except Mondays
Payment-Form:
range: (Cash, CreditCard, Check, Washing-Dishes-Script)
Event-Sequence:
default: Eat-at-Restaurant Script
Alternatives:
range: all restaurants with same Foodstyle 118
if-needed: (Find all Restaurants with the same Foodstyle) [Rogers 1999]
Restaurant Script
EAT-AT-RESTAURANT Script

Props: (Restaurant, Money, Food, Menu, Tables, Chairs)


Roles: (Hungry-Persons, Wait-Persons, Chef-Persons)
Point-of-View: Hungry-Persons
Time-of-Occurrence: (Times-of-Operation of Restaurant)
Place-of-Occurrence: (Location of Restaurant)
Event-Sequence:
first: Enter-Restaurant Script
then: if (Wait-To-Be-Seated-Sign or Reservations)
then Get-Maitre-d's-Attention Script
then: Please-Be-Seated Script
then: Order-Food-Script
then: Eat-Food-Script unless (Long-Wait) when Exit-Restaurant-Angry Script
then: if (Food-Quality was better than Palatable)
then Compliments-To-The-Chef Script
then: Pay-For-It-Script
finally: Leave-Restaurant Script
119
[Rogers 1999]
Frame Advantages
• fairly intuitive for many applications
– similar to human knowledge organization
– suitable for causal knowledge
– easier to understand than logic or rules
• very flexible

120
Frame Problems
• it is tempting to use frames as definitions of
concepts
– not appropriate because there may be valid
instances of a concept that do not fit the
stereotype
– exceptions can be used to overcome this
• can get very messy
• inheritance
– not all properties of a class stereotype should
be propagated to subclasses
– alteration of slots can have unintended 121
Logic and Sets

• Knowledge can also be represented by symbols


of logic.

• Logic is the study of rules of exact reasoning –


inferring conclusions from premises.

• Automated reasoning – logic programming in the


context of expert systems.

122
Forms of Logic

• Earliest form of logic was based on the syllogism


– developed by Aristotle.
• Syllogisms – have two premises that provide
evidence to support a conclusion.
• Example:
– Premise: All cats are climbers.
– Premise: Garfield is a cat.
– Conclusion: Garfield is a climber.

123
Venn Diagrams

• Venn diagrams can be used to represent


knowledge.
• Universal set is the topic of discussion.
• Subsets, proper subsets, intersection, union ,
contained in, and complement are all familiar
terms related to sets.
• An empty set (null set) has no elements.

124
Figure 2.13 Venn Diagrams

125
Propositional Logic

• Formal logic is concerned with syntax of


statements, not semantics.
• Syllogism:
• All goons are loons.
• Zadok is a goon.
• Zadok is a loon.
• The words may be nonsense, but the form is
correct – this is a “valid argument.”
126
Boolean vs. Aristotelian Logic

• Existential import – states that the subject of the


argument must have existence.

• “All elves wear pointed shoes.” – not allowed


under Aristotelian view since there are no elves.

• Boolean view relaxes this by permitting


reasoning about empty sets.

127
Figure 2.14 Intersecting Sets

128
Boolean Logic

• Defines a set of axioms consisting of symbols to


represent objects / classes.
• Defines a set of algebraic expressions to
manipulate those symbols.
• Using axioms, theorems can be constructed.
• A theorem can be proved by showing how it is
derived from a set of axioms.

129
Features of Propositional Logic

• Concerned with the subset of declarative sentences that


can be classified as true or false.
• We call these sentences “statements” or “propositions”.
• Paradoxes – statements that cannot be classified as true or
false.
• Open sentences – statements that cannot be answered
absolutely.
– Spinach tastes wonderful. (may or may not be true for a person)
– He is tall. (he is a variable)

130
Features Continued

• Compound statements – formed by using logical


connectives (e.g., AND, OR, NOT, conditional,
and biconditional) on individual statements.

• Material implication – p  q states that if p is


true, it must follow that q is true.

• Biconditional – p  q states that p implies q and


q implies p.
131
Features Continued

• Tautology – a statement that is true for all


possible cases.

• Contradiction – a statement that is false for all


possible cases.

• Contingent statement – a statement that is neither


a tautology nor a contradiction.

132
Truth Tables

133
Predicate Logic

• Compared to propositional logic, includes


predicates as well as Universal and Existential
Quantifiers.
• More details in the next chapter!

134
Universal Quantifier

• The universal quantifier, represented by the


symbol  means “for every” or “for all”.
(  x) (x is a rectangle  x has four sides)

• The existential quantifier, represented by the


symbol  means “there exists”.
(  x) (x – 3 = 5)

• Limitations of predicate logic – most quantifier.


135
Figure 2.6: General Organization of a
PROLOG System

136
PROLOG and Semantic Nets

• In PROLOG, predicate expressions consist of the


predicate name, followed by zero or more
arguments enclosed in parentheses, separated by
commas.
• Example:
mother(becky,heather)
means that becky is the mother of heather

137
PROLOG Continued

• Programs consist of facts and rules in the


general form of goals.
• General form: p:- p1, p2, …, pN
p is called the rule’s head and the pi
represents the subgoals
• Example:
spouse(x,y) :- wife(x,y)
x is the spouse of y if x is the wife of y
138
Chapter 3:
Methods of
Inference
Objectives

• Learn the definitions of trees, lattices, and graphs


• Learn about state and problem spaces
• Learn about AND-OR trees and goals
• Explore different methods and rules of inference
• Learn the characteristics of first-order predicate
logic and logic systems

140
Objectives

• Discuss the resolution rule of inference,


resolution systems, and deduction
• Compare shallow and causal reasoning
• How to apply resolution to first-order predicate
logic
• Learn the meaning of forward and backward
chaining

141
Objectives

• Explore additional methods of inference

• Learn the meaning of Metaknowledge

• Explore the Markov decision process

142
Trees

• A tree is a hierarchical data structure consisting


of:
– Nodes – store information
– Branches – connect the nodes
• The top node is the root, occupying the highest
hierarchy.
• The leaves are at the bottom, occupying the
lowest hierarcy.

143
Trees

• Every node, except the root, has exactly one


parent.
• Every node may give rise to zero or more child
nodes.
• A binary tree restricts the number of children per
node to a maximum of two.
• Degenerate trees have only a single pathway
from root to its one leaf.
144
Figure 3.1 Binary Tree

145
Graphs

• Graphs are sometimes called a network or net.


• A graph can have zero or more links between
nodes – there is no distinction between parent
and child.
• Sometimes links have weights – weighted graph;
or, arrows – directed graph.
• Simple graphs have no loops – links that come
back onto the node itself.

146
Graphs
• A circuit (cycle) is a path through the graph beginning
and ending with the same node.
• Acyclic graphs have no cycles.
• Connected graphs have links to all the nodes.
• Digraphs are graphs with directed links.
• Lattice is a directed acyclic graph.
• A Degenerate tree is a tree with only a single path from
the root to its one leaf.

147
Figure 3.2 Simple Graphs

148
Making Decisions

• Trees / lattices are useful for classifying objects


in a hierarchical nature.
• Trees / lattices are useful for making decisions.
• We refer to trees / lattices as structures.
• Decision trees are useful for representing and
reasoning about knowledge.

149
Binary Decision Trees
• Every question takes us down one level in the
tree.
• A binary decision tree having N nodes:
– All leaves will be answers.
– All internal nodes are questions.
– There will be a maximum of 2N answers for N
questions.
• Decision trees can be self learning.
• Decision trees can be translated into production
rules.
150
Decision Tree Example

151
State and Problem Spaces

• A state space can be used to define an object’s


behavior.

• Different states refer to characteristics that define


the status of the object.

• A state space shows the transitions an object can


make in going from one state to another.

152
Finite State Machine

• A FSM is a diagram describing the finite number


of states of a machine.
• At any one time, the machine is in one particular
state.
• The machine accepts input and progresses to the
next state.
• FSMs are often used in compilers and validity
checking programs.

153
Using FSM to Solve Problems

• Characterizing ill-structured problems – one


having uncertainties.
• Well-formed problems:
– Explicit problem, goal, and operations are
known
– Deterministic – we are sure of the next state
when an operator is applied to a state.
– The problem space is bounded.
– The states are discrete.
154
Figure 3.5 State Diagram for a Soft Drink Vending
Machine Accepting Quarters (Q) and Nickels (N)

155
156
AND-OR Trees and Goals

• 1990s, PROLOG was used for commercial


applications in business and industry.
• PROLOG uses backward chaining to divide
problems into smaller problems and then solves
them.
• AND-OR trees also use backward chaining.
• AND-OR-NOT lattices use logic gates to
describe problems.

157
158
159
Types of Logic
 Deduction – reasoning where conclusions must
follow from premises (general to specific)
 Induction – inference is from the specific case to the
general
 Intuition – no proven theory-Recognizing a
pattern(unconsciously) ANN
 Heuristics – rules of thumb based on experience
 Generate and test – trial and error – often used to
reach efficiency.

160
Types of Logic
 Abduction – reasoning back from a true condition
to the premises that may have caused the condition
 Default – absence of specific knowledge
 Autoepistemic – self-knowledge…The color of the
sky as it appears to you.
 Nonmonotonic – New evidence may invalidate
previous knowledge
 Analogy – inferring conclusions based on
similarities with other situations  ANN
 Commonsense knowledge – A combination of all
based on our experience

161
Deductive Logic

• Argument – group of statements where the last is


justified on the basis of the previous ones
• Deductive logic can determine the validity of an
argument.
• Syllogism – has two premises and one conclusion
• Deductive argument – conclusions reached by
following true premises must themselves be true

162
Syllogisms vs. Rules

• Syllogism:
– All basketball players are tall.
– Jason is a basketball player.
Jason is tall.

• IF-THEN rule:
IF All basketball players are tall and
Jason is a basketball player
THEN Jason is tall.
163
Categorical Syllogism
Premises and conclusions are defined using
categorical statements of the form:

164
Categorical Syllogisms

165
Categorical Syllogisms

166
Proving the Validity of Syllogistic
Arguments Using Venn Diagrams
1. If a class is empty, it is shaded.
2. Universal statements, A and E are always drawn
before particular ones.
3. If a class has at least one member, mark it with an *.
4. If a statement does not specify in which of two
adjacent classes an object exists, place an * on the
line between the classes.
5. If an area has been shaded, no * can be put in it.

Review pages 131 to 135

167
Proving the Validity of Syllogistic
Arguments Using Venn Diagrams

Invalid

168
Valid
Proving the Validity of Syllogistic
Arguments Using Venn Diagrams

Valid

169
Rules of Inference

• Venn diagrams are insufficient for complex


arguments.

• Syllogisms address only a small portion of the


possible logical statements.

• Propositional logic offers another means of


describing arguments  Variables

170
Direct Reasoning
Modus Ponens

171
Truth Table Modus Ponens

172
Some Rules of Inference
(Modus Ponens)

173
Rules of Inference

174
The Modus Meanings

175
The Conditional and Its Variants

176
Requirements of a Formal System

1. An alphabet of symbols
2. A set of finite strings of these symbols, the
wffs.
3. Axioms, the definitions of the system.
4. Rules of inference, which enable a wff to be
deduced as the conclusion of a finite set of
other wffs – axioms or other theorems of the
logic system.

177
Requirements of a FS Continued

5. Completeness – every wff can either be proved


or refuted.
6. The system must be sound – every theorem is a
logically valid wff.

178
Logic Systems
A logic system consists of four parts:
• Alphabet: a set of basic symbols from which more
complex sentences are made.
• Syntax: a set of rules or operators for constructing
expressions (sentences).
• Semantics: for defining the meaning of the
sentences
• Inference rules: for constructing semantically
equivalent but syntactically different sentences

179
WFF and Wang’s Propositional
Theorem Proofer
• Well Formed Formula for Propositional Calculus
• Wang’s Propositional Theorem Proofer

Handouts are provided in the class

180
Predicate Logic

• Syllogistic logic can be completely described by


predicate logic.

• The Rule of Universal Instantiation states that an


individual may be substituted for a universe.

• Compared to propositional logic, it has


predicates, universal and existential quantifies.
181
Predicate Logic

The following types of symbols are allowed in


predicate logic:
• Terms
• Predicates
• Connectives
• Quantifiers

182
Predicate Logic
Terms:
• Constant symbols: symbols, expressions, or entities
which do not change during execution (e.g., true /
false)
• Variable symbols: represent entities that can change
during execution
• Function symbols: represent functions which
process input values on a predefined list of
parameters and obtain resulting values

183
Predicate Logic
Predicates:
• Predicate symbols: represent true/false-type
relations between objects. Objects are represented
by constant symbols.

184
Predicate Logic
Connectives:
• Conjunction
• Disjunction
• Negation
• Implication
• Equivalence
… (same as for propositional calculus)

185
Predicate Logic
Quantifiers:
• valid for variable symbols
• Existential quantifier: “There exists at least one
value for x from its domain.”
• Universal quantifier: “For all x in its domain.”

186
First-Order Logic
• First-order logic allows quantified variables to refer
to objects, but not to predicates or functions.
• For applying an inference to a set of predicate
expressions, the system has to process matches of
expressions.
• The process of matching is called unification.

187
PROLOG
Programming in Logic

188
PROLOG: Horn Clauses

189
PROLOG: Facts

190
Knowledge Representation

191
PROLOG: Architecture

192
Family Example: Facts

193
Family Example: Rules

194
PROLOG Sample Dialogue

195
PROLOG Sample Inference

196
PROLOG Sample Inference

197
Shallow and Causal Reasoning

• Experiential knowledge is based on experience.


• In shallow reasoning, there is little/no causal
chain of cause and effect from one rule to
another.
• Advantage of shallow reasoning is ease of
programming.
• Frames are used for causal / deep reasoning.
• Causal reasoning can be used to construct a
model that behaves like the real system.
198
Chaining

• Chain – a group of multiple inferences that


connect a problem with its solution
• A chain that is searched / traversed from a
problem to its solution is called a forward chain.
• A chain traversed from a hypothesis back to the
facts that support the hypothesis is a backward
chain.
• Problem with backward chaining is find a chain
linking the evidence to the hypothesis.
199
Causal Forward Chaining

200
Backward Chaining

201
Some Characteristics of
Forward and Backward Chaining

202
Figure 3.14 Types of Inference

203
Metaknowledge

• The Markov decision process (MDP) is a good


application to path planning.
• In the real world, there is always uncertainty, and
pure logic is not a good guide when there is
uncertainty.
• A MDP is more realistic in the cases where there
is partial or hidden information about the state
and parameters, and the need for planning.

204
Chapter 4:
Reasoning Under
Uncertainty
Objectives

• Learn the meaning of uncertainty and explore


some theories designed to deal with it
• Find out what types of errors can be attributed to
uncertainty and induction
• Learn about classical probability, experimental,
and subjective probability, and conditional
probability
• Explore hypothetical reasoning and backward
induction
206
10/11/2019
Objectives

• Examine temporal reasoning and Markov chains


• Define odds of belief, sufficiency, and necessity
• Determine the role of uncertainty in inference
chains
• Explore the implications of combining evidence
• Look at the role of inference nets in expert
systems and see how probabilities are propagated

207
10/11/2019
How to Expert Systems Deal with
Uncertainty?
• Expert systems provide an advantage when
dealing with uncertainty as compared to decision
trees.
• With decision trees, all the facts must be known
to arrive at an outcome.
• Probability theory is devoted to dealing with
theories of uncertainty.
• There are many theories of probability – each
with advantages and disadvantages.
208
10/11/2019
What is Uncertainty?

• Uncertainty is essentially lack of information to


formulate a decision.
• Uncertainty may result in making poor or bad
decisions.
• As living creatures, we are accustomed to dealing
with uncertainty – that’s how we survive.
• Dealing with uncertainty requires reasoning
under uncertainty along with possessing a lot of
common sense.
209
10/11/2019
Dealing with Uncertainty

• Deductive reasoning – deals with exact facts and


exact conclusions
• Inductive reasoning – not as strong as deductive
– premises support the conclusion but do not
guarantee it.
• There are a number of methods to pick the best
solution in light of uncertainty.
• When dealing with uncertainty, we may have to
settle for just a good solution.
210
10/11/2019
Theories to Deal with
Uncertainty
• Bayesian Probability
• Hartley Theory
• Shannon Theory
• Dempster-Shafer Theory
• Markov Models
• Zadeh’s Fuzzy Theory

211
10/11/2019
Errors Related to
Hypothesis
• Many types of errors contribute to uncertainty.

– Type I Error – accepting a hypothesis when it


is not true – False Positive.

– Type II Error – Rejecting a hypothesis when it


is true – False Negative

212
10/11/2019
Errors Related to
Measurement
• Errors of precision – how well the truth is known
• Errors of accuracy – whether something is true or
not
• Unreliability stems from faulty measurement of
data – results in erratic data.
• Random fluctuations – termed random error
• Systematic errors result from bias

213
10/11/2019
Errors in Induction
• Where deduction proceeds from general to
specific, induction proceeds from specific to
general.
• Inductive arguments can never be proven correct
(except in mathematical induction).
• Expert systems may consist of both deductive
and inductive rules based on heuristic
information.
• When rules are based on heuristics, there will be
uncertainty.
214
10/11/2019
Deductive and Inductive Reasoning
about Populations and Samples

215
10/11/2019
Types of Errors

216
10/11/2019
Examples of Common Types of
Errors

217
10/11/2019
Classical Probability

• First proposed by Pascal and Fermat in 1654


• Also called a priori probability because it deals
with ideal games or systems:
– Assumes all possible events are known
– Each event is equally likely to happen
• Fundamental theorem for classical probability is
P = W / N, where W is the number of wins and N
is the number of equally possible events.

218
10/11/2019
Deterministic vs.
Nondeterministic Systems
• When repeated trials give the exact same results,
the system is deterministic.
• Otherwise, the system is nondeterministic.
• Nondeterministic does not necessarily mean
random – could just be more than one way to
meet one of the goals given the same input.

219
10/11/2019
Three Axioms of Formal
Theory of Probability

If E1 and E2 are mutually exclusive events

220
10/11/2019
Experimental and Subjective
Probabilities
• Experimental probability defines the probability
of an event, as the limit of a frequency
distribution:

• Experimental probability is also called a


posteriori (after the event)
• Subjective probability deals with events that are
not reproducible and have no historical basis on
which to extrapolate.

221
Compound Probabilities
• Compound probabilities can be expressed by:

S is the sample space and A and B are events.

• Independent events are events that do not affect


each other. For pairwise independent events,

222
Additive Law

223
10/11/2019
Conditional Probabilities
• The probability of an event A occurring, given
that event B has already occurred is called
conditional probability:

224
Sample Space of Intersecting Events

225
10/11/2019
Advantages and Disadvantages of
Probabilities
• Advantages:
– formal foundation
– reflection of reality (posteriori)
• Disadvantages:
– may be inappropriate
• the future is not always similar to the past
– inexact or incorrect
• especially for subjective probabilities
– Ignorance
• probabilities must be assigned even if no information is available
– assigns an equal amount of probability to all such items
– non-local reasoning
• requires the consideration of all available evidence, not only from the rules
currently under consideration
– no compositionality
• complex statements with conditional dependencies can not be decomposed
into independent parts 226
10/11/2019
Bayes’ Theorem
• This is the inverse of conditional probability.
• Find the probability of an earlier event given
that a later one occurred.

227
Hypothetical Reasoning
Backward Induction
• Bayes’ Theorem is commonly used for decision tree
analysis of business and social sciences.
• especially useful in diagnostic systems
• medicine, computer help systems
• inverse or a posteriori probability
• inverse to conditional probability of an earlier
event given that a later one occurred
• PROSPECTOR (expert system) achieved great fame as
the first expert system to discover a valuable
molybdenum deposit worth $100,000,000.

228
10/11/2019
Bayes’ Rule for Multiple
Events
Multiple hypotheses Hi, multiple events E1,
…, En
P(Hi|E1, E2, …, En) =
(P(E1, E2, …, En|Hi) * P(Hi)) / P(E1,
E2, …, En)
or
P(Hi|E1, E2, …, En)=
(P(E1|Hi) * P(E2|Hi) * …* P(En|Hi) *
P(Hi)) /
229
Advantages and Disadvantages
of Bayesian Reasoning
Advantages:
• sound theoretical foundation
• well-defined semantics for decision making
Disadvantages:
• requires large amounts of probability data
• subjective evidence may not be reliable
• independence of evidences assumption often not
valid
• relationship between hypothesis and evidence is
reduced to a number
• explanations for the user difficult
• high computational overhead
230
Temporal Reasoning

• Reasoning about events that depend on time


• Expert systems designed to do temporal
reasoning to explore multiple hypotheses in real
time are difficult to build.
• One approach to temporal reasoning is with
probabilities – a system moving from one state to
another over time.
• The process is stochastic if it is probabilistic.

231
10/11/2019
Markov Chain Process

• Transition matrix – represents the probabilities


that the system in one state will move to another.
• State matrix – depicts the probabilities that the
system is in any certain state.
• One can show whether the states converge on a
matrix called the steady-state matrix – a time of
equilibrium

232
10/11/2019
Markov Chain Characteristics

1. The process has a finite number of possible


states.
2. The process can be in one and only one state at
any one time.
3. The process moves or steps successively from
one state to another over time.
4. The probability of a move depends only on the
immediately preceding state.

233
10/11/2019
State Diagram
Interpretation of a Transition Matrix

234
10/11/2019
The Odds of Belief

• To make expert systems work for use, we must


expand the scope of events to deal with
propositions.
• Rather than interpreting conditional probabilities
P(A|B) in the classical sense, we interpret it to
mean the degree of belief that A is true, given B.
• We talk about the likelihood of A, based on some
evidence B.
• This can be interpreted in terms of odds.
235
10/11/2019
The Odds of Belief (cont.)

• The conditional probability could be referred to


as the likelihood.
• Likelihood could be interpreted in terms of odds
of a bet.
• The odds of A against B given some event C is:
P( A | C ) P( A | C )
odds  If B  A': odds 
P( B | C ) 1  P( A | C )
defining P  P( A | C ) then :
P wins odds
odds  ( ) and P 
1 P losses 1  odds
236
10/11/2019
The Odds of Belief (cont.)
• The likelihood of P=95% (for the car to start in the
morning) is thus equivalent to:

P 0.95
odds    19 to 1
1  P 1  0.95

• Probability is natural forward chaining or deductive while


likelihood is backward chaining and inductive.
• Although we use the same simbol, P(A|B), for probability
and likelihood the applications are different.

237
10/11/2019
Sufficiency and Necessity
• The likelihood of sufficiency, LS, is calculated as:

• The likelihood of necessity is defined as:

238
Relationship Among Likelihood Ratio,
Hypothesis, and Evidence

239
10/11/2019
Relationship Among Likelihood of
Necessity, Hypothesis, and Evidence

240
10/11/2019
Uncertainty in Inference
Chains

• Uncertainty may be present in rules, evidence


used by rules, or both.

• One way of correcting uncertainty is to assume


that P(H|e) is a piecewise linear function.

241
10/11/2019
Intersection of H and e

242
10/11/2019
Piecewise Linear Interpolation Function for
Partial Evidence in PROSPECTOR

243
10/11/2019
Combination of Evidence

• The simplest type of rule is of the form:


– IF E THEN H
where E is a single piece of known evidence from
which we can conclude that H is true.
• Not all rules may be this simple – compensation
for uncertainty may be necessary.
• As the number of pieces of evidence increases, it
becomes impossible to determine all the joint and
prior probabilities or likelihoods.
244
10/11/2019
Combination of Evidence
Continued
• If the antecedent is a logical combination of
evidence, then fuzzy logic and negation rules can
be used to combine evidence.

245
10/11/2019
Types of Belief

• Possible – no matter how remote, the hypothesis


cannot be ruled out.
• Probable – there is some evidence favoring the
hypothesis but not enough to prove it.
• Certain – evidence is logically true or false.
• Impossible – it is false.
• Plausible – more than a possibility exists.

246
10/11/2019
Figure 4.20 Relative Meaning of Some
Terms Used to Describe Evidence

247
10/11/2019
Propagation of
Probabilities
• The chapter examines the classic expert system
PROSPECTOR to illustrate how concepts of
probability are used in a real system.

• Inference nets like PROSPECTOR have a static


knowledge structure.

• Common rule-based system is a dynamic


knowledge structure.
248
10/11/2019
Summary
 In this chapter, we began by discussing reasoning
under uncertainty and the types of errors caused
by uncertainty.
 Classical, experimental, and subjective
probabilities were discussed.
 Methods of combining probabilities and Bayes’
theorem were examined.
 PROSPECTOR was examined in detail to see
how probability concepts were used in a real
system.

249
10/11/2019
Summary

• An expert system must be designed to fit the real


world, not visa versa.
• Theories of uncertainty are based on axioms;
often we don’t know the correct axioms – hence
we must introduce extra factors, fuzzy logic, etc.
• We looked at different degrees of belief which
are important when interviewing an expert.

250
10/11/2019
Chapter 5:
Inexact Reasoning
Objectives

• Explore the sources of uncertainty in rules


• Analyze some methods for dealing with
uncertainty
• Learn about the Dempster-Shafer theory
• Learn about the theory of uncertainty based on
fuzzy logic
• Discuss some commercial applications of fuzzy
logic

252
4/4/2011
Uncertainty and Rules

• We have already seen that expert systems can


operate within the realm of uncertainty.

• There are several sources of uncertainty in rules:


– Uncertainty related to individual rules
– Uncertainty due to conflict resolution
– Uncertainty due to incompatibility of rules

253
4/4/2011
Figure 5.1 Major Uncertainties in
Rule-Based Expert Systems

254
4/4/2011
Figure 5.2 Uncertainties in Individual
Rules

255
4/4/2011
Figure 5.3 Uncertainty Associated with
the Compatibilities of Rules

256
4/4/2011
Figure 5.4 Uncertainty Associated
with Conflict Resolution

257
4/4/2011
Goal of Knowledge Engineer

• The knowledge engineer endeavors to minimize,


or eliminate, uncertainty if possible.

• Minimizing uncertainty is part of the verification


of rules.

• Verification is concerned with the correctness of


the system’s building blocks – rules.

258
4/4/2011
Verification vs. Validation

• Even if all the rules are correct, it does not


necessarily mean that the system will give the
correct answer.
• Verification refers to minimizing the local
uncertainties.
• Validation refers to minimizing the global
uncertainties of the entire expert system.
• Uncertainties are associated with creation of rules
and also with assignment of values.
259
4/4/2011
Ad Hoc Methods

• The ad hoc introduction of formulas such as


fuzzy logic to a probabilistic system introduces a
problem.
• The expert system lacks the sound theoretical
foundation based on classical probability.
• The danger of ad hoc methods is the lack of
complete theory to guide the application or warn
of inappropriate situations.

260
4/4/2011
Sources of Uncertainty

• Potential contradiction of rules – the rules may


fire with contradictory consequents, possibly as a
result of antecedents not being specified properly.

• Subsumption of rules – one rules is subsumed by


another if a portion of its antecedent is a subset of
another rule.

261
4/4/2011
Uncertainty in Conflict
Resolution
 There is uncertainty in conflict resolution with
regard to priority of firing and may depend on a
number of factors, including:
 Explicit priority rules
 Implicit priority of rules
 Specificity of patterns
 Recency of facts matching patterns
 Ordering of patterns
 Lexicographic
 Means-Ends Analysis
 Ordering that rules are entered

262
4/4/2011
Uncertainty

• When a fact is entered in the working memory, it


receives a unique timetag – indicating when it
was entered.
• The order that rules are entered may be a factor
in conflict resolution – if the inference engine
cannot prioritize rules, arbitrary choices must be
made.
• Redundant rules are accidentally entered / occur
when a rule is modified by pattern deletion.

263
4/4/2011
Uncertainty

• Deciding which redundant rule to delete is not a


trivial matter.

• Uncertainty arising from missing rules occurs if


the human expert forgets or is unaware of a rule.

• Data fusion is another cause of uncertainty –


fusing of data from different types of
information.
264
4/4/2011
Certainty Factors

• Another method of dealing with uncertainty uses


certainty factors, originally developed for the
MYCIN expert system.

265
4/4/2011
Difficulties with Bayesian Method

• The Bayesian method is useful in medicine /


geology because we are determining the
probability of a specific event (disease / location
of mineral deposit), given certain symptoms /
analyses.
• The problem is with the difficulty / impossibility
of determining the probabilities of these givens –
symptoms / analyses.
• Evidence tends to accumulate over time.

266
4/4/2011
Belief and Disbelief

• Consider the statement:


“The probability that I have a disease plus the
probability that I do not have the disease equals
one.”
• Now, consider an alternate form of the statement:
“The probability that I have a disease is one
minus the probability that I don’t have it.”

267
4/4/2011
Belief and Disbelief

• It was found that physicians were reluctant to


state their knowledge in the form:
“The probability that I have a disease is one
minus the probability that I don’t have it.”

• Symbolically, P(H|E) = 1 – P(H’|E), where E


represents evidence

268
4/4/2011
Likelihood of Belief / Disbelief

• The reluctance by the physicians stems from the


likelihood of belief / disbelief – not in the
probabilities.
• The equation, P(H|E) = 1 – P(H’|E), implies a
cause-and-effect relationship between E and H.
• The equation implies a cause-and-effect
relationship between E and H’ if there is a cause-
and-effect between E and H.

269
4/4/2011
Measures of Belief and Disbelief

• measure of belief
– degree to which hypothesis H is
supported by evidence E
– MB(H,E) = 1 if P(H) =1
(P(H|E) - P(H)) / (1- P(H)) otherwise
• measure of disbelief
– degree to which doubt in hypothesis H is
supported by evidence E
– MB(H,E) = 1 if P(H) =0
270
4/4/2011
Certainty Factor

• The certainty factor, CF, is a way of combining


belief and disbelief into a single number.

• This has two uses:


1. The certainty factor can be used to rank
hypotheses in order of importance.
2. The certainty factor indicates the net belief
in a hypothesis based on some evidence.

271
4/4/2011
Certainty Factor

• certainty factor CF
– ranges between -1 (denial of the hypothesis H) and +1
(confirmation of H)
– allows the ranking of hypotheses

• difference between belief and disbelief


CF (H,E) = MB(H,E) - MD (H,E)

• combining antecedent evidence


– use of premises with less than absolute confidence
– E1 ∧ E2 = min(CF(H, E1), CF(H, E2))
– E1 ∨ E2 = max(CF(H, E1), CF(H, E2))
– ¬E = ¬ CF(H, E) 272
4/4/2011
Certainty Factor Values

• Positive CF – evidence supports the hypothesis


• CF = 1 – evidence definitely proves the
hypothesis
• CF = 0 – there is no evidence or the belief and
disbelief completely cancel each other.
• Negative CF – evidence favors negation of the
hypothesis – more reason to disbelieve the
hypothesis than believe it

273
4/4/2011
Combining Certainty Factors

• certainty factors that support the same


conclusion
• several rules can lead to the same
conclusion
• applied incrementally as new evidence
becomes available
CFc(CF1, CF2) =
CF1 + CF 2(1 - CF1) if both
>0
CF1 + CF 2(1 + CF1) if both
4/4/2011
<0 274
Characteristics of Certainty Factors

Ranges
measure of belief 0 ≤ MB ≤ 1
measure of disbelief 0 ≤ MD ≤ 1
certainty factor -1 ≤ CF ≤ +1

275
4/4/2011
Threshold Values

• In MYCIN, a rule’s antecedent CF must be


greater than 0.2 for the antecedent to be
considered true and activate the rule.
• This threshold value minimizes the activation of
rules that only weakly suggest the hypothesis.
• This improves efficiency of the system –
preventing rules to be activated with little or no
value.
• A combining function can be used.
276
4/4/2011
Difficulties with Certainty
Factors
• In MYCIN, which was very successful in
diagnosis, there were difficulties with theoretical
foundations of certain factors.
• There was some basis for the CF values in
probability theory and confirmation theory, but
the CF values were partly ad hoc.
• Also, the CF values could be the opposite of
conditional probabilities.

277
4/4/2011
Dempster-Shafer Theory

• The Dempster-Shafer Theory is a method of


inexact reasoning.

• It is based on the work of Dempster who


attempted to model uncertainty by a range of
probabilities rather than a single probabilistic
number.

278
4/4/2011
Dempster-Shafer

1. The Dempster-Shafer theory assumes that there


is a fixed set of mutually exclusive and
exhaustive elements called environment and
symbolized by the Greek letter :

 = {1, 2, …, N}

279
4/4/2011
Dempster-Shafer

• The environment is another term for the universe


of discourse in set theory.
• Consider the following:
 = {rowboat, sailboat, destroyer, aircraft carrier}

• These are all mutually exclusive elements

280
4/4/2011
Dempster-Shafer

• Consider the question:


“What are the military boats?”

• The answer would be a subset of  :


{3, 4} = {destroyer, aircraft carrier}

281
4/4/2011
Dempster-Shafer

• Consider the question:


“What boat is powered by oars?”

• The answer would also be a subset of  :


{1} = {rowboat}

This set is called a singleton because it contains


only one element.

282
4/4/2011
Dempster-Shafer

• Each of these subsets of  is a possible answer


to the question, but there can be only one correct
answer.
• Consider each subset an implied proposition:
– The correct answer is: {1, 2, 3)
– The correct answer is: {1, 3}
• All subsets of the environment can be drawn as a
hierarchical lattice with  at the top and the null
set  at the bottom
283
4/4/2011
Dempster-Shafer

• An environment is called a frame of discernment


when its elements may be interpreted as possible
answers and only one answer is correct.
• If the answer is not in the frame, the frame must
be enlarged to accommodate the additional
knowledge of element..

284
4/4/2011
Dempster-Shafer

2. Mass Functions and Ignorance

In Bayesian theory, the posterior probability


changes as evidence is acquired. In Dempster-
Shafer theory, the belief in evidence may vary.
We talk about the degree of belief in evidence
as analogous to the mass of a physical object –
evidence measures the amount of mass.

285
4/4/2011
Dempster-Shafer

• Dempster-Shafer does not force belief to be


assigned to ignorance – any belief not assigned to
a subset is considered no belief (or non-belief)
and just associated with the environment.
• Every set in the power set of the environment
which has mass > 0 is a focal element.
• Every mass can be thought of as a function:
m: P ( )  [0, 1]

286
4/4/2011
Dempster-Shafer

3. Combining Evidence
Dempster’s rule combines mass to produce a
new mass that represents the consensus of the
original, possibly conflicting evidence
The lower bound is called the support; the
upper bound is called the plausibility; the belief
measure is the total belief of a set and all its
subsets.

287
4/4/2011
Dempster-Shafer

4. The moving mass analogy is helpful to


understanding the support and plausibility.
– The support is the mass assigned to a set and
all its subsets
– Mass of a set can move freely into its subsets
– Mass in a set cannot move into its supersets
– Moving mass from a set into its subsets can
only contribute to the plausibility of the
subset, not its support.
– Mass in the environment can move into any288
4/4/2011
Approximate Reasoning
• This is theory of uncertainty based on fuzzy logic
and concerned with quantifying and reasoning
using natural language where words have
ambiguous meaning.
• Fuzzy logic is a superset of conventional logic –
extended to handle partial truth.
• Soft-computing means computing not based on
classical two-valued logics – includes fuzzy
logic, neural networks, and probabilistic
reasoning.
289
4/4/2011
Fuzzy Sets and Natural Language

• A discrimination function is a way to represent


which objects are members of a set.
– 1 means an object is an element
– 0 means an object is not an element
• Sets using this type of representation are called
“crisp sets” as opposed to “fuzzy sets”.
• Fuzzy logic plays the middle ground – like
human reasoning – everything consists of degrees
– beauty, height, grace, etc.
290
4/4/2011
Fuzzy Sets and Natural Language

• In fuzzy sets, an object may partially belong to a


set measured by the membership function – grade
of membership.
• A fuzzy truth value is called a fuzzy qualifier.
• Compatibility means how well one object
conforms to some attribute.
• There are many type of membership functions.
• The crossover point is where  = 0.5

291
4/4/2011
Fuzzy Set Operations

• An ordinary crisp set is a special case of a fuzzy


set with membership function [0, 1].

• All definitions, proofs, and theorems of fuzzy


sets must be compatible in the limit as the
fuzziness goes to 0 and the fuzzy sets become
crisp sets.

292
4/4/2011
Fuzzy Set Operations

Set equality Set Complement


Set Containment Proper Subset
Set Union Set Intersection
Set Product Power of a Set
Probabilistic Sum Bounded Sum
Bounded Product Bounded Difference
Concentration Dilation
Intensification Normalization

293
Fuzzy Relations

• A relation from a set A to a set B is a subset of


the Cartesian product:
A × B = {(a,b) | a  A and b  B}

• If X and Y are universal sets, then


R = {R(x, y) / (x, y) | (x, y)  X × Y}

294
4/4/2011
Fuzzy Relations

• The composition of relations is the net effect of


applying one relation after another.

• For two binary relations P and Q, the


composition of their relations is the binary
relation:
R(A, C) = Q(A, B)  P(B, C)

295
4/4/2011
Table 5.7 Some Applications of
Fuzzy Theory

296
4/4/2011
Table 5.8 Some Fuzzy Terms of
Natural Language

297
4/4/2011
Linguistic Variables

• One application of fuzzy sets is computational


linguistics – calculating with natural language
statements.
• Fuzzy sets and linguistic variables can be used to
quantify the meaning of natural language, which
can then be manipulated.
• Linguistic variables must have a valid syntax and
semantics.

298
4/4/2011
Extension Principle

• The extension principle defines how to extend the


domain of a given crisp function to include fuzzy
sets.
• Using this principle, ordinary or crisp functions
can be extended to work a fuzzy domain with
fuzzy sets.
• This principle makes fuzzy sets applicable to all
fields.

299
4/4/2011
Fuzzy Logic

• Just as classical logic forms the basis of expert


systems, fuzzy logic forms the basis of fuzzy
expert systems.

• Fuzzy logic is an extension of multivalued logic


– the logic of approximate reasoning – inference
of possibly imprecise conclusions from a set of
possibly imprecise premises.

300
4/4/2011
Possibility and Probability
and Fuzzy Logic
• In fuzzy logic, possibility refers to allowed
values.

• Possibility distributions are not the same as


probability distributions – frequency of expected
occurrence of some random variable.

301
4/4/2011
Translation Rules

• Translation rules specify how modified or


composite propositions are generated from their
elementary propositions.
1. Type I modification rules
2. Type II composition rules
3. Type III quantification rules
4. Type IV quantification rules

302
4/4/2011
State of Uncertainty
Commercial Applications
• There are two mountains – logic and uncertainty
• Expert systems are built on the mountain of logic
and must reach valid conclusions given a set of
premises – valid conclusions given that –
– The rules were written correctly
– The facts upon which the inference engine
generates valid conclusions are true facts
• Today, fuzzy logic and Bayesian theory are most
often used for uncertainty.
303
4/4/2011
Summary

• In this chapter, non-classical probability theories


of uncertainty were discussed.
• Certainty factors, Dempster-Shafer and fuzzy
theory are ways of dealing with uncertainty in
expert systems.
• Certainty factors are simple to implement where
inference chains are short (e.g. MYCIN)
• Certainty factors are not generally valid for
longer inference chains.
304
4/4/2011
Summary

• Dempster-Shafer theory has a rigorous


foundation and is used for expert systems.

• Fuzzy theory is the most general theory of


uncertainty formulated to date and has wide
applicability due to the extension principle.

305
4/4/2011
Chapter 6:
Design of Expert
Systems

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn how to select an appropriate problem


• Learn the stages in the development of an expert
system
• Anticipate certain types of errors in the
development process
• Explore the role of the knowledge engineer in the
building of an expert system
• Learn about life cycles and their models for
expert systems

Expert Systems: Principles and Programming, Fourth Edition 307


Considerations when Building an
Expert system
• We will describe the necessary prerequisites
when building an expert system.
• The system should be a quality product.
• The development should be cost effective and
timely.
• Designing of expert systems of part of a general
field known as Knowledge Management (KM).

Expert Systems: Principles and Programming, Fourth Edition 308


Selecting the Appropriate
Problem
• We need to answer the questions, “Why are we
building this expert system?”.
– Intellectual Property Agreement must be considered
– Clearly identify the problem
– Clearly identify the expert
– Clearly identify the users

• We need to know the payoff – money, efficiency,


etc.

Expert Systems: Principles and Programming, Fourth Edition 309


Selecting the Appropriate
Problem
• What tools will be available to build the expert
system?
– Check the Web for applications in existence
– Know the language necessary to create a semantic net
of relationships on which the system will be based

• How much will the expert system cost?


– A function of people, resources, time, etc.
– How available is the knowledge?

Expert Systems: Principles and Programming, Fourth Edition 310


Figure 6.1 Project Management
Tasks

Expert Systems: Principles and Programming, Fourth Edition 311


Stages in the Development

• How will the system be developed?


– This will depend on the resources provided

Stages:
1. Feasibility Study – see if the project is feasible
2. Rapid Prototype – demonstrate ideas / impress
3. Refined System – verification by knowledge
engineers
4. Field Testable – system tested by selected users

Expert Systems: Principles and Programming, Fourth Edition 312


Stages in the Development

5. Commercial quality system – validation /


testing
6. Maintenance and evolution – repair bugs,
enhance capabilities

Expert Systems: Principles and Programming, Fourth Edition 313


Other Considerations

• How will the system be delivered?


– Should be considered in earliest stages of development
– Integration with existing programs

• How will the system be maintained and evolve?


– Performance is dependent on knowledge / expertise
– Performance must be maintained
– New knowledge will be acquired
– Old knowledge will be modified

Expert Systems: Principles and Programming, Fourth Edition 314


Figure 6.2 General Stages in the
Development of an Expert System

Expert Systems: Principles and Programming, Fourth Edition 315


Errors in Development Stages

• Expert’s knowledge may be erroneous,


propagating errors throughout the entire
development process.

– Formal procedures may be necessary to certify expert


– Technique panels can scrutinize expert’s knowledge
– Focus groups can also be used

Expert Systems: Principles and Programming, Fourth Edition 316


Errors in the Development Stages

• Knowledge may not be properly communicated


to knowledge engineer, or knowledge may be
misinterpreted.

• Knowledge base may be corrupted by entering


incorrect form of a rule or fact.

Expert Systems: Principles and Programming, Fourth Edition 317


Errors in the Development Stages

• Inference engine errors may result from errors in


pattern matching, conflict resolution, and
execution of actions.
• Inference chain errors may be caused by
erroneous knowledge, semantic errors, inference
engine bugs, incorrect specifications of rule
priorities, and strange interaction among rules.
• Limits of ignorance – a problem common to all
previous stages

Expert Systems: Principles and Programming, Fourth Edition 318


Figure 6.3 Major Errors in
Expert Systems and Some Causes

Expert Systems: Principles and Programming, Fourth Edition 319


Software Engineering
and Expert Systems
• Expert systems are products like any other
software product and require good
standards for development.
• Expert systems may have serious
responsibilities – life and death.
• High standards are a necessity and can be
measured by “mean time between
failures”.

Expert Systems: Principles and Programming, Fourth Edition 320


Figure 6.4 Software Engineering
Methodology

Expert Systems: Principles and Programming, Fourth Edition 321


Expert System Life Cycle
• Begins with the initial concept of the software
and ends with its retirement from use.
• Expert systems require more maintenance
because they are based on knowledge that is:
– Heuristic
– Experiential

• A number of life cycle models have been


developed.

Expert Systems: Principles and Programming, Fourth Edition 322


Waterfall Model

• Each stage ends with a verification and


validation activity to minimize any
problems in that stage.
• Arrows go back and forth only one stage
at a time.
• It is assumed that all information
necessary for a stage is known.

Expert Systems: Principles and Programming, Fourth Edition 323


Figure 6.5 Waterfall Model of the
Software Life Cycle

Expert Systems: Principles and Programming, Fourth Edition 324


Code-and-Fix Model

• Some code is written and then fixed when


it does not work correctly.
• Usually the method of choice for new
programming students in conventional and
expert systems
• This eventually led to the do-it-twice
concept where a prototype then a final
system was built.

Expert Systems: Principles and Programming, Fourth Edition 325


Incremental Model

• This is a refinement of the waterfall and


top-down-approach.
• The idea is to develop software in
increments of functional capability.
– Major increment – assistant  colleague 
expert
– Minor increment – expertise w/in each level
– Microincrement – add/refining individual rules

Expert Systems: Principles and Programming, Fourth Edition 326


Spiral Model
Each circuit of the spiral adds some functional
capability to the system.

Expert Systems: Principles and Programming, Fourth Edition 327


Detailed Life Cycle Model
Linear Model
1. Planning Stage

The purpose of this stage is to produce a


formal work plan for the expert system
development – documents to guide and
evaluate the development.

Expert Systems: Principles and Programming, Fourth Edition 328


Table 6.2 Planning Stage Tasks

Expert Systems: Principles and Programming, Fourth Edition 329


Linear Model

2. Knowledge Definition

The objective of this stage is to define the


knowledge requirements of the expert system,
which consists of two main tasks:
• Knowledge source identification and selection
• Knowledge acquisition, analysis, and extraction

Expert Systems: Principles and Programming, Fourth Edition 330


Table 6.3 Knowledge Source /
Identification

Expert Systems: Principles and Programming, Fourth Edition 331


Table 6.4 Knowledge Acquisition,
Analysis, and Extraction Tasks

Expert Systems: Principles and Programming, Fourth Edition 332


Linear Model

3. Knowledge Design

The objective is to produce the detailed design


for an expert system and involves:
• Knowledge definition
• Detailed design

Expert Systems: Principles and Programming, Fourth Edition 333


Table 6.5 Knowledge Definition
Tasks

Expert Systems: Principles and Programming, Fourth Edition 334


Table 6.6 Detailed Design of
Knowledge Tasks

Expert Systems: Principles and Programming, Fourth Edition 335


Linear Model

4. Code and Checkout

This begins the actual code implementation

Expert Systems: Principles and Programming, Fourth Edition 336


Table 6.7 Code and Checkout
Tasks

Expert Systems: Principles and Programming, Fourth Edition 337


Linear Model

5. Knowledge Verification
The objective here is to determine the
correctness, completeness, and
consistency of the system.
• Formal tests
• Test Analysis

Expert Systems: Principles and Programming, Fourth Edition 338


Table 6.8 Formal Test Tasks of
Knowledge Verification Stage

Expert Systems: Principles and Programming, Fourth Edition 339


Test Analysis Tasks

Expert Systems: Principles and Programming, Fourth Edition 340


Linear Model

6. System Evaluation

This stage is for summarizing what has been


learned with recommendations for
improvements and corrections.

Expert Systems: Principles and Programming, Fourth Edition 341


Table 6.10 System Evaluation
Stage Tasks

Expert Systems: Principles and Programming, Fourth Edition 342


Figure 6.7 Linear Model of Expert
System Development Life Cycle

Expert Systems: Principles and Programming, Fourth Edition 343


Summary

• In this chapter, we have discussed a


software engineering approach to the
construction of an expert system.
• Principles about good interviewing
techniques were mentioned.
• Now that expert systems are widely used,
they must be quality products due to the
sensitive nature of their applications.

Expert Systems: Principles and Programming, Fourth Edition 344


Summary

• Factors to be considered in the design of


expert systems include problem selection,
cost, and payoff.
• Both managerial and technical aspects
must be considered.
• By consistently following the outlined “life
cycle” it should be possible to construct
quality software.

Expert Systems: Principles and Programming, Fourth Edition 345


Chapter 7:
Introduction to
CLIPS

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn what type of language CLIPS is


• Study the notation (syntax) used by CLIPS
• Learn the meaning of a field and what types exit
• Learn how to launch and exit from CLIPS
• Learn how to represent, add, remove, modified,
and duplicated in CLIPS

Expert Systems: Principles and Programming, Fourth Edition 347


Objectives

• Learn how to debug programs using the watch


command
• Learn how to use the deffacts construct to define
a group of facts
• Learn how to use the agenda command and
execute CLIPS programs
• Learn about commands that can manipulate
constructs

Expert Systems: Principles and Programming, Fourth Edition 348


Objectives

• Learn how to use the printout command


• Learn how to use multiple rules
• Learn how to use the set-break command
• Learn how to use the load and save constructs
• Learn how to use variables, single and multifield
wildcards, and comment constructs

Expert Systems: Principles and Programming, Fourth Edition 349


What is CLIPS?

• CLIPS is a multiparadigm programming


language that provides support for:
– Rule-based
– Object-oriented
– Procedural programming
• Syntactically, CLIPS resembles:
– Eclipse
– CLIPS/R2
– JESS

Expert Systems: Principles and Programming, Fourth Edition 350


Other CLIPS Characteristics

• CLIPS supports only forward-chaining rules.


• The OOP capabilities of CLIPS are referred to as
CLIPS Object-Oriented Language (COOL).
• The procedural language capabilities of CLIPS
are similar to languages such as:
– C
– Ada
– Pascal
– Lisp

Expert Systems: Principles and Programming, Fourth Edition 351


CLIPS Characteristics

• CLIPS is an acronym for C Language Integrated


Production System.

• CLIPS was designed using the C language at the


NASA/Johnson Space Center.

• CLIPS is portable – PC  CRAY.

Expert Systems: Principles and Programming, Fourth Edition 352


CLIPS Notation

• Symbols other than those delimited by < >, [ ], or


{ } should be typed exactly as shown.
• [ ] mean the contents are optional and < > mean
that a replacement is to be made.
• * following a description means that the
description can be replaced by zero or more
occurrences of the specified value.

Expert Systems: Principles and Programming, Fourth Edition 353


CLIPS Notation

• Descriptions followed by + mean that one or


more values specified by description should be
used in place of the syntax description.

• A vertical bar | indicates a choice among one or


more of the items separated by the bars.

Expert Systems: Principles and Programming, Fourth Edition 354


Fields

• To build a knowledge base, CLIPS must read


input from keyboard / files to execute commands
and load programs.
• During the execution process, CLIPS groups
symbols together into tokens – groups of
characters that have the same meaning.
• A field is a special type of token of which there
are 8 types.

Expert Systems: Principles and Programming, Fourth Edition 355


Numeric Fields

• The floats and integers make up the numeric


fields – simply numbers.

• Integers have only a sign and digits.

• Floats have a decimal and possibly “e” for


scientific notation.

Expert Systems: Principles and Programming, Fourth Edition 356


Symbol Fields

• Symbols begin with printable ASCII characters


followed by zero or more characters, followed by
a delimiter.
• CLIPS is case sensitive.

Expert Systems: Principles and Programming, Fourth Edition 357


String Fields

• Strings must begin and end with double quotation


marks.
• Spaces w/in the string are significant.
• The actual delimiter symbols can be included in a
string by preceding the character with a
backslash.

Expert Systems: Principles and Programming, Fourth Edition 358


Address Fields

• External addresses represent the address of an


external data structure returned by a user-defined
function.
• Fact address fields are used to refer to a specific
fact.
• Instance Name / Address field – instances are
similar to facts addresses but refer to the instance
rather than a fact.

Expert Systems: Principles and Programming, Fourth Edition 359


Entering / Exiting CLIPS

• The CLIPS prompt is: CLIPS>


• This is the type-level mode where commands can
be entered.
• To exit CLIPS, one types: CLIPS> (exit) 
• CLIPS will accept input from the user / evaluate
it / return an appropriate response:
CLIPS> (+ 3 4)   value 7 would be returned.

Expert Systems: Principles and Programming, Fourth Edition 360


Facts and CLIPS

• To solve a problem, CLIPS must have data or


information with which to reason.
• Each chunk of information is called a fact.
• Facts consist of:
– Relation name (symbolic field)
– Zero or more slots w/associated values

Expert Systems: Principles and Programming, Fourth Edition 361


Example Fact in CLIPS

Expert Systems: Principles and Programming, Fourth Edition 362


Deftemplate

• Before facts can be constructed, CLIPS must be


informed of the list of valid slots for a given
relation name.
• A deftemplate is used to describe groups of facts
sharing the same relation name and contain
common information.

Expert Systems: Principles and Programming, Fourth Edition 363


Deftemplate General Format

Expert Systems: Principles and Programming, Fourth Edition 364


Deftemplate vs. Ordered Facts

• Facts with a relation name defined using


deftemplate are called deftemplate facts.

• Facts with a relation name that does not have a


corresponding deftemplate are called ordered
facts – have a single implied multifield slot for
storing all the values of the relation name.

Expert Systems: Principles and Programming, Fourth Edition 365


Adding Facts
• CLIPS store all facts known to it in a fact list.
• To add a fact to the list, we use the assert command.

Expert Systems: Principles and Programming, Fourth Edition 366


Displaying Facts
• CLIPS> (facts) 

Expert Systems: Principles and Programming, Fourth Edition 367


Removing Facts

• Just as facts can be added, they can also be


removed.
• Removing facts results in gaps in the fact
identifier list.
• To remove a fact:
CLIPS> (retract 2) 

Expert Systems: Principles and Programming, Fourth Edition 368


Modifying Facts
• Slot values of deftemplate facts can be modified using
the modify command:

Expert Systems: Principles and Programming, Fourth Edition 369


Results of Modification

• A new fact index is generated because when a


fact is modified:
– The original fact is retracted
– The modified fact is asserted

• The duplicate command is similar to the modify


command, except it does not retract the original
fact.

Expert Systems: Principles and Programming, Fourth Edition 370


Watch Command

• The watch command is useful for debugging


purposes.
• If facts are “watched”, CLIPS will automatically
print a message indicating an update has been
made to the fact list whenever either of the
following has been made:
– Assertion
– Retraction

Expert Systems: Principles and Programming, Fourth Edition 371


Deffacts Construct

• The deffacts construct can be used to assert a


group of facts.
• Groups of facts representing knowledge can be
defined as follows:
(deffacts <deffacts name> [<optional] comment]
<facts> * )
• The reset command is used to assert the facts in a
deffacts statement.

Expert Systems: Principles and Programming, Fourth Edition 372


The Components of a Rule

• To accomplish work, an expert system must have


rules as well as facts.
• Rules can be typed into CLIPS (or loaded from a
file).
• Consider the pseudocode for a possible rule:
IF the emergency is a fire
THEN the response is to activate the sprinkler system

Expert Systems: Principles and Programming, Fourth Edition 373


Rule Components

• First, we need to create the deftemplate for the


types of facts:
(deftemplate emergency (slot type))
-- type would be fire, flood, etc.

• Similarly, we must create the deftemplate for the


types of responses:
(deftemplate response (slot action))
-- action would be “activate the sprinkler”

Expert Systems: Principles and Programming, Fourth Edition 374


Rule Components

• The rule would be shown as follows:

(defrule fire-emergency “An example rule”


(emergency )type fire))
=>
(assert (response
(action activate-sprinkler-system))))

Expert Systems: Principles and Programming, Fourth Edition 375


Analysis of the Rule

• The header of the rule consists of three parts:


1. Keyword defrule
2. Name of the rule – fire-emergency
3. Optional comment string – “An example rule”
• After the rule header are 1+ conditional
elements – pattern CEs
• Each pattern consists of 1+ constraints intended
to match the fields of the deftemplate fact

Expert Systems: Principles and Programming, Fourth Edition 376


Analysis of Rule

• If all the patterns of a rule match facts, the rule is


activated and put on the agenda.
• The agenda is a collection of activated rules.
• The arrow => represents the beginning of the
THEN part of the IF-THEN rule.
• The last part of the rule is the list of actions that
will execute when the rule fires.

Expert Systems: Principles and Programming, Fourth Edition 377


The Agenda and Execution

• To run the CLIPS program, use the run


command:
CLIPS> (run [<limit>])

-- the optional argument <limit> is the maximum


number of rules to be fired – if omitted, rules will fire
until the agenda is empty.

Expert Systems: Principles and Programming, Fourth Edition 378


Execution

• When the program runs, the rule with the highest


salience on the agenda is fired.
• Rules become activated whenever all the patterns
of the rule are matched by facts.
• The reset command is the key method for starting
or restarting .
• Facts asserted by a reset satisfy the patterns of
one or more rules and place activation of these
rules on the agenda.

Expert Systems: Principles and Programming, Fourth Edition 379


What is on the Agenda?

• To display the rules on the agenda, use the


agenda command:
CLIPS> (agenda) 

• Refraction is the property that rules will not fire


more than once for a specific set of facts.
• The refresh command can be used to make a rule
fire again by placing all activations that have
already fired for a rule back on the agenda.
Expert Systems: Principles and Programming, Fourth Edition 380
Command for Manipulating
Constructs
• The list-defrules command is used to display the
current list of rules maintained by CLIPS.
• The list-deftemplates displays the current list of
deftemplates.
• The list-deffacts command displays the current
list of deffacts.
• The ppdefrule, ppdeftemplate and ppdeffacts
commands display the text representations of a
defrule, deftemplate, and a deffact, respectively.

Expert Systems: Principles and Programming, Fourth Edition 381


Commands

• The undefrule, undeftemplate, and undeffacts


commands are used to delete a defrule, a
deftemplate, and a deffact, respectively.
• The clear command clears the CLIPS
environment and adds the initialfact-defacts to
the CLIPS environment.
• The printout command can also be used to print
information.

Expert Systems: Principles and Programming, Fourth Edition 382


Other Commands

• Set-break – allows execution to be halted before


any rule from a specified group of rules is fired.

• Load – allows loading of rules from an external


file.

• Save – opposite of load, allows saving of


constructs to disk

Expert Systems: Principles and Programming, Fourth Edition 383


Commenting and Variables

• Comments – provide a good way to document


programs to explain what constructs are doing.

• Variables – store values, syntax requires


preceding with a question mark (?)

Expert Systems: Principles and Programming, Fourth Edition 384


Fact Addresses, Single-Field
Wildcards, and Multifield Variables
• A variable can be bound to a fact address of a
fact matching a particular pattern on the LHS of a
rule by using the pattern binding operator “<-”.
• Single-field wildcards can be used in place of
variables when the field to be matched against
can be anything and its value is not needed later
in the LHS or RHS of the rule.
• Multifield variables and wildcards allow
matching against more than one field in a pattern.

Expert Systems: Principles and Programming, Fourth Edition 385


Summary

• In this chapter, we looked at the fundamental


components of CLIPS.
• Facts make up the first component of CLIPS,
made up of fields – symbol, string, integer, or
float.
• The deftemplate construct was used to assign slot
names to specific fields of a fact.
• The deffacts construct was used to specify facts
as initial knowledge.

Expert Systems: Principles and Programming, Fourth Edition 386


Summary

• Rules make up the second component of a CLIPS


system.
• Rules are divided into LHS – IF portion and the
RHS – THEN portion.
• Rules can have multiple patterns and actions.
• The third component is the inference engine –
rules having their patterns satisfied by facts
produce an activation that is placed on the
agenda.

Expert Systems: Principles and Programming, Fourth Edition 387


Summary
• Refraction prevents old facts from activating
rules.
• Variables are used to receive information from
facts and constrain slot values when pattern
matching on the LHS of a rule.
• Variables can store fact addresses of patterns on
the LHS of a rule so that the fact bound to the
pattern can be retracted on the RHS of the rule.
• We also looked at single-field wildcards and
multifield variables.
Expert Systems: Principles and Programming, Fourth Edition 388
Chapter 8:
Advanced Pattern
Matching

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn about field constraints


• Learn how to use functions and expressions
• Learn how to perform summing values using
rules
• Learn how to use the bind function

• Learn how to use I/O functions

Expert Systems: Principles and Programming, Fourth Edition 390


Objectives

• Examine the two-player game called Sticks


• Learn how to use the test conditional element
• Learn how to use the predicate field constraint
and the return value constraint
• Examine the or, and, not, exists, forall logical
elements.

Expert Systems: Principles and Programming, Fourth Edition 391


Field Constraints

• In addition to pattern matching capabilities and


variable bindings, CLIPS has more powerful
pattern matching operators.
• Consider writing a rule for all people who do not
have brown hair:
– We could write a rule for every type of hair color that
is not brown.
– This involves testing the condition in a roundabout
manner – tedious, but effective.

Expert Systems: Principles and Programming, Fourth Edition 392


Field Constraints

• The technique for writing a rule for all non-


brown hair colors implies that we have the ability
to supply all hair colors – virtually impossible.

• An alternative is to use a field constraint to


restrict the values a field may have on the LHS –
the THEN part of the rule.

Expert Systems: Principles and Programming, Fourth Edition 393


Connective Constraints
• Connective constraints are used to connect
variables and other constraints.
• Not connective – the ~ acts on the one constraint
or variable that immediately follows it.
• Or constraint – the symbol | is used to allow one
or more possible values to match a field or a
pattern.
• And constraint – the symbol & is useful with
binding instances of variables and on conjunction
with the not constraint.
Expert Systems: Principles and Programming, Fourth Edition 394
Combining Field Constraints
• Field constraints can be used together with
variables and other literals to provide powerful
pattern matching capabilities.
• Example #1: ?eyes1&blue|green
– This constraint binds the person’s eye color to the
variable, ?eyes1 if the eye color of the fact being
matched is either blue or green.
• Example #2: ?hair1&~black
– This constraint binds the variable ?hair1 if the hair
color of the fact being matched is not black.

Expert Systems: Principles and Programming, Fourth Edition 395


Functions and Expressions
• CLIPS has the capability to perform calculations.
• The math functions in CLIPS are primarily used for
modifying numbers that are used to make inferences by
the application program.

Expert Systems: Principles and Programming, Fourth Edition 396


Numeric Expressions in CLIPS

• Numeric expressions are written in CLIPS in


LISP-style – using prefix form – the operator
symbol goes before the operands to which it
pertains.
• Example #1:
5 + 8 (infix form)  + 5 8 (prefix form)

• Example #2:
(infix) (y2 – y1) / (x2 – x1) > 0
(prefix) (> ( / ( - y2 y1 ) (- x2 x1 ) ) 0)

Expert Systems: Principles and Programming, Fourth Edition 397


Return Values

• Most functions (addition) have a return value that


can be an integer, float, symbol, string, or
multivalued value.
• Some functions (facts, agenda commands) have
no return values – just side effects.
• Division results are usually rounded off.
• Return values for +, -, and * will be integer if all
arguments are integer, but if at least one value is
floating point, the value returned will be float.

Expert Systems: Principles and Programming, Fourth Edition 398


Variable Numbers of Arguments

• Many CLIPS functions accept variable numbers


of arguments.
• Example:
CLIPS> (- 3 5 7)  returns 3 - 5 =-2 - 7 = -9
• There is no built-in arithmetic precedence in
CLIPS – everything is evaluated from left to
right.
• To compensate for this, precedence must be
explicitly written.

Expert Systems: Principles and Programming, Fourth Edition 399


Embedding Expressions
• Expressions may be freely embedded within other
expressions:

Expert Systems: Principles and Programming, Fourth Edition 400


Summing Values Using Rules

• Suppose you wanted to sum the areas of a group


of rectangles.
– The heights and widths of the rectangles can be
specified using the deftemplate:
(deftemplate rectangle (slot height) (slot
width))

• The sum of the rectangle areas could be specified


using an ordered fact such as:
(sum 20)

Expert Systems: Principles and Programming, Fourth Edition 401


Summing Values

• A deffacts containing sample information is:


(deffacts initial-information
(rectangle (height 10) (width 6))
(rectangle (height 7) (width 5)
(rectangle (height 6) (width 8))
(rectangle (height 2) (width 5))
(sum 0))

Expert Systems: Principles and Programming, Fourth Edition 402


Summing Values

Expert Systems: Principles and Programming, Fourth Edition 403


The Bind Function

• Sometimes it is advantageous to store a value in a


temporary variable to avoid recalculation.
• The bind function can be used to bind the value
of a variable to the value of an expression using
the following syntax:

(bind <variable> <value>)

Expert Systems: Principles and Programming, Fourth Edition 404


I/O Functions
• When a CLIPS program requires input from the user of
a program, a read function can be used to provide input
from the keyboard:

Expert Systems: Principles and Programming, Fourth Edition 405


Read Function from Keyboard

• The read function can only input a single field at


a time.
• Characters entered after the first field up to the 
are discarded.
• To input, say a first and last name, they must be
delimited with quotes, “xxx xxx”.
• Data must be followed by a carriage return to be
read.

Expert Systems: Principles and Programming, Fourth Edition 406


I/O from/to Files

• Input can also come from external files.


• Output can be directed to external files.
• Before a file can be accessed, it must be opened
using the open function:
Example:
(open “mydata.dat” data “r”)
• mydata.dat – is the name of the file (path can also
be provided)

Expert Systems: Principles and Programming, Fourth Edition 407


I/O from/to Files

• data – is the logical name that CLIPS associates


with the file
• “r” – represents the mode – how the file will be
used – here read access
• The open function acts as a predicate function
– Returns true if the file was successfully opened
– Returns false otherwise

Expert Systems: Principles and Programming, Fourth Edition 408


Table 8.2 File Access Modes

Expert Systems: Principles and Programming, Fourth Edition 409


Close Function

• Once access to the file is no longer needed, it


should be closed.
• Failure to close a file may result in loss of
information.
• General format of the close function:
(close [<file-ID>])
(close data)  example

Expert Systems: Principles and Programming, Fourth Edition 410


Reading / Writing to a File
• Which logical name used, depends on where information
will be written – logical name t refers to the terminal
(standard output device).

Expert Systems: Principles and Programming, Fourth Edition 411


Formatting

• Output sent to a terminal or file may need to be


formatted – enhanced in appearance.
• To do this, we use the format function which
provides a variety of formatting styles.
• General format:
(format <logical-name> <control-
string> <parameters>*)

Expert Systems: Principles and Programming, Fourth Edition 412


Formatting

• Logical name – t – standard output device; or


logical name associated with a file.
• Control string:
– Must be delimited with quotes
– Consists of format flags to indicate how parameters
should be printed
– 1 – 1 correspondence between flags and number of
parameters – constant values or expressions
– Return value of the format function is the formatted
string – nil can be used to suppress printing.

Expert Systems: Principles and Programming, Fourth Edition 413


Formatting

• Example:
(format nil “Name: %-15s Age: %3d” “Bob Green”
35) 
Produces the results:
“Name: Bob green Age: 35”

Expert Systems: Principles and Programming, Fourth Edition 414


Specifications of Format Flag

%-m.Nx

• The “-” means to left justify (right is the default)


• M – total field width – no truncation occurs
• N – number of digits of precision – default = 6
• x – display format specification

Expert Systems: Principles and Programming, Fourth Edition 415


Table 8.3 Display Format
Specifications

Expert Systems: Principles and Programming, Fourth Edition 416


Readline Function

• To read an entire line of input, the readline


function can be used:
(readline [<logical-name>])
• Example:

(defrule get-name
=>
(printout t “What is your name? “
(bind ?response (readline))
(assert (user’s-name ?response)))

Expert Systems: Principles and Programming, Fourth Edition 417


Predicate Functions

• A predicate function is defined to be any function


that returns:
– TRUE
– FALSE
• Any value other than FALSE is considered
TRUE.
• We say the predicate function returns a Boolean
value.

Expert Systems: Principles and Programming, Fourth Edition 418


The Test Conditional Element

• Processing of information often requires a loop.


• Sometimes a loop needs to terminate
automatically as the result of an arbitrary
expression.
• The test condition provides a powerful way to
evaluate expressions on the LHS of a rule.
• Rather than pattern matching against a fact in a
fact list, the test CE evaluates an expression –
outermost function must be a predicate function.

Expert Systems: Principles and Programming, Fourth Edition 419


Test Condition

• A rule will be triggered only if all its test CEs are


satisfied along with other patterns.

(test <predicate-function>)

Example:
(test (> ?value 1))

Expert Systems: Principles and Programming, Fourth Edition 420


Predicate Field Constraint

• The predicate field constraint : allows for


performing predicate tests directly within
patterns.
• The predicate field constraint is more efficient
than using the test CE.
• It can be used just like a literal field constraint –
by itself or part of a complex field.
• The predicate field constraint is always followed
by a function for evaluation (predicate function).

Expert Systems: Principles and Programming, Fourth Edition 421


Return Value Constraint

• The return value constraint = allows the return


value of a function to be used for comparison
inside a pattern.
• The return value constraint must be followed by a
function (not necessarily a predicate function).
• The function must have a single-field return
value.

Expert Systems: Principles and Programming, Fourth Edition 422


The OR Conditional Element
• Consider the two rules:

Expert Systems: Principles and Programming, Fourth Edition 423


OR Conditional Element
These two rules can be combined into one rule – or CE
requires only one CE be satisfied:

Expert Systems: Principles and Programming, Fourth Edition 424


The And Conditional Element
The and CE is opposite in concept to the or CE –
requiring all the CEs be satisfied:

Expert Systems: Principles and Programming, Fourth Edition 425


Not Conditional Element
When it is advantageous to activate a rule based on the
absence of a particular fact in the list, CLIPS allows the
specification of the absence of the fact in the LHS of a
rule using the not conditional element:

Expert Systems: Principles and Programming, Fourth Edition 426


Not Conditional
We can implement this as follows:

Expert Systems: Principles and Programming, Fourth Edition 427


The Exists Conditional Element

• The exists CE allows one to pattern match based


on the existence of at least one fact that matches
a pattern without regard to the total number of
facts that actually match the pattern.
• This allows a single partial match or activation
for a rule to be generated based on the existence
of one fact out of a class of facts.

Expert Systems: Principles and Programming, Fourth Edition 428


Exists Conditional

Expert Systems: Principles and Programming, Fourth Edition 429


Exists
• When more than one emergency fact is asserted, the
message to the operators is printed more than once. The
following modification prevents this:

Expert Systems: Principles and Programming, Fourth Edition 430


Exists
• This assumes there was already an alert – triggered by
an operator-emergency rule. What if the operators were
merely placed on alert to a drill:

Expert Systems: Principles and Programming, Fourth Edition 431


Exists

• Now consider how the rule has been modified


using the exists CE:
(defrule operator-alert-for-emergency
(exists (emergency))
(not (operator-alert))
=>
(printout t “Emergency: Operator Alert”
crlf)
(assert (operator-alert))))

Expert Systems: Principles and Programming, Fourth Edition 432


Forall / Logical
Conditional Elements
• The forall CE allows one to pattern match based
on a set of CEs that are satisfied for every
occurrence of another CE.

• The logical CE allows one to specify that the


existence of a fact depends on the existence of
another fact or group of facts.

Expert Systems: Principles and Programming, Fourth Edition 433


Summary

• We have introduced the concept of field


constraints – not, and, and or.
• Functions are entered into CLIPS top-level
command loop or are used on the LHS or RHS of
a rule.
– Many functions have variable number of arguments
– Function calls can be nested w/in other function calls

Expert Systems: Principles and Programming, Fourth Edition 434


Summary

• CLIPS provides several I/O functions.


– Open / close
– Printout / read / readline
– Format
• Various concepts for controlling the flow of
execution are available
• Also included in the discussion were the
following CEs:
– Test, And, Or, Exists, Forall, and logical

Expert Systems: Principles and Programming, Fourth Edition 435


Chapter 9:
Modular Design,
Execution Control,
and Rule Efficiency

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn how to use deftemplate attributes


• See how salience and modules can be used to
control the execution of rules
• Determine the different phases for fault detection,
isolation, and recover
• Explore some misuses of salience
• Learn how to use the defmodule construct

Expert Systems: Principles and Programming, Fourth Edition 437


Objectives

• Learn how to import / export facts


• Learn how modules affect execution control
• Analyze the Rete pattern-matching algorithm
• Learn the role of the pattern network, join
network, pattern order, and how to order patterns
for efficiency
• Learn the role of multifield variables and
efficiency

Expert Systems: Principles and Programming, Fourth Edition 438


Objectives

• Learn the role of the test CE and efficiency


• Determine the difference between general and
specific rules
• Determine the difference between simple and
complex rules

Expert Systems: Principles and Programming, Fourth Edition 439


Deftemplate Attributes
• CLIPS provides slot attributes which can be
specified when deftemplate slots are defined.
• Slot attributes provide strong typing and
constraint checking.
• One can define the allowed types that can be
stored in a slot, range of numeric values.
• Multislots can specify min / max numbers of
fields they can contain.
• Default attributes can be provided for slots not
specified in an assert command.
Expert Systems: Principles and Programming, Fourth Edition 440
Type Attribute
• Defines the data types can be placed in a slot
• Example:
(deftemplate person
(multislot name (type SYMBOL))
(SLOT AGE (TYPE integer)))

• Once defined, CLIPS will enforce these


restrictions on the slot attributes
name – must store symbols
age – must store integers

Expert Systems: Principles and Programming, Fourth Edition 441


Static and Dynamic Constraint
Checking
• CLIPS provides two levels of constraint checking
– Static constraint checking
• Performed when CLIPS parses expression / constant
• Can be disabled by calling the set-static-constraint-checking
function and passing it FALSE

– Dynamic constraint checking


• Performed on facts when they are asserted
• Can be enabled / disabled with set-dynamic-constraint-
checking

Expert Systems: Principles and Programming, Fourth Edition 442


Allowed Value Attributes
• CLIPS allows one to specify a list of allowed values
for a specific type – 8 are provided:

Symbols Strings
Lexemes Integers
Floats Numbers
Instance-names Values

Expert Systems: Principles and Programming, Fourth Edition 443


Range Attributes

• This attribute allows the specification of


minimum and maximum numeric values.
• Example:
(deftemplate person
(multislot name (type SYMBOL))
(slot age (type INTEGER)
(range 0 ?VARIABLE)))

Expert Systems: Principles and Programming, Fourth Edition 444


Default Attribute
• Previously, each deftemplate fact asserted had an
explicit value stated for every slot.
• It is often convenient to automatically have a
specified value stored in a slot if no value is
explicitly stated in an assert command.
– Example:
(default <default-specification>)

can be either ?DERIVE or ?NONE or single
expression, zero or more expressions

Expert Systems: Principles and Programming, Fourth Edition 445


Default-Dynamic Attribute

When the default attribute is used, the default


value for a slot is determined when the slot
definition is parsed or when the fact that will use
the default value is asserted.

Expert Systems: Principles and Programming, Fourth Edition 446


Conflicting Slot Attributes

CLIPS does not allow you to specify conflicting


attributes for a slot.

Expert Systems: Principles and Programming, Fourth Edition 447


Salience

• CLIPS provides two explicit techniques for


controlling the execution of rules:
– Salience
– Modules
• Salience allows the priority of rules to be
explicitly specified.
• The agenda acts like a stack (LIFO) – most recent
activation placed on the agenda being first to fire.

Expert Systems: Principles and Programming, Fourth Edition 448


Salience

• Salience allows more important rules to stay at


the top of the agenda, regardless of when they
were added.
• Lower salience rules are pushed lower on the
agenda; higher salience rules are higher.
• Salience is set using numeric values in the range -
10,000  +10,000 – zero is intermediate priority.
• Salience can be used to force rules to fire in a
sequential fashion.

Expert Systems: Principles and Programming, Fourth Edition 449


Salience

• Rules of equal salience, activated by different


patterns are prioritized based on the stack order
of facts.
• If 2+ rules with same salience are activated by
the same fact, no guarantee about the order in
which they will be place on the agenda.

Expert Systems: Principles and Programming, Fourth Edition 450


Phases and Control Facts

Expert Systems: Principles and Programming, Fourth Edition 451


Figure 9.2 Assignment of Salience
for Different Phases

Expert Systems: Principles and Programming, Fourth Edition 452


Implementation of System

Approaches:
1. Embed the control knowledge directly into the rules.
Example:
Detection rules would include rules indicating when
the isolation phase should be entered. Each group of
rules would be given a pattern indicating in which
phase it would be applicable.

Expert Systems: Principles and Programming, Fourth Edition 453


Implementation

2. Use salience to organize the rules.

3. Separate the control knowledge from the domain


knowledge. Each rule is given a control pattern that
indicates its applicable phase. Control rules are then
written to transfer control between the different
phases.

Expert Systems: Principles and Programming, Fourth Edition 454


Salience Hierarchy
• Salience hierarchy is a description of the salience values
used by an expert system.
• Each level corresponds to a specific set of rules whose
members are all given the same salience.
• When rules for detection / isolation / recovery are zero,
salience hierarchy is:

Expert Systems: Principles and Programming, Fourth Edition 455


Misuse of Salience
• Because salience is such a powerful tool,
allowing explicit control over execution, there is
potential for misuse.
• Well-designed rule-based programs should allow
inference engine to control firings in an optimal
manner.
• Salience should be used to determine the order
when rules fire, not for selecting a single rule
from a group of rules when patterns can control
criteria for selection.
Expert Systems: Principles and Programming, Fourth Edition 456
Rule of Thumb

• No more than seven salience values should ever


be required for coding an expert system – bested
limited to 3 – 4.

• For large expert systems, programmers should


use modules to control the flow of execution –
limited to 2 – 3 salience values.

Expert Systems: Principles and Programming, Fourth Edition 457


The Defmodule Construct

• Up to now, all defrules, deftemplates, and


deffacts have been contained in a single work
space.
• CLIPS uses the defmodule construct to partition a
knowledge base by defining the various modules.
• Syntax:
(defmodule <module-name> [<comment>])

Expert Systems: Principles and Programming, Fourth Edition 458


The MAIN Module

By default, CLIPS defines a MAIN module as seen


below:
CLIPS>(clear)
CLIPS>(DEFTEMPLATE SENSOR (SLOT NAME))
CLIPS>(PPDEFTEMPLATE sensor)
CLIPS>(DEFTEMPLATE MAIN::sensor (slot name))
CLIPS>

The symbol :: is called the module separator

Expert Systems: Principles and Programming, Fourth Edition 459


Examples of Defining Modules
• Examples:
CLIPS> (defmodule DETECTION) 
CLIPS> (defmodule ISOLATION) 
CLIPS> (defmodule RECOVERY) 

• By default, CLIPS puts newly defined constructs


in the current module – MAIN, when CLIPS
starts or is cleared.
• When a new modules is defined, it becomes
current.

Expert Systems: Principles and Programming, Fourth Edition 460


Examples

• To override this, the module where the construct


will be placed can be specified in the construct’s
name:
CLIPS> (defule ISOLATION::example2 =>) 
CLIPS> (ppdefrule example2) 
(defrule ISOLATION::example2 =>)
CLIP>

Expert Systems: Principles and Programming, Fourth Edition 461


Examples
• To find out which module is current:
CLIPS> (get-current-module) 

• To change the current module:


CLIPS> (set-current-module DETECTION) 

• Specifying modules in commands:


CLIPS> (list-defrules RECOVERY) 

Expert Systems: Principles and Programming, Fourth Edition 462


Specifying Modules
in Commands
By default, most CLIPS commands operating on
a construct work only on the constructs contained
in the current module.
CLIPS> (list-defrules RECOVERY) 

Note the list-defrules command accepts a module


name as an optional argument.

Expert Systems: Principles and Programming, Fourth Edition 463


Importing and Exporting Facts
• Just as constructs can be partitioned by placing
them in separate modules, facts can also be
partitioned.
• Asserted facts are automatically associated with
the module in which their corresponding
deftemplates are defined.
• The facts command can accept a module name as
an argument:
(facts [<module-name>]
[<start> [<end> [<maximum>]]])

Expert Systems: Principles and Programming, Fourth Edition 464


Importing/Exporting Facts

• Unlike defrule and deffacts constructs,


deftemplate constructs can be shared with other
modules.
• A fact is “owned” by the module in which its
deftemplate is contained.
• The owning module can export the deftemplate
associated with the fact making that fact and all
other facts using that deftemplate visible to other
modules.

Expert Systems: Principles and Programming, Fourth Edition 465


Importing/Exporting Facts
• It is not sufficient just to export the deftemplate
to make a fact visible to another module.
• To use a deftemplate defined in another module,
a module must also import the deftemplate
definition.
• A construct must be defined before it can be
specified in an import list, but it does not have to
be defined before it can be specified in an export
list; so, it is impossible for two modules to import
from each other.
Expert Systems: Principles and Programming, Fourth Edition 466
Modules and Execution Control

• The defmodule construct can be used to control


the execution of rules.
• Each module defined in CLIPS has its own
agenda.
• Execution can be controlled by deciding which
module’s agenda is selected for executing rules.

Expert Systems: Principles and Programming, Fourth Edition 467


The Agenda Command

• To display activations for the current module:


CLIPS> (agenda) 

• To display activations for the DETECTION


module:
CLIPS> (agenda DETECTION) 

Expert Systems: Principles and Programming, Fourth Edition 468


The Focus Command

• Assume there are rules on several agendas –


when the run command is issued, no rules fire.
• CLIPS maintains a current focus that determines
which agenda the run command uses during
execution.
• The reset and clear commands automatically set
the current focus to the MAIN module.
• The current focus does not change when the
current module is changed.

Expert Systems: Principles and Programming, Fourth Edition 469


The Focus Command

• To change the current focus:


CLIPS> (focus <module-name>+)

• Example:
CLIPS> (focus DETECTION) 
TRUE
CLIPS> (run)

• Now rules on the DETECTION module will be


fired.

Expert Systems: Principles and Programming, Fourth Edition 470


The Focus Command

• The focus command also recalls the previous


value of the current focus – the top of the stack
data structure called the focus stack.
• When the focus command changes the current
focus, it is pushing a new current focus onto the
top of the stack.
• As rules execute, the current focus becomes
empty, is popped from the focus stack, the next
module becomes the current focus until empty.

Expert Systems: Principles and Programming, Fourth Edition 471


Manipulating/Examining
the Focus Stack
CLIPS provides several commands for
manipulating the current focus and stack:
1. Clear-focus-stack – removes all modules from
focus stack
2. Get-focus – returns module name of current
focus or FALSE if empty
3. Pop-focus – removes current focus from stack
or FALSE if empty

Expert Systems: Principles and Programming, Fourth Edition 472


Manipulating/Examining the
Focus Stack
4. Get-focus-stack – returns a multifield value
containing the modules on the focus stack
5. Watch command – can be used to see changes
in the focus stack
6. Return command – terminate execution of a
rule’s RHS, remove current focus from focus
stack, return execution to next module
7. Auto-focus – changes default that a rule’s
module is not auto focused upon when that rule
is activated.
Expert Systems: Principles and Programming, Fourth Edition 473
The Rete Pattern-Matching
Algorithm
• Rule-based languages like CLIPS use the Rete
Pattern-Matching Algorithm for matching facts
against the patterns in rules to determine which
rules have had their conditions satisfied.
• If the matching process occurs only once, the
inference engine simply examines each rule and
then searches the set of facts to see if the rule’s
patterns have been satisfied – if so it is place on
the agenda.

Expert Systems: Principles and Programming, Fourth Edition 474


Rete Algorithm

• In rule-based languages, the matching process


takes place repeatedly and the fact list is
modified on each cycle of the execution.
• Such changes can cause previously unsatisfied
patterns to be satisfied or vice versa – as facts are
added/removed, the set of rules must be
updated/maintained.
• Checking rules against facts each cycle is a slow
process.

Expert Systems: Principles and Programming, Fourth Edition 475


Rete Algorithm

• Unnecessary computation can be avoided by


remembering what has already been matched
from cycle to cycle and computing only
necessary changes.
• The Rete algorithm uses temporal redundancy to
save the state of the matching process from cycle
to cycle, recomputing changes in this state only
for the change that occurred in the fact list.

Expert Systems: Principles and Programming, Fourth Edition 476


Rete Algorithm

• The Rete algorithm also takes advantage of


structural similarity in a rules.

Expert Systems: Principles and Programming, Fourth Edition 477


Pattern Network

Problems related to matching facts can be


divided into two steps:
1. When facts are added and removed it must be
determined which patterns have been matched.

2. Comparison of variable bindings across patterns


must be checked to determine the partial matches for
a group of patterns.
This process is performed in the pattern network
– similar to a tree.

Expert Systems: Principles and Programming, Fourth Edition 478


Join Network

• Once it has been determined which patterns have


been matched by facts, comparison of variable
binding across patterns must be checked to
ensure that variables used in more than one
pattern have consistent values.
• This is done in the join network which takes
advantage of structural similarity by sharing joins
between rules.

Expert Systems: Principles and Programming, Fourth Edition 479


Importance of Pattern Order

• Because the Rete algorithm saves the state from


one cycle to the next, it is important to make sure
that rules do not generate large numbers of partial
matches.

Expert Systems: Principles and Programming, Fourth Edition 480


Guidelines for Ordering Patterns

Guidelines:
– Place the most specific pattern toward the front of the
LHS of a rule.
– Patterns matching against facts frequently
added/removed from fact list should be placed toward
the end of the LHS of a rule.
– Patterns that will match very few facts in the fact list
should be placed near the front of the rule.

Expert Systems: Principles and Programming, Fourth Edition 481


General Rules vs. Specific Rules

1. Specific rules tend to isolate much of the


pattern-matching process in the pattern
network, reducing the amount of work in the
join network.
2. General rules often provide more opportunity
to sharing in the pattern and join networks.
3. A single rule can also be more maintained than
a group.
4. General rules need to be written carefully.

Expert Systems: Principles and Programming, Fourth Edition 482


Simple Rules vs. Complex Rules

1. The easiest way to code a problem in a rule-


based language is not necessarily the best.

2. The number of comparisons performed can


often be reduced by using temporary facts to
store data.

Expert Systems: Principles and Programming, Fourth Edition 483


Summary
• We discussed various CLIPS features to aid in
developing more robust expert systems.
• Deftemplate attributes permit enforcement of
type and value constraints.
• Salience provides a mechanism for more
complex control structures to prioritize rules.
• The defmodule construct allows a knowledge
base to be partitioned.
• We demonstrated the importance of matching
facts against rules efficiently.
Expert Systems: Principles and Programming, Fourth Edition 484
Summary

• Rules are converted into data structures in a rule


network consisting of a pattern network and a
join network.
• Ordering of patterns can have a significant effect
on the performance of rules.
• The most specific patterns and patterns matching
the fewest facts should be placed first in the LHS
of a rule, volatile patterns last in the LHS of a
rule.

Expert Systems: Principles and Programming, Fourth Edition 485


Chapter 10:
Procedural
Programming

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn how to use procedural functions to control


the flow of execution
• Learn how to use the deffunction construct to
define new functions
• Learn how to use the defglobal construct to
define global variables
• Learn how to use the defgeneric and defmethod
constructs to define generic functions and their
methods

Expert Systems: Principles and Programming, Fourth Edition 487


Objective

• Learn how defglobal, deffunction, and defgeneric


constructs can be imported and exported by
modules
• Learn how to use utility commands and functions

Expert Systems: Principles and Programming, Fourth Edition 488


Procedural Function
CLIPS provides several functions for controlling the
flow of execution:

if function while function

switch function loop-for-count


function
progn$ function break function

halt function

Expert Systems: Principles and Programming, Fourth Edition 489


if Function

General format:
(if <predicate-expression>
then <expression>+
[else <expression>+])

The predicate expression is first evaluated. If the


condition is anything other than FALSE, the
THEN clause actions are executed; otherwise, the
ELSE actions are executed (unless omitted).

Expert Systems: Principles and Programming, Fourth Edition 490


while Function

General format:
(while <predicate-expression> [do]
<expression>*)

The predicate expression is evaluated before


actions of the body are executed. If the
evaluation is anything other than FALSE,
expressions in the body are executed; if FALSE,
control passes to the next statement after the
while.
Expert Systems: Principles and Programming, Fourth Edition 491
switch Function
General format:
(switch <test-expression>
<case-statement>*
[<default-statement>])

Test-expression is evaluated first. Each comparison


expression is then evaluated in order defined and
if it matches the value of comparison expression,
actions after then are executed, then termination.
If no match, default statement actions are
executed (if present).
Expert Systems: Principles and Programming, Fourth Edition 492
loop-for-count Function

General format:
(loop-for-count <range-spec> [do] <expression*)

The body of the function is executed a given


number of times, depending on <range-spec>.
This is similar to the “for” loop structure in most
high-level languages.

Expert Systems: Principles and Programming, Fourth Edition 493


progn$ Function

General format:
(progn$ <list-spec> (expression>*)

<list-spec> can be :
<multifield-expression> - body is executed once for each
field
<list-variable> - field of current iteration is retrieved by
referencing variable. Special variable is created by
appending –index to <list-variable> - index of
current iteration

Expert Systems: Principles and Programming, Fourth Edition 494


break Function

General format:
(break)
The break function terminates the execution of
the while, loop-for-count, or progn$ function in
which it is immediately contained. It causes
early termination of a loop when a specified
condition has been met.

Expert Systems: Principles and Programming, Fourth Edition 495


halt Function

General format:
(assert (phrase halt))
The halt function can be used on the RHS of a
rule to stop execution of rules on the agenda.
When called, no further actions will be executed
from the RHS of the rule being fired and control
returns to the top-level prompt.

Expert Systems: Principles and Programming, Fourth Edition 496


The Deffunction Construct

• CLIPS allows one to define functions similar to


the way it is done in procedural languages.
• New functions are defined using the deffunction
construct:
(deffunction <deffunction-name> [<optional-comment>]
(<regular-parameter>* [<wildcard-parameter>])
<expression>*)

the body of the deffunction

Expert Systems: Principles and Programming, Fourth Edition 497


Deffunction

• The body of the deffunction is a series of


expressions similar to the RHS of a rule that are
executed in order when the deffunction is called.

• Deffunctions can be deleted and the watch


command can be used to trace their execution.

Expert Systems: Principles and Programming, Fourth Edition 498


Deffunction

• The <regular-parameter> and <wildcard-


parameter> declarations allow you to specify the
arguments that will be passed to the deffunction
when it is called.
• A deffunction can return values – value returned
is the value of the last expression evaluated
within the body of the deffunction.

Expert Systems: Principles and Programming, Fourth Edition 499


return Function

General format:
(return [<expression>])

In addition to terminating the execution of the


RHS of a rule, the return function also allows the
currently executing deffunction to be terminated.

Expert Systems: Principles and Programming, Fourth Edition 500


Recursions and Forward
Declarations
• Deffunctions can call other functions within their
body as well as themselves – a recursive call.
• Sometimes functions make circular references to
one another. To accomplish this:
– A forward declaration is done
– The name and argument list of the function are
declared
– The function body is left empty
– The forward declaration can be replaced later with a
version that includes the body

Expert Systems: Principles and Programming, Fourth Edition 501


Watching Deffunctions

• When deffunctions are watched using the watch


command, an informational message is printed
whenever a deffunction begins or ends execution.

• The >> symbol means that a deffunction is being


entered, and the << symbol means it is being
exited.

Expert Systems: Principles and Programming, Fourth Edition 502


Wildcard Parameters

• If all arguments in the parameter list of a


deffunction are single-field variables:
– 1-1 correspondence between these parameters and the
number of arguments that must be passed when the
deffunction is called.

• If the last parameter declared in a deffunction is a


multifield variable (wildcard parameter)
– Deffunction can be called with more arguments than
are specified in the parameter list.

Expert Systems: Principles and Programming, Fourth Edition 503


Deffunction Commands

1. ppdeffunction – displays the text representation


of a deffunction
2. pndeffunction – used to delete a deffunction
3. list-deffunction – displays the list of
deffunctions defined.
4. get-deffunction-list – returns a multifield value
containing the list of deffunctions

Expert Systems: Principles and Programming, Fourth Edition 504


User-Defined Function

User-defined functions are functions written in


the C language and called from CLIPS.

Expert Systems: Principles and Programming, Fourth Edition 505


The Defglobal Construct

• CLIPS allows one to define global variables that


retain their values outside the scope of a
construct.

• Local variables are local to the construct that


references them.

Expert Systems: Principles and Programming, Fourth Edition 506


Defining Global Variables

The defglobal construct has the following format:


(defglobal [<defmodule-name>] <global-assignment>*)

Defmodule-name = module in which globals will be defined (current if


omitted)

Global-assignment = <global-variable> = <expression>

Expression = provides the initial value for the global variable

Global-variable = ?*<symbol>

Expert Systems: Principles and Programming, Fourth Edition 507


Global Variables

• By entering the global variable’s name at the


command line, one can see its value.
• References to such variables can be made
anywhere it is legal to use an <expression>.
• They cannot be used as a parameter of a
deffunction.
• Can only be used on the LHS of a rule if
contained within a function call.

Expert Systems: Principles and Programming, Fourth Edition 508


Global Commands

1. ppdefglobal – displays text representation of a


defglobal
2. undefglobal – deletes a defglobal
3. list-defglobals – displays list of defglobals
defined in CLIPS
4. show-defglobals – displays names and values of
defglobals defined in CLIPS
5. get-defglobal-list – returns multifield value
containing the list of defglobals.

Expert Systems: Principles and Programming, Fourth Edition 509


The Defgeneric and
Defmethod Constructs
• Generic functions is a group of related functions
sharing a common name.
• Overloaded functions are those w/more than one
method.
• When called, the method w/signature matching is
the one executed – process called generic
dispatch

Expert Systems: Principles and Programming, Fourth Edition 510


Defgeneric / Defmethod

• General formats:
(defgeneric <defgeneric-name>
[<optional-comment>])

(defmethod <defgeneric-name> [index>]


[<optional-comment>]
[<regular-parameter-restriction>*
[<wildcard-parameter-restriction>])
<expression>*)

Expert Systems: Principles and Programming, Fourth Edition 511


Defmethods

• Specific defmethods do not have names but are


assigned a unique integer index that can be used
to reference the method.
• You can assign a specific index or CLIPS will
assign one for you.
• For a given generic function, each method must
have a unique signature from the other methods
in that generic function.

Expert Systems: Principles and Programming, Fourth Edition 512


Regular Parameter Restrictions

Each <regular-parameter-restriction> can be


one of two forms:

– A single-field variable (as in a deffunction), or

– Of the form (<single-field-variable>


<type>* [<query>])

Expert Systems: Principles and Programming, Fourth Edition 513


Method Precedence

• When a generic function is called, CLIPS


executes only one of the methods.
• Generic dispatch is the process of determining
which method will execute.
• CLIPS defines a precedence order to the methods
defined for a particular method.
• The preview-generic command displays the
precedence order.

Expert Systems: Principles and Programming, Fourth Edition 514


Precedence

Given two methods of a generic function,


CLIPS uses the following steps to determine
which method has the higher precedence – see
Fig. 10.1.
1. Compare the leftmost unexamined parameter
restrictions of the two methods. If only one method has
parameters left, proceed to step 6. If neither method
has parameters remaining, proceed to step 7.
Otherwise, proceed to step 2.

Expert Systems: Principles and Programming, Fourth Edition 515


Precedence
2. If one parameter is a regular parameter and the other
parameter is a wildcard parameter, then the method with
the regular parameter has precedence. Otherwise,
proceed to step 3.
3. If one parameter has type restrictions and the other
parameter does not, then the method with the types
restrictions has precedence. Otherwise, proceed to step
4.
4. Compare the leftmost unexamined type restriction of the
two parameters. If the type restrictions on one parameter
is more specific than on the other, the method with more
restrictions has precedence.
Expert Systems: Principles and Programming, Fourth Edition 516
Precedence
5. If one parameter has a query restriction and the other
does not, then the method with the query restriction has
precedence. Otherwise return to step 1 and compare
the next set of parameters.
6. If the next parameter of the method with parameters
remaining is a regular parameter, then this method has
precedence. Otherwise, the other method has
precedence.
7. The method that was defined first has precedence.

Expert Systems: Principles and Programming, Fourth Edition 517


Query Restrictions
• A query restriction is a defglobal reference or function
that is evaluated to determine the applicability of a
method when a generic function is called.
• If the query restriction evaluates to false, the method is
not applicable.
• The query restriction for a parameter is not evaluated
unless the type restrictions for that parameter are
satisfied.
• A method w/multiple parameters can have multiple query
restrictions and each must be satisfied for the method to
be applicable.

Expert Systems: Principles and Programming, Fourth Edition 518


Defmethod Commands

1. ppdefmethod – displays the text representation


of a defmethod
2. undefmethod – deletes a defmethod
3. list-defmethods – displays the list of
defmethods defined in CLIPS
4. get-defmethod-list – returns a multifield value
containing the list of defmethods

Expert Systems: Principles and Programming, Fourth Edition 519


Defgeneric Commands

1. ppdefgeneric – displays the text representation


of a defgeneric
2. undefgeneric – deletes a defgeneric
3. list-defgenerics – displays the list of
defgenerics defined in CLIPS
4. get-defgeneric-list – returns a multifield value
containing the list of defgenerics

Expert Systems: Principles and Programming, Fourth Edition 520


Procedural Constructs
and Defmodules
• Similar to deftemplate constructs, defglobal,
and defgeneric constructs can be imported and
exported by modules.
• Four of the export/import statements previously
discussed apply to procedural constructs:
1. (export ?ALL)
2. (export ?NONE)
3. (import <module-name> ?ALL)
4. (import <module-name> ?NONE)

Expert Systems: Principles and Programming, Fourth Edition 521


Procedural Constructs
and Defmodules
1. This format will export all exportable
constructs from a module.
2. Indicates that no constructs are exported.
3. Imports all exported constructs from the
specified module.
4. Imports none of the exported constructs from
the specified module.

Expert Systems: Principles and Programming, Fourth Edition 522


Useful Commands / Functions

1. load-facts / save-facts – allows facts to be


loaded from / saved to a file.
2. system command – allows execution of O/S
commands within CLIPS – not all O/S provides
functionality for this command.
3. batch command – allows commands and
responses normally entered at the top-level to
be read from a file.
4. dribble-on / dribble-off – session capture

Expert Systems: Principles and Programming, Fourth Edition 523


Commands / Functions

5. random – generates random integer values


6. string-to-field – converts a string-field value
into a field.
7. apropos command – displays al symbols
defined in CLIPS containing a specific
substring
8. sort function – sorts a list of fields

Expert Systems: Principles and Programming, Fourth Edition 524


Summary

• The if, while, switch, loop-for-count, progn$, and


break functions can alter the flow in a
deffunction, generic function, or on the RHS of a
rule – overuse is considered poor programming.
• The deffunction construct allows for defining
new functions (similar to procedural languages)
• The defglobal construct allows one to define
global variables that retain values outside the
scope of constructs.

Expert Systems: Principles and Programming, Fourth Edition 525


Summary

• Generic functions (w/defgeneric / defmethod)


provide more power and flexibility than
deffunctions by associating multiple procedural
methods with a generic function name.
• When generic functions are called, generic
dispatch examines the types of arguments passed
to the function and evaluates any associated
query restrictions to determine the appropriate
method to invoke.

Expert Systems: Principles and Programming, Fourth Edition 526


Summary
• Several utility commands were introduced,
including:
save-facts load-facts

system batch

dribble-on/off random

sort apropos

string-to-field

Expert Systems: Principles and Programming, Fourth Edition 527


Chapter 11:
Classes, Instances,
and Message-
Handlers

Expert Systems: Principles and


Programming, Fourth Edition
Objectives

• Learn how to define a class using the defclass


construct
• See how to create instances of classes using the
make-instance command
• Use message-handlers to attach procedural
information to a class
• Use the definstances construct to create a set of
specific instances of a class
• Learn about inheritance between classes

Expert Systems: Principles and Programming, Fourth Edition 529


Objectives

• See how pattern matching works with objects


• Create user-defined message-handlers
• Learn how to control slot access
• Survey the different types of message-handlers
• Create instances, initialization, and deletion of
message-handlers

Expert Systems: Principles and Programming, Fourth Edition 530


Objectives

• Learn how to modify and duplicate instances


• Learn how to uses classes and generic functions
• Learn how to use instance set query functions
• Learn about multiple inheritance between classes
• See how to import and export defclass constructs
and how to load and save instances

Expert Systems: Principles and Programming, Fourth Edition 531


The Defclass Construct

• Before instances of classes can be created, CLIPS


needs to know the list of valid slots for the given
class.
• To provide this information, the defclass
construct is used:
(defclass <class-name> [<optional-
comment>]
(is-a <superclass-name>)
<slot-definition>*)

Expert Systems: Principles and Programming, Fourth Edition 532


The Defclass Construct

• Note that <superclass-name> is the class from


which the newly defined class will inherit
information.
• All user-defined classes ultimately inherit from
the system class USER.
• A user-defined class will therefore inherit from
the USER class or from another user-defined
class.

Expert Systems: Principles and Programming, Fourth Edition 533


The Slot Definition

The syntax of the <slot-definition> is:


(slot <slot-name> <slot-attribute>* |
(multislot <slot-name> <slot-attribute>*)

type, range, cardinality, allowed-symbols


allowed-strings, allowed-lexemes, allowed-integers
allowed-floats, allowed-numbers, allowed-values
allowed-instance-names, default, and default-dynamic

Expert Systems: Principles and Programming, Fourth Edition 534


Creating Instances

To create an instance of a class, use the make-


instance command as follows:
(make-instance [<instance-name-expression>]
of <class-name-expression>
<slot-override>*)

where <slot-override> is:


(<slot-name-expression> <expression>)

Instances belong to the module in which their corresponding defclass


is defined.

Expert Systems: Principles and Programming, Fourth Edition 535


System-Defined Message-
Handlers
• Just like data, procedural information can be
attached to classes.
• Such procedures are called message-handlers.
– User-defined
– System-defined – automatically created
• Message handlers can be invoked for an instance
(object) using the send command.
(send <object-expression>
<message-name-expression> <expression>*)

Expert Systems: Principles and Programming, Fourth Edition 536


System Message-Handlers

• For each slot defined in a defclass, CLIPS


automatically defines get- and put-slot message-
handlers that are used to retrieve and set slot
values.
• The get-message-handlers have no arguments and
return the value of the slot.
• The put-message-handlers take zero or more
arguments. If not arguments are supplied, the
slot is restored to its original default-value.

Expert Systems: Principles and Programming, Fourth Edition 537


System Message-Handlers
• Supplying the arguments will set the slot value to
those values.
• The return value of a put-message-handler is the
new value of the slot.
• When slots are being watched, an informational
message is printed whenever the value of an
instance slot is changed.
• When instances are watched, an informative
message appears when an instance is
created/deleted.
Expert Systems: Principles and Programming, Fourth Edition 538
The Definstances Construct

• The definstances construct is the equivalent of


the deffacts construct.
• When a reset command is issued, all instances are
sent a delete message.
• Then all instances found in the definstances
constructs are created.

Expert Systems: Principles and Programming, Fourth Edition 539


Definstances Construct

General format:
(definstances <definstances name> [active]
[<optional comment>]
<instance-definition>*)

Where <instance-definition> is:


([instance-name-expression>] of
<class-name-expression>
<slot-override>*)

Expert Systems: Principles and Programming, Fourth Edition 540


Definstances Construct

• By default, pattern matching does not occur for


definstances instances until all the slot overrides
have been processed.
• Several commands exist for manipulating
definstances:
list-definstances – displays list of definstances maintained by CLIPS
ppdefinstances – displays text representations of definstances
undefinstances – deletes definstances
get-definstances-list – returns multifield value containing list of
definstances

Expert Systems: Principles and Programming, Fourth Edition 541


Classes and Inheritance

• One benefit of using COOL is class inheritance.


• Classes allow us to share common information
among multiple classes w/o duplication of
information or inclusion of unnecessary
information.
• It is possible for a class to redefine a slot that was
already defined by one of its superclasses.

Expert Systems: Principles and Programming, Fourth Edition 542


Classes and Inheritance

• Definitions:
– Subclass – class that inherits directly/indirectly from
another class
– Superclass – class from which subclass inherits
• A single-inheritance class hierarchy is one in
which each class has only one direct superclass.
• A multiple-inheritance hierarchy (COOL) is
where a class may have more than one direct
superclass.

Expert Systems: Principles and Programming, Fourth Edition 543


Classes and Inheritance
• The source slot attribute allows slot attributes to inherit
from superclasses.
• The default value for the slot is exclusive – attributes
determined by most specific class defining the slot.
– Single inheritance – the class having fewest
superclasses
• Source slot composite, attributes not explicitly defined in
most specific class defining slot are taken from next most
specific that defines the attribute.

Expert Systems: Principles and Programming, Fourth Edition 544


Classes and Inheritance

• It is possible to disable the inheritance of a slot


using the propagation slot attribute:
– Inherit (default) – slot will be inherited by subclasses
– No-inherit – slot will not be inherited by subclasses
• It is possible to define classes to be used only for
inheritance – abstract classes; instances cannot
be created – concrete by default.
• The role class attribute specifies whether a class
is abstract or concrete.

Expert Systems: Principles and Programming, Fourth Edition 545


Commands to Manipulate
Defclasses
1. list-defclasses – displays the current list of
defclasses maintained by CLIPS
2. browse-classes – displays the inheritance
relationships between a class and its subclasses
3. ppdefclass – displays the text representation of
a defclass
4. undefclass – deletes a defclass

Expert Systems: Principles and Programming, Fourth Edition 546


Object Pattern Matching

• Single object pattern can match instances from


several classes.
• Changes to slot values that are not specified in an
object pattern do not retrigger the rule to which
the pattern belongs.
• Changes to slot values that are not specified in an
object pattern within a logical conditional
element do not remove logical support provided
by the associated rule.

Expert Systems: Principles and Programming, Fourth Edition 547


Object Pattern

General format:
(object <attribute-constraint>*)

where <attribute-constraint> is:


(is-a <constraint>) |
(name <constraint>) |
(<slot-name> <constraint>*)

and <constraint> is the same as the pattern slot


constraints that are used in deftemplate patterns.

Expert Systems: Principles and Programming, Fourth Edition 548


Object Patterns
• One difference between object patterns and fact
patterns is that only those object patterns that
explicitly match on a slot are affected when the
slot value of an instance is changed.
• It is possible to force a slot or a class not to
participate in pattern matching using the pattern-
match attribute.
• Object patterns and instance creation can be used
in conjunction with the logical conditional
element just as facts and facts patterns can.
Expert Systems: Principles and Programming, Fourth Edition 549
Object Patterns

• Changes in instance slots do not affect the logical


support for a fact or instance if the slot was not
referenced in an object pattern within the logical
conditional element.
• When using object patterns in a rule, CLIPS will
sometimes use the initial-object/fact pattern. If
so, an initial-fact pattern is added if the pattern
preceding the insertion position is a fact pattern –
if an object pattern an initial-object pattern is
inserted.
Expert Systems: Principles and Programming, Fourth Edition 550
User-Defined Message-Handlers

• In addition to print, delete, put, and get-system-


defined message handlers, COOL automatically
defines for each class, you can define your own
message-handlers using defmessage-handler.
• General format:
(defmessage-handler <class-name> <message-name>
[<handler-type>]
[<optional-comment>]
(<regular-parameter>*
[<wildcard-parameter>])
<expression>*)
Expert Systems: Principles and Programming, Fourth Edition 551
User-Defined Message-Handlers

• By default, a message-handler is a primary


message-handler.
• Each class has its own set of message-handlers.
• The body of the message-handler, represented by
<expression>*, behaves like the body of a
deffunction.
• The bind function can be used to bind local
variables and the last expression evaluated in the
body is the value returned.

Expert Systems: Principles and Programming, Fourth Edition 552


Slot Shorthand References
• A shorthand mechanism allows one to access the
slots of the instances bound to the ?self variable.
• The expression: ?self:<slot-name> can be used
to retrieve the value of a slot.
• Similarly,
(bind ?self:slot-name <expression>*)
can be used to set a slot value.
• Both bypass message-passing and directly
manipulate slots and can only be used for slots
directly defined by the class.
Expert Systems: Principles and Programming, Fourth Edition 553
Encapsulation

• COOL supports object encapsulation


– Hiding the details of the class
– Limiting access to the class via a well-defined
interface – message-handlers defined for the class
• If the visibility slot attribute is set to private
(default), the slot can only be directly accessed
by message-handlers of class defining it; if
public, slot can be directly accessed by
subclasses and superclasses defining it.

Expert Systems: Principles and Programming, Fourth Edition 554


Watching Messages & Message
Handlers
• If message-handlers or messages are watched with
watch, informational messages are printed when a
message-handler begins or ends execution.
• Defmessage-handler commands
– list-defmessage-handlers – displays current list of
defmessage-handlers maintained by CLIPS
– ppdefmessage-handler – displays text representation
of defmessage-handler
– undefmessage-handler – deletes a defmessage-handler
– get-defmessage-handler – returns multifield value
containing list of defmessage-handlers for class
Expert Systems: Principles and Programming, Fourth Edition 555
Slot Access and Handler Creation

• The access and create-accessor slot attributes


control the access of slots.
• Access attribute restricts the type of access
allowed to slots – read-write, read-only, initialize
only.
• The create-accessor attribute controls the
automatic creation of the get- and put-handlers for
class slots – read-write, read-only, write-only, and
none.

Expert Systems: Principles and Programming, Fourth Edition 556


Before/After/Around
Message-Handlers
• When an existing class does not meet your needs
and may depend on other code to maintain its
behavior – unfamiliar code or code you don’t
want to modify.
• To get around this, you can define a new class
that will inherit whatever behavior you want
from the existing class.
• Message-handler can be one of four types:
primary, before/after, and around.

Expert Systems: Principles and Programming, Fourth Edition 557


Before/After/Around
Message-Handlers
1. Primary handler – (default) typically the main
handler for responding to a message, overrides
/ shadows primary message-handler for same
message inherited from a superclass.
2. Before/After handlers – invoked before and
after the primary handler, respectively.
3. Around handlers – must explicitly invoke the
other handler types.

Expert Systems: Principles and Programming, Fourth Edition 558


Before/After/Around
Message-Handlers
4. #2 and #3 handlers of superclasses are now
shadowed by subclass definition.
5. It is possible to override the arguments passed
to a message-handler by using the override-
next-handler command.

Expert Systems: Principles and Programming, Fourth Edition 559


Handler Execution Order

• With all the available techniques to modify class


behaviors, which is the best approach?
• A class can slightly modify the behavior of a
superclass using before and after handler w/o
overriding the primary handler.
• Subclass cannot prevent the execution of a before
or after handler unless they terminate the
message – preventing the execution of all before,
after, and primary handlers.

Expert Systems: Principles and Programming, Fourth Edition 560


Handler Execution Order

• If an existing class’s behavior is modified by


redefining a new class and then overriding the
primary handler, the primary handler is also
subject to being overridden by a subclass.
• Unless the overriding class calls the call-next-
handler function, the primary handler will not be
executed.
• See page 670 of the text for steps.

Expert Systems: Principles and Programming, Fourth Edition 561


Instance Creation, Initialization,
and Deletion Message-Handlers
1. create -- is called after an instance is created
but before any default values or slot overrides
have been applied.
2. init – is called after slot overrides have been
processed to set any remaining slot values that
were not overridden to their default values
3. delete – either explicitly called to delete an
instance or automatically called when you call
make-instance and specify instance name of
existing instance.
Expert Systems: Principles and Programming, Fourth Edition 562
Modifying / Duplicating Instances

1. modify-instance – slot values are changed


directly by the direct-modify message-handler
w/o invoking message passing.
2. message-modify-instance – same as #1 but uses
message-passing to change slot values.
3. active-modify
4. active-message-modify

Expert Systems: Principles and Programming, Fourth Edition 563


Commands for
Duplicating Instances
1. duplicate-instance

2. message-duplicate-instance

3. active-duplicate-instance

4. active-message-modify-instance

Expert Systems: Principles and Programming, Fourth Edition 564


Instance Set Query Functions
1. Any-instancep function – if a set of instances is found
that satisfy the query, then the any-instancep function
returns the symbol TRUE; otherwise FALSE.
2. Find-instance query function – returns a multifield
value containing the first instance set satisfying the
query, then the multifield value will be empty.
3. find-all-instances function – returns a multifield value
containing all instance sets satisfying the query.
4. Do-for-instance, do-for-all-instances, and delayed-do-
for-all allow actions on the instance sets satisfying a
query.
Expert Systems: Principles and Programming, Fourth Edition 565
Multiple Inheritance

Specifying:
– Single inheritance – a single class specified in the is-a
attribute
– Multiple inheritance – specify more than one class in
the is-a attribute

Expert Systems: Principles and Programming, Fourth Edition 566


Multiple Inheritance Conflicts

• The most practical examples involve cases where


the superclass from which the class is inheriting
do not share slots or message-handlers – no
conflicts occur here.
• In simple cases where the classes specified in the
is-a attribute do not share common user-defined
superclasses, the order in which the classes are
specified determines the precedence when there
are multiple definitions of the same slot or
message-handler.
Expert Systems: Principles and Programming, Fourth Edition 567
Defclasses and Defmodules

• In a similar manner to other constructs, defclass


constructs can be imported and exported by
modules.
• The export and import statements previously
discussed which export or import all constructs,
also apply to defclasses.
• Explicit specifying which defclasses are exported
or imported is possible.

Expert Systems: Principles and Programming, Fourth Edition 568


Loading and Saving Instances

1. save-instances command – saves instances to a


file
2. load-instances command – loads in a group of
instances stored in a file
3. bsave- and boad-instances command – similar
to #1 and #2, except binary format is used

Expert Systems: Principles and Programming, Fourth Edition 569


Summary
• This chapter introduced the CLIPS Object-
Oriented Language (COOL)
• Instances (objects) are another data representation
provided by CLIPS.
• Instance attributes are specified using the defclass
construct.
• Procedural code is implemented using the
defmessage-handler construct.
• Inheritance allows classes to make use of slots and
message-handlers associated with another class.
Expert Systems: Principles and Programming, Fourth Edition 570
Summary

• COOL supports single- and multiple- inheritance.


• In addition to the slot attributes provided with
deftemplates, several additional slot attributes are
also supported by defclasses.
• Several predefined system message-handlers for
creating, initializing, printing, and deleting
instances are available.
• User-defined message-handlers can also be
created.

Expert Systems: Principles and Programming, Fourth Edition 571


Summary

• Message-handlers are invoked by sending an


instance a message name along with associated
arguments via the send command
• Object pattern matching provides several
capabilities not found with fact pattern matching.
• Finally, COOL provides several instance set
query functions that allow direct queries on sets
of instances satisfying a specified set of
conditions.

Expert Systems: Principles and Programming, Fourth Edition 572


Chapter 12:
Expert Systems
Design Examples

Expert Systems: Principles and


Programming, Fourth Edition
Summary

• Learn about incorporating uncertainty into


CLIPS
• The use of decision trees to provide a useful
paradigm
• Learn how to emulate backward chaining in a
CLIPS program
• Constructing a framework of simpler expert
systems into one large program

Expert Systems: Principles and Programming, Fourth Edition 574


Uncertainty Factors

• Although CLIPS has no built-in capabilities for


handling uncertainty, it is possible to incorporate
uncertainty into CLIPS by placing information
dealing with uncertainty directly into the facts
and rules.
• We begin by exploring how uncertainty was
incorporated into the expert system MYCIN.

Expert Systems: Principles and Programming, Fourth Edition 575


MYCIN and Uncertainty

1. MYCIN represents factual information as


object-attribute-value (OAV) triplets.
2. MYCIN also associates with each fact a
certainty factor (CF) which represents a degree
of belief in the fact.
• -1 means the fact is false
• 0 means no information is known about the fact
• 1 means the fact is known to be true

Expert Systems: Principles and Programming, Fourth Edition 576


MYCIN and Uncertainty

3. Because CLIPS does not handle uncertainty


factors automatically, a slot in each fact will be
used to represent the uncertainty factor.

4. MYCIN allows the same OAV triple to be


derived by separate rules. The OAV triples are
then combined producing a single OAV triple
that combines the certainty factors.

Expert Systems: Principles and Programming, Fourth Edition 577


MYCIN and Uncertainty

5. In order to allow identical OAV triples to be


asserted with the same certainty factors, the set-
fact-duplication command can be used to
disable the CLIPS behavior preventing
duplicated facts from being asserted.
6. MYCIN combines two identical OAV triples
into a single OAV triple with a combined
uncertainty, computed as:
New Uncertainty = (CF1 + CF2) – (CF1 * CF2)

Expert Systems: Principles and Programming, Fourth Edition 578


MYCIN and Uncertainty
7. Since CLIPS does not automatically handle
certainty factors for facts, it does not
automatically combine two OAV triplets
derived from different rules. The combination
is handled by a rule that searches the fact list
for identical OAV triples to be combined.
8. Next, it is necessary to link the certainty
factors of the facts that match the LHS of the
rule to the certainty factors of the facts
asserted by the RHS of the rule – CF of
LHS < 0.2 will not fire.

Expert Systems: Principles and Programming, Fourth Edition 579


MYCIN and Uncertainty

9. The certainty factor of a fact asserted from


the RHS of a rule is derived by multiplying
the CF of the assertion by the certainty
factor of the LHS of the rule.

Expert Systems: Principles and Programming, Fourth Edition 580


MYCIN and Uncertainty
10. The single combine-certainties method handles only
the case where both certainty factors are positive. By
additional methods the other cases of certainty can be
handled.

Expert Systems: Principles and Programming, Fourth Edition 581


Decision Trees

1. Decision trees provide a useful paradigm for


solving certain types of classification problems.
2. Decision trees derive solutions by reducing the
set of possible solutions with a series of
decisions or questions that prune their search
space.
3. Problems suitable for decision trees are those
that provide the answer to a problem from a
predetermined set of possible answers.

Expert Systems: Principles and Programming, Fourth Edition 582


Decision Trees

4. Decision trees consist of nodes and branches,


connecting parent nodes to child from the top to
bottom. The top node (root) has no parent.
Every other node has only one parent. Nodes
with no children are leaves.
5. Leaf nodes represent all possible solutions that
can be derived from the tree – answer nodes.
All other nodes are decision nodes.

Expert Systems: Principles and Programming, Fourth Edition 583


Decision Trees

6. In general, a decision node may use any criteria


to select which branch to follow as long as it
yields only one branch. The branch selected
may be a set or range of values, etc.
7. The procedure to traverse a tree to reach an
answer is simple – begin at root, if the current
node is decision, answer the question – YES,
move left, NO, move right. When answer node
is current location value is derived from the
decision tree.
Expert Systems: Principles and Programming, Fourth Edition 584
Decision Trees with
Multiple Branches

• A binary decision tree may prove inefficient –


not allowing for a set of responses or a series of
cases.
• A modified decision tree allows for multiple
branches – giving a series of possible decisions.

Expert Systems: Principles and Programming, Fourth Edition 585


Figure 12.1 Binary Decision Tree

Expert Systems: Principles and Programming, Fourth Edition 586


Figure 12.2 Decision Tree
with Multiple Branches

Expert Systems: Principles and Programming, Fourth Edition 587


Decision Trees
That Learn

• Sometimes it is useful to add new knowledge to a


decision tree.

• Learning can result in the decision tree becoming


unbalanced – efficient decision trees are
balanced.

Expert Systems: Principles and Programming, Fourth Edition 588


Figure 12.3 Animal Identification
Decision Tree

Expert Systems: Principles and Programming, Fourth Edition 589


Figure 12.4 Animal Identification
Decision Tree After Learning Bird

Expert Systems: Principles and Programming, Fourth Edition 590


A Rule-Based
Decision Tree Program
• The first step implementing the learning process
in a decision tree in CLIPS is to decide how
knowledge should be represented.
• Since the tree should learn, the tree should be
represented as facts instead of rules – facts are
easily added / deleted from a tree.
• A set of CLIPS rules can be used to traverse the
decision tree by implementing the
Solve_Tree_and_Learn algorithm using rule-
based approach.
Expert Systems: Principles and Programming, Fourth Edition 591
A Rule-Based
Decision Tree Program
• Each node of the tree should be represented by a
fact.
• From one run to the next, information about what
has been learned will be stored in a file.
• The rules for traversal of the tree must be
determined.

Expert Systems: Principles and Programming, Fourth Edition 592


Backward Chaining

• CLIPS does not directly implement backward


chaining but it can be emulated using forward
chaining CLIPS rules.

• Backward chaining rules are represented as facts


and acted on by the CLIPS backward-chaining
inference rules.

Expert Systems: Principles and Programming, Fourth Edition 593


Backward Chaining

1. Backward chaining rules are represented as


facts so the antecedents and consequents can be
examined by rules that will act as a backward
chaining inference engine.
2. As backward chaining proceeds, subgoals will
be generated to determine the value of
attributes. A fact will be needed to represent
information about goal attributes. Ordered
facts will be used to represent goal attributes.

Expert Systems: Principles and Programming, Fourth Edition 594


Backward Chaining

3. The backward chaining inference engine can be


implemented with two sets of rules.
1. The first group generates goals for attributes and
asks the user to supply attribute values when these
values cannot be determined by rules.
2. The second group of rules will perform update
operations, including modifying rules when their
conditions have been satisfied and removing goals
when they have been satisfied.

Expert Systems: Principles and Programming, Fourth Edition 595


A Monitoring Program

Problem Statement:
The problem to be solved is an example of a
simple monitoring system – well suited for
forward chaining rule-based languages.
– Input consists of sensor values read during program
cycles.
– Inference occurs until all possible conclusions can be
derived from the input data are reached.

Expert Systems: Principles and Programming, Fourth Edition 596


A Monitoring Program
The Details Needed to Begin:
Several problem specifications are necessary
before an expert system for any example can be
built.
– First, the expected behavior of the expert system
should be specified.
– Decisions should be made regarding whether or not to
broaden specifications to allow future modifications or
upgrading.
– It must be determined how data is to be retrieved.
Expert Systems: Principles and Programming, Fourth Edition 597
A Monitoring Program

Knowledge Definitions:

Determine how the knowledge should be


represented.

Expert Systems: Principles and Programming, Fourth Edition 598


A Monitoring Program

Control of Execution:
Determine the phases of the monitoring process –

– Read values from sensors, associate guard line and red


line conditions for sensor values and determine
developing trends.
– After trends have been established, the system should
issue warnings, shut down, restart, etc.

Expert Systems: Principles and Programming, Fourth Edition 599


A Monitoring Program

Reading the Raw Sensor Values:

Sensor values will be read from a file.

Expert Systems: Principles and Programming, Fourth Edition 600


A Monitoring Program

Detecting a Trend:

Determine the current state of the sensors and


calculate trends that may be developing.

Expert Systems: Principles and Programming, Fourth Edition 601


A Monitoring Program

Issue Warnings:
The final phase is the warning phase.
– Sensors having entered red line regions will have their
associated devices shut off.
– Sensors staying w/in guard line region for a number of
cycles will have their devices shut off.
– Sensors in guard line region and did not have their
devices shut off will have a warning issued

Expert Systems: Principles and Programming, Fourth Edition 602


Summary

• This chapter demonstrated a technique for


representing MYCIN-style certainty factors in
CLIPS.
• Facts are used to represent OAV triplets.
• An additional slot in each fact represents the
certainty factor of the fact.
• Rules are used to compute certainty values for
newly asserted facts on the on RHS of a rule
using certainty factors bound in the LHS of rule.

Expert Systems: Principles and Programming, Fourth Edition 603


Summary
• Decision trees can be represented using the
forward chaining paradigm of CLIPS.
• There are several algorithms for traversing
decision trees.
• The algorithm for a multiple-branch decision tree
that learns is implemented in CLIPS.
• CLIPS can emulate backward chaining inference
strategy; backward chaining rules are represented
as facts and acted on by backward chaining
inference rules.
Expert Systems: Principles and Programming, Fourth Edition 604
Summary

• The final example was a simple monitoring


expert system.

Expert Systems: Principles and Programming, Fourth Edition 605

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