comp5211 lecture 4: knowledge representation and...

83
COMP5211 Lecture 4: Knowledge Representation and Reasoning Fangzhen Lin Department of Computer Science and Engineering Hong Kong University of Science and Technology Fangzhen Lin (HKUST) Lecture 4: KR & R 1 / 83

Upload: others

Post on 13-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

COMP5211 Lecture 4: Knowledge Representation andReasoning

Fangzhen Lin

Department of Computer Science and EngineeringHong Kong University of Science and Technology

Fangzhen Lin (HKUST) Lecture 4: KR & R 1 / 83

Page 2: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

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;}

Fangzhen Lin (HKUST) Lecture 4: KR & R 2 / 83

Page 3: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

ELIZAELIZA (Weizenbaum, 1963), a computer psychotherapist based on patternmatching that fooled a lot of people (from Machines Who Thinks, by P.McCorduck, 1979):

DOCTOR: Good afternoon. Please type in your name.

[The visitor did so, and the doctor continued with some politepreliminaries]

DOCTOR: What brought you here to see me today?

VISITOR: Oh, nothing much. I’, feeling a bit tired, that’s all.

DOCTOR: Why do you think you’re feeling a bit tired?

VISITOR: Well, I’ve been travelling a lot, and away from home.

DOCTOR: Tell me about your family.

[From here, the conversation became intimate...]

Fangzhen Lin (HKUST) Lecture 4: KR & R 3 / 83

Page 4: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Agents That Reason Logically

The lady or the tiger?

There are two rooms. Each of them is occupied by either a tiger or a lady,but not both. There is also a sign on the door of each room, and the signsare either both true or both false. Here are the signs:

Room I Room II

either a tiger isin this room or a a lady is in thelady is in the other other roomroom

What does Room I contain? How about Room II?

Fangzhen Lin (HKUST) Lecture 4: KR & R 4 / 83

Page 5: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Knowledge-Based Agents

Two main components:

Knowledge baseI The collection of known facts about the world.I Each item, called a sentence, expressed in some representation

language as a computer-tractable form.I Should be updated over time.

Reasoning (or inference)I Reasoning about knowledge in the KB to decide the best action to

achieve some given goal.I Search algoritms that we have learnt can be used to carry out the

reasoning

Fangzhen Lin (HKUST) Lecture 4: KR & R 5 / 83

Page 6: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Knowledge Representation and Reasoning

Knowledge representation is about how to represent things that are neededto 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) andsemantics tells us the meaning of these legal expressions.

Good knowledge representation languages should be:I expressive and concise like natural languagesI unambiguous and precise like formal languages.

Logical inference: the process of deriving new sentences from old ones.

Two common kinds of logic:

Propositional logic (also called Boolean logic)

First-order predicate logic (or simply first-order logic)

Fangzhen Lin (HKUST) Lecture 4: KR & R 6 / 83

Page 7: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Propositional Logic

Fangzhen Lin (HKUST) Lecture 4: KR & R 7 / 83

Page 8: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Language

The language (syntax) of a logic describes how to form sentences.

This is like the syntax of a programming language. E.g. thesyntax of C says that ++x is a legal expression, but **x is not.

To define a language, we need to define its available symbols andformation rules. The former are the basic building blocks (tokens). Thelatter describes how to form sentences from these symbols.

Fangzhen Lin (HKUST) Lecture 4: KR & R 8 / 83

Page 9: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Language (Continued)

Symbols:

Logical symbols (those whose meanings are fixed by the logic):parentheses ”(” and ”)”, sentential connectives ¬, ∧, ∨, ⊃, ≡.

Non-logical symbols (those whose meanings depend oninterpretations): proposition symbols p1, p2, ....

Formation rules:

a proposition symbol is a sentence

if α and β are sentences, so are (¬α), (α ∧ β), (α ∨ β), (α ⊃ β), and(α ≡ β).

no other expressions are sentencesConvention: the outermost parentheses can be droped, and we shall usethe following precedence orders for the connectives: ¬ 7→ ∧,∨ 7→⊃7→≡.p ∨ ¬q ≡ q ⊃ r is (p ∨ (¬q)) ≡ (q ⊃ r).

Fangzhen Lin (HKUST) Lecture 4: KR & R 9 / 83

Page 10: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example

The suspect must be released from custody: RThe evidence obtained is admissible: EThe evidence obtained is inadmisible: ¬EThe evidence obtained is admissible, and the suspect need not be releasedfrom custody: E ∧ (¬R)Either the evidence is admissible or the suspect must be released fromcustody

Quiz: E ∨ R or (E ∨ R) ∧ ¬(E ∧ R)?

The evidence is inadmissible, but the suspect need not be released fromcustody

Quiz: How do we translate ”but”?

Fangzhen Lin (HKUST) Lecture 4: KR & R 10 / 83

Page 11: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Semantics - Truth Conditions

To specify semantics of a logic, we need to first assume an interpretationof the non-logical symbols in the language, and then define the meaning oflogical 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 theset of truth values {T ,F}.Given such a truth assignment v , we can extend it to the set of sentencesusing 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

Fangzhen Lin (HKUST) Lecture 4: KR & R 11 / 83

Page 12: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Entailments

We say a truth assignment v satisfies a sentence α iff v(α) = T .We say that a set of sentences Σ (tautologically) implies α, written Σ |= α,iff every truth assignment that satisfies every member of Σ also satisfies α.We say that a sentence α is a tautology (valid), written |= α, iff it isimplied by the empty set of sentences, i.e. ∅ |= α.Tautologies are all we care about because of the following deductiontheorem:Theorem {α1, ..., αn} |= α iff |= α1 ∧ · · · ∧ αn ⊃ α.Sketch of proof. Only if case: Suppose v is a truth assignment. Thereare two cases: (1) v does not satisfies α1 ∧ · · · ∧ αn. In this case, vtrivially satisfies α1 ∧ · · · ∧ αn ⊃ α. (2) v satisfies α1 ∧ · · · ∧ αn. So v alsosatisfies every memebr of {α1, ..., αn}. By the assumption that{α1, ..., αn} |= α, v satisfies α as well.If case: Similar.

Fangzhen Lin (HKUST) Lecture 4: KR & R 12 / 83

Page 13: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Tautologies

