Today’s Topics 11/24/15CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 131 Inference in FOPC Unification Resolution with Unification Forward vs Backward

Embed Size (px)

DESCRIPTION

The Need for an Algo that Matches FOPC Vars and Constants (and more) Consider p(Joe), [  x p(x) → q(x) ] what can we infer? Exact match fails, be seems we should say p(Joe) matches p(x) and then infer q(Joe) The UNIFICATION algorithm does this 11/24/15CS Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13 3

Citation preview

Todays Topics 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 131 Inference in FOPC Unification Resolution with Unification Forward vs Backward Chaining, Prolog Using Logical Inference to Create Plans Wrapup of Logic Next: Probabilistic Logic (MLNs) NextNext: Unsupervised Learning NextNextNext: Reinforcement Learning Last Topics: Philosophical Foundations, Future of AI? Detailed List of Course Topics: Updated 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 132 Reasoning from concrete cases Cased-based reasoning Nearest-neighbor algorithm Kernels Reasoning logically First-order predicate calculus Representing domain knowledge using mathematical logic Logical inference Probabilistic logic Problem-solving methods based on the biophysical world Genetic algorithms Simulated annealing Neural networks Philosophical aspects Turing test Searle's Chinese Room thought experiment The coming singularity Strong vs. weak AI Societal impact of AI Learning from labeled data Experimental methodologies for choosing parameter settings and estimating future accuracy Decision trees and random forests Probabilistic models Nearest-neighbor methods Genetic algorithms Neural networks Support vector machines Reinforcement learning (if time permits) Learning from unlabeled data K-Means Expectation Maximization Searching for solutions Heuristically finding shortest paths Algorithms for playing games like chess Simulated annealing Genetic algorithms Reasoning probabilistically Probabilistic inference Bayes' rule, Bayesian networks The Need for an Algo that Matches FOPC Vars and Constants (and more) Consider p(Joe), [ x p(x) q(x) ] what can we infer? Exact match fails, be seems we should say p(Joe) matches p(x) and then infer q(Joe) The UNIFICATION algorithm does this 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 3 We Need to use ForAll (Universal) Vars Consider p(Joe), [ x p(x) q(x) ] what can we infer? Nothing! We dont know to WHICH person the ThereExists (existential) variable applies 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 4 The Task of Unification Given TWO FOPC expressions Determine the minimal constraints we need to add in order to make the two wffs char-by-char identical 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 5 Unify(A, B) A B (the binding list) p(?x) p(Mary) p(John) p(Mary) p(1, 2) q(1, 2) p(1, 2, 3) p(1, 2) p(John) p(John) p(Jon) p(Jonathan) p(plus(1, 2)) p(3) p(?x) p(?y) p(?x, ?x, 1) p(?z, f(?y), ?y) Sample Unifications (all vars universal and indicated by a leading ?) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 6 { ?x / Mary } Fails, different constants Fails, p q; pred names must match Fails, # args must be the same { } // Note: the empty list does NOT mean fail Fails, even if we knew Jon = Jonathan Fails, the unifier doesnt calculate { ?x / ?y } // { ?y / ?x } is also correct { ?x / f(1), ?y / 1, ?z / f(1) } The Occurs Check ?x and f(?x) do not unify More generally, ?x and some function f(), where ?x occurs anywhere inside f(), do not unify, eg ?x and f(1, g(2, h(3, ?x)), 4) Consider ?x and fatherOf(?x) they can never refer to the same person (ignore cloning) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 7 Why? Why can we unify ?x and f(?y) but can not unify ?x and f(?x) ? Consider the constants in our world visualized: 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 8 Alice Since ?x is universal, it refers to all of these constants Independently, ?y refers to all the constants and f(?y) always refers to SOME constant So at some point, f(?y) and ?x must refer to the SAME constant However, ?x and f(?x) are not independent, so we cannot guarantee that they ever refer to the same constant Zachary For Those that Prefer Code For all ?x in Constants For all ?y in Constants if (?x = f(?y)) return FOUND_IT Return THIS_LINE_NEVER_REACHED For all ?x in Constants if (?x = f(?x)) return FOUND_IT Return THIS_LINE_CAN_BE_REACHED 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 9 The Binding List () and the substitute(, wff) Function { ?var1 / term1, ?var2 / term2, } ?var1 bound to term1 and ?var2 to term2 substitute(, wff) for each variable, ?x, in the binding list, replace each occurrence of ?x with the term to which it is bound (repeat until no bound terms remain) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 10 Examples substitute( { ?x / 3, ?y / 2 }, p(?x, ?y) ) p(3, 2) substitute( { ?x / ?y, ?y / 2 }, p(?x, ?y) ) 1 st pass: p(?y, 2) Final result: p(2, 2) substitute( { ?x / ?z, ?y / 2 }, p(?x, ?y) q(?y) ) p(?z, 2) q(2) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 11 Unification and Connectives Note: we assume some other code handles unifying compound wffs, such as unify fancy ( p(?x, ?y) q(?y), q(?a) p(?b, ?c) ) This requires a SEARCH PROCESS, eg unify fancy ( p(?a, ?a) p(?b, ?c) q(?a), p(?x, 2) q(1) p(1, ?y) ) - if we unify p(?a, ?a) and p(?x, 2) then when we get to q(?a), we need to BACKTRACK 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 12 At Long Last The Unification Algorithm Unify(E1, E2, ) // Handles WFFs and Functions if = FAIL return FAIL // Unwind recursion if E1 exactly matches E2 return if E1 is a var, return UnifyVar(E1, E2, ) if E2 is a var, return UnifyVar(E2, E1, ) if E1 or E2 is a constant return FAIL if predicate/function name of E1 and E2 are different or #args differ return FAIL handle the RECURSIVE case (next slide) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 13 At Long Last The Unification Algorithm The RECURSIVE Case (if here, have two predicates [or two functions] with the same name, ie head, and the same # of arguments) Let new = For i = 1 to #args(E1) new = unify(arg(i, E1), arg(i, E2), new) If new = FAIL return FAIL Return new 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 14 At Long Last The Unification Algorithm UnifyVar (Var, E, ) // First arg is a VARIABLE // See if Var or E are already bound If { Var / Value } in Return Unify(Value, E, ) If { E / Value } in Return Unify(Var, Value, ) // Do the occurs check If Var appears in substitute(E, ) return FAIL // Bind Var to E since we are free to do so Return { Var / E } UNION // Add { Var / E } to 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 15 At Long Last The Unification Algorithm Example: Unify(p(?x, ?x, A), p(?z, f(?y), ?y), { }) First check all our base cases. OK TIP: Unify arg-by-arg. When a BINDING occurs, immediately apply it to the two expressions When done, the two expression will be identical or FAIL will have been returned 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 16 At Long Last The Unification Algorithm Example: Unify(p(?x, ?x, A), p(?z, f(?y), ?y), { }) Bind ?x to ?z. So = { ?x / ?z } and our two expressions become p(?z, ?z, A) and p(?z, f(?y), ?y) Next bind ?z to f(?y). So = { ?x / ?z, ?z / f(?y) } and our two expressions become p(f(?y), f(?y), A) and p(f(?y), f(?y), ?y) Next bind ?y to A. So = { ?x / ?z, ?z / f(?y), ?y / A } and our two expressions become p(f(A), f(A), A) and p(f(A), f(A), A) DONE! 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 17 MGU Our unification algorithm produces the most general unifier (MGU) because it only binds variables when forced to do so, and does so as generally as possible Eg, { ?x / 1, ?y / 1 } unifies p(?x) and p(?y) but is more constrained than need be 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 18 Deduction in FOPC 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Universal Instantiation (UI) x p(x) p(constant) Deduction in FOPC (2) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Existential Introduction (EI) p(constant) x p(x) Existential Instantiation (EI2) x p(x) p(skolemConstant101) We dont know WHICH constant, so we make up a dummy name Deduction in FOPC (3) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Modus Ponens (MP) [ ?x ?y p(?x, ?y) q(?x, ?y) ], ?a p(?a, 1) substitute(unify(p(?x, ?y), p(?a, 1), { }), q(?x, ?y) ) From now on, well assume all our variables are UNIVERSAL unless explicitly stated otherwise Deduction in FOPC (4) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & General Resolution (GR) [ p(?x, ?y) q(?x, ?y) ], [ p(?a, ?b) r(?a, ?b) ] substitute(unify(p(?x, ?y), p(?a, ?b), { }), q(?x, ?y) r(?a, ?b) ) If we preprocess all our FOPC wffs, we only need this inference rule Deduction in FOPC (5) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & General Resolution (GR): Example [ p(?x) q(?x) ], [ p(John) r(John) ] q(John) r(John) due to = { ?x / John } Ie, we should state the BINDING LISTS when we say which inference rule we used Conjunctive Normal Form (aka Clausal Form) Example In FOPC well have set of clauses that look like this { p(?x) r(?y) q(?x, ?y) } { q(?z) p(?z) } // Note: no repeated vars across rows! { q(1) r(1) } { w(?a) z(?a) a(?a) } { c(?b) d(?c) e(?b, 1, ?c) } { c(?d) d(?e) f(?d, ?e, 4, ?d) } { m(?f) g(?g) b(?h, 5) h(?f, ?g, ?h, 3) } 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 24 Resolution Theorem Proving in FOPC P(1) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & P(?x) S(?x) S(?z) S(1) Contradiction shown! = { ?x / 1 } = { ?z / 1 } Given: P(1) and ?x P(?x) S(?x) Prove: ?z S(?z) Negate Query: ?z S(?z) Putting FOPC WFFs into CNF (Sec of the text) Some additional steps needed to what we did for propositional logic 1.Move NOTs inwards needs to also handle x p(x) becomes x p(x) // DeMorgan generalized x p(x) becomes x p(x) 2.Replace Existential Variables with Skolems x, y z p(x, y, z) becomes x, y p(x, y, SkolemFunction99(x, y)) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 26 Putting FOPC WFFs into CNF (Sec of the text) 3.Standardize apart use unique vars in each WFF x p(x) becomes x1 p(x1) x q(x) becomes x2 q(x2) 4.Drop ForAlls since no existential vars left x1 p(x1) becomes p(x1) x2 q(x2) becomes q(x2) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 27 Knowledge Bases In AI Knowledge Bases (KBs) we typically have bunch of facts Eg, about the current patient temperature( id1, 101, 11/24/15) bloodPressure(id1, 140/100, 11/24/15) And a bunch of broadly applicable rules, eg tempHigh(?x) bpHigh (?x) give(?x, Drug123) olderThan85(?x) underWgt (?x) give(?x, Drug789) We want to (a) make useful inferences, eg collect diagnoses and/or (b) answer queries, eg needsHospitalization(?x) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 28 Searching for Proofs (when our KB only has facts and implications, ie s) Forward Chaining Look at each IF-THEN rule and see if we can add its consequent to what we know Repeat until nothing more to add (dont want to use ALL valid inference rules since that is unbounded) Eg Given: W and Q and P Q R and W P Add: P then add R Good way to design tax s/w that looks for audit flags 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 29 Searching for Proofs Backward Chaining (Prolog works this way) Start with WFF to prove Work back to see what we need to know to prove it Eg Given: W and Q and P Q R and W P Show: R To show R, we need to know P and Q. We know Q is true, but to show P we need to show W. Fortunately W is given. So were done. Note: there might be dead ends (eg, if W not given in above) and wed need to BACKTRACK (eg, see if another way to deduce P) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Typically work BACKWARDS through implications (IF-THEN rules) Backward Chaining for Resolution Proofs Work BACK from Query P 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & R S S P R Q DEAD END! P R Q R Can set this up as a SEARCH task. Prolog uses a variant of Depth- First Search Note: this is a variant on the KB from the previous page Prolog in Two Slides (Prolog = Programming in Logic) Given a KB of the Form (Prolog handles logical vars, but well simply look at the propositional case) p q r p a w q r b p b w Internally (1) p (2) q (3) r (4) p a w (5) q r b (6) p b w 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Query: w Negated: (7) w Prologs Search Space for Previous Pages Example 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & WFFs 1-7 (8) p a Rez on 4+7 (9) p b Rez on 6+7 (10) a Rez on 1+8 (11) b Rez on 1+9 (12) q r Rez on 5+11 (13) r Rez on 2+12 Rez on 3+13 SUCCESS! DEAD END, BACKTRACK (1) p (2) q (3) r (4) p a w (5) q r b (6) p b w (7) w Power of Logic Programming Fact Base add( 1, 1, 2) add( 1, 2, 3) add( 1, 10, 11) add(10, 1, 11) add(10, 2, 12) add(10, 10, 20) Rule Base add(?x, 0, ?x) add(0, ?x, ?x) [short for number(?x) add(?x, 0, ?x)] Queries add(1, 2, 3) add(1, 2, 4) // The 1 st returns TRUE and the 2 nd FAIL add(1, 2, ?z) add(?z, 2, 4) add(5, ?z, 9) // All args input or output! add(?w, 2, ?z) add(?w, ?z, 4) add(?a, ?b, ?c) // Get multiple answers! 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & ILP addresses the learning of FOPC Semi-Decidability If proof by contradiction exists, resolution will find it But might not terminate if no proof exists (our old friend, infinite search spaces or we might exhaust the search space w/o finding a contradiction) Example number(0), successor(0, 1), x,y number(x) successor(x, y) number(y) Query: number(John) 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 35 Examples Resolution Search Space 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Negated Query numb(x2) succ(x2, John) { y / John } numb(John) numb(x1) succ(x1, y) numb(y) numb(x3) succ(x3, x4) succ(x4, John) { y / x2 } Provide unique variable names in each new WFF numb(x5) succ(x5, x6) succ(x6, x7) succ(x7, John) { y / x3 } Why not Try to Prove P and P What about simultaneously: (i) try to prove P by contradiction (ii) try to prove P by contradiction Sometimes this would work, but in some cases neither might lead to a contradiction Example: given Q cant prove P nor P via contradiction 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 37 AI Planning Given(a) a set of actions (b) an initial state of the world (c) a specification of GOALs Find a sequence of actions that lead from the initial state to a goal state AI Search does much of this, though we usually have MULTIPLE (conjunctive) goals here and need to worry about how they interact 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & A C B B A C Goal(on(A,B)) Goal(on(B,C)) The Sussman Anomaly Example: Planning via Situation Calculus and Deduction (more efficient, specialized planning algos exist) Number WFF Justification 1 box(Box1) box(Box2) (Box1 Box2) given paint(Red) paint(Blue) (Red Blue) location(Loc1) location(Loc2) (Loc1 Loc2) // Types of all our constants 2 at(Box1, Loc2, S0) at(Box2, Loc2, S0) given color(Box1, Red, S0) color(Box2, Red, S0) // The initial state 3 b,p,s box(b) paint(p) state(s) at(b, Loc1, s) given color(b, p, result(Paint(b, p), s)) // In order to paint a box it must be at loc 1. 4 b1,b2,p1,p2,s box(b1) box(b2) (b1 b2) state(s) given paint(p1) paint(p2) color(b1, p1, s) // Painting one box does not color(b1, p1, result(Paint(b2, p2), s)) // change the color of other boxes. 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 39 Number WFF Justification 5 b1,b2,p,x,s box(b1) box(b2) location(x) state(s) given paint(p) at(b2, x, s) // Painting a box does not at(b2, x, result(Paint(b1, p), s)) // change the location of boxes. 6 b,x,y,s box(b) location(x) location(y) state(s) given at(b, x, s) (x y) at(b, y, result(Move(b, x, y), s)) // Moving a box changes where it is at. 7 b1,b2,x,y,z,s box(b1) box(b2) (b1 b2) state(s) given location(x) location(y) (x y) location(z) at(b1, x, s) at(b2, z, s) at(b2, z, result(Move(b1, x, y), s)) // Moving one box does not change where other boxes are at. 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Example: Planning via Situation Calculus and Deduction Number WFF Justification 8 b1,b2,x,y,p,s box(b1) box(b2) location(x) location(y) given (x y) paint(p) state(s) color(b2, p, s) // Moving a box does not color(b2, p, result(Move(b1, x, y), s)) // change the color of boxes. 9 x,y (x y) (y x) // Inequality is symmetric. given Prove: s color(Box2, Blue, s) // Find a plan that paints Box2 BLUE. 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Example: Planning via Situation Calculus and Deduction Number WFF Justification 10 at(Box2, Loc1, result(Move(Box2, Loc2, Loc1), S0)) Generalized Modus Ponens on 1 and 6 with { b / Box2, x / Loc2, y / Loc1, s / S0 } 11 color(Box2, Red, result(Move(Box2, Loc2, Loc1), S0)) Generalized Modus Ponens on 1 and 8 with { b1 / Box2, b2 / Box2, x / Loc2, x / Loc1, p / Red, s / S0 } Note: for simplicity, we skip justifying AND Elimination and simply pulled out of wffs 1 and 2 the predicates we need. We also simply use the symmetry of inequality without justification. 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Example: Planning via Situation Calculus and Deduction Number WFF Justification 12 color(Box2, Blue, result(Paint(Box2, Blue), result(Move(Box2, Loc2, Loc1), S0)) ) Generalized Modus Ponens on 1 and 3 with { b / Box2, p / Blue, s / result(Paint(Box2, Blue), result(Move(Box2, Loc2, Loc1), S0)) } 13 s color(Box2, Blue, s) Existential Introduction on 12 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & Example: Planning via Situation Calculus and Deduction Read the PLAN from the final state, right-to-left Wrap Up of Logic Formal logic provides a way to get world knowledge into computers in a way that allows for sound deductive inference Useful for building knowledge bases, expert systems, and theorem provers BRITTLENESS of pure logic a challenge Goal: probabilistic logic (next lecture) Goal: combine domain knowledge and ML 11/24/15CS Fall 2015 (Shavlik), Lecture 26, Weeks 12 & 13 44