AI Lec6 KnowledgeRepresentation&Reasoning
AI Lec6 KnowledgeRepresentation&Reasoning
(HKUST) Reasoning 1 / 57
Intelligence and Reasoning
Copycats are not intelligent:
Emperor: It’s white.
Minister: It’s white.
Emperor: It’s black.
Minister: It’s black.
Simple pattern-matching-substitution agents are not intelligent either:
John’s program:
char *name = “John”;
main() {
int len = 0;
while (name++ != ’0’) ++len;}
Jack’s program:
char *myname = “John”;
main() {
int mylen = 0;
while (myname++ != ’0’) ++mylen;}
(HKUST) Reasoning 2 / 57
ELIZA - We Now Have ChatGPT
ELIZA (Weizenbaum, 1963), a computer psychotherapist based on pattern
matching that fooled a lot of people (from Machines Who Thinks, by P.
McCorduck, 1979):
[The visitor did so, and the doctor continued with some polite
preliminaries]
There are two rooms. Each of them is occupied by either a tiger or a cat,
but not both. There is also a sign on the door of each room, and the signs
are either both true or both false. Here are the signs:
Room I Room II
either a tiger is
in this room or a a cat is in the
cat is in the other other room
room
(HKUST) Reasoning 4 / 57
Agents That Reason Dynamically
Stench Breeze
4 PIT
Breeze
Breeze
3 Stench PIT
Gold
Stench Breeze
2
Breeze Breeze
1 PIT
START
1 2 3 4
(HKUST) Reasoning 5 / 57
Wumpus World
Initial state: the agent is in [1,1], facing east (up), and with one
arrow.
Goal: Get the gold and return to [1,1] and climb out of the cave.
Percepts: given by an array of five sensors
[Stench,Breeze,Glitter,Bump,Scream]. Notice that the agent does not
know where she is – the agent was put in a cell, and she call that cell
[1,1].
Actions: grab the gold; turn 90 degree clockwise; turn 90 degree
counter clockwise; move forward to the next cell; shoot the arrow in
the direction that it is facing.
Question: How can the agent figure out that [2,2] are safe to go into?
(HKUST) Reasoning 6 / 57
Agents That Reason about Other Agents Logically
Sarah (the girl) and Ted (the boy) came home from playing outside.
The mother (to both children): At least one of your foreheads is dirty.
The boy: I don’t know if mine is dirty.
The girl: My forehead is dirty.
The boy: My forehead is dirty.
How do they know?
(HKUST) Reasoning 7 / 57
Knowledge-Based Agents
(HKUST) Reasoning 8 / 57
Knowledge Representation and Reasoning
Knowledge representation is about how to represent things that are needed
to solve a problem on a computer:
A KR language has two components: its syntax and semantics.
(Compare it with computer programming language.)
Syntax determines what are legal expressions (sentences) and
semantics tells us the meaning of these legal expressions.
Good knowledge representation languages should be:
I expressive and concise like natural languages
I unambiguous and precise like formal languages.
Logical inference: the process of deriving new sentences from old ones.
(HKUST) Reasoning 9 / 57
Propositional Logic
(HKUST) Reasoning 10 / 57
Language
(HKUST) Reasoning 11 / 57
Language (Continued)
Symbols:
Logical symbols (those whose meanings are fixed by the logic):
parentheses ”(” and ”)”, sentential connectives ¬, ∧, ∨, ⊃, ≡.
Non-logical symbols (those whose meanings depend on
interpretations): proposition symbols p1 , p2 , ....
Formation rules:
a proposition symbol is a sentence
if α and β are sentences, so are (¬α), (α ∧ β), (α ∨ β), (α ⊃ β), and
(α ≡ β).
no other expressions are sentences
Convention: the outermost parentheses can be dropped, and we shall use
the following precedence orders for the connectives: ¬ 7→ ∧, ∨ 7→⊃7→≡.
p ∨ ¬q ≡ q ⊃ r is (p ∨ (¬q)) ≡ (q ⊃ r ).
(HKUST) Reasoning 12 / 57
Example
(HKUST) Reasoning 13 / 57
Semantics - Truth Conditions
To specify semantics of a logic, we need to first assume an interpretation
of the non-logical symbols in the language, and then define the meaning of
logical symbols in terms of this interpretation.
For propositional logic, an interpretation, often called a truth assignment,
is a function from the set of propositional symbols in the language to the
set of truth values {T , F }.
Given such a truth assignment v , we can extend it to the set of sentences
using the following truth table
p q ¬p p ∧ q p ∨ q p ⊃ q p ≡ q
T T F T T T T
T F F F T F F
F T T F T T F
F F T F F T T
(HKUST) Reasoning 14 / 57
Entailment
(HKUST) Reasoning 15 / 57
Example Tautologies
De Morgan’s laws:
¬(p ∧ q) ≡ ¬p ∨ ¬q and ¬(p ∨ q) ≡ ¬p ∧ ¬q
Distributive laws:
p ∧ (q ∨ r ) ≡ (p ∧ q) ∨ (p ∧ r ) and p ∨ (q ∧ r ) ≡ (p ∨ q) ∧ (p ∨ r )
Excluded middle: p ∨ ¬p
Contradiction: ¬(p ∧ ¬p)
Contraposition: p ⊃ q ≡ ¬q ⊃ ¬p
Exportation: p ∧ q ⊃ r ≡ p ⊃ (q ⊃ r )
Tautologies that use ¬ and ∨ to define all other connectives:
p ∧ q ≡ ¬(¬p ∨ ¬q) p ⊃ q ≡ ¬p ∨ q
(p ≡ q) ≡ (p ⊃ q) ∧ (q ⊃ p)
Quiz: Is ((p ⊃ q) ⊃ p) ⊃ q a tautology?
How about ((p ⊃ q) ⊃ p) ⊃ p?
(HKUST) Reasoning 16 / 57
Applications: Query Answering
Suppose: if it is sunny, then you get mail; if it is either rain or sleet, then
you still get mail. It will be either raining or sunny tomorrow, would you
get mail?
Axiomatization:
Propositions: Rain, Sunny , Sleet, Mail.
KB: Rain ∨ Sunny Sunny ⊃ Mail (Rain ∨ Sleet) ⊃ Mail.
Query: Does KB |= Mail?
(HKUST) Reasoning 17 / 57
Applications: Problem Solving by Finding Models
Consider the graph coloring problem: you have three colors, say black,
white, and gray, and you want to assign a color to every node in a graph
such that no adjacent nodes have the same color.
Axiomatization:
Propositions: n(c), for each node n, and each color c, adj(n1 , n2 ), for
every pair of nodes.
Axioms:
I Each node must have exactly one color assigned to it: for each node n,
(HKUST) Reasoning 19 / 57
KB:
We also need to add: each person can have exactly one rank, and no two
people can occupy a same rank.
Any other ways to axiomatize the domain?
(HKUST) Reasoning 20 / 57
Applications: Diagnosis
Example:
tennis-elbow ⊃ sore-elbow
tennis-elbow ⊃ tennis-player
arthritis ∧ untreated ⊃ sore-joint
sore-joint ⊃ sore-elbow ∧ sore-hip
(HKUST) Reasoning 21 / 57
Clausal Representation
(HKUST) Reasoning 22 / 57
CNF and DNF
A set S of clauses is the conjunction of the clauses in S, thus denoting a
sentence in Conjunctive Normal Form (CNF): a Conjunction of
disjunctions of literals.
Every sentence α can be converted into a CNF α0 in such a way that
|= α ≡ α0 :
1 eliminate ⊃ and ≡ using
α ≡ β ⇒ (α ⊃ β) ∧ (β ⊃ α) and α ⊃ β ⇒ ¬α ∨ β
2 push ¬ inside using
¬(α ∧ β) ⇒ ¬α ∨ ¬β and ¬(α ∨ β) ⇒ ¬α ∧ ¬β
3 distribute ∨ over ∧ using
(α ∧ β) ∨ γ ⇒ (α ∨ γ) ∧ (β ∨ γ)
4 simplifying items using
α ∨ α ⇒ α and ¬¬α ⇒ α.
5 eliminate disjunctions that contain both a literal and its compliment.
(HKUST) Reasoning 23 / 57
Example
((p ⊃ q) ⊃ p) ⊃ q ⇒ ¬((p ⊃ q) ⊃ p) ∨ q
⇒ ¬(¬(p ⊃ q) ∨ p) ∨ q
⇒ ¬(¬(¬p ∨ q) ∨ p) ∨ q
⇒ ¬((¬¬p ∧ ¬q) ∨ p) ∨ q
⇒ (¬(¬¬p ∧ ¬q) ∧ ¬p) ∨ q
⇒ ((¬¬¬p ∨ ¬¬q) ∧ ¬p) ∨ q
⇒ (¬¬¬p ∨ ¬¬q ∨ q) ∧ (¬p ∨ q)
⇒ (¬p ∨ q) ∧ (¬p ∨ q)
⇒ clausal form representation:
{¬p ∨ q}
(HKUST) Reasoning 24 / 57
Resolution Rule of Inference
Given two clauses, infer a new clause:
from clauses p ∨ C1 and ¬p ∨ C2
infer clause C1 ∨ C2 . This new clause is called the resolvent of input
clauses with respect to p.
Example: from clauses w ∨ p ∨ q and w ∨ s ∨ ¬p infer w ∨ q ∨ s as
resolvent wrt p.
Special case: p and ¬p resolve to [] (the empty clause, false,
contradiction).
A derivation of a clause c from a set S of clauses is a sequence
c1 , c2 , · · · , cn of clauses, where the last clause cn = c, and for each ci ,
either
1 ci ∈ S or
2 ci is a resolvent of two earlier clauses in the derivation
Write: S → c if there is a derivation of c from S.
(HKUST) Reasoning 25 / 57
Properties of Resolution
(HKUST) Reasoning 26 / 57
Proof By refutation
(HKUST) Reasoning 27 / 57
A Procedure
To determine if KB |= α:
put KB and ¬α into CNF to get S.
check if S → []:
1 check if [] is in S. If yes, then return ”unsatisfiable”
2 check if there are two clauses c1 and c2 in S such that they resolve to
produce a c3 not already in S. If no such c3 can be generated, then
return ”satisfiable”
3 add c3 to S and go back to the first step.
Note: need only convert KB to CNF once:
can handle multiple queries with same KB
after addition of a new fact β, can simply add new clauses from β’s
CNF to KB
(HKUST) Reasoning 28 / 57
An Example
KB:
[Girl]
[]
(HKUST) Reasoning 29 / 57
An Example
[~Sun] [~Rain]
[Rain]
[]
We can also show that KB 6|= Rain by enumerating all possible derivations.
(HKUST) Reasoning 30 / 57
Problems with the propositional agents
(HKUST) Reasoning 31 / 57
First-Order Logic
(HKUST) Reasoning 32 / 57
Syntax - Alphabet
Logical Symbols: fixed meaning and use, like keywords in a programming
language
punctuations and parentheses.
connectives: ¬, ∧, ∨, ⊃, ≡.
quantifiers: ∀, ∃.
equality: =
variables: x, x1 , ..., x 0 , x 00 , ..., y , ..., z, ...
Non-logical symbols: domain-dependent meaning and use, like identifiers
in a programming language
predicate symbols: have arity, i.e. take fixed number of argument.
E.g. Dog is a 1-ary (unary) predicate, Dog (fido) means that Fido is a
dog. Propositional symbols are 0-ary predicates.
function symbols: have arity as well. E.g. plus is a 2-ary (binary)
function, plus(x, y ) means the sum of x and y . Constant symbols like
fido are 0-ary functions.
(HKUST) Reasoning 33 / 57
Syntax - Terms and Sentences
A term is a logical expression that refers to an object:
a constant is a term.
a variable is a term.
if t1 , · · · , tn are terms, and f is an n-ary function symbol, then
f (t1 , ..., tn ) is a term.
Example: fido, fatherOf (fido),fatherOf (fatherOf (fido)).
fatherOf (firstChild(fido, motherOf (dido))).
(HKUST) Reasoning 35 / 57
Example Sentences
Quantifiers
I Nested quantifiers:
F “brothers are also siblings”:
(HKUST) Reasoning 36 / 57
Some Examples
All purple mushrooms are poisonous:
∀x Mushroom(x) ⊃
(Purple(x) ∧ ¬Poisonous(x)) ∨
(¬Purple(x) ∧ Poisonous(x))
(HKUST) Reasoning 37 / 57
Examples
(HKUST) Reasoning 38 / 57
Interpretation
(HKUST) Reasoning 39 / 57
Entailment
(HKUST) Reasoning 40 / 57
Refutation (Proof By Contradiction)
(HKUST) Reasoning 41 / 57
Resolution (First-Order case)
∀x ¬Man(x) ∨ Mortal(x)
(HKUST) Reasoning 42 / 57
Example 2:
then ∀z ¬Man(z) ∨ Die(z) (“every man has to die”). Notice that this
rule can be written as:
¬Man(x) ∨ Mortal(x), ¬Mortal(y ) ∨ Die(y )
Subst({x/z, y /z}, ¬Man(x) ∨ Die(y ))
(HKUST) Reasoning 43 / 57
Resolution (Cont’d)
Resolution (first-order case): if θ is a substitution such that
Subst(θ, r ) = Subst(θ, r 0 ), then
p1 ∨ · · · pi ∨ r ∨ pi+1 · · · ∨ pm , q1 ∨ · · · qj ∨ ¬r 0 ∨ qj+1 · · · ∨ qn
Subst(θ, p1 ∨ · · · ∨ pm ∨ q1 ∨ · · · ∨ qn )
where p1 , .., pm , q1 , ..., qn are literals and r and r 0 are atoms.
Example 1 (Modus Ponens):
¬Man(x) ∨ Mortal(x), Man(socrates)
Mortal(socrates)
because
Subst({x/socrates}, Man(x)) =
Subst({x/socrates}, Man(socrates))
and
Subst({x/socrates}, Mortal(x)) = Mortal(socrates).
(HKUST) Reasoning 44 / 57
Example 2:
because
and
(HKUST) Reasoning 45 / 57
Example Refutation Using Resolution
KB:
∀x GradStudent(x) ⊃ Student(x),
∀x Student(x) ⊃ HardWorker (x),
GradStudent(sue)
Q: HardWorker (sue)?
~Student(x) v HardWorker(x) ~HardWorker(sue) (negation of the query)
x/sue
~GradStudent(x) v Student(x)
~Student(sue)
x/sue
GradStudent(sue) ~GradStudent(sue)
False
(HKUST) Reasoning 46 / 57
Conversion to Clauses
(HKUST) Reasoning 47 / 57
Answer Predicate
Given a KB, to find out an object a that satisfies P(a), we normally
pose the query as ∃x P(x):
KB |= ∃x P(x)?
KB |= ∃x P(x) ∧ ¬A(x)?
(HKUST) Reasoning 48 / 57
The Answer Predicate
Example 1:
KB: {Stu(john), Stu(jane), Happy (john)}
Q: ∃x Stu(x) ∧ Happy (x)
{Stu(john), Stu(jane), Happy (john),
¬Stu(x) ∨ ¬Happy (x) ∨ A(x)}
leads to A(john)
So an answer is: John.
Example 2.
KB: {Stu(john), Stu(jane), Happy (john) ∨ Happy (jane)}
Q: ∃x[Stu(x) ∧ Happy (x)]
{Stu(john), Stu(jane), Happy (john) ∨ Happy (jane),
¬Stu(x) ∨ ¬Happy (x) ∨ A(x)}
leads to A(john) ∨ A(jane)
So an answer is: either John or Jane
(HKUST) Reasoning 49 / 57
Rule-Based Expert Systems
(HKUST) Reasoning 50 / 57
Basic Architecture
Expert User
Knowledge User
acquisition interface
subsystem
Explanation
subsystem
Knowledge base
Inference
facts, heuristics engine
Knowledge engineer
© 1998 Morgan Kaufman Publishers
(HKUST) Reasoning 51 / 57
An Example
Rule-based expert systems are often based on Horn clauses (clauses with
exactly one positive literal). Consider the following loan-approval system:
where OK means the loan should be approved, COLLAT the collateral for
the loan is satisfactory, PYMT the applicant is able to make the loan
payments, REP the applicant has a good financial reputation, APP the
appraisal on the collateral is sufficiently larger than the loan amount,
RATING the applicant has a good credit rating, INC the applicant’s
income exceeds his/her expenses, and BAL the applicant has an excellent
balance sheet.
(HKUST) Reasoning 52 / 57
Rule Learning
(HKUST) Reasoning 53 / 57
Loan-Approval Example
Suppose we have following data from bank record:
Individual APP RATING INC BAL OK
1 1 0 0 1 0
2 0 0 1 0 0
3 1 1 0 1 1
4 0 1 1 1 1
5 0 1 1 0 0
6 1 1 1 0 1
7 1 1 1 1 1
8 1 0 1 0 0
9 1 1 0 0 0
We want to find out rules about when to approve a loan, i.e. learn rules of
the form:
α1 ∧ · · · ∧ αn ⊃ OK
where αi are atoms from the set {APP, RATING , INC , BAL}.
(HKUST) Reasoning 54 / 57
GSCA
Generic Separate-Conquer Algorithm: Given an atom γ that we want to
learn rules about, and Σ, a training set, GSCA works as follows:
1 Initialize Σcur = Σ.
2 Initialize π = empty set of rules.
3 repeat
1 Initialize Γ = true.
2 Initialize τ = Γ ⊃ γ.
3 repeat
1 Select an atom α from feature set. This is a nondeterministic step, and
a backtracking point.
2 Let Γ be Γ ∧ α.
4 until τ covers only (or mainly) positive instances in Σcur .
5 Let π be π, τ (add the newly learned rule).
6 Let Σcur be Σcur - {the positive instances in Σcur covered by π}.
4 until π covers all (or most) of the positive instance in Σ.
(HKUST) Reasoning 55 / 57
A Heuristic
(HKUST) Reasoning 56 / 57
GSCA - The Loan-Approval Example
We began with the rule: true ⊃ OK .
It covers all instances, including all negative ones. So we have to narrow
them by adding a feature into its antecedent. We do that by choosing one
that yields the largest value of rα :
rAPP = 3/6 = 0.5, rRATING = 4/6 = 0.667, rINC = 3/6 = 0.5, rBAL = 3/4 = 0.75.
So we choose BAL, and generate the following rule: BAL ⊃ OK .
This rule still covers the negative instance 1, so we still need to narrow it.
This time we have:
rAPP = 2/3, rRATING = 3/3, rINC = 2/2
so we can choose either RATING or INC . Suppose we choose RATING ,
then this yields the rule:
BAL ∧ RATING ⊃ OK
This rule covers only positive instances, so we have learned one rule. To
learn the next one, we eliminate from the sample those rules already cover
by it, and continue like above, until no more positive instances are left with.
(HKUST) Reasoning 57 / 57