Module - 2 Knowledge and Reasoning
Module - 2 Knowledge and Reasoning
• Logical agents
• Propositional logic
• First-order logic (predicate logic)
• Inference techniques in first order logic
• Acting under uncertainty
• Basic probability theory
• Bayes Rule
• Naïve Bayes Model
Knowledge bases
Then it can Ask itself what to do - answers should follow from the KB
Agents can be viewed at the knowledge level
i.e., what they know, regardless of how implemented
• Unification (prerequisite)
• Forward Chaining
• Backward Chaining
– Logic Programming (Prolog)
• Resolution
– Transform to CNF (Chomsky normal form )
– Generalization of Prop. Logic resolution
4
Knowledge-based agent
•A knowledge-based agent includes a knowledge base and an inference
system.
• Given:
– “The red block is above the blue block”
– “The green block is above the red block”
• Infer:
– “The green block is above the blue block”
– “The blocks form a tower”
Example 2
• Given:
• If it is sunny today, then the sun shines on the
screen. If the sun shines on the screen, the
blinds are brought down. The blinds are not
down.
• Find out:
• Is it sunny today?
A KR language needs to be
• expressive
• unambiguous
• flexible
The inference procedures need to be
• Correct (sound)
• Complete
• Efficient
Candidates (for now)
• English (natural language)
• Java (programming language)
• Logic (special KR language)
Knowledge Representations
• Propositional logic
• Predicate Logic (First-order logic)
Propositional logic
• The symbols of propositional calculus are the
propositional symbols:
P, Q, R, S, …
• the truth symbols:
true, false
• and connectives:
, , , ,
Propositional Calculus Sentences
• Every propositional symbol and truth symbol
is a sentence.
Examples: true, P, Q, R.
• The negation of a sentence is a sentence.
Examples: P, false.
• The conjunction, or and, of two sentences is a
sentence.
Example: P P
Propositional Calculus Sentences (cont’d)
• The symbol true is always assigned T, and the symbol false is assigned F.
P Q P Q
T T T
T F F
T T F
T F F
Fig. 2.2 Truth table demonstrating the
equivalence of PQ and PQ
Proofs in propositional calculus
• If it is sunny today, then the sun shines on the screen. If the sun shines on
the screen, the blinds are brought down. The blinds are not down.
• P: It is sunny today.
• Q: The sun shines on the screen.
• R: The blinds are down.
• Premises: PQ, QR, R
• Question: P
Prove using a truth table
Variables Premises Trial
Conclusions
P Q R P Q Q R R P P
T T T T T F T F
T T F T F T T F
T F T F T F T F
T F F F T T T F
F T T T T F F T
F T F T F T F T
F F T T T F F T
F F F T T T F T
Propositional calculus is cumbersome
• Given:
– If it is sunny today, then the sun shines on the screen.
– If the sun shines on the screen, the blinds are brought down.
– The blinds are not down.
• Conclusion:
– If it is sunny on a particular day, then the sun shines on the screen.
– If the sun shines on the screen on a particular day, the blinds are brought
down.
– The blinds are not down today.
• Is it sunny today?
Problems with Propositional
Logic
25
Propositional logic is a weak language
• Hard to identify “individuals” (e.g., Mary, 3yrs)
26
Example
• Consider the problem of representing the following information:
– Every person is mortal.
– Confucius is a person.
– Confucius is mortal.
• How can these sentences be represented so that we can infer the third
sentence from the first two?
27
First-order logic (Predicate logic)
• First-order logic (FOL) models the world in terms of
– Objects, which are things with individual identities
– Properties of objects that distinguish them from other objects
– Relations that hold among sets of objects
– Functions, which are a subset of relations where there is only one “value”
for any given “input”
• Examples:
– Objects: Students, lectures, companies, cars ...
– Relations: Brother-of, bigger-than, outside, part-of, has-color, occurs-after,
owns, visits, precedes, ...
– Properties: blue, oval, even, large, ...
– Functions: father-of, best-friend, second-half, one-more-than ...
28
Syntax of FOL: Basic elements
• Constants John, 2, DIT,...
• Predicates Brother, >,...
• Functions Sqrt, LeftLegOf,...
• Variables x, y, a, b,...
• Connectives , , , ,
• Equality =
• Quantifiers ,
Quantifiers
• Universal quantification
– (x)P(x) means that P holds for all values of x in the domain
associated with that variable
– E.g., (x) dolphin(x) mammal(x)
• Existential quantification
– ( x)P(x) means that P holds for some value of x in the
domain associated with that variable
– E.g., ( x) mammal(x) lays-eggs(x)
– Permits one to make a statement about some object
without naming it
30
Quantifiers
• Universal quantifiers are often used with “implies” to form “rules”:
(x) student(x) smart(x) “All students are smart”
31
Quantifier Scope
• Switching the order of universal quantifiers does not change the meaning:
– (x)(y)P(x,y) = (y)(x) P(x,y)
32
Translating English to FOL
33
Knowledge bases
Then it can Ask itself what to do - answers should follow from the KB
Agents can be viewed at the knowledge level
i.e., what they know, regardless of how implemented
• Forward Chaining
• Backward Chaining
• Resolution
• Unification
Forward Chaining
• Forward Chaining
– Start with atomic sentences in the KB and apply
Modus Ponens in the forward direction, adding
new atomic sentences, until no further inferences
can be made.
– P Q and P is asserted to be true, so
therefore Q must be true
36
Forward Chaining
• Given a new fact, generate all consequences
• Assumes all rules are of the form
– C1 and C2 and C3 and…. --> Result
• Each rule & binding generates a new fact
• This new fact will “trigger” other rules
• Keep going until the desired fact is generated
• (Semi-decidable as is FOL in general)
37
FC: Example Knowledge Base
• The law says that it is a crime for an American to sell weapons
to hostile nations. The country Nono, an enemy America, has
some missiles, and all of its missiles were sold to it by Col.
West, who is an American.
39
FC: Example Knowledge Base
• An enemy of America counts as “hostile”
Enemy( x, America ) Hostile(x)
40
FC: Example Knowledge Base
41
FC: Example Knowledge Base
42
FC: Example Knowledge Base
43
Efficient Forward Chaining
• Order conjuncts appropriately
– E.g. most constrained variable
• Don’t generate redundant facts; each new fact
should depend on at least one newly
generated fact.
– Production systems
– RETE matching
– CLIPS
44
Forward Chaining Algorithm
45
Backward Chaining
• Consider the item to be proven a goal
• Find a rule whose head is the goal (and bindings)
• Apply bindings to the body, and prove these
(subgoals) in turn
• If you prove all the subgoals, increasing the binding
set as you go, you will prove the item.
• Logic Programming (cprolog )
46
Backward Chaining Example
47
Backward Chaining Example
48
Backward Chaining Example
49
Backward Chaining Example
50
Backward Chaining Example
51
Backward Chaining Example
52
Backward Chaining Example
53
Backward Chaining Algorithm
54
Properties of Backward Chaining
• Depth-first recursive proof search: space is linear in
size of proof
• Incomplete due to infinite loops
– Fix by checking current goal with every subgoal on the
stack
• Inefficient due to repeated subgoals (both success
and failure)
– Fix using caching of previous results (extra space)
• Widely used without improvements for logic
programming
55
Inference Methods
• Unification (prerequisite)
• Forward Chaining
• Backward Chaining
– Logic Programming (Prolog)
• Resolution
– Transform to CNF (Chomsky normal form )
– Generalization of Prop. Logic resolution
56
Resolution for propositional Logic
• Convert everything to CNF
(conjunctive normal form/ clausal normal form)
• If resolution is successful, proof succeeds
57
Resolution
• Resolution allows a complete inference mechanism (search-
based) using only one rule of inference
• Resolution rule:
Complementary literals P1 and P1 “cancel out”
58
Rules
• Rules
– Cold and precipitation -> snow
¬cold ¬precipitation snow
– January -> cold
¬January cold
– Clouds -> precipitation
¬clouds precipitation
• Facts
– January, clouds
• Prove
– snow
64
Propositional Resolution Example
¬snow ¬cold ¬precipitation snow
¬clouds clouds
65
Resolution Theorem Proving (FOL(Predicate Logic))
66
Converting to CNF(Rules)
1. Replace implication (A B) by A B
2. Move “inwards”
• x P(x) is equivalent to x P(x) & vice versa
3. Standardize variables
• x P(x) x Q(x) becomes x P(x) y Q(y)
4. Skolemize
• x P(x) becomes P(A)
5. Drop universal quantifiers
• Since all quantifiers are now , we don’t need them
6. Distributive Law
a(b + c) = ab + ac
67
RESOLUTION IN PREDICATE LOGIC
2. Negate S and convert the result to clause form. Add it to the set of clauses obtained in 1.
The resolvent will be the disjunction of all of these literals of both clauses. If there is a pair
of literals T1 and T2 such that one parent clause contains Ti and the other contains T2 and if
T1 and T2 are unifiable, then neither t1 nor T2 should appear in the resolvent. Here Ti and
C) If the resolvent is the empty clause , then a contradiction has been found. If it is not, then
• Open PDF
• Step-1:Examples for Conversion from Natural Language
Sentences to Predicate Logic
• Step 2: Convert to Clausal Form
• Step 3: Negate query and start
• Step 4:Resolve with a rule from the knowledge base (that
contains F)
• Step 5: Repeat until either a contradiction is found or no
progress can be made
Step-1:Examples for Conversion from Natural
Language Sentences to Predicate Logic
• 1. Marcus was a man
Man(Marcus)
• 2. Marcus was a Pompeian
Pompeian(Marcus)
• 3. All Pompeians were Romans
∀x [Pompeian(x) Roman(x)]
• 4. Caesar was a ruler
Ruler(Caesar)
5. All Romans were either loyal to Caesar or hated him
∀x [Roman(x) (LoyalTo(x, Caesar) ∨ Hate(x, Caesar))]
6. Everyone is loyal to someone
∀x ∃y LoyalTo(x,y)
7. People only try to assassinate rulers they aren't loyal to
∀x∀y[(Person(x) ∧ Ruler(y) ∧ TryAssassinate(x,y))
¬LoyalTo(x,y)]
8. Marcus tried to assassinate Caesar
TryAssassinate(Marcus, Caesar)
81
What is Unification?
• Step.2: If the initial Predicate symbol in S1 and S2 are not same, then return FAILURE.
• Step. 3: IF S1 and S2 have a different number of arguments, then return FAILURE.
• Step. 4: Set Substitution set(SUBST) to NIL.
• Step. 5: For i=1 to the number of elements in S1.
• a) Call Unify function with the ith element of S1 and ith element of S2, and put the
result into Sub.
• b) If Sub = failure then returns Failure
• c) If Sub ≠ NIL then do,
• a. Apply Sub to the remainder of both L1 and L2.
• b. SUBST= APPEND(Sub, SUBST).
• Step.6: Return SUBST.
Implementation of the Algorithm
1. SUBST θ={b/Z}
S1 => { p(b, X, f(g(b))); p(b, f(Y), f(Y))}
• Unified Successfully.
Unifier = { b/Z, f(Y) /X , g(b) /Y}.
• 3. Find the MGU of {p (X, X), and p (Z, f(Z))}
• Here, Ψ1 = {p (X, X), and Ψ2 = p (Z, f(Z))
S0 => {p (X, X), p (Z, f(Z))}
SUBST θ= {X/Z}
S1 => {p (Z, Z), p (Z, f(Z))}
SUBST θ= {f(Z) / Z}, Unification Failed.
• Hence, unification is not possible for these
expressions.
• 4. Find the MGU of UNIFY(prime (11),
prime(y))
• Here, Ψ1 = {prime(11) , and Ψ2 = prime(y)}
S0 => {prime(11) , prime(y)}
SUBST θ= {11/y}
• S1 => {prime(11) , prime(11)} , Successfully
unified.
Unifier: {11/y}.
Acting under uncertainty
A logical agent uses propositions that are true, false or unknown
When the logical agent knows enough facts about it
environment it derives plans that are guaranteed to work.
Unfortunately, agents almost never have access to the whole
truth about their environment and therefore agents must act
under uncertainty
utility theory
Probability
Basics
P(m)=0.0001, P(s)=0.1 P(m|s) = P(s|m) P(m) / P(s) = 0.8 × 0.0001 / 0.1 = 0.0008
◦ Note: even though the probability of having a stiff neck given meningitis is very
large (0.8), the
posterior probability of meningitis given a stiff neck is still very small