COMP
COMP 4200:
4200:
Expert
Expert Systems
Systems
Dr. Christel Kemke
Department of Computer Science
University of Manitoba
C. Kemke Control 1
Rule-based
Rule-based Systems
Systems Control
Control
Procedural Control
Conflict Resolution Strategies
Salience
Modules
C. Kemke Control 2
Procedural
Procedural Control
Control
if-then-else
while-do
C. Kemke Control 3
Procedural
Procedural Control
Control in
in Actions
Actions
Procedural Control Elements can appear on the
RHS of a rule or in message-handlers of classes.
(if <predicate-expression>
then <expression>+
[else <expression>+ ]) ;;else-part optional
(while <predicate-expression>
[do] <expression>* ]) ;;do not mandatory
C. Kemke Control 4
Example
Example if-then-else
if-then-else
(defrule special-age 18, 21, 100
(or (person (name ?name) (age ?age&18))
(person (name ?name) (age ?age&21))
(person (name ?name) (age ?age&100)))
=>
(if (= ?age 18) then (printout t ?name can buy beer
in Canada.)
else
(if (= ?age 21) then (printout t ?name can
buy beer in the USA.)
else
(if (= ?age 100) then (printout t The
major will visit ?name ))...)
C. Kemke Control 5
Rule
Rule Activation
Activation and
and
Execution
Execution
Pattern Matching, Rule Activation,
Rule Execution
Conflict Set, Conflict Resolution,
Strategies
C. Kemke Control 6
Facts,
Facts, Rules,
Rules, Pattern
Pattern Matching
Matching
Forward-chaining systems begin with an initial
set of facts and in an inference process
generate new facts until a goal state is reached.
Facts are entered into the Working Memory
(WM).
The conditions of each rule are matched to
these facts Pattern Matching (e.g. Rete
Algorithm)
Rules whose conditions are satisfied are
activated and entered on the agenda.
C. Kemke Control 7
Rule
Rule Activation
Activation
The pattern matching algorithm determines, in
which sequence rules are being activated, i.e.
placed on the agenda.
In CLIPS, you cannot easily determine the
sequence of rule activations.
Thus, do not write programs, which implicitly
depend on a certain sequence of facts
activating rules.
C. Kemke Control 8
Conflict
Conflict Resolution
Resolution
The set of activated rules on the agenda is called
the Conflict Set.
Choosing which of the activated rules to fire next
is known as Conflict Resolution.
A simple strategy is to select rules according to
the order, in which they are put on the agenda.
CLIPS, for example, uses a stack (last-in first-
out).
There are different types of Conflict Resolution
Strategies.
C. Kemke Control 9
Conflict
Conflict Resolution
Resolution Strategies
Strategies
There are two categories of strategies:
global strategies
local strategies
Global Strategies are domain-independent and
part of the Rule Interpreter/Inference Engine.
Local Strategies are domain-dependent and
implemented as part of the Rule Base.
C. Kemke Control 10
Conflict
Conflict Resolution:
Resolution:
Refractoriness
Refractoriness
Refractoriness
Forward chainers typically implement a
refractory conflict resolution strategy - once a
rule is fired, it isn't used again on the same
data
C. Kemke Control 11
Conflict
Conflict Resolution:
Resolution: Specificity
Specificity
Specificity:
Choose a rule with the most conditions or
the most specific conditions ahead of a
more general rule (prefer most specific
knowledge instead of general knowledge)
C. Kemke Control 12
Conflict
Conflict Resolution:
Resolution: Recency
Recency
Recency:
Fire a rule first that is activated by a fact just
added to Working Memory, i.e. fire most
recently activated rule first ( focus on one
line of reasoning, with all related facts and
rules)
C. Kemke Control 13
Explicit
Explicit Control
Control
Salience
Meta-Rules
C. Kemke Control 14
Salience
Salience
We can use salience measures to prioritize rules.
CLIPS provides a built-in method for prioritizing
rules:
(declare (salience value))
Salience values can range from -10000 to +10000.
Default is 0.
We can thus force the execution of one rule over
another. We can implement sequencing of rules.
C. Kemke Control 15
Rule
Rule Prioritization
Prioritization in
in Clips
Clips
for example, consider the following rules...
(forced order of execution)
C. Kemke Control 16
Two
Two Nifty
Nifty Rules
Rules
(defrule fire-first
(declare (salience 30))
(priority first)
=>
(printout t "Print First" crlf) )
(defrule fire-second
(declare (salience 20))
(priority second)
=>
(printout t "Print Second" crlf) )
C. Kemke Control 17
And
And One
One More...
More...
(defrule fire-third
(declare (salience 10))
(priority third)
=>
(printout t "Print Third" crlf) )
C. Kemke Control 18
Getting
Getting Ready
Ready to
to Run...
Run...
(assert (priority second))
(assert (priority first))
(assert (priority third))
(agenda)
30 fire-first: f-2
20 fire-second: f-1
10 fire-third: f-3
For a total of 3 activations.
C. Kemke Control 19
Running
Running This...
This...
The CLIPS agenda acts like a stack - last rule
on, first fired
If salience were not used, the third rule, then
the first, then the second would fire, due to the
sequence of facts and activation of rules:
(assert (priority second))
(assert (priority first))
(assert (priority third))
C. Kemke Control 20
Reasoning
Reasoning Control
Control
Classes of Rules
C. Kemke Control 21
Categories
Categories of
of Rules
Rules
Salience values are arbitrary; often what we want is
that a certain class of rules are considered before
others.
This can be built into the rules themselves using a
kind of 'tag'
IF (status is check-for-emergencies) ....
and employed by setting a fact to allow various
categories of rules to be selected - for example,
asserting that the status is check-for-emergencies
Another rule can be implemented to change status to
the next group of rules.
C. Kemke Control 22
Categories
Categories Example
Example
(deffacts control-information
(phase detection)
(phase-after detection isolation)
(phase-after isolation recovery)
)
C. Kemke Control 23
Categories
Categories Example
Example
Context Limiting
(defrule change-phase
(declare (salience -10))
?phase <- (phase ?current-phase)
(phase-after ?current-phase ?next-phase)
=>
(retract ?phase)
(assert (phase ?next-phase)
)
C. Kemke Control 24
Another
Another Way...
Way...
(deffacts control-information
(phase detection)
(phase-sequence isolation recovery detection))
C. Kemke Control 25
Another
Another Way...
Way...
(defrule change-phase
(declare (salience -10))
?phase <- (phase ?current-phase)
?list <- (phase-sequence ?next-phase $?other-phases)
=>
(retract ?phase ?list)
(assert (phase ?next-phase)
(assert (phase-sequence ?other-phases ?next-phase))
)
C. Kemke Control 26
Explanation
Explanation
Note the $?other-phases
The $ operator causes the variable to be
bound to several symbols (the remainder of
the list of phases, in this case) rather than
one. Note that the $ is not part of the variable
name and isn't used later when referencing
the variable.
C. Kemke Control 27
Explanation
Explanation
This approach could be used in a monitoring or
control system - forward reasoning is typically
used in such systems because we get
information, then want to see the ramifications
of it (analyze, provide treatment, and so on)
C. Kemke Control 28
Control:
Control: Meta-Rules
Meta-Rules
Meta-Rules
Use Meta-Rules to divide rules into classes.
Choose one class over another at a given
point.
This implements domain-dependent
knowledge about which set of rules to use
during reasoning.
CLIPS provides a Module-construct with
similar effects.
C. Kemke Control 29