550 likes | 575 Views
Inference in Propositional Logic. Inference is the process of building a proof of a sentence, that is an implementation of the entailment relation between sentences. Inference is carried out by inference rules, which allow one formula to be
E N D
Inference in Propositional Logic Inference is the process of building a proof of a sentence, that is an implementation of the entailment relation between sentences. Inference is carried out by inference rules, which allow one formula to be inferred from a set of other formulas. For example, A |-- B meaning that B can be derived from A. An inference procedure is sound iff its inference rules are sound. An inference rule is sound iff its conclusion is true whenever the rule's premises are true.
PL inference rules • Modus ponens: if sentence A and implication A => B hold, then B also holds, i.e. (A, A => B) |-- B. Example: Let A means “lights are off”, A => B means “if lights are off, then there is no one in the office” B means “there is no one in the office” • AND-elimination: if conjunction A1 & A2 & ... & An holds, then any of its conjuncts also holds, i.e. A1 & A2 & ... & An |-- Ai. • AND-introduction: if a list of sentences holds, then their conjunction also holds, i.e. A1, A2,...,An |-- (A1 & A2 & ... & An). • OR-introduction: If Ai holds, then any disjunction containing Ai also holds, i.e. Ai |-- (A1 v ... v Ai v ... v An). • Double-negation elimination: states that a formula can be either true or false, i.e. (not (not A)) |-- A
PL inference rules (cont.) • Unit resolution: (A v B), not B |-- A. Note that (A v B) is equivalent to (not B => A), i.e. unit resolution is a modification of modus ponens. • Resolution: (A v B), (not B v C) |-- (A v C). Note that (A v B) is equivalent to (not A => B), (not B v C) is equivalent to (B => C) By eliminating the intermediate conclusion, we get (not A => C). The soundness of each one of these rules can be checked by means of the truth table method. Once the soundness of a rule has been established, it can be used for building proofs. Proofs are sequences of applications of inference rules, starting with sentences initially contained in the KB.
Example (adapted from Dean,Allen & Aloimonos "Artificial Intelligence: theory and practice") Bob, Lisa, Jim and Mary got their dormitory rooms according to the results of a housing lottery. Determine how they were ranked with respect to each other, if the following is known: 1. Lisa is not next to Bob in the ranking. 2. Jim is ranked immediately ahead of a biology major. 3. Bob is ranked immediately ahead of Jim. 4. Either Lisa or Mary is a biology major. 5. Either Lisa or Mary is ranked first. We must start with deciding how to represent the problem in PL. Let the following two be the only types of propositions that we will use: X-ahead-of-Y meaning “X is immediately ahead of Y” X-bio-major meaning “X is a biology major”
Statements representing the initial KB: 1. (not Lisa-ahead-of-Bob ¬ Bob-ahead-of-Lisa) 2. ((Jim-ahead-of-Mary & Mary-bio-major) V (Jim-ahead-of-Bob & Bob-bio-major) V V (Jim-ahead-of-Lisa & Lisa-bio-major)) 3. Bob-ahead-of-Jim 4. Mary-bio-major V Lisa-bio-major 5. ((Mary-ahead-of-Bob & Bob-ahead-of-Lisa & Lisa-ahead-of-Jim) V V (Mary-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Lisa) V V (Mary-ahead-of-Lisa & Lisa-ahead-of-Bob & Bob-ahead-of-Jim) V V (Mary-ahead-of-Lisa & Lisa-ahead-of-Jim &Jim-ahead-of-Bob) V V (Mary-ahead-of-Jim & Jim-ahead-of-Lisa & Lisa-ahead-of-Bob) V V (Mary-ahead-of-Jim &Jim-ahead-of-Bob& Bob-ahead-of-Lisa) V V (Lisa-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Mary) V V (Lisa-ahead-of-Bob & Bob-ahead-of-Mary & Mary-ahead-of-Jim) V V (Lisa-ahead-of-Mary & Mary-ahead-of-Jim &Jim-ahead-of-Bob) V V (Lisa-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim) V V (Lisa-ahead-of-Jim &Jim-ahead-of-Bob& Bob-ahead-of-Mary) V V (Lisa-ahead-of-Jim & Jim-ahead-of-Mary & Mary-ahead-of-Bob))
Example (contd.) In addition to the explicit knowledge encoded by the five sentences above, the following background (or common-sense) knowledge is needed to solve the problem: BGRule1: A student can be immediately ahead of at most one other student. Cons1: Bob-ahead-of-Jim ==> not Bob-ahead-of-Lisa Cons2: Bob-ahead-of-Jim ==> not Lisa-ahead-of-Jim BGRule2: If X is immediately ahead of Y, then Y cannot be immediately ahead of X. Cons3: Bob-ahead-of-Jim ==> not Jim-ahead-of-Bob
Example (contd.) By sentence 3, Cons3 and MP, we can infer 6. not Jim-ahead-of-Bob By sentences 5 and 6, and the Unit Resolution rule, we can infer 7. ((Mary-ahead-of-Bob & Bob-ahead-of-Lisa &Lisa-ahead-of-Jim) V V (Mary-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Lisa) V V (Mary-ahead-of-Lisa & Lisa-ahead-of-Bob & Bob-ahead-of-Jim) V V (Mary-ahead-of-Jim & Jim-ahead-of-Lisa & Lisa-ahead-of-Bob) V V (Lisa-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Mary) V V (Lisa-ahead-of-Bob & Bob-ahead-of-Mary & Mary-ahead-of-Jim) V V (Lisa-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim) V V (Lisa-ahead-of-Jim& Jim-ahead-of-Mary & Mary-ahead-of-Bob))
Example (contd.) By sentence 3, Cons2 of BGRule1, and MP, we can infer 8. not Lisa-ahead-of-Jim By sentences 7 and 8, and the Unit Resolution rule (in several steps) , we can infer 9. ((Mary-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Lisa) V V (Mary-ahead-of-Lisa &Lisa-ahead-of-Bob& Bob-ahead-of-Jim) V V (Mary-ahead-of-Jim & Jim-ahead-of-Lisa &Lisa-ahead-of-Bob) V V (Lisa-ahead-of-Bob& Bob-ahead-of-Jim & Jim-ahead-of-Mary) V V (Lisa-ahead-of-Bob& Bob-ahead-of-Mary & Mary-ahead-of-Jim) V V (Lisa-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim)) (those will go next because of sentence 1 and AND-elimination rule – see next slide resulting in sentence 12)
Example (contd.) By sentences 6 and 2, and the Unit resolution rule, we can infer 10. (Jim-ahead-of-Mary & Mary-bio-major) V (Jim-ahead-of-Lisa & Lisa-bio-major) By sentence 1 and the AND Elimination rule, we can infer 11. not Lisa-ahead-of-Bob By sentences 9 and 11, and the Unit Resolution rule, we can infer 12. (Mary-ahead-of-Bob & Bob-ahead-of-Jim & Jim-ahead-of-Lisa) V V (Lisa-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim) Notice that the second disjunct in 12 contradicts sentence 10 (Jim is ranked immediately ahead of a biology major, therefore cannot be the last one in the ranking). To arrive at the correct conclusion, we must eliminate this disjunct from sentence 12.
Example (contd.) Let us apply the following transformation on sentence 10. (Jim-ahead-of-Mary & Mary-bio-major) V (Jim-ahead-of-Lisa & Lisa-bio-major) (A & B) v (C & D) (A v C) & (A v D) & (B v C) & (B v D) (the validity of this equivalence can be proved by the truth tables method) By the AND-elimination rule, we can infer (A v C), i.e. 13. Jim-ahead-of-Mary V Jim-ahead-of-Lisa Unfortunately, it is not possible to logically prove that 13 contradicts the second disjunct in sentence 12. The only way to complete the proof is to change the representation so that it suits our purposes. For that, we can add 2 more propositions in our language: Nobody-ahead-of-X X-ahead-of-Nobody Now we can derive the following consequent from BGRule1: Cons4: Jim-ahead-of-Mary V Jim-ahead-of-Lisa not Jim-ahead-of-Nobody
Example (contd.) 12 now becomes: 12. (Nobody-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim & & Jim-ahead-of-Lisa & Lisa-ahead-of-Nobody) V V (Nobody-ahead-of-Lisa & Lisa-ahead-of-Mary & Mary-ahead-of-Bob & & Bob-ahead-of-Jim &Jim-ahead-of-Nobody) By 13, Cons4 and MP, we can infer 14. not Jim-ahead-of-Nobody (makes the second disjunct in 12 FALSE) By 12, 14 and the Unit Resolution Rule, we now can infer 15. Nobody-ahead-of-Mary & Mary-ahead-of-Bob & Bob-ahead-of-Jim & & Jim-ahead-of-Lisa & Lisa-ahead-of-Nobody
Complexity of propositional inference • Propositional inference is complete, but NP-complete in the worst case. 2. Inference rules in PL are monotonic. That is, if KB1 |-- A, then KB1 U KB2 |-- A But, if KB2 is not consistent with KB1, then the monotonicity of PL rules cause a major representation problem. • Inference rules in PL are local, i.e. they depend only on their premises (this is a consequent of the monotonicity property). This, in turn, makes the inference procedure much better than exponential, because only a small number of propositions are involved in each inference.
Building knowledge bases (just the beginning) The fundamental problem of understanding intelligence is not the identification of a few powerful techniques, but rather the question of how to represent large amounts of knowledge in a fashion that permits their effective use and interaction. To do this, we must first say how to acquire this knowledge. Think knowledge = codified experience. This way we can view knowledge as a transportable substance, manipulated by a process called knowledge acquisition (KA). KA consists of two interrelated activities: • Eliciting knowledge from knowledge source (domain experts, data bases, textbooks, etc.). This activity is called knowledge engineering. • Representation of the elicited knowledge in selected representation language. This activity is called ontological engineering.
An overview of the KA process Reformulation Reformulation Redesign Refinement Identifica- tion of participants, problem cha- racteristics and goals Conceptualiza- tion: find key concepts and relations already mentioned during the identification stage Formalization: mapping key concepts into more formal representation Implementation formulate rules to embody knowledge Testing: validate rules representing knowledge
Knowledge base Inference engine Knowledge base Inference engine Modes of knowledge acquisition and building ontologies • “Domain expert(s) -- Knowledge engineer” mode Domain expert(s) Knowledge Engineer • “Domain expert -- Intelligent Editing Program” mode Domain expert Intelligent Editing Program
Knowledge base Inference engine Knowledge base Inference engine Modes of knowledge acquisition (cont.) • “Data mining” mode Data bases Induction Program • “Text Understanding” mode Textbooks Text Understanding Program
Building ontologies in PL: shallow knowledge Knowledge can be expressed as heuristic rules, which map data abstractions (such as symptoms in diagnostic domains) to solution abstractions (such as diagnoses). In many domains, this very simple representation can be perfectly good. Example. This is one of MYCIN top-level goal rules: If there is an organism which requires therapy, and consideration has been given to any other organisms requiring therapy Then compile a list of possible therapies, and determine the best one on the list This knowledge is referred to as shallow knowledge and it reflects empirical associations derived from experience.
Acquiring and representing shallow knowledge Consider the following example (adapted from Gonzalez and Dankel “The Engineering of KBS”). We want to build a Knowledge-Based System (Expert System, or Agent, or …) to advise a not mechanically inclined motorist about his car’s cooling system malfunction. Assume that the KE serves also as a domain expert. The first step (identification step) is to compile a list of all possible problems with the cooling system. Let the following be the (small set of) outputs (goals) expected in this domain: • radiator leaks • broken fan belt • defective water pump • broken water hose • frozen coolant
Example (cont.) Next (conceptualization step), the KE must identify key concepts and relations to discover these problems. Part of that are possible inputs such as: • temperature indicator on the dashboard • weather conditions • spots of coolant underneath engine compartment • steam coming out of the hood (the presence of a hissing sound) Step 3, formalization, includes determining the relationships between the inputs and the outputs, which may require some intermediate derivations. These relationships are translated into the following heuristic rules: Rule1: The presence of a “hot” reading on the dashboard implies that at least one problem exists. Rule 2: The absence of a “hot” reading on the dashboard does not necessarily imply absence of a problem. Rule 3: A large pool of coolant under the engine compartment can indicate radiator leaks, broken hoses, and/or a defective water pump. Rule 4: A relatively small pool of coolant under the engine compartment usually implies a defective water pump.
Example (cont.) Rule 5: Absence of a pool of coolant under the engine compartment, and a “hot” reading on the dashboard implies a broken fan belt. Rule 6: An ambient temperature below 10 degrees Fahrenheit implies that the coolant is frozen. Rule 7: The presence of a hissing sound accompanied by a small pool of coolant under the engine compartment indicates a radiator and/or hose leak. To represent this domain in PL (part of formalization step), we can use the following vocabulary: A: “hot” reading on the dashboard B: at least one problem with the cooling system exists C: there is a large pool of coolant under the engine compartment D: radiator leaks E: broken water hose F: defective water pump H: there is a relatively small pool of coolant under the engine compartment J: broken fan belt I: an ambient temperature is below 10 degrees Fahrenheit G: frozen coolant K: a hissing sound is present
Example (cont.) Rules 1 to 7 can now be represented as follows (implementation step): Rule1: A => B Rule 2: not B => not A Rule 3: C => D v E v F Rule 4: H => F Rule 5: not (C V H) & A => J Rule 6: I => G Rule 7: K & H => D v E Further refinement of this set of rules may be required to improve the performance adequacy of the KBS (in needed as part of the testing process).
Efficient reasoning in PL: forward and backward chaining Assume that we are building a KBS in a domain, where knowledge can be represented as heuristic rules of the form: P2 & … & Pn P1, or equivalent P1 V P2 V … V Pn (disjunctions of literals with at most one positive literal) Such formulas are called Horn formulas. Then: 1. Given the initial description of the problem, generate all possible conclusions that can be derived from the initial set of formulas by the Modus Ponens and AND-introduction rules ONLY. No other rule will be needed in this case, because of the limited representation language used. This type of reasoning from the premises to the conclusions is called forward chaining. 2. Given the goal, find implication sentences that would derive this goal. This type of reasoning from the goal to the premises is called backward chaining.
Forward chaining: an overview We start with a set of data collected through observations, and check each rule to see if the data satisfy its premises. This process is called rule interpretation; it is performed by the Inference Engine (IE) and involves the following steps: Rules New rules Applicable Selected rules rules Facts New facts Knowledge (rule memory) Step1: Match Step 3: Execution Step2: Conflict Resolution Facts (working memory)
Augmenting the rule memory with new rules: explanation-based learning To illustrate how one type of machine learning, known as explanation-based learning, can help an agent to augment its knowledge consider again the 5 puzzle problem. Let the following solution be found by the breadth-first search for the initial state (4 5 3 0 1 2): 19 ;; the length of the shortest path ((4 5 3 0 1 2) (0 5 3 4 1 2) (5 0 3 4 1 2) (5 1 3 4 0 2) (5 1 3 4 2 0) (5 1 0 4 2 3) (5 0 1 4 2 3) (0 5 1 4 2 3) (4 5 1 0 2 3) (4 5 1 2 0 3) (4 0 1 2 5 3) (4 1 0 2 5 3) (4 1 3 2 5 0) (4 1 3 2 0 5) (4 1 3 0 2 5) (0 1 3 4 2 5) (1 0 3 4 2 5) (1 2 3 4 0 5) (1 2 3 4 5 0)) Let us rewrite this solution in terms of moves of the empty tile (not in terms of states as we did it so far): up right down right up left left down right up right down left left up right down right
new operator new operator Search for expensive or repetitive components of the reasoning (search) process The two outlined components of the solution are exactly the same. Instead of repeating the work involved in the generation of intermediate states, we can create a new operator which, if applicable, will transform the starting state of the sequence into the finish state of the sequence. That is, 4 5 3 5 1 3 0 1 2 4 2 0 4 1 3 1 2 3 0 2 5 4 5 0
Here is the revised set of operators for the specified position of the empty tile: (defun move-4 (state) ;; defines all moves if the empty tile is in position 4 (setf new-states (cons (append (list (second state)) ;; the new operator (list (fifth state)) (list (third state)) ;; will be tried (list (first state)) (list (sixth state)) ;; first (list (fourth state))) new-states)) (setf new-states (cons (append (list (fourth state)) (list (second state)) (list (third state)) (list (first state)) (nthcdr 4 state)) new-states)) (setf new-states (cons (append (butlast state 3) (list (fifth state)) (list (fourth state)) (last state)) new-states))) The revised solution has a length of 13, and as seen below the new operator Was applied twice, at state 1 (4 5 3 0 1 2) and state 12 (4 1 3 0 2 5). Here is the new solution after learning: ((4 5 3 0 1 2) (5 1 3 4 2 0) (5 1 0 4 2 3) (5 0 1 4 2 3) (0 5 1 4 2 3) (4 5 1 0 2 3) (4 5 1 2 0 3) (4 0 1 2 5 3) (4 1 0 2 5 3) (4 1 3 2 5 0) (4 1 3 2 0 5) (4 1 3 0 2 5) (1 2 3 4 5 0))
Forward chaining: The fruit identification example (adapted from Gonzalez & Dankel) To illustrate chaining algorithms (no learning component is assumed), consider the following set of rules intended to recognize different fruits provided fruit descriptions. Rule 1: If (shape = long) and (color = green) Then (fruit = banana) Rule 1A: If (shape = long) and (color = yellow) Then (fruit = banana) Rule 2: If (shape = round) and (diameter > 4 inches) Then (fruitclass = vine) Rule 2A: If (shape = oblong) and (diameter > 4 inches) Then (fruitclass = vine) Rule 3: If (shape = round) and (diameter < 4 inches) Then (fruitclass = tree) Rule 4: If (seedcount = 1) Then (seedclass = stonefruit) Rule 5: If (seedcount > 1) Then (seedclass = multiple) Rule 6: If (fruitclass = vine) and (color = green) Then (fruit = watermelon)
The fruit identification example (cont.) Rule 7: If (fruitclass = vine) and (surface = smooth) and (color = yellow) Then (fruit = honeydew) Rule 8: If (fruitclass = vine) and (surface = rough) and (color = tan) Then (fruit = cantaloupe) Rule 9: If (fruitclass = tree) and (color = orange) and (seedclass = stonefruit) Then (fruit = apricot) Rule 10: If (fruitclass = tree) and (color = orange) and (seedclass = multiple) Then (fruit = orange) Rule 11: If (fruitclass = tree) and (color = red) and (seedclass = stonefruit) Then (fruit = cherry) Rule 12: If (fruitclass = tree) and (color = orange) and (seedclass = stonefruit) Then (fruit = peach) Rule 13: If (fruitclass = tree) and (color = red) and (seedclass = multiple) Then (fruit = apple) Rule 13A: If (fruitclass = tree) and (color = yellow) and (seedclass = multiple) Then (fruit = apple) Rule 13B: If (fruitclass = tree) and (color = green) and (seedclass = multiple) Then (fruit = apple)
The fruit identification example (cont.) Assume the following set of facts comprising the initial working memory: FB = ((diameter = 1 inch) (shape = round) (seedcount = 1) (color = red)) The forward reasoning process is carried out as follows: Cycle 1: Step1 (matching) Rules 3 and 4 are applicable Step2 (conflict resolution) Select rule 3 Step 3 (execution) FB (fruitclass = tree) Cycle 2: Step1 (matching) Rules 3 and 4 are applicable Step2 (conflict resolution) Select rule 4 Step 3 (execution) FB (seedclass = stonefruit)
The fruit identification example (cont.) Cycle 3: Step1 (matching) Rules 3, 4 and 11 are applicable Step2 (conflict resolution) Select rule 11 Step 3 (execution) FB (fruit = cherry) Cycle 4: Step1 (matching) Rules 3, 4 and 11 are applicable Step2 (conflict resolution) No new rule can be selected. Stop.
Forward chaining: general rule format Rules used to represent “diagnostic” or procedural knowledge have the following format: If <antecedent 1> is true, <antecedent 2> is true, … <antecedent i> is true Then <consequent> is true. The rule interpretation procedure utilizes the idea of renaming, which states that one sentence is a renaming of another if they are the same except for the names of the variables, called pattern variables. Examples: • likes(x, ice-cream) is a renaming of likes(y, ice-cream) • flies(bird1) is a renaming of flies(bird2), and both are renaming of flies(Tom). Here x, y, bird1 and bird2 are pattern variables.
Pattern matching To recognize pattern variables more easily, we will arrange them in two-element lists, where the first element is ?, and the second element is the pattern variable. Examples: (color (? X) red) (color apple (? Y)) (color (? X) (? Y)) If the pattern contains no pattern variables, then the pattern matches the “basic” statement (called a datum) iff the pattern and the datum are exactly the same. Example: Pattern (color apple red) matches datum (color apple red). If the pattern contains a pattern variable, then an appropriate substitution must be found to make the pattern match the datum. Example: Pattern (color (? X) red) matches datum (color apple red) given substitution = {x / apple}
Pattern matching (cont.) To implement pattern matching, we need a function, match, which works as follows: > (match ’(color (? X) red) ’(color apple red)) ((X apple)) > (match ’((? Animal) is a parent of (? Child)) ’(Tim is a parent of Bob)) ((Child Bob) (Animal Tim)) (? _) will denote anonymous variables; these match everything. Example: (color (? _) (? _)) match (color apple red), (color grass green), etc.
Macro procedures in LISP Macro procedures in Lisp have the following format: (defmacro <macro name> (<parameter1> … <parameter m>) <form 1> … <form n>) Contrary to ordinary procedures they do not evaluate their arguments, and evaluating the body results in a new form which is then evaluated to produce a value. Example: Develop a procedure, when-plusp, which prints out alarm whenever variable pressure becomes greater than zero. Version 1. > (defun when-plusp (number result) (when (plusp number) result)) > (setf pressure -3) > (when-plusp pressure (print ’Alarm)) ALARM ;the side effect produced when the second argument is evaluated NIL
Example (cont.) Version 2. > (defmacro when-plusp (number result) (list ’when (list ’plusp number) result)) > (when-plusp pressure (print ’Alarm)) Step 1: Evaluation of the body produces the following form (when (plusp pressure) (print ’Alarm)) Step 2A: Evaluation of the form generated at the first step produces the final result if pressure is greater than zero. ALARM ALARM Step 2B: Evaluation of the form generated at the first step produces the final result if pressure is less than zero. NIL
The backquote mechanism Recall that the normal quote, ’ , isolates the entire expression from evaluation; the backquote,`, does not have such a strong meaning. Whenever a comma appears inside a backquoted expression, the sub-expression immediately following the comma is replaced by its value. * (setf variable 'test) TEST * `(This is a ,variable) (THIS IS A TEST) * (setf variable '(Yet another example)) (YET ANOTHER EXAMPLE) * `(This is a ,variable) (THIS IS A (YET ANOTHER EXAMPLE)) * `(This is a ,@variable) (THIS IS A YET ANOTHER EXAMPLE) The backquote mechanism is widely used in macro procedures to fill macro templates.
Object streams Streams are lists of objects intended to be processed in the exact order in which they appear in the stream, namely from the front to the back of the stream. When a new object is added to the stream, it must be added to its back. To represent streams, we can use ordinary lists. Then, first allow us to access the first element, and rest will trim the first element off. However, we can also access the other elements of the list by means of second, third, etc., thus violating the definition of the stream. To prevent this from happening, streams will be represented as two-element lists, where the first element is the first object in the stream, and the second element is itself a stream. Example: * 'empty-stream EMPTY-STREAM * (stream-cons 'object1 'empty-stream) (OBJECT1 EMPTY-STREAM) * (stream-cons 'object1 '(OBJECT1 EMPTY-STREAM) ) (OBJECT1 (OBJECT1 EMPTY-STREAM))
Operations on streams (defun stream-endp (stream) (eq stream 'empty-stream)) (defun stream-first (stream) (first stream)) (defun stream-rest (stream) (second stream)) (defun stream-cons (object stream) (list object stream)) (defun stream-append (stream1 stream2) (if (stream-endp stream1) stream2 (stream-cons (stream-first stream1) (stream-append (stream-rest stream1) stream2)))) (defun stream-concatenate (streams) (if (stream-endp streams) 'empty-stream (if (stream-endp (stream-first streams)) (stream-concatenate (stream-rest streams)) (stream-cons (stream-first (stream-first streams)) (stream-concatenate (stream-cons (stream-rest (stream-first streams)) (stream-rest streams)))))))
Operations on streams (cont.) (defun stream-transform (procedure stream) (if (stream-endp stream) 'empty-stream (stream-cons (funcall procedure (stream-first stream)) (stream-transform procedure (stream-rest stream))))) (defun stream-member (object stream) (cond ((stream-endp stream) nil) ((equal object (stream-first stream)) t) (t (stream-member object (stream-rest stream))))) (defmacro stream-remember (object variable) `(unless (stream-member ,object ,variable) (setf ,variable (stream-append ,variable (stream-cons ,object 'empty-stream))) ,object)) Here STREAM-REMEMBER is a macro that inserts new assertions at the end of the stream, so that when the stream is processed the assertions will be processed in the order in which they have been entered.
An implementation of forward chaining Consider the “Zoo” example from Winston & Horn. The KBS is intended to identify animals provided their descriptions. Assume that all facts about the domain are stored in the fact base, *assertions*, represented as a stream, and all rules are stored in the rule base, *rules*, also represented as a stream. Pattern variables make it possible for a rule to match the fact base in different ways. Let us keep all such possibilities in a binding stream. Example: Consider the following rule set containing just one rule: ((identify ((? Animal) is a (? Species)) ((? Animal) is a parent of (? Child)) ((? Child) is a (? Species))) ‘empty-stream) Let the fact base contains the following facts: ((Bozo is a dog) ((Deedee is a horse) ((Deedee is a parent of Sugar) ((Deedee is a parent of Brassy) ‘empty-stream))))
Example (cont.) The first match produces the following two-element binding stream: (((species dog) (animal Bozo)) ((species horse) (animal Deedee)) ‘empty-stream) Next, the second rule antecedent is matched against each of the assertions in The fact base. However, this time we have the binding list from the first step, Which suggests particular substitutions: • Matching ((? Animal) is a parent of (? Child)) against the fact base fails for substitution ((species dog) (animal Bozo)) • Matching ((? Animal) is a parent of (? Child)) against the fact base given the substitution ((species horse) (animal Deedee)) succeeds resulting in a new binding list: (((child Sugar) (species horse) (animal Deedee)) ((child Brassy) (species horse) (animal Deedee)) ‘empty-stream)
More about efficiency and monotonicity of forward chaining Consider the following set of rules: (remember-rule '(identify1 ((? x) caresFor (? y)) ((? x) helpsWithHomework (? y)))) (remember-rule '(identify2 ((? x) isParentOf (? y)) ((? y) isMarriedTo (? z)) ((? x) isParentInLawOf (? z)))) (remember-rule '(identify3 ((? x) isSisterOf (? y)) ((? x) caresFor (? y)))) (remember-rule '(identify4 ((? x) isBrotherOf (? y)) ((? x) caresFor (? y)))) (remember-rule '(identify5 ((? x) isParentOf (? y)) ((? y) isParentOf (? z)) ((? x) isGrandparentOf (? z)))) (remember-rule '(identify6 ((? x) isSiblingOf (? y)) ((? y) isMale) ((? y) isBrotherOf (? x)))) (remember-rule '(identify7 ((? x) isSiblingOf (? y)) ((? y) isFemale) ((? y) isSisterOf (? x)))) (remember-rule '(identify8 ((? x) isParentOf (? y)) ((? x) isParentOf (? z)) ((? y) isSiblingOf (? z)))) ;; marriage is a symmetric relation (remember-rule '(identify9 ((? x) isMarriedTo (? y)) ((? y) isMarriedTo (? x))))
Assume the following set of facts: (remember-assertion '(Anna isFemale)) (remember-assertion '(Bobby isMale)) (remember-assertion '(Mary isParentOf Anna)) (remember-assertion '(Mary isParentOf Bobby)) (remember-assertion '(Sally isParentOf Mary)) (remember-assertion '(Bob isMarriedTo Sally)) (remember-assertion '(Mary isMarriedTo Jack)) Here is the result of forward-chaining process: CG-USER(3): (forward-chain) Rule IDENTIFY2 indicates (SALLY ISPARENTINLAWOF JACK). Rule IDENTIFY5 indicates (SALLY ISGRANDPARENTOF ANNA). Rule IDENTIFY5 indicates (SALLY ISGRANDPARENTOF BOBBY). Rule IDENTIFY8 indicates (ANNA ISSIBLINGOF ANNA). Rule IDENTIFY8 indicates (ANNA ISSIBLINGOF BOBBY). Rule IDENTIFY8 indicates (BOBBY ISSIBLINGOF ANNA). Rule IDENTIFY8 indicates (BOBBY ISSIBLINGOF BOBBY). Rule IDENTIFY8 indicates (MARY ISSIBLINGOF MARY). Rule IDENTIFY9 indicates (SALLY ISMARRIEDTO BOB). Rule IDENTIFY9 indicates (JACK ISMARRIEDTO MARY).
I am trying the rules again. Rule IDENTIFY6 indicates (BOBBY ISBROTHEROF ANNA). Rule IDENTIFY6 indicates (BOBBY ISBROTHEROF BOBBY). Rule IDENTIFY7 indicates (ANNA ISSISTEROF ANNA). Rule IDENTIFY7 indicates (ANNA ISSISTEROF BOBBY). I am trying the rules again. Rule IDENTIFY3 indicates (ANNA CARESFOR ANNA). Rule IDENTIFY3 indicates (ANNA CARESFOR BOBBY). Rule IDENTIFY4 indicates (BOBBY CARESFOR ANNA). Rule IDENTIFY4 indicates (BOBBY CARESFOR BOBBY). I am trying the rules again. Rule IDENTIFY1 indicates (ANNA HELPSWITHHOMEWORK ANNA). Rule IDENTIFY1 indicates (ANNA HELPSWITHHOMEWORK BOBBY). Rule IDENTIFY1 indicates (BOBBY HELPSWITHHOMEWORK ANNA). Rule IDENTIFY1 indicates (BOBBY HELPSWITHHOMEWORK BOBBY). I am trying the rules again. Nothing new noted. DONE
Outlined statements are clearly counterintuitive. They are caused by the rule (remember-rule '(identify8 ((? x) isParentOf (? y)) ((? x) isParentOf (? z)) ((? y) isSiblingOf (? z)))) Unfortunately, we cannot easily say that (? y) and (? z) must be different for the conclusion to hold, nor we can “remove” redundant conclusions from the KB. For the later, we should be able to define that sibling relation is irreflexive, i.e. X cannot be a sibling to themselves. On the other hand, this is not really an “error”, and one of the conclusions (ex. “ANNA CARESFOR ANNA”) actually make sense, because “caresfor” is reflexive relation – everybody cares for themselves.
Backward chaining Assume the same representation of rules as in forward chaining, i.e. If <antecedent 1> is true, <antecedent 2> is true, … <antecedent i> is true Then <consequent> is true. Rule interpretation starts with (i) an empty fact base, and (ii) a list of goals Which the system tries to derive, and consists of the following steps: • Form a stack initially composed of all “top-level” goals. • Consider the first goal from the stack, and gather all of the rules capable of satisfying this goal. • For each of these rules, examine the rule’s premises: • If all premises are satisfied, execute the rule to infer its conclusion, and remove the satisfied goal from the stack.
Backward chaining (cont.) • If there is a premise which is not satisfied, look for rules by means of which this premise can be derived; if such rules exist, add the premise as a sub-goal on the top of the stack, and go to 2. • If no rule exists to satisfy the unknown premise, place a query to the user and add the supplied value to the fact base. If the premise cannot be satisfied, consider the next rule which has the initial goal as its conclusion. • If all rules that can satisfy the current goal have been attempted, and all failed, then the goal is unsatisfiable; remove the unsatisfiable goal from the stack and go to 2. If the stack is empty (i.e. all of the goals have been satisfied) stop.
The fruit identification example Assuming that we do not have any information about the object that we are trying to recognize, let the “top-level” goal be (fruit = (? X)). Step1Initial fact base: ( ) Initial stack of goals: ((fruit = (? X))) Step 2 Rules capable of satisfying this goal are: 1, 1A, 6, 7, 8, 9, 10, 11, 12, 13, 13A, 13B. Step 3 Consider Rule 1. Its first premise is (shape = long). There is no data in the FB matching this premise and no rule has (shape = (? Y)) as its conclusion. Therefore, a query is placed to the user to acquire for the shape of the fruit under consideration. Assume that the user replies that the fruit is round, i.e. the current FB becomes Current fact base: ((shape = round)). Rule 1 fails, and Rule 6 is examined next. The first premise of Rule 6 results in a new goal which is added at the beginning of the current stack of goals. Current stack of goals: ((fruitclass = (? Y)) (fruit = (? X)))
The fruit identification example (cont.) There are three rules capable of satisfying the newly stated goal: 2, 2A, and 3.The first premise of Rule 2, (shape = round), matches a datum in the FB. The second premise leads to a new query regarding the diameter of the fruit. Assume that the answer is (diameter = 1 inch). Current fact base: ((shape = round)(diameter = 1 inch)). Rules 2 and 2A fail, and Rule 3 is examined next. It succeeds, thus a new conclusion, (fruitclass = tree) is added to the FB and the first goal is removed from the current stack of goals. Current fact base: ((shape = round)(diameter = 1 inch)(fruitclass = tree)). Now Rules 6, 7 and 8 fail, and Rule 9 is examined next. Its first premise succeeds, but its second premise places a new query regarding the color of the fruit. Assume that the user enters (color = red), which fails Rules 9 and 10. The first two premises of Rule 11 are satisfied, the third premise places a query regarding the seedclass. Rules 4 and 5 have conclusions (seedclass = (? Z)), which becomes a new subgoal. Rule 4 only premise (seedcouunt = 1) cannot be derived by any rule --> place a query to the user about the seedcount and assume that the answer is (seedcouunt = 1). Rule 4 succeeds and (seedclass = stonefruit), is added to the FB resulting in all premises of Rule 11 to hold and its conclusion (fruit = cherry) is added to the FB. Note that this proves our top-level goal. Current fact base: ((shape = round)(diameter = 1 inch)(fruitclass = tree)(color = red) (seedcount = 1)(seedclass = stonefruit)(fruit = cherry)). Current stack of goals: () Step 4 Stop, no more goals remain to be proved.
Family example – backward chaining CG-USER(26): (backward-chain '(Anna isSisterOf Bobby)) YES CG-USER(28): (backward-chain '(Anna isSisterOf (? x))) --> X = ANNA --> X = BOBBY NO-MORE CG-USER(29): (backward-chain '(Mary isParentOf (? x))) --> X = ANNA --> X = BOBBY NO-MORE CG-USER(30): (backward-chain '((? x) isParentOf (? y))) --> X = MARY Y = ANNA --> X = MARY Y = BOBBY --> X = SALLY Y = MARY NO-MORE CG-USER(36): (backward-chain '(Mary isParentOf Anna) '(Anna isSisterOf Bobby)) YES