Rule Based Expert System دكترمحسن كاهاني kahani
-
Upload
erik-hamilton -
Category
Documents
-
view
223 -
download
2
Transcript of Rule Based Expert System دكترمحسن كاهاني kahani
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Introduction Knowledge is a theoretical or practical
understanding of a subject or a domain. Knowledge is also the sum of what is currently known, and apparently knowledge is power. Those who possess knowledge are called experts.
Anyone can be considered a domain expert if he or she has deep knowledge (of both facts and rules) and strong practical experience in a particular domain. The area of the domain may be limited. In general, an expert is a skilful person who can do things other people cannot.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Introduction The human mental process is internal, and it is too
complex to be represented as an algorithm. However, most experts are capable of expressing their knowledge in the form of rules for problem solving.
IF the ‘traffic light’ is green
THEN the action is go
IF the ‘traffic light’ is red
THEN the action is stop
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rules as a knowledge representation technique
The term rule in AI can be defined as an IF-THEN structure that relates given information or facts in the IF part to some action in the THEN part.
Any rule consists of two parts: IF part, called the antecedent (premise or condition) THEN part called the consequent (conclusion or action).
IF <antecedent>
THEN <consequent>
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rules (cont.) A rule can have multiple antecedents joined by the
keywords AND (conjunction), OR (disjunction) or a combination of both.
IF <antecedent 1> IF <antecedent 1>
AND <antecedent 2> OR <antecedent 2>
AND <antecedent n> OR <antecedent n>
THEN <consequent> THEN <consequent>
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
The antecedent of a rule incorporates two parts:
an object (linguistic object) its value. The object and its value are linked by an operator.
The operator identifies the object and assigns the value. Expert systems can also use mathematical operators to
define an object as numerical and assign it to the numerical value.IF ‘age of the customer’ < 18
AND ‘cash withdrawal’ > 1000
THEN ‘signature of the parent’ is required
Rules (cont.)
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
RelationIF the ‘fuel tank’ is empty
THEN the car is dead Recommendation
IF the season is autumn
AND the sky is cloudy
AND the forecast is drizzle
THEN the advice is ‘take an umbrella’
Rule Types
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
DirectiveIF the car is dead
AND the ‘fuel tank’ is empty
THEN the action is ‘refuel the car’ Strategy
IF the car is dead
THEN the action is ‘check the fuel tank’;
step1 is complete
IF step1 is complete
AND the ‘fuel tank’ is full
THEN the action is ‘check the battery’;
step2 is complete
Rule Types(cont.)
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
HeuristicIF the spill is liquid
AND the ‘spill pH’ < 6
AND the ‘spill smell’ is vinegar
THEN the ‘spill material’ is ‘acetic acid’
Rule Types (cont.)
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
The main players in the development team
There are five members of the expert system development team: the domain expert, the knowledge engineer, the programmer, the project manager and the end-user.
The success of their expert system entirely depends on how well the members work together.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Structure of a rule-based expert system
In the early seventies, Newell and Simon from Carnegie-Mellon University proposed a production system model, the foundation of the modern rule based expert systems.
The production model is based on the idea that humans solve problems by applying their knowledge (expressed as production rules) to a given problem represented by problem-specific information.
The production rules are stored in the long-term memory and the problem-specific information or facts in the short-term memory.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Elements of a rule-based expert system
The knowledge base contains the domain knowledge useful for problem solving. In a rule based expert system, the knowledge is represented as a set of rules.
The database includes a set of facts used to match against the IF (condition) parts of rules stored in the knowledge base.
The inference engine carries out the reasoning whereby the expert system reaches a solution. It links the rules given in the knowledge base with the facts provided in the database.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Elements of a rule-based expert system
The explanation facilities enable the user to ask the expert system how a particular conclusion is reached and why a specific fact is needed.
The user interface is the means of communication between a user seeking a solution to the problem and an expert system.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Characteristics of an expert system
An expert system is built to perform at a human expert level in a narrow, specialised domain. Thus, the most important characteristic of an expert system is its high-quality performance. No matter how fast the system can solve a problem, the user will not be satisfied if the result is wrong.
On the other hand, the speed of reaching a solution is very important. Even the most accurate decision or diagnosis may not be useful if it is too late to apply, for instance, in an emergency, when a patient dies or a nuclear power plant explodes.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Characteristics of an expert system
Expert systems apply heuristics to guide the reasoning and thus reduce the search area for a solution.
A unique feature of an expert system is its explanation capability. It enables the expert system to review its own reasoning and explain its decisions.
Expert systems employ symbolic reasoning when solving a problem. Symbols are used to represent different types of knowledge such as facts, concepts and rules.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Can expert systems make mistakes?
Even a brilliant expert is only a human and thus can make mistakes. This suggests that an expert system built to perform at a human expert level also should be allowed to make mistakes.
But we still trust experts, even we recognize that their judgments are sometimes wrong. Likewise, at least in most cases, we can rely on solutions provided by expert systems, but mistakes are possible and we should be aware of this.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Can expert systems make mistakes?
In expert systems, knowledge is separated from its processing (the knowledge base and the inference engine are split up).
A conventional program is a mixture of knowledge and the control structure to process this knowledge.
When an expert system shell is used, a knowledge engineer or an expert simply enters rules in the knowledge base. Each new rule adds some new knowledge and makes the expert system smarter.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Forward chaining and backward chaining
domain knowledge is represented by a set of IF-THEN production rules
data is represented by a set of facts about the current situation.
The inference engine compares each rule stored in the knowledge base with facts contained in the database.
When the IF (condition) part of the rule matches a fact, the rule is fired and its THEN (action) part is executed.
The matching of the rule IF parts to the facts produces inference chains. The inference chain indicates how an expert system applies the rules to reach a conclusion.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Forward chaining Forward chaining is the data-driven reasoning. The
reasoning starts from the known data and proceeds forward with that data. Each time only the topmost rule is executed. When fired, the rule adds a new fact in the database. Any rule can be executed only once. The match-fire cycle stops when no further rules can be fired.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Forward chaining Forward chaining is a technique for gathering
information and then inferring from it whatever can be inferred.
However, in forward chaining, many rules may be executed that have nothing to do with the established goal.
Therefore, if our goal is to infer only one particular fact, the forward chaining inference technique would not be efficient.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Backward chaining Backward chaining is the goal-driven reasoning. In
backward chaining, an expert system has the goal (a hypothetical solution) and the inference engine attempts to find the evidence to prove it.
First, the knowledge base is searched to find rules that might have the desired solution. Such rules must have the goal in their THEN (action) parts. If such a rule is found and its IF (condition) part matches data in the database, then the rule is fired and the goal is proved. However, this is rarely the case.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Backward chaining Thus the inference engine puts aside the rule it is
working with (the rule is said to stack) and sets up a new goal, a subgoal, to prove the IF part of this rule.
Then the knowledge base is searched again for rules that can prove the subgoal.
The inference engine repeats the process of stacking the rules until no rules are found in the knowledge base to prove the current subgoal.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
How do we choose between forward and backward chaining?
If an expert first needs to gather some information and then tries to infer from it whatever can be inferred, choose the forward chaining inference engine.
However, if your expert begins with a hypothetical solution and then attempts to find facts to prove it, choose the backward chaining inference engine.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Pattern Matching used to match rules with appropriate facts in working
memory rules for which facts can be found are satisfied the combination of a rule with the facts that satisfy it
is used to form activation records one of the activation records is selected for execution
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Simplistic Pattern Matching
go through the list of rules, and check the antecedent (LHS) of each rule against the facts in working memory create an activation record for each rule with a
matching set of facts repeat after each rule firing
very inefficient roughly (number of rules) * (number of facts)(number of patterns)
the actual performance depends on the formulation of the rules and the contents of the working memory
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Algorithm in most cases, the set of rules in a rule-based system is
relatively constant the facts (contents of working memory) change frequently
most of the contents of working memory, however, don’t change every time
optimization of the matching algorithm remember previous results change only those matches that rely on facts that changed
the Rete algorithm performs an improved matching of rules and facts invented by Charles Forgy in the early 80s basis for many rule-based expert system shells
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Network the name comes from the latin word rete
stands for net consists of a network of interconnected nodes
each node represents one or more tests on the LHS of a rule input nodes are at the top, output nodes at the bottom
pattern nodes have one input, and check the names of facts join nodes have two inputs, and combine facts terminal node at the bottom of the network represent individual
rules a rule is satisfied if there is a combination of facts that passes
all the test nodes from the top to the output node at the bottom that represents the rule
the Rete network effectively is the working memory for a rule-based system
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Network Example 1
(deftemplate x (slot a))
(deftemplate y (slot b))
(defrule example-1
(x (a ?v1))
(y (b ?v1))
==> )
?= x ?= y
Left.0.a ?= Right.b
example-1
?v1 ?v1
?v1 = ?v1
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Left and Right Memories
left (alpha) memory contains the left input of a join node
right (beta) memory contains the right input of a join
node notation:
Left.p.q ?= Right.r compare the contents of slot q in
fact p from the left memory with slot r in the fact from the right memory
(deftemplate x (slot a))(deftemplate y (slot b))
(defrule example-1(x (a ?v1))(y (b ?v1))==> )
?= x ?= y
Left.0.a ?= Right.b
example-1
?v1 ?v1
?v1 = ?v1
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Running the Network
only facts x and y are considered all facts where x.a == y.b pass the
join network all {x, y} tuples are forwarded to
the next node compare the contents of slot q in
fact p from the left memory with slot r in the fact from the right memory
(deftemplate x (slot a))(deftemplate y (slot b))
(defrule example-1(x (a ?v1))(y (b ?v1))==> )
?= x ?= y
Left.0.a ?= Right.b
example-1
?v1 ?v1
?v1 = ?v1
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Network Example 2
shares some facts with Example 1(deftemplate x (slot a))
(deftemplate y (slot b))
(deftemplate z (slot c))
(defrule example-2
(x (a ?v2))
(y (b ?v2))
(z)
==> )
?= x ?= y ?= z
Left.0.a ?= Right.b
example-2
?v2 ?v2
?v2 = ?v2
?v2
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Network Example 2 with Assert
additional fact asserted
(deftemplate x (slot a))
(deftemplate y (slot b))
(deftemplate z (slot c))
(defrule example-2
(x (a ?v2))
(y (b ?v2))
(z)
==> )
(assert (z (c 17))
?= x ?= y ?= z
Left.0.a ?= Right.b
example-2
?v2 ?v2
?v2 = ?v2
?v2
?v2 = 17
17
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Assert and Retract with Rete
asserting additional facts imposes some more constraints on the network retracting facts indicates that some previously computed activation records
are not valid anymore, and should be discarded in addition to the actual facts, tags are sent through the networks
ADD to add facts (i.e. for assert) REMOVE to remove facts (i.e. for retract) CLEAR to flush the network memories (i.e. for reset) UPDATE to populate the join nodes of newly added rules
already existing join nodes can neglect these tokens
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rete Network Optimization
networks with shared facts can be combined
(deftemplate x (slot a))(deftemplate y (slot b))(deftemplate z (slot c))
(defrule example-1(x (a ?v1))(y (b ?v1))==> )
(defrule example-2(x (a ?v2))(y (b ?v2))(z)==> )
?= x ?= y ?= z
Left.0.a ?= Right.b
example-1 example-2
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Further Optimizations sophisticated data structures to optimize the network
hash table to presort the tokens before running the join node tests
fine-tuning via parameters frequently trade-off between memory usage and time
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Special Cases for Pattern Matching
additional enhancements of the Rete network can be used to implement specific methods backward chaining
requires a signal indicating to the network that a particular fact is needed
not conditional element indicates the absence of a fact requires special join nodes and special fields in the
tokens passing through the network test conditional element
uses a special join node that ignores its right input the result of the function is passed on
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Rule Formulation Pattern Order General vs. Specific Rules Simple vs. Complex Rules Loading and Saving Facts
[Giarratano & Riley 1998]
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Pattern Order since Rete saves information about rules and facts, it
can be critical to order patterns in the right way otherwise a potentially huge number of partial
matches can be generated
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Example Pattern Order(deffacts information
(find-match a c e g)f1
(item a) f2(item b) f3(item c) f4(item d) f5(item e) f6(item f) f7(item g)) f8
(defrule match-1(find-match ?x ?y ?z ?w) P1(item ?x) P2(item ?y) P3(item ?z) P4(item ?w) P5==>(assert (found-match ?x ?y ?z ?w))
(deffacts information(find-match a c e g)(item a)(item b)(item c)(item d)(item e)(item f)(item g))
(defrule match-1(item ?x)(item ?y)(item ?z)(item ?w)(find-match ?x ?y ?z ?w)==>(assert (found-match ?x ?y ?z ?w))
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Pattern Matches full matchesP1: f1P2: f2,f3,f4,f5,f6,f7,f8P3: f2,f3,f4,f5,f6,f7,f8P4: f2,f3,f4,f5,f6,f7,f8P5: f2,f3,f4,f5,f6,f7,f8 partial matchesP1: [f1]P1-2: [f1,f2]P1-3: [f1,f2,f4]P1-4: [f1,f2,f4,f6]P1-5: [f1,f2,f4,f6,f8]
Total: 29 full, 5 partial matches
full matchesP1: f1
P2: f2,f3,f4,f5,f6,f7,f8
P3: f2,f3,f4,f5,f6,f7,f8
P4: f2,f3,f4,f5,f6,f7,f8
P5: f2,f3,f4,f5,f6,f7,f8
partial matchesP1: [f2,f3,f4,f5,f6,f7,f8]
P1-2: [f2,f2],[f2,f3],[f2,f4],[f2,f5],
[f2,f6],[f2,f7],[f2,f8],
[f3,f2],[f3,f3],[f3,f4],[f3,f5],
[f3,f6],[f3,f7],[f3,f8],
...
P1-3, P1-4: ...
P1-5: [f2,f4,f6,f8, f1]
Total: 29 full, 2801 partial matches
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Guidelines for Pattern Ordering
most specific patterns first smallest number of matching facts largest number of variable bindings to constrain other
facts patterns matching volatile facts go last
facts that are changing frequently should be used by patterns late in the LHS
smallest number of changes in partial matches may cause a dilemma with the above guideline
patterns matching the fewest facts first reduces the number of partial matches
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Multifield Variables multifield wildcards and multifield variables are very
powerful, but possible very inefficient should only be used when needed limit their number in a single slot of a pattern
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Test Conditional Element the test conditional element should be placed as
close to the top of the rule as possible reduces the number of partial matches evaluation of expressions during pattern matching is
usually more efficient
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Built-In Pattern Matching Constraints
the built-in constraints are always more efficient than the equivalent expression
not so good:(defrule primary-color
color ?x&: (or (eq ?x red)(eq ?x green)(eq ?x blue)
==>(assert (primary-color ?x)))
better:(defrule primary-color
color ?x&red|green|blue)
==>(assert (primary-color ?x)))
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
General vs. Specific Rules some knowledge can be expressed through many
specific, or a few general rules specific rules generate a top-heavy Rete network with
many pattern nodes and fewer join nodes general rules offer better opportunities for sharing
pattern and join nodes it usually is easier to write an inefficient general rule
than an inefficient specific rule
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Simple vs. Complex Rules simple rules are sometimes elegant, but not
necessarily efficient storing temporary facts can be very helpful
especially in recursive or repetitive programs
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Loading and Saving Facts
facts can be kept in a file, and loaded into memory when needed (load-facts) and (save-facts) functions may lead to visibility or scoping problems if the
respective deftemplates are not contained in the current module
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Conflict resolution Earlier we considered two simple rules for crossing a
road. Let us now add third rule: Rule 1:
IF the ‘traffic light’ is green
THEN the action is go Rule 2:
IF the ‘traffic light’ is red
THEN the action is stop Rule 3:
IF the ‘traffic light’ is red
THEN the action is go
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Conflict resolution We have two rules, Rule 2 and Rule 3, with the same IF part.
Thus both of them can be set to fire when the condition part is satisfied. These rules represent a conflict set. The inference engine must determine which rule to fire from such a set. A method for choosing a rule to fire when more than one rule can be fired in a given cycle is called conflict resolution.
In forward chaining, BOTH rules would be fired. Rule 2 is fired first as the topmost one, and as a result, its THEN part is executed and linguistic object action obtains value stop. However, Rule 3 is also fired because the condition part of this rule matches the fact ‘traffic light’ is red, which is still in the database. As a consequence, object action takes new value go.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Methods used for conflict resolution
Fire the rule with the highest priority. In simple applications, the priority can be established by placing the rules in an appropriate order in the knowledge base. Usually this strategy works well for expert systems with around 100 rules.
Fire the most specific rule. This method is also known as the longest matching strategy. It is based on the assumption that a specific rule processes more information than a general one.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Methods used for conflict resolution
Fire the rule that uses the data most recently entered in the database. This method relies on time tags attached to each fact in the database. In the conflict set, the expert system first fires the rule whose antecedent uses the data most recently added to the database.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Methods used for conflict resolution
First in first serve It involves firing the first rule that matches the
content of the working memory or the facts asserted.
Last in first serve The rule applied will be the last rule that is
matched.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Conflict Resolution (example)First we'll look at a very simple set of rules: 1. IF (lecturing X)
AND (marking- practicals X) THEN ADD (overworked X)
2. IF (month February) THEN ADD (lecturing Alison)
3. IF (month February) THEN ADD (marking- practicals Alison)
4. IF (overworked X) OR (slept-badly X) THEN ADD (bad-mood X)
5. IF (bad-mood X) THEN DELETE (happy X)
6. IF (lecturing X) THEN DELETE (researching X)
7. IF (marking – praticals X) THEN ADD(Needsrest X)
Here we use capital letters to indicate variables
(month February)
(researching Alison)(overworked Alison)
First-serve apply Rule 2 Last in first serve apply rule 3
(month February) (researching Alison)(overworked Alison) (marking- practicals Alison)
Recency Apply Rule that match most recent factRule # 7
Fired Rules – don’t fire the same rule again
Specificity: If we had two rules but one of them matched more facts than we’’ chose that rule
Prioritization If we add priority to these rules then the higher priority rule will be fired
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Metaknowledge In rule-based expert systems, metaknowledge is
represented by metarules. A metarule determines a strategy for the use of task-
specific rules in the expert system.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Metarules Metarule 1:
Rules supplied by experts have higher priorities than rules supplied by novices.
Metarule 2: Rules governing the rescue of human lives have
higher priorities than rules concerned with clearing overloads on power system equipment.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Advantages of rule-based expert systems
Natural knowledge representation. An expert usually explains the problem-solving
procedure with such expressions as this: “In such-and-such situation, I do so-and-so”. These expressions can be represented quite naturally as IF-THEN production rules.
Uniform structure. Production rules have the uniform IF-THEN
structure. Each rule is an independent piece of knowledge. The very syntax of production rules enables them to be self documented.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Advantages of rule-based expert systems
Separation of knowledge from its processing. The structure of a rule-based expert system provides
an effective separation of the knowledge base from the inference engine. This makes it possible to develop different applications using the same expert system shell.
Dealing with incomplete and uncertain knowledge. Most rule-based expert systems are capable of
representing and reasoning with incomplete and uncertain knowledge.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Disadvantages of rule-based expert systems
Opaque relations between rules. Although the individual production rules are relatively
simple and self-documented, their logical interactions within the large set of rules may be opaque. Rule-based systems make it difficult to observe how individual rules serve the overall strategy.
Ineffective search strategy. The inference engine applies an exhaustive search
through all the production rules during each cycle. Expert systems with a large set of rules (over 100 rules) can be slow, and thus large rule-based systems can be unsuitable for real-time applications.
دانش مهندسي و خبره -سيستمهاي
كاهاني دكتر
Disadvantages of rule-based expert systems
Inability to learn. In general, rule-based expert systems do not have an
ability to learn from the experience. Unlike a human expert, who knows when to “break the rules”, an expert system cannot automatically modify its knowledge base, or adjust existing rules or add new ones. The knowledge engineer is still responsible for revising and maintaining the system.