Download presentation
Presentation is loading. Please wait.
1
Rule Based Expert System دكترمحسن كاهاني http://www.um.ac.ir/~kahani/
2
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
3
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
4
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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 THEN
5
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Rules (cont.) A rule can have multiple antecedents joined by the keywords AND (conjunction), OR (disjunction) or a combination of both. IF IF AND OR THEN THEN
6
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.)
7
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Relation IF 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
8
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Directive IF 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.)
9
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Heuristic IF 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.)
10
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
11
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني The main players in the development team
12
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
13
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Production system model
14
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Basic structure of a rule- based expert system
15
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
16
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
17
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Complete structure of a rule-based expert system
18
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
19
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
20
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
21
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
22
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Comparison
23
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Comparison (cont.)
24
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
25
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Inference engine cycles via a match-fire procedure
26
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني An example of an inference chain
27
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
28
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Forward chaining
29
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
30
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
31
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
32
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Backward chaining
33
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
34
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
35
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
36
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Rete Algorithm uin most cases, the set of rules in a rule-based system is relatively constant uthe facts (contents of working memory) change frequently umost of the contents of working memory, however, don’t change every time uoptimization of the matching algorithm uremember previous results uchange only those matches that rely on facts that changed uthe Rete algorithm performs an improved matching of rules and facts uinvented by Charles Forgy in the early 80s ubasis for many rule-based expert system shells
37
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Rete Network uthe name comes from the latin word rete ustands for net uconsists of a network of interconnected nodes ueach node represents one or more tests on the LHS of a rule uinput nodes are at the top, output nodes at the bottom upattern nodes have one input, and check the names of facts ujoin nodes have two inputs, and combine facts uterminal node at the bottom of the network represent individual rules ua 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 uthe Rete network effectively is the working memory for a rule-based system
38
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
39
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
40
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
41
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
42
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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 = 17 17
43
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
44
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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-1example-2
45
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
46
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
47
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Rule Formulation uPattern Order uGeneral vs. Specific Rules uSimple vs. Complex Rules uLoading and Saving Facts [Giarratano & Riley 1998]
48
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
49
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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))
50
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني Pattern Matches full matches P1: 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 matches P1: [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 matches P1: 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 matches P1: [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
51
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
52
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
53
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
54
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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)))
55
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
56
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
57
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
58
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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
59
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
60
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
61
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
62
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
63
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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 fact Rule # 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
64
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
65
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
66
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
67
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
68
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
69
سيستمهاي خبره و مهندسي دانش - دكتر كاهاني 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.