Transcript lecture26

Today’s Topics
•
•
•
•
•
•
Inference in FOPC
Unification
Resolution with Unification
Forward vs Backward Chaining, Prolog
Using Logical Inference to Create Plans
Wrapup of Logic
•
•
•
•
Next:
NextNext:
NextNextNext:
Last Topics:
11/24/15
Probabilistic Logic (MLNs)
Unsupervised Learning
Reinforcement Learning
Philosophical Foundations, Future of AI?
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
1
Detailed List of Course Topics:
Updated
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
11/24/15
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
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
2
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/15
CS 540 - 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 don’t know to WHICH person
the ThereExists (‘existential’) variable applies
11/24/15
CS 540 - 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
wff’s char-by-char identical
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
5
Sample Unifications
(all vars universal and indicated by a leading ‘?’)
Unify(A, B)
θ (the ‘binding’ list)
A
B
p(?x)
p(Mary)
{ ?x / Mary }
p(John)
p(Mary)
Fails, different constants
p(1, 2)
q(1, 2)
Fails, p  q; pred names must match
p(1, 2, 3)
p(1, 2)
Fails, # args must be the same
p(John)
p(John)
{ } // Note: the empty list does NOT mean fail
p(Jon)
p(Jonathan)
Fails, even if we knew Jon = Jonathan
p(plus(1, 2))
p(3)
Fails, the unifier doesn’t calculate
p(?x)
p(?y)
{ ?x / ?y } // { ?y / ?x } is also correct
p(?x, ?x, 1)
p(?z, f(?y), ?y) { ?x / f(1), ?y / 1, ?z / f(1) }
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
6
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/15
CS 540 - 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:
Alice
…
Zachary
11/24/15
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
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
8
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/15
CS 540 - 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/15
CS 540 - 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) )
1st 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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
11
Unification and Connectives
Note: we assume some other code handles
unifying compound wff’s, such as
unifyfancy( p(?x, ?y) ˄ q(?y), q(?a) ˄ p(?b, ?c) )
This requires a SEARCH PROCESS, eg
unifyfancy( 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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
12
At Long Last …
The Unification Algorithm
Unify(E1, E2, θ) // Handle’s WFF’s 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/15
CS 540 - 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/15
CS 540 - 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/15
CS 540 - 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/15
CS 540 - 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)
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
DONE!
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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
18
Deduction in FOPC
Universal Instantiation (UI)
x p(x)
p(constant)
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
19
Deduction in FOPC (2)
Existential Introduction (EI)
p(constant)
 x p(x)
Existential Instantiation (EI2)
x p(x)
p(skolemConstant101)
11/24/15
We don’t know
WHICH constant,
so we make up a
‘dummy’ name
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
20
Deduction in FOPC (3)
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, we’ll assume all our variables are
UNIVERSAL unless explicitly stated otherwise
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
21
Deduction in FOPC (4)
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 wff’s,
we only need this inference rule
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
22
Deduction in FOPC (5)
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
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
23
Conjunctive Normal Form
(aka Clausal Form) Example
In FOPC we’ll 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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
24
Resolution Theorem Proving in FOPC
P(1)
¬ P(?x) ˅ S(?x)
¬ S(?z)
Given:
P(1) and  ?x P(?x) → S(?x)
θ = { ?x / 1 }
Prove:  ?z S(?z)
Negate Query:
 ?z ¬ S(?z)
Contradiction
shown!
S(1)
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
25
Putting FOPC WFFs into CNF
(Sec 9.5.1 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)
  x p(x) becomes  x  p(x)