De Morgan’s laws:¬(p ∧ q) ≡ ¬p ∨ ¬q and ¬(p ∨ q) ≡ ¬p ∧ ¬qDistributie laws:p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) and p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)Excluded middle: p ∨ ¬p

Contrdication: ¬(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?

Fangzhen Lin (HKUST) Lecture 4: KR & R 13 / 83

Page 14: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Applications: Query Answering

Suppose: if it is sunny, then you get mail; if it is either rain or sleet, thenyou still get mail. It will be either raining or sunny tomorrow, would youget mail?

Axiomatization:

Propositions: Rain, Sunny , Sleet, Mail .

KB: Rain ∨ Sunny Sunny ⊃ Mail (Rain ∨ Sleet) ⊃ Mail .

Query: Does KB |= Mail?

Fangzhen Lin (HKUST) Lecture 4: KR & R 14 / 83

Page 15: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Applications: Problem Solving by Finding ModelsConsider the graph coloring problem: you have three colors, say black,white, and grey, and you want to assign a color to every node in a graphsuch that no adjacent nodes have the same color.Axiomatization:

Propositions: n(c), for each node n, and each color c , adj(n1, n2), forevery pair of nodes.

Axioms:I Each node must have eactly one color assigned to it: for each node n,

[n(b) ∨ n(w) ∨ n(g)] ∧ [n(b) ⊃ (¬n(w) ∧ ¬n(g))] ∧[n(w) ⊃ (¬n(b) ∧ ¬n(g))] ∧ [n(g) ⊃ (¬n(w) ∧ ¬n(b))]

I No adjacent nodes can have the same color:

adj(n1, n2) ⊃ ¬(n1(b)∧ n2(b))∧¬(n1(w)∧ n2(w))∧¬(n1(g)∧ n2(g)).

I A database of adj(n1, n2) facts,

Now every model of the above theory is a coloring of the graph.Fangzhen Lin (HKUST) Lecture 4: KR & R 15 / 83

Page 16: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Applications: Problem Solving by Finding Models

Given the following facts:

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 One of the women (Lisa and Mary) is a biology major

5 One of the women is ranked first

What are possible rankings for the four people?Axiomatization:

Propositions: n(l , b), n(l , j), n(l ,m), ..., bm(l), bm(m), bm(b),bm(j).

Fangzhen Lin (HKUST) Lecture 4: KR & R 16 / 83

Page 17: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

KB:

¬n(l , b) ∧ ¬n(b, l),

n(j , l) ∨ n(j , b) ∨ n(j ,m) ∧n(j , l) ⊃ bm(l) ∧n(j , b) ⊃ bm(b) ∧n(j ,m) ⊃ bm(m),

n(b, j),

bm(l) ∨ bm(m),

(¬n(b, l) ∧ ¬n(m, l) ∧ ¬n(j , l)) ∨ (¬n(b,m) ∧ ¬n(l ,m) ∧ ¬n(j ,m))

We also need to add: each person can have exactly one rank, and no twopeople can occupy a same rank.Any other ways to axiomatize the domain?

Fangzhen Lin (HKUST) Lecture 4: KR & R 17 / 83

Page 18: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Applications: Diagnosis

Example:

tennis-elbow ⊃ sore-elbow

tennis-elbow ⊃ tennis-player

arthritis ∧ untreated ⊃ sore-joint

sore-joint ⊃ sore-elbow ∧ sore-hip

Explain: sore-elbow . Possible explanations: tennis-elbow ,arthritis ∧ untreated .Here, KB 6|= sore-elbow , but KB ∪ {tennis-elbow} |= sore-elbow .Generally, an abduction of α under KB is a formula β such thatKB ∪{β} |= α. This type of reasoning is wide-spread in many applications.

Fangzhen Lin (HKUST) Lecture 4: KR & R 18 / 83

Page 19: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Clausal Representation

Literal = propositional symbol (positive literal) or its negation (negativeliteral)Notation: If l is a literal, then ∼ l is its complement:

p ⇒ ¬p ¬p ⇒ pE.g. p is a positive literal, ¬p a negative literal. ∼ p is ¬p, and ∼ ¬p is p.Clause = set of literals, understood as disjunction of the literals in the set.Notation: a clause will be written using square brackets. E.g. [p,¬q,¬r ] isa clause, and stands for the sentence: p ∨ ¬q ∨ ¬r .Clausal Form = set of clauses, understood as conjunction of the clauses inthe set.Notation: a clausal form will be written using ”{” and ”}”, to distinguishit from a clause. E.g. {[p,¬q], [¬p,¬q, r ], [r ]} is a clausal form, andstands for(p ∨ ¬q) ∧ (¬p ∨ ¬q ∨ r) ∧ r .

Fangzhen Lin (HKUST) Lecture 4: KR & R 19 / 83

Page 20: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

CNF and DNF

A clausal form actually denotes a sentence in Conjunctive Normal Form(CNF): a Conjunction of disjunctions of literals.Every sentence α can be converted into a CNF α′ in such a way that|= α ≡ α′:

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.

Fangzhen Lin (HKUST) Lecture 4: KR & R 20 / 83

Page 21: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

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]}

Fangzhen Lin (HKUST) Lecture 4: KR & R 21 / 83

Page 22: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

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 inputclauses with respect to p.Example: from clauses [w , p, q] and [w , s,¬p] infer [w , q, s] as resolventwrt p.Special case: [p] and [¬p] resolve to []A derivation of a clause c from a set S of clauses is a sequencec1, 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.

Fangzhen Lin (HKUST) Lecture 4: KR & R 22 / 83

Page 23: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Propeties of Resolution

Resolvent is entailed by input clauses:

{(p ∨ α), (¬p ∨ β)} |= α ∨ β

Proof: Let v be a truth assignment, annd v satisfies p ∨ α and ¬p ∨ β.There are two cases:

Case 1: v satisfies p. In this case, v satisfies β, so α ∨ β as well.

Case 2: v does not satisfies p. In this case, v satisfies α, so α ∨ β aswell.

Either way, v is a model of α ∨ β.This can be extended to derivations: If S → c then S |= c . (Proof: Byinduction on the length of the derivation.)Special case: If S → [], then S |= FALSE , i.e. S is unsatisfiable, that is,there is no truth assignment that satisfies every member of S.

Fangzhen Lin (HKUST) Lecture 4: KR & R 23 / 83

Page 24: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Proof By refutation

