240 likes | 280 Views
Learn about forward and backward chaining, conflict resolution, and matching facts to rules in rule-based systems. Examples and implementations provided.
E N D
Inference with Production Rules • The way in which the knowledge base is used is determined by the inference engine • It is a basic principle of production systems that each rule should be an independent item of knowledge and essentially ignorant of other rules • The inference engine could then simply "fire" rules at any time when its premises are satisfied.
Inference with Production Rules • Forward and Backward chaining through the rules may be used • The two systems each have their advantages and disadvantages and in fact answer different types of question • For example in Mycin • a forward chaining system might answer the question "what do these symptoms suggest?" • a backward chaining system might answer the question "does this patient suffer from amnesia?"
Inference with Production Rules • Before looking at Forward and Backward chaining in more detail we need to clarify some things • There are two main issues involved in the implementation of a rule-based system • How is conflict resolution implemented? • Various strategies • How are facts matched to rules? • Pattern matching required • And many more issues need to be resolved
Matching facts to rules • First of all we need to define a syntax for the rules and the facts • In the following slides we will discuss pattern matching assuming a forward chaining system is used
Rule-based Representation Example • R0: • IF mortgage is due AND checking account has enough money to pay mortgage THEN pay mortgage AND reduce checking balance by amount of mortgage
Rule-based Representation Example • The previous rule uses natural language • In reality we must restrict the rule-base representation to a limited machine-processable representation • There is no standard rule syntax • For example: • We can think of the LHS of a rule as being a list that contains the name of an object followed by pairs of attributes and values associated with that object
Rule-based Representation Example • R0: • IF (bill name mortgage status due amount 500) AND (account name checking balance 500) THEN (assert (pay item mortgage)) AND (remove (bill name mortgage status due amount 500)) AND (remove (account name checking balance 500)) AND (assert (account name checking balance 0))
Rule-based Representation Example • Assume the working memory has the following contents • WM: • (bill name mortgage status due amount 500 account name checking balance 500) • The rule will fire
Rule-based Representation Example • bill is an object • name, status, amount are attributes followed by their values • remove and assert are primitives used to remove and add facts to working memory • Problem: • A rule would have to be created for each amount and each type of bill • To solve it we can introduce variables and operations
Rule-based Representation Example • R0: • IF (bill name <BILL> status due amount <AMOUNT>) AND (account name checking balance <BALANCE> (<BALANCE> ≥ <AMOUNT>)) THEN (assert (pay item <BILL>)) AND (remove (bill name <BILL> status due amount <AMOUNT>)) AND (remove (account name checking balance <AMOUNT>)) AND (assert (account name checking balance (<BALANCE> - <AMOUNT>)))
Rule-based Representation Example • Assume the working memory has the following contents • WM: • (bill name electric status due amount 100 account name checking balance 400) • The rule will fire • The fact balance 300 will be added to the working memory
Rule-based Representation Example (disjunctions) • R0: • IF (bill name <BILL> status [due over_due] amount <AMOUNT>) AND (account name checking balance <BALANCE> (<BALANCE> ≥ <AMOUNT>)) THEN (assert (pay item <BILL>)) AND (remove (bill name <BILL> status due amount <AMOUNT>)) AND (remove (account name checking balance <AMOUNT>)) AND (assert (account name checking balance (<BALANCE> - <AMOUNT>)))
Rule-based Representation Example (disjunctions) • Assume the working memory has the following contents • (bill name mortgage status due amount 700) (bill name electric status over_due amount 200) (bill name water status not_due amount 50) (account name checking balance 900) • How many times will the rule fire?
Matching facts to rules • Once a rule syntax has been defined we must specify how the matching of facts to conditions of rules will be performed • I.e. trying to determine if the LHS of a rule is satisfied by the facts in working memory • It has been estimated that 90% of a rule-based system’s run time is spent on performing repetitive pattern matching between rules and facts in the working memory
The Rete Matching Algorithm • What is a solution to this pattern matching problem? • Try exhaustively to match rules to facts one by one • Use indexing techniques • The Rete algorithm was the first efficient solution to the facts-rules pattern matching problem • It stores information about matches in a network structure
The Rete Matching Algorithm • Nodes of the network correspond to individual condition elements • Conditions and conjunctions of conditions • Each node has two sets associated with it • The first set contains all the working memory elements that the condition node matches • The second set contains combinations of working memory elements and the bindings which produce a consistent match of the conditions that chain up to the node condition
The Rete Matching Algorithm • With this configuration repetitive testing of all rule conditions in each cycle is avoided • Only the nodes affected by a newly inserted or modified fact are checked • For example, consider the rules • IF a(X,1) and b(X,Z) THEN g1(X,Z) IF a(X,2) and b(X,Z) THEN g2(X,Z)
The Rete Matching Algorithm Initially the working memory is empty start b(X,Z) a(X,Y) - There is a starting node and a node for each of the rule conditions and conjunctions of conditions. - Arcs are labeled with variable bindings Y=1 Y=2 a(X,2),b(X,Z) a(X,1),b(X,Z)
The Rete Matching Algorithm Fact a(3,1) is added to the working memory start a(3,1) b(X,Z) a(X,Y) a(3,1) is deposited in the node labeled a(X,Y) and will propagate through the arc labeled Y=1 Y=1 Y=2 a(3,1) a(X,2),b(X,Z) a(X,1),b(X,Z) Rule doesn’t match
The Rete Matching Algorithm Fact b(3,4) is added to the working memory start a(3,1) a(X,Y) b(X,Z) b(3,4) b(3,4) is deposited in the node labeled b(Y,Z) and will propagate through the arcs labeled Y=1 and Y=2 Y=1 Y=2 a(3,1),b(3,4) b(3,4) a(X,2),b(X,Z) a(X,1),b(X,Z) Rule matches Rule doesn’t match
The Rete Matching Algorithm Fact a(3,2) is added to the working memory start a(3,1),a(3,2) a(X,Y) b(X,Z) b(3,4) a(3,2) is deposited in the node labeled a(X,Y) and will propagate through the arc labeled Y=2 Y=1 Y=2 a(3,1),b(3,4) a(3,2),b(3,4) a(X,2),b(X,Z) a(X,1),b(X,Z) Rule matches Rule matches
The Rete Matching Algorithm • The Rete algorithm (and extensions) are widely used in rule-based systems • It allows for an efficient matching process (on average) • A naïve algorithm that tries all combinations of rules and facts has exponential complexity
Inference with Production Rules • There are two main issues involved in the implementation of a rule-based system • How is conflict resolution implemented? • Various strategies • How are facts matched to rules? • Pattern matching required • And many more issues need to be resolved
Inference with Production Rules • There are many issues to be decided when implementing the inference mechanism • In what order do we check rules? • In what order do we check facts? • Algorithms may produce facts that are irrelevant to goals. How do we avoid producing such facts? • Backward chaining does not suffer from this problem