// DeMorgan generalized
2. Replace Existential Variables with ‘Skolems’
 x, y  z p(x, y, z) becomes  x, y p(x, y, SkolemFunction99(x, y))
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
26
Putting FOPC WFFs into CNF
(Sec 9.5.1 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 ForAll’s since no existential vars left
 x1 p(x1) becomes p(x1)
 x2 q(x2) becomes q(x2)
11/24/15
CS 540 - 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/15
CS 540 - 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 (don’t 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/15
CS 540 - 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.”
Typically work
BACKWARDS
through
implications
(IF-THEN rules)
“We know Q is true, but to show P we need to
show W. Fortunately W is given. So we’re done.”
Note: there might be ‘dead ends’ (eg, if W not given in above) and
we’d need to BACKTRACK (eg, see if another way to deduce P)
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
30
Backward Chaining for
Resolution Proofs – Work BACK from Query
P
¬R˅S
¬Q˅R
¬S
¬P˅R
Note: this is a
variant on the KB
from the previous
page
¬R
¬Q
DEAD END!
¬P
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
Can set this up as
a SEARCH task.
Prolog uses a
variant of DepthFirst Search
31
Prolog in Two Slides
(Prolog = Programming in Logic)
Given a KB of the Form
(Prolog handles logical vars, but we’ll
simply look at the propositional case)
p
paw
pbw
Internally
(1) p
(4)  p   a  w
q
qrb
r
(2) q
(5)  q   r  b
Query:
w
Negated: (7)  w
(6)  p   b  w
11/24/15
(3) r
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
32
Prolog’s Search Space
for Previous Page’s Example
Rez on 4+7
WFF’s
1-7
(8)  p   a
Rez on 1+8
Rez on 6+7
(9)  p   b
Rez on 1+9
(10)  a
(11)  b
Rez on 5+11
DEAD END,
BACKTRACK
(12)  q   r
Rez on 2+12
(13)  r
11/24/15
(1)
(2)
(3)
(4)
(5)
(6)
(7)
p
q
r
paw
qrb
pbw
w
Rez on 3+13
□
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
SUCCESS!
33
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) …
ILP addresses
the learning of
FOPC
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, ?z)
add(?w, 2, ?z)
11/24/15
add(1, 2, 4) // The 1st returns TRUE and the 2nd FAIL
add(?z, 2, 4)
add(5, ?z, 9) // All args input or output!
add(?w, ?z, 4) add(?a, ?b, ?c) // Get multiple answers!
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
34
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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
35
Example’s Resolution Search Space
Negated Query
…
 numb(x1)   succ(x1, y)  numb(y)
 numb(John)
{ y / John }
 numb(x2)   succ(x2, John)
Provide unique variable
names in each new WFF
{ y / x2 }
 numb(x3)   succ(x3, x4)   succ(x4, John)
{ y / x3 }
 numb(x5)   succ(x5, x6)   succ(x6, x7)   succ(x7, John)
…
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
36
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 can’t prove P nor  P via contradiction
11/24/15
CS 540 - 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
C
A
11/24/15
B
A
B
C
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
The ‘Sussman Anomaly’
Goal(on(A,B)) 
Goal(on(B,C))
38
Example: Planning via Situation Calculus
and Deduction (more efficient, specialized planning algo’s 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) 
color(Box1, Red, S0)  color(Box2, Red, S0)
given
// 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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
39
Example: Planning via Situation
Calculus and Deduction
Number
5
WFF
Justification
 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) 
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))
given
// “Moving one box does not change where other boxes are at.”
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
40
Example: Planning via Situation
Calculus and Deduction
Number
8
WFF
Justification
 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)
Prove:
11/24/15
s color(Box2, Blue, s)
// “Inequality is symmetric.”
given
// “Find a plan that paints Box2 BLUE.”
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
41
Example: Planning via Situation
Calculus and Deduction
Number
10
WFF
Justification
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 wff’s 1 and 2 the predicates we need.
We also simply use the symmetry of inequality without justification.
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
42
Example: Planning via Situation
Calculus and Deduction
Number
12
WFF
Justification
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
Read the PLAN from the
final state, ‘right-to-left’
11/24/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
43
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/15
CS 540 - Fall 2015 (Shavlik©), Lecture 26, Weeks 12 & 13
44