So resolution rule of inference is sound. However, it is not complete: canhave S |= c without having S → c

{[¬p]} |= [¬p, q] ({¬p} |= ¬p ∨ q), but there is no derivation usingresolution.However, resolution is sound and complete for []:

Theorem S is unsatisfiable iff S → []This completeness is also called refutation complete, and is all we need:

Σ |= α iff Σ ∪ {¬α} is unsatisfiable iff the CNF S of Σ ∪ {¬α} isunsatisfiable iff we can derive [] from S.

Proving Σ |= α by checking the satisfiability of Σ ∪ {α} is often calledproof by refutation.

Fangzhen Lin (HKUST) Lecture 4: KR & R 24 / 83

Page 25: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

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, thenreturn ”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 β’sCNF to KB

Fangzhen Lin (HKUST) Lecture 4: KR & R 25 / 83

Page 26: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

An Example

KB:

FirstGrade,FirstGrade ⊃ Child ,Child ∧Male ⊃ Boy ,

Kindergarden ⊃ Child ,Child ∧ Female ⊃ Girl ,Female.

Show that KB |= Girl

[FirstGrade]

[~FirstGrade, Child]

[~Child, ~Male, Boy]

[~Kindergarden, Child]

[~Child, ~Female, Girl]

[Female]

[~Girl] (negation of query)

[Child]

[~Female, Girl]

[Girl]

[ ]

Fangzhen Lin (HKUST) Lecture 4: KR & R 26 / 83

Page 27: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

An Example

KB: Rain ∨ Sun Sun ⊃ Mail (Rain ∨ Sleet) ⊃ Mail

Show KB |= Mail

[Rain, Sun] [~Sun, Mail] [~Rain, Mail] [~Sleet, Mail] [~Mail] (negation of goal)

[~Sun]

[Rain]

[~Rain]

[ ]

We can also show that KB 6|= Rain by enumerating all possible derivations.

Fangzhen Lin (HKUST) Lecture 4: KR & R 27 / 83

Page 28: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Satisfiability

A theory T is satisfiable if there is a truth assignment that satisfiesevery sentence in T .

If S is a CNF of T , then T is satisfiable iff S is satisfiable.

SAT refers to the problem of deciding if a set of clauses is satisfiable.It was the first NP-complete problem discovered by Cook, it is alsoone of the most intensely studied NP-complete problem.

There is a huge literature on SAT. SAT algorithms have manyapplications.

3SAT referes to the problem of deciding if a set of clauses that haveno more than 3 literals is satisfiable.

In terms of computational complexity, SAT is equivalent to 3SAT.

A procedure for SAT is sound if whenever it returns yes, the input isindeed satisfiable; it is complete if it will return yes whenever theinput is satisfiable. Obviously, we want only sound procedures. Butincomplete ones can sometimes be very useful.

Fangzhen Lin (HKUST) Lecture 4: KR & R 28 / 83

Page 29: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

DPLL Procedure

The most popular and widely studied compete method for SAT is the socalled Davis-Putnam-Longemann-Loveland (DPLL) procedure.Let α be a set of clauses. If l is a literal occurring in α, then by α(l), wemean the result of letting l true and simplifying: if a clause C does notmention l and ¬l , then do nothing; if C mention l , then eliminate it; if Cmention ¬l , then delete ¬l from C .

Procedure DPLL(CNF: α)

if α is empty, then return yes.else if there is an empty clause in α, return no.else if there is a pure literal l in α, then return DPLL(α(l)).else if there is a unit clause {l} in α, then return DPLL(α(l)).else select a variable p in α, and do the following

if DPLL(α(p)) return yes, then return yes.else return DPLL(α(¬p)).

Where a literal in α is pure if it occurrs only positively or negatively, and aunit clause is one whose length is 1.

Fangzhen Lin (HKUST) Lecture 4: KR & R 29 / 83

Page 30: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

GSAT

Incomplete methods cannot be used to check unsatisfiable CNFs. But onsatisfiable CNFs, the currently known best incomplete methods are oftenmuch faster than the currently known best complete methods.The following randomized local search algorithm, often called GSAT, istypical of current best known incomplete methods:

Procedure GSAT(CNF: α, max-restart: mr, max-climb: mc)

for i = 1 to mr doget a randomly generated truth assignment v .for j = 1 to mc do

if v satisfies α, return yeselse let v be a random choice of one the best successors of v

return failure

where a successor of v is an assignment with the truth value of one of thevariables flipped, and a best successor is one that satisfies maximumnumber of clauses in α.

Fangzhen Lin (HKUST) Lecture 4: KR & R 30 / 83

Page 31: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Wumpus World

A typical state in the Wumpus World:

Breeze Breeze

Breeze

BreezeBreeze

Stench

Stench

BreezePIT

PIT

PIT

1 2 3 4

1

2

3

4

START

Gold

Stench

Fangzhen Lin (HKUST) Lecture 4: KR & R 31 / 83

Page 32: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Wumpus World

Initial state: the agent is in [1,1], facing east (up), and with onearrow.

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 notknow 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 degreecounter clockwise; move forward to the next cell; shoot the arrow inthe direction that it is facing.

Fangzhen Lin (HKUST) Lecture 4: KR & R 32 / 83

Page 33: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Acting in the Wumpus World

Conceptulization: let Sij mean that there is a stench in [i,j], Aij meanthat the agent is in [i,j], Bij mean that there is a breeze in [i,j],...

Axiomatize how the agent should (should not) act based on what shebelieves:

A11 ∧ EastA ∧W21 → ¬Forward ,

A11 ∧ G11 → Grab,

· · ·

To decide what to do based on these rules, the agent needs to findout whether, for example, W21 is true or not according to herknowledge about the environment and her percepts.

Fangzhen Lin (HKUST) Lecture 4: KR & R 33 / 83

Page 34: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Reasoning in the Wumpus World

Assume that the agent is in the following state, after

(turn-right, forward, turn-left, turn-left, forward, turn-right, forward)

BG

PS

W

A = Agent = Breeze = Glitter, Gold

= Pit = Stench

= Wumpus

OK = Safe square

V = Visited

B P!

A

OK OK

OK

1,1 2,1 3,1 4,1

1,2 2,2 3,2 4,2

1,3 2,3 3,3 4,3

1,4 2,4 3,4 4,4

V

SOK

W!

V

