Rule Based Expert System http www um ac
Rule Based Expert System ﺩﻛﺘﺮﻣﺤﺴﻦ ﻛﺎﻫﺎﻧﻲ http: //www. um. ac. ir/~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 THEN the ‘traffic light’ is green the action is go IF THEN the ‘traffic light’ is red 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 AND THEN <antecedent 1> <antecedent 2> <antecedent n> <consequent> IF <antecedent 1> OR <antecedent 2> OR <antecedent n> THEN <consequent> ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Rules (cont. ) § 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 AND THEN ‘age of the customer’ < 18 ‘cash withdrawal’ > 1000 ﺩﻛﺘﺮ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ ‘signature of the parent’ is ﻛﺎﻫﺎﻧﻲ required
Rule Types § Relation IF THEN § the ‘fuel tank’ is empty the car is dead Recommendation IF AND THEN the season is autumn the sky is cloudy the forecast is drizzle the advice is ‘take an umbrella’ ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Rule Types(cont. ) § Directive IF AND THEN § the car is dead the ‘fuel tank’ is empty the action is ‘refuel the car’ Strategy IF the car is dead THEN the action is ‘check the fuel tank’; step 1 is complete IF step 1 is complete AND the ‘fuel tank’ is full THEN the action is ‘check the battery’; ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ step 2 is complete
Rule Types (cont. ) § Heuristic IF AND THEN the spill is liquid the ‘spill p. H’ < 6 the ‘spill smell’ is vinegar the ‘spill material’ is ‘acetic acid’ ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
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 u u in most cases, the set of rules in a rule-based system is relatively constant the facts (contents of working memory) change frequently u u most of the contents of working memory, however, don’t change every time optimization of the matching algorithm remember previous results u change only those matches that rely on facts that changed u u the Rete algorithm performs an improved matching of rules and facts invented by Charles Forgy in the early 80 s u basis for many rule-based expert system shells u ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Rete Network u the name comes from the latin word rete u u stands for net consists of a network of interconnected nodes u each node represents one or more tests on the LHS of a rule u input nodes are at the top, output nodes at the bottom pattern nodes have one input, and check the names of facts u join nodes have two inputs, and combine facts u terminal node at the bottom of the network represent individual rules u u u 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 rulebased system ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Rete Network Example 1 (deftemplate x (slot a)) (deftemplate y (slot b)) ? = x ? = ? v 1 (defrule example-1 (x (a ? v 1)) (y (b ? v 1)) ==> ) y ? v 1 Left. 0. a ? = Right. b ? v 1 = ? v 1 example-1 ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
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 ? = x ? = ? v 1 y ? v 1 Left. 0. a ? = Right. b ? v 1 = ? v 1 (deftemplate x (slot a)) (deftemplate y (slot b)) (defrule example-1 (x (a ? v 1)) (y (b ? v 1)) ==> ) example-1 ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
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 ? = x ? = ? v 1 y ? v 1 Left. 0. a ? = Right. b ? v 1 = ? v 1 (deftemplate x (slot a)) (deftemplate y (slot b)) (defrule example-1 (x (a ? v 1)) (y (b ? v 1)) ==> ) example-1 ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
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 ? v 2)) (y (b ? v 2)) (z) ==> ) ? = x ? = ? v 2 y ? = z ? v 2 Left. 0. a ? = Right. b ? v 2 = ? v 2 example-2 ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
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 ? v 2)) (y (b ? v 2)) (z) ==> ) x ? = ? v 2 y ? = ? v 2 z 17 Left. 0. a ? = Right. b ? v 2 = ? v 2 = 17 example-2 (assert (z (c 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 ? = x ? = y ? = z (deftemplate x (slot a)) (deftemplate y (slot b)) (deftemplate z (slot c)) Left. 0. a ? = Right. b (defrule example-1 (x (a ? v 1)) (y (b ? v 1)) ==> ) (defrule example-2 (x (a ? v 2)) (y (b ? v 2)) (z) ==> ) 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 § § not conditional element § § § requires a signal indicating to the network that a particular fact is needed 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 u General vs. Specific Rules u Simple vs. Complex Rules u Loading and Saving Facts u ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ [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) f 1 (item a) f 2 (item b) f 3 (item c) f 4 (item d) f 5 (item e) f 6 (item f) f 7 (item g)) f 8 (defrule match-1 (item ? x) (find-match ? x ? y ? z ? w) P 1 (item ? y) (item ? x) (item ? z) P 2 (item ? w) (item ? y) (find-match ? x ? y ? z ? w) P 3 ==> (item ? z) P 4 (assert (found-match ? x ? y ? z ? w)) (item ? w) P 5 ==> ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ (assert (found-match ? x ? y ? z ? w))
Pattern Matches § full matches P 1: f 1 P 2: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 3: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 4: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 5: f 2, f 3, f 4, f 5, f 6, f 7, f 8 § partial matches P 1: [f 1] P 1 -2: [f 1, f 2] P 1 -3: [f 1, f 2, f 4] P 1 -4: [f 1, f 2, f 4, f 6] P 1 -5: [f 1, f 2, f 4, f 6, f 8] Total: 29 full, 5 partial matches § full matches P 1: f 1 P 2: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 3: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 4: f 2, f 3, f 4, f 5, f 6, f 7, f 8 P 5: f 2, f 3, f 4, f 5, f 6, f 7, f 8 § partial matches P 1: P 1 -2: [f 2, f 3, f 4, f 5, f 6, f 7, f 8] [f 2, f 2], [f 2, f 3], [f 2, f 4], [f 2, f 5], [f 2, f 6], [f 2, f 7], [f 2, f 8], [f 3, f 2], [f 3, f 3], [f 3, f 4], [f 3, f 5], [f 3, f 6], [f 3, f 7], [f 3, f 8], . . . P 1 -3, P 1 -4: . . . P 1 -5: [f 2, f 4, f 6, f 8, f 1] Total: 29 full, 2801 partial matches ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Guidelines for Pattern Ordering § most specific patterns first § § § patterns matching volatile facts go last § § smallest number of matching facts largest number of variable bindings to constrain other 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 ? x&: (or (eq ? x red) (eq ? x green) (eq ? x blue) ==> (assert (primary-color ? x))) § better: (defrule primary-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 THEN § Rule 2: IF THEN § the ‘traffic light’ is green the action is go the ‘traffic light’ is red the action is stop Rule 3: IF THEN the ‘traffic light’ is red 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) (month February) (researching Alison) (overworked Alison) § § § Here we use capital letters to indicate variables § 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 ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
Metaknowledge § § In rule-based expert systems, metaknowledge is represented by metarules. A metarule determines a strategy for the use of taskspecific 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-andsuch 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. Rulebased 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. ﺩﻛﺘﺮ ﻛﺎﻫﺎﻧﻲ - ﺳﻴﺴﺘﻤﻬﺎﻱ ﺧﺒﺮﻩ ﻭ ﻣﻬﻨﺪﺳﻲ ﺩﺍﻧﺶ
- Slides: 69