Assume that the current percept is [Stench,None,None,None,None]. Weshall show that at this point. the agent knows that the wumpus is in cell[1,3], so should not heading into that direction.

Fangzhen Lin (HKUST) Lecture 4: KR & R 34 / 83

Page 35: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Reasoning in the Wumpus World (Cont’d)Knowledge base:

¬S11, ¬B11

¬S21, B21

S12, ¬B12

R1 ¬S11 ⊃ ¬W11 ∧ ¬W12 ∧ ¬W21,

R2 ¬S21 ⊃ ¬W11 ∧ ¬W21 ∧ ¬W22 ∧ ¬W31,

R3 ¬S12 ⊃ ¬W11 ∧ ¬W12 ∧ ¬W22 ∧ ¬W13,

R4 S12 ⊃ W13 ∨W12 ∨W22 ∨W11

Finding the wumpus:

1 Modus Ponens on ¬S11 and R1, and then AND-Elimination.

2 Modus Ponens on ¬S21 and R2, and then AND-Elimination.

3 Modus Ponens on S12 and R4.

4 Unit resulotion on the resulting formulas.

Fangzhen Lin (HKUST) Lecture 4: KR & R 35 / 83

Page 36: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Problems with the propositional agent

Problem: Too many propositions, and too many rules!

Solution: onto first-order logic.

Fangzhen Lin (HKUST) Lecture 4: KR & R 36 / 83

Page 37: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

First-Order Logic

Propositional logic is not expressive and concise enoguh – the world isa set of facts according to it.

First-order logic (FOL) is more expressive than propositional logic.

According to FOL, the world consists of objects. The facts in theworld are simply properties about or relations among these objects.

First-order logic is universal - whatever can be said can be writtendown in FOL, any fact can be thought of as referring to objects andproperties or relations:

I “one plus two equals three” – objects: one two, three, one plus two;relation: equal.

I “squares neighboring the wumpus are smelly” – objects: wumpus,squares; property: smelly; relation: neighboring.

I “evil King John ruled England in 1200”.

Fangzhen Lin (HKUST) Lecture 4: KR & R 37 / 83

Page 38: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Syntax - AlphabetLogical Symbols: fixed meaning and use, like keywords in a programminglanguage

punctuations and parentheses.

connectives: ¬, ∧, ∨, ⊃, ≡.

quantifiers: ∀, ∃.equality: =

variables: x , x1, ..., x ′, x ′′, ..., y , ..., z , ...

Non-logical symbols: domain-dependent meaning and use, like identifiersin 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 adog. 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 likefido are 0-ary functions.

Fangzhen Lin (HKUST) Lecture 4: KR & R 38 / 83

Page 39: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Syntax - Terms and SentencesA 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, thenf (t1, ..., tn) is a term.

Example: fido, fatherOf (fido),fatherOf (fatherOf (fido)).fatherOf (firstChild(fido,motherOf (dido))).

A sentence is a logical expression that represents a fact:

if t1, · · · , tn are terms, and P is an n-ary predicate (relation) symbol,then P(t1, ..., tn) is a sentece (atomic sentence).

if t1 and t2 are terms, then t1 = t2 is a sentence (atomic sentence).

if α and β are sentences, then ¬α, (α ∧ β), (α ∨ β), (α ⊃ β),(α ≡ β) are also sentences.

if α is a sentence, and v is a variable, then (∀v α), (∃v α) are alsosentences.

Fangzhen Lin (HKUST) Lecture 4: KR & R 39 / 83

Page 40: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Sentences

Atomic sentences: simple facts about the world. Examples:Brother(richard , john),fatherOf (richard) = fatherOf (john),Student(firstChild(john,mary), ust).

Quantifiers:I Universal quantification (∀):

F General rules (for all objects in universe)F Example: ”COMP101 students are UST students.”∀x Enrolled(x , comp101) ⊃ Student(x , hkust)

I Existential quantification (∃):F For one or more objects in universeF Example: ”not all UST students enroll COMP101”∃x Student(x , hkust) ∧ ¬Enrolled(x , comp101)

Fangzhen Lin (HKUST) Lecture 4: KR & R 40 / 83

Page 41: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Sentences

QuantifiersI Nested quantifiers:

F “brothers are also siblings”:

∀x , y Brother(x , y) ⊃ Sibling(x , y)

Here ∀x , y is a shorthand for ∀x∀y .F “everybody loves somebody”: ∀x∃y Loves(x , y).F “a mother is someone who is a female and has a child”:

∀x [Mother(x) ≡ Female(x) ∧ ∃y Child(x , y)]

I Relationships between ∀ and ∃:F ¬∃x P ≡ ∀x ¬P ∃x ¬P ≡ ¬∀x PF ∀x P ≡ ¬∃x ¬P ∃x P ≡ ¬∀x ¬P

Fangzhen Lin (HKUST) Lecture 4: KR & R 41 / 83

Page 42: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Some Examples

All purple mushrooms are poisonous:

∀x Mushroom(x) ∧ Purple(x) ⊃ Poisonous(x)

No purple mushroom is poisonous:

∀x Mushroom(x) ∧ Purple(x) ⊃ ¬Poisonous(x)

All mushrooms are either purple or poisonous:

∀x Mushroom(x) ⊃ Purple(x) ∨ Poisonous(x)

All mushrooms are either purple or poisonous but not both:

∀x Mushroom(x) ⊃(Purple(x) ∧ ¬Poisonous(x)) ∨(¬Purple(x) ∧ Poisonous(x))

Fangzhen Lin (HKUST) Lecture 4: KR & R 42 / 83

Page 43: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Examples

All purple mushrooms except one are poisonous:

∃x Purple(x) ∧Mushroom(x) ∧ ¬Poisonous(x) ∧(∀y Purple(y) ∧Mushroom(y) ∧ ¬(x = y) ⊃Poisonous(y))

There are exactly two purple mushrooms:

∃x , y Mushroom(x) ∧ Purple(x) ∧Mushroom(y) ∧ Purple(y) ∧ ¬(x = y) ∧(∀z Mushroom(z) ∧ Purple(z) ⊃ (z = x) ∨ (z = y))

Fangzhen Lin (HKUST) Lecture 4: KR & R 43 / 83

Page 44: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Interpretation

An interpretation for FOL settles:

what objects there are

which of them satisfy a predicate P

what mapping is denoted by a function f

Given this, it will be possible to say which sentences of FOL are true andwhcih are not.Formally, an interpretation I is a pair < D,Φ > where

D is the domain of discourse, and can be any set

Φ is an interpretation mapping:I if P is a predicate symbol of arity n, then Φ(P) is an n-ary relation

over D: Φ(P) ⊆ D × · · · × DI if f is a function symbol of arity n, then Φ(f ) is an n-ary function over

D: Φ(f ) ∈ [D × · · · × D → D]

Fangzhen Lin (HKUST) Lecture 4: KR & R 44 / 83

Page 45: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

ExampleAssume we have: a binary predicate Less, a unary function succ , and aconstant zero.Let the interpretation I =< D,Φ > be:

D is the set of natural numbers.

Φ(zero) = 0.

Φ(succ)(n) = n + 1 for any number n ∈ D.

Φ(Less) is the less than relation over D: for any < m, n >, it is in therelation iff m < n.

Under this interpretation:

the constant zero denotes the number 0.

the term succ(zero) denotes the number 0 + 1 (= 1).

the term succ(succ(zero)) denotes the number (0 + 1) + 1.

The atomic sentence Less(zero, succ(zero)) represents the fact“0 < (0 + 1)” which is true.

Fangzhen Lin (HKUST) Lecture 4: KR & R 45 / 83

Page 46: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The sentence

Less(zero, succ(zero)) ∧ Less(succ(zero), succ(zero))

is false.

The sentence ∀x Less(zero, succ(x)) represents the fact“0 < (x + 1)” for all x in the domain D, which is true.

Fangzhen Lin (HKUST) Lecture 4: KR & R 46 / 83

Page 47: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Entailment

An interpretation I is a model of a sentence α (α is satisfied in I ) if αis true in I .

A set of sentences Σ logically entails a sentence α if for anyinterpretation I , whenever I is a model for all sentences in Σ, then itis a model of α.

α is valid if it is entailed by the empty set, that is, if it is true in allinterpretations.

Which of the following sentences are valid?

Less(zero, zero) ∨ ¬Less(zero, zero).

Less(zero, succ(zero)).

¬Less(zero, succ(zero)).

[∀x Less(zero, x)] ⊃ Less(zero, zero).

Fangzhen Lin (HKUST) Lecture 4: KR & R 47 / 83

Page 48: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Denotation

Materials in this and the next slide are for your reference only.Under an interpretation I = (D,Φ), terms denote elements of D. Forterms with variables, denotation depends on the value of variables. Let sbe a mapping from the set of variables to D. Such a mapping is called avariable assignment.The denotation of a term t in interpretation I and under variableassignment s, written t[I , s], is defined as follows:

if v is a variable, then t[I , s] = s(v).

recursively, if f is a n-ary function, and t1, ..., tn are terms, thenf (t1, ..., tn)[I , s] = Φ(f )(d1, ..., dn), where di = ti [I , s] for i = 1, ..., n.Notice that if f is a constant symbol, then f [I , s] = Φ(f ).

Fangzhen Lin (HKUST) Lecture 4: KR & R 48 / 83

Page 49: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Satisfaction

In terms of I , wffs will be true for some values of the free variables andfalse for others. We write I , s |= α to mean that α is true in interpretationI under variable assignment s:

I , s |= P(t1, ..., tn) iff < d1, ..., dn >∈ Φ(P), where di = ti [I , s] fori = 1, ..., n

I , s |= t1 = t2 iff t1[I , s] is the same as t2[I , s]

I , s |= ¬α iff I , s 6|= α

I , s |= α ∧ β iff I , s |= α and I , s |= β

I , s |= α ∨ β iff I , s |= α or I , s |= β

I , s |= ∃v α iff for some d ∈ D, I , s(v/d) |= α

I , s |= ∀v α iff for all d ∈ D, I , s(v/d) |= α

where s ′ = s(v/d) is a variable assigment just like s, except on v , wheres ′(v) = d .

Fangzhen Lin (HKUST) Lecture 4: KR & R 49 / 83

Page 50: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Reasoning By Inference Rules

An inference rule is an expression of the form:

α1, ..., αn

β

which says that one can infer β given α1, ..., αn. α1, · · · , αn are calledthe premises and β the conclusion of the inference rule.

Given a set of inference rules, a proof of α from Σ is a sequence ofsentences such that the last sentence is α, and each sentence in thesequence is either a premise in Σ or the conclusion of a rule whosepremises have already appeared earlier in the sequence.

Fangzhen Lin (HKUST) Lecture 4: KR & R 50 / 83

Page 51: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Inference Rules

Modus ponens (MP):α ⊃ β, α

β

Universal elimination (UE):

∀v α

Subst({v/g}, α)

where v is a variable and g is a ground term (i.e. a term containingno variables). Substitution Subst(θ, α) is defined as follows:

I Applying substitution (or binding list) θ to sentence α.I Example:

Subst({x/Tom, y/Eva}, Likes(x , y)) = Likes(Tom,Eva)

Fangzhen Lin (HKUST) Lecture 4: KR & R 51 / 83

Page 52: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Proof

KB:∀x Man(x) ⊃ Mortal(x),

Man(socrates)

Query: Mortal(socrates).

Proof:

(1) ∀x Man(x) ⊃ Mortal(x) {premise}(2) Man(socrates) ⊃ Mortal(socrates) {(1), UE}(3) Man(socrates) {premise}(4) Mortal(socrates) {(2), (3), MP}

Fangzhen Lin (HKUST) Lecture 4: KR & R 52 / 83

Page 53: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Refutation (Proof By Contradiction)

A KB is contradictory if there is a sentence α such that bothKB |= α and KB |= ¬α.

A trivial contradictory KB: {Man(socrates),¬Man(socrates)}.Finding out if a KB is contradictory is as hard as doing otherreasoning tasks such as entailments.

Refutation (proof by contradiction): to show that KB |= α, add ¬αto KB and show that the new knowledge base, KB ∪ {¬α} iscontradictory.

We do this all the time: If the driver had fastened his seat belt, hewouldn’t have been thrown out of the car; since he was thrown out ofthe car, he must have not fastened his seat belt.

Fangzhen Lin (HKUST) Lecture 4: KR & R 53 / 83

Page 54: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Resolution (First-Order case)

Example 1 (Modus Ponens):

¬Man(x) ∨Mortal(x), Man(socrates)

Mortal(socrates)

I The variables in the rule are implicitly universally quatified.I The first premise really is

∀x ¬Man(x) ∨Mortal(x)

which is equivalent to ∀x Man(x) ⊃ Mortal(x).I This rule is the same as:

¬Man(x) ∨Mortal(x), Man(socrates)

Subst({x/socrates},Mortal(x))

Fangzhen Lin (HKUST) Lecture 4: KR & R 54 / 83

Page 55: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example 2:

¬Man(x) ∨Mortal(x), ¬Mortal(y) ∨ Die(y)

¬Man(z) ∨ Die(z)

This rules says: if

∀x ¬Man(x) ∨Mortal(x), “every man is mortal”

∀y¬Mortal(y) ∨ Die(y), “every mortal thing has to die”

then ∀z ¬Man(z)∨Die(z) (“every man has to die”). Notice that thisrule can be written as:

¬Man(x) ∨Mortal(x), ¬Mortal(y) ∨ Die(y)

Subst({x/z , y/z},¬Man(x) ∨ Die(y))

Fangzhen Lin (HKUST) Lecture 4: KR & R 55 / 83

Page 56: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Resolution (Cont’d)

Resolution (first-order case): if θ is a substitution such thatSubst(θ, r) = Subst(θ, r ′), then

p1 ∨ · · · pi ∨ r ∨ pi+1 · · · ∨ pm, q1 ∨ · · · qj ∨ ¬r ′ ∨ qj+1 · · · ∨ qn

Subst(θ, p1 ∨ · · · ∨ pm ∨ q1 ∨ · · · ∨ qn)

where p1, .., pm, q1, ..., qn are literals and r and r ′ 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).

Fangzhen Lin (HKUST) Lecture 4: KR & R 56 / 83

Page 57: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example 2:

¬Man(x) ∨Mortal(x), ¬Mortal(y) ∨ Die(y)

¬Man(z) ∨ Die(z)

because

Subst({x/z , y/z},Mortal(x)) =

Subst({x/z , y/z},Mortal(y))

and

Subst({x/z , y/z},¬Man(x) ∨ Die(y)) = ¬Man(z) ∨ Die(z).

Fangzhen Lin (HKUST) Lecture 4: KR & R 57 / 83

Page 58: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Example Refutation Using Resolution

KB:

∀x GradStudent(x) ⊃ Student(x),

∀x Student(x) ⊃ HardWorker(x),

GradStudent(sue)

Q: HardWorker(sue)?

GradStudent(sue)

x/sue~Student(sue)

~GradStudent(sue)

~HardWorker(sue) (negation of the query)x/sue

False

~Student(x) v HardWorker(x)

~GradStudent(x) v Student(x)

Fangzhen Lin (HKUST) Lecture 4: KR & R 58 / 83

Page 59: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Conjunctive Normal Form (CNF)

To use resolution, sentences need to be in the form of disjunctions ofliterals, which are normally called clauses.

A conjunctive normal form (CNF) is a conjunction of clauses, i.e. hasthe following form:

(p11 ∨ · · · ∨ p1k) ∧(p21 ∨ · · · ∨ p2m) ∧· · · ∧(pi1 ∨ · · · ∨ pin)

where all the pxy ’s are literals.

Just as we normally take a KB to be a set of sentences, we oftenwrite a CNF as a set of clauses:

{p11 ∨ · · · ∨ p1k , p21 ∨ · · · ∨ p2m, · · · , pi1 ∨ · · · ∨ pin}

Fangzhen Lin (HKUST) Lecture 4: KR & R 59 / 83

Page 60: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Thoerem proving in first-order logic using resolution

To prove Σ |= α, transform Σ and ¬α into a set S ofclauses, and try to generate the empty clause False from Susing resolution.

We can always transform a sentence into a CNF (a set of clauses).

The above procedure is sound and complete.

Fangzhen Lin (HKUST) Lecture 4: KR & R 60 / 83

Page 61: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Conversion to CNFGiven a sentence, follow the following steps to transform it into a CNF:

1 Eliminate implication and equivalence:

α ⊃ β 7→ ¬α ∨ β

α ≡ β 7→ (α ⊃ β) ∧ (β ⊃ α)

2 move ¬ inwards:

¬(α ∨ β) 7→ ¬α ∧ ¬β, ¬(α ∧ β) 7→ ¬α ∨ ¬β¬¬α 7→ α ¬∀x α 7→ ∃x ¬α, ¬∃x α 7→ ∀x ¬α.

3 standardize variables: for sentences like ∀xP(x) ∨ ∃xQ(x) that usethe same variable twice, change the name of one of the variables, e.g.∀xP(x) ∨ ∃yQ(y). So each quantifier gets a unique variable name.This avoids confusion later when we drop the quantifiers.

4 Skolemize: Skolemization is the process of removing existentialquantifiers by Skolem functions. See the next slide.

5 move quantifiers to the front: using rules ∀xα ∧ β 7→ ∀x(α ∧ β)and ∀xα ∨ β 7→ ∀x(α ∨ β), where β does not use x .

Fangzhen Lin (HKUST) Lecture 4: KR & R 61 / 83

Page 62: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Conversion to CNF (Cont’d)

6. drop quantifiers: so now variables are implicitly universallyquantified.

7. distribute ∨ over ∧:

(α ∧ β) ∨ γ 7→ (α ∨ γ) ∧ (β ∨ γ).

8. flatten nested conjunctions and disjunctions:

(α ∧ β) ∧ γ 7→ α ∧ β ∧ γ.

(α ∨ β) ∨ γ 7→ α ∨ β ∨ γ.

9. Remove conjunctions to get a set of clauses.

Fangzhen Lin (HKUST) Lecture 4: KR & R 62 / 83

Page 63: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

SkolemizationA sentence such as ∃x∀y∃zP(x , y , z) says: ”there is an x such that for ally there is a z such that P(x,y,z) holds”.Suppose we use the constant a to name the x , then the sentence becomes∀y∃zP(a, y , z). We can similarly eliminate ∃z but notice that the existenceof such z is depended on the y : for different y , we may have a different z .So we use a function, called Skolem function: ∀yP(a, y , sk(y)).In general:

∀x1(...∀x2(...∀xn(...∃y [...y ...]...)...)...)

is replaced by

∀x1(...∀x2(...∀xn(...[...f (x1, x2, ..., xn)...]...)...)...)

where f is a new function symbol that appears nowhere else.Skolemization does not preserve equivalence (all of our other rules intransforming a sentence to its CNF do): 6|= ∃xP(x) ≡ P(a). But it doespreserve satisfiability: α is satisfiable iff α′ is, where α′ is the result ofskolemization.This is sufficient for resolution!

Fangzhen Lin (HKUST) Lecture 4: KR & R 63 / 83

Page 64: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Examples

∀x [∃y(Dog(y) ∧ Owns(x , y)) ⊃ AnimalLover(x)] 7→ (eliminate implication)

∀x [¬∃y(Dog(y) ∧ Owns(x , y)) ∨ AnimalLover(x)] 7→ (move ¬ inside)

∀x [∀y¬(Dog(y) ∧ Owns(x , y)) ∨ AnimalLover(x)] 7→ (move ¬ inside)

∀x [∀y(¬Dog(y) ∨ ¬Owns(x , y)) ∨ AnimalLover(x)] 7→ (move quantifiers left)

∀x∀y [¬Dog(y) ∨ ¬Owns(x , y) ∨ AnimalLover(x)] 7→ (drop quantifiers)

¬Dog(y) ∨ ¬Owns(x , y) ∨ AnimalLover(x)

∀x∃yLess(x , y) ∧ ∀x∃y(double(x) = y) 7→ (standardize variables)

∀x∃yLess(x , y) ∧ ∀x1∃y1(double(x1) = y1) 7→ (skolemize)

∀xLess(x , sk1(x)) ∧ ∀x1(double(x1) = sk2(x1)) 7→ (move quantifiers left)

∀x∀x1[Less(x , sk1(x)) ∧ double(x1) = sk2(x1)] 7→ (drop quantifiers)

Less(x , sk1(x)) ∧ double(x1) = sk2(x1)

Fangzhen Lin (HKUST) Lecture 4: KR & R 64 / 83

Page 65: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Answer Predicate

Given a KB, to find out an object a that satisfies P(a), we normallypose the query as ∃x P(x):

KB |= ∃x P(x)?

Using resolution, this means that we want to derive False fromKB ∪ {¬P(x)}.This strategy can be improved by introducing a new predicate A (theanswer predicate). Instead of the above query, we ask:

KB |= ∃x P(x) ∧ ¬A(x)?

Instead of deriving False, we want to derive fromKB ∪ {¬P(x) ∨ A(x)} a clause that mentions only A, and then“read” the answer out of it.

Fangzhen Lin (HKUST) Lecture 4: KR & R 65 / 83

Page 66: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The Answer PredicateExample 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 JaneFangzhen Lin (HKUST) Lecture 4: KR & R 66 / 83

Page 67: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Knowledge Engineering

Baisc ideas:

1 Write down the knowledge required for solving the problem.

2 Pose the problem as a query and try to infer it from the knowledgebase (KB) that you constructed in step 1.

Fangzhen Lin (HKUST) Lecture 4: KR & R 67 / 83

Page 68: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

An ExampleExample: the wumpus world.

KB:

A11 ∧ EastA ∧W21 ⊃ ¬Forward ,

A11 ∧ G11 ⊃ Grab, ...

¬S11,¬S21,S12,

¬S11 ⊃ ¬W11 ∧ ¬W12 ∧ ¬W21,

¬S21 ⊃ ¬W11 ∧ ¬W21 ∧ ¬W22 ∧ ¬W31,

¬S12 ⊃ ¬W11 ∧ ¬W12 ∧ ¬W22 ∧ ¬W13,

S12 ⊃ W13 ∨W12 ∨W22 ∨W11, ...

Queries:

Shall I go forward: KB |= Forward?

Shall I avoid going forward: KB |= ¬Forward?

Shall I grab the gold: KB |= Grab?

Fangzhen Lin (HKUST) Lecture 4: KR & R 68 / 83

Page 69: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Knowledge Engineering - Five Steps

Must have enough knowledge about the domain in question.

Must have enough knowledge about the representation language.

Must have enough knowledge about the inference procedure to ensurethat queries can be answered in a reasonable amount of time.

Five steps in successful knowledge engineering:

decide what to talk about - what are the relevant objects and facts.

decide on a vocabulary - constants, functions, predicates.

encode general knowledge about the domain.

encode knowledge that is specific to the problem in hand.

pose queries to the inference procedure and get answers.

Fangzhen Lin (HKUST) Lecture 4: KR & R 69 / 83

Page 70: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The Queens Problem

Problem:

Arrange 2 queens on a 3x3 board so that none of them isattacked by others.

What to talk about: queens, squares.

Vocabularies:I Constants for queens, rows, and columns: q1, q2, 1, 2, 3.I Ternary predicate Q(n, i , j) - “Queen n is in square (i , j)”.I Predicate Diag(i , j , i ′, j ′) - “the square (i , j) is diagonal to the square

(i ′, j ′).

Fangzhen Lin (HKUST) Lecture 4: KR & R 70 / 83

Page 71: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The Queens Problem (Cont’d)General laws:

“Each queen has to be in one of the squares”:

∀n∃i , j Q(n, i , j).

“Each queen can only occupy one square”:

∀n, i , j , i ′, j ′ Q(n, i , j) ∧ Q(n, i ′, j ′) ⊃ i = i ′ ∧ j = j ′.

“No two queens can be in the same column”:

∀n, n′, i , j , i ′ Q(n, i , j) ∧ Q(n′, i ′, j) ⊃ n = n′.

“No two queens can be in the same row”:

∀n, n′, i , j , j ′ Q(n, i , j) ∧ Q(n′, i , j ′) ⊃ n = n′.

“No two queens can be in the same diagonal”:

∀n, n′, i , j , j ′ Q(n, i , j) ∧ Q(n′, i ′, j ′) ∧ Diag(i , j , i ′, j ′) ⊃n = n′.

Fangzhen Lin (HKUST) Lecture 4: KR & R 71 / 83

Page 72: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The Queens Problem (Cont’d)Problem specific knowledge:

We have two different queens:

¬q1 = q2.

There are just 2 queens:

∀n, i , j Q(n, i , j) ⊃ n = q1 ∨ n = q2.

We have three different rows and columns:

¬1 = 2 ∧ ¬2 = 3 ∧ ¬1 = 3

There are three rows:

∀n, i , j Q(n, i , j) ⊃ i = 1 ∨ i = 2 ∨ i = 3.

There are three columns:

∀n, i , j Q(n, i , j) ⊃ j = 1 ∨ j = 2 ∨ j = 3.

Facts about Diag :

Diag(1, 1, 2, 2), Diag(1, 1, 3, 3), Diag(1, 2, 2, 1), ...

Denote by KB the above set of axioms.Fangzhen Lin (HKUST) Lecture 4: KR & R 72 / 83

Page 73: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

The Queens Problem (Cont’d)Reasoning by queries

Do I have to put q1 in (2,2)? KB |= Q(q1, 2, 2)?

If I put q1 in (1,1), do I have to put q2 in (2,3)?

KB |= Q(q1, 1, 1) ⊃ Q(q2, 2, 3)?

Reasoning by model construction:

Find an interpretation that satisfies all the sentences in KB. IfQ(q1, i , j) ∧ Q(q2, i ′, j ′) is true in this interpretaton, then asolution is to put q1 in (i,j) and q2 in (i’,j’).

Reasoning by contradiction:

Can I put q1 in (1,1) and q2 in (1,2)?

KB ∪ {Q(q1, 1, 1),Q(q2, 1, 2)} |= false?

Can I put q1 in (1,1) and q2 in (2,3)?

KB ∪ {Q(q1, 1, 1),Q(q2, 2, 3)} |= false?

Fangzhen Lin (HKUST) Lecture 4: KR & R 73 / 83

Page 74: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Rule-Based Expert Systems

An expert system embodies knowledge about a specialized field suchas medicine, engineering, or business.

It is one of the most successful applications of AI reasoningtechniques.

Some well-known expert systems:I DENDRAL (1965-83) - one of the earliest expert systems, molecular

structure analysis.I MYCIN (1972-80) - one of the most influential expert system, medical

diagnosis.I PROSPECTOR - a mining expert system.I XCOM (R1) - a computer configuration system used in DEC.I American Express Inc. loan processing expert system.

Fangzhen Lin (HKUST) Lecture 4: KR & R 74 / 83

Page 75: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Basic Architecture

Expert

Knowledge engineer

User

Knowledgeacquisitionsubsystem

Userinterface

Explanationsubsystem

Knowledge base

facts, heuristicsInference

engine

© 1998 Morgan Kaufman Publishers

Fangzhen Lin (HKUST) Lecture 4: KR & R 75 / 83

Page 76: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

An ExampleRule-based expert systems are often based on Horn clauses (clauses withexactly one positive literal). Consider the following loan-approval system:

1. COLLAT ∧ PYMT ∧ REP ⊃ OK

2. APP ⊃ COLLAT

3. RATING ⊃ REP

4. INC ⊃ PYMT

5. BAL ∧ REP ⊃ OK

where OK means the loan should be approved, COLLAT the collateral forthe loan is satisfactory, PYMT the applicant is able to make the loanpayments, REP the applicant has a good financial reputation, APP theappraisal on the collateral is sufficiently larger than the loan amount,RATING the applicant has a good credit rating, INC the applicant’sincome exceeds his/her expenses, and BAL the applciant has an excellentbalance sheet.

Fangzhen Lin (HKUST) Lecture 4: KR & R 76 / 83

Page 77: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Search Tree

BAL

OK

OK

REP

rule 5

REP

rule 3

COLLAT PYMT

rule 1

COLLAT PYMT

rule 2 rule 4

REP

REP

rule 3

RATING APP INC RATING

OK

© 1998 Morgan Kaufman Publishers

Fangzhen Lin (HKUST) Lecture 4: KR & R 77 / 83

Page 78: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Uncertanty in Rules

Often, experts have only uncertain rules. For instance, in MYCIN, a rulecould be:

if (a) the infection is primary-bacteremia, and(b) the site of the culture is one of the sterile sites, and(c) the suspected portal of entry is the gastrointestinal tract

then there is evidence (.7) that the infection is bateroid

Here .7 is called certanty factor in MYCIN. Now a better way to deal withuncertainty is by using Bayes networks.

Fangzhen Lin (HKUST) Lecture 4: KR & R 78 / 83

Page 79: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Rule Learning

Rule-based expert systems are still widely used.

It takes a lot of effort to collect these rules from experts.

An automatic way of acquiring these rules will be very useful.

We describe an algorithm called generic separate-and-conqueralgoritm for learning propositional rules.

Fangzhen Lin (HKUST) Lecture 4: KR & R 79 / 83

Page 80: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

Loan-Approval ExampleSuppose we have following data from bank record:

Individual APP RATING INC BAL OK

1 1 0 0 1 02 0 0 1 0 03 1 1 0 1 14 0 1 1 1 15 0 1 1 0 06 1 1 1 0 17 1 1 1 1 18 1 0 1 0 09 1 1 0 0 0

We want to find out rules about when to approve a loan, i.e. learn rules ofthe form:

α1 ∧ · · · ∧ αn ⊃ OK

where αi are atoms from the set {APP,RATING , INC ,BAL}.Fangzhen Lin (HKUST) Lecture 4: KR & R 80 / 83

Page 81: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

GSCA

Generic Separate-Conquer Algorithm: Given an atom γ that we want tolearn 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, anda 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 Σ.

Fangzhen Lin (HKUST) Lecture 4: KR & R 81 / 83

Page 82: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

A Heuristic

A common heuristic is to select a feature α that will yield the maximumvalue of rα = n+

α /nα, where nα is the total number of instances in Σcur

that is covered by the new rule when α is added to Γ, and n+α the total

number of positive instances in Σcur that is covered by the new rule.

Fangzhen Lin (HKUST) Lecture 4: KR & R 82 / 83

Page 83: COMP5211 Lecture 4: Knowledge Representation and Reasoninghome.cse.ust.hk/faculty/flin/comp5211/lect4.pdf · Knowledge-Based Agents Two main components: Knowledge base I The collection

GSCA - The Loan-Approval Example

We began with the rule: true ⊃ OK .

It covers all instances, including all negative ones. So we have to narrowthem by adding a feature into its antecedent. We do that by choosing onethat 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. Tolearn the next one, we eliminate from the sample those rules aready cover byit, and continue like above, until no more positive instances are left with.

Fangzhen Lin (HKUST) Lecture 4: KR & R 83 / 83