63
Chương 3 Tri thức và lập luận

Chương 3 Tri thức và lập luận

Embed Size (px)

DESCRIPTION

Chương 3 Tri thức và lập luận. Nội dung chính chương 3. Logic – ngôn ngữ của tư duy Logic mệnh đề (cú pháp, ngữ nghĩa, sức mạnh biểu diễn, các thuật toán suy diễn) Prolog (cú pháp, ngữ nghĩa, lập trình prolog, bài tập và thực hành) - PowerPoint PPT Presentation

Citation preview

Page 1: Chương 3 Tri thức và lập luận

Chương 3Tri thức và lập luận

Page 2: Chương 3 Tri thức và lập luận

Nội dung chính chương 3

I. Logic – ngôn ngữ của tư duy

II. Logic mệnh đề (cú pháp, ngữ nghĩa, sức mạnh biểu diễn, các thuật toán suy diễn)

III. Prolog (cú pháp, ngữ nghĩa, lập trình prolog, bài tập và thực hành)

IV. Logic cấp một (cú pháp, ngữ nghĩa, sức mạnh biểu diễn, các thuật toán suy diễn)

Lecture 1

Lecture 2

Lecture 3,4

Page 3: Chương 3 Tri thức và lập luận

Lecture 4Inference in First-Order Logic

Page 4: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 4

Lecture 4: Inference in First-Order Logic - Outline

Proofs (chứng minh)

Unification (hợp nhất)

Generalized modus ponens - GMP(Luật modus ponens tổng quát)

Inference procedures based on GMP (các thủ tục dựa trên luật modus ponens tổng quát)

• Forward chaining (suy diễn tiến)

• Backward chaining (suy diễn lùi)

Incompleteness of GMP-based inferences (tính không đầy đủ của suy diễn tiến và lùi).

Inference procedures based on Resolution (thủ tục suy diễn hợp giải, thủ tục có tính đầy đủ trong logic cấp một)

Logic programming (lập trình logic)

Page 5: Chương 3 Tri thức và lập luận

Entailment and proof - Hệ quả logic và chứng minh

CS 561, Session 16-18 5

Page 6: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 6

Remember:propositionallogic

Nhớ lại:Các định lý trong logicmệnh đề

Page 7: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 7

Proofs – chứng minh

Page 8: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 8

Proofs – Chứng minh (tiếp)

The three new inference rules for FOL (compared to propositional logic) are:

• Universal Elimination (UE):for any sentence , variable x and ground term ,

x {x/}

• Existential Elimination (EE):for any sentence , variable x and constant symbol k not in KB,

x {x/k}

• Existential Introduction (EI):for any sentence , variable x not in and ground term g in ,

x {g/x}

Page 9: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 9

Proofs – Chứng minh (tiếp)

The three new inference rules for FOL (compared to propositional logic) are:

• Universal Elimination (UE):for any sentence , variable x and ground term ,

x e.g., from x Likes(x, Candy) and {x/Joe} {x/} we can infer Likes(Joe, Candy)

• Existential Elimination (EE):for any sentence , variable x and constant symbol k not in KB,

x e.g., from x Kill(x, Victim) we can infer{x/k} Kill(Murderer, Victim), if Murderer new

symbol

• Existential Introduction (EI):for any sentence , variable x not in and ground term g in ,

e.g., from Likes(Joe, Candy) we can inferx {g/x} x Likes(x, Candy)

Page 10: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 10

Example Proof

Page 11: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 11

Example Proof

Page 12: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 12

Example Proof

Page 13: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 13

Example Proof

Page 14: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 14

Search with primitive example rules

Page 15: Chương 3 Tri thức và lập luận

Unification – Hợp nhất

• Unification is a "pattern matching" procedure that takes two atomic sentences, called literals, as input, and returns "failure" if they do not match and a substitution list, Theta, if they do match. • That is, unify(p,q) = Theta means subst(Theta, p) =

subst(Theta, q) for two atomic sentences p and q. • Theta is called the most general unifier (mgu)

• All variables in the given two literals are implicitly universally quantified

• To make literals match, replace (universally-quantified) variables by terms

Page 16: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 16

Unification – Hợp nhất

Goal of unification: finding σ

Page 17: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 17

Unification – Hợp nhất

Page 18: Chương 3 Tri thức và lập luận

Unification Algorithm – Giải thuật hợp nhất

procedure unify(p, q, theta) Scan p and q left-to-right and find the first

corresponding terms where p and q "disagree" ; where p and q not equal

If there is no disagreement, return theta ; success

Let r and s be the terms in p and q, respectively, where disagreement first occurs

If variable(r) then theta = union(theta, {r/s}) unify(subst(theta, p), subst(theta, q), theta)

else if variable(s) then theta = union(theta, {s/r}) unify(subst(theta, p), subst(theta, q), theta)

else return "failure" end

Page 19: Chương 3 Tri thức và lập luận

Unification…

• Examples

Literal 1 Literal 2 theta

parents(x, father(x), mother(Bill))

parents(x, father(x), mother(Bill))

parents(x, father(x), mother(Jane))

parents(Bill, father(Bill), y)

parents(Bill, father(y), z)

parents(Bill, father(y), mother(y))

{x/Bill, y/mother(Bill)}

{x/Bill, y/Bill, z/mother(Bill)}

Failure

Page 20: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 20

More Unification Examples

1 – unify(P(a,X), P(a,b)) σ = {X/b}

2 – unify(P(a,X), P(Y,b)) σ = {Y/a, X/b}

3 – unify(P(a,X), P(Y,f(a)) σ = {Y/a, X/f(a)}

4 – unify(P(a,X), P(X,b)) σ = failure

Note: If P(a,X) and P(X,b) are independent, then we can replace X with Y and get the unification to work.

VARIABLE term

Page 21: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 21

Generalized Modus Ponens (GMP) - Luật Modus Ponens tổng quát

Page 22: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 22

Soundness of GMP – Tính đúng đắn của GMP

Page 23: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 23

Horn form – Dạng câu Horn

• We convert sentences to Horn form as they are entered into the KB

• Using Existential Elimination and And Elimination

• e.g., x Owns(Nono, x) Missile(x) becomes

Owns(Nono, M)Missile(M)

(with M a new symbol that was not already in the KB)

Page 24: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 24

Forward chaining procedure – Thủ tục suy diễn tiến

Page 25: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 25

Backward chaining procedure – Thủ tục suy diễn lùi

Page 26: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 26

An Example (from Konelsky)

• Nintendo example.• Nintendo says it is Criminal for a programmer to provide

emulators to people. My friends don’t have a Nintendo 64, but they use software that runs N64 games on their PC, which is written by Reality Man, who is a programmer.

• Query: Has Reality Man done anything criminal?

Page 27: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 27

Forward Chaining

Programmer(x) Emulator(y) People(z) Provide(x,z,y) Criminal(x)(1)

Use(friends, x) Runs(x, N64 games) Provide(Reality Man, friends, x)(2)

Software(x) Runs(x, N64 games) Emulator(x)(3)

Programmer(Reality Man) (4)People(friends) (5)

Software(U64) (6)Use(friends, U64) (7)

Runs(U64, N64 games) (8)

• Premises (6), (7) and (8) satisfy the implications fully.

Page 28: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 28

Forward Chaining

Programmer(x) Emulator(y) People(z) Provide(x,z,y) Criminal(x) (1)

Use(friends, x) Runs(x, N64 games) Provide(Reality Man, friends, x) (2)

Software(x) Runs(x, N64 games) Emulator(x) (3)

Programmer(Reality Man) (4)People(friends) (5)

Software(U64) (6)Use(friends, U64) (7)

Runs(U64, N64 games) (8)

• So we can infer the consequents, which are now added to the knowledge base (this is done in two separate steps).

Page 29: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 29

Forward Chaining

Programmer(x) Emulator(y) People(z) Provide(x,z,y) Criminal(x) (1)

Use(friends, x) Runs(x, N64 games) Provide(Reality Man, friends, x) (2)

Software(x) Runs(x, N64 games) Emulator(x) (3)

Programmer(Reality Man) (4)People(friends) (5)

Software(U64) (6)Use(friends, U64) (7)

Runs(U64, N64 games) (8)Provide(Reality Man, friends, U64) (9)

Emulator(U64) (10)

• Addition of these new facts triggers further forward chaining.

Page 30: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 30

Forward Chaining

Programmer(x) Emulator(y) People(z) Provide(x,z,y) Criminal(x) (1)

Use(friends, x) Runs(x, N64 games) Provide(Reality Man, friends, x) (2)

Software(x) Runs(x, N64 games) Emulator(x) (3)

Programmer(Reality Man) (4)

People(friends) (5)

Software(U64) (6)

Use(friends, U64) (7)

Runs(U64, N64 games) (8)Provide(Reality Man, friends, U64) (9)

Emulator(U64) (10)

Criminal(Reality Man) (11)

• Which results in the final conclusion: Criminal(Reality Man)

Page 31: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 31

Backward Chaining

• Question: Has Reality Man done anything criminal?

• We will use the same knowledge as in our forward-chaining version of this example:

Programmer(x) Emulator(y) People(z) Provide(x,z,y) Criminal(x)

Use(friends, x) Runs(x, N64 games) Provide(Reality Man, friends, x)

Software(x) Runs(x, N64 games) Emulator(x)

Programmer(Reality Man)

People(friends)

Software(U64)

Use(friends, U64)

Runs(U64, N64 games)

Page 32: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 32

Backward Chaining

• Question: Has Reality Man done anything criminal?

Criminal(x)

Page 33: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 33

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man}

Criminal(x)

Programmer(x)

Page 34: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 34

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Criminal(x)

People(Z)Programmer(x)

Page 35: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 35

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Criminal(x)

People(Z)Programmer(x) Emulator(y)

Page 36: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 36

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Yes, {y/U64}

Criminal(x)

People(z)Programmer(x) Emulator(y)

Software(y)

Page 37: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 37

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Yes, {y/U64} yes, {}

Criminal(x)

People(z)Programmer(x) Emulator(y)

Software(y)

Runs(U64, N64 games)

Page 38: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 38

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Yes, {y/U64} yes, {}

Criminal(x)

People(z)Programmer(x) Emulator(y)

Software(y)

Runs(U64, N64 games)

Provide(reality man,

U64,friends)

Page 39: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 39

Backward Chaining

• Question: Has Reality Man done anything criminal?

Yes, {x/Reality Man} Yes, {z/friends}

Yes, {y/U64}

yes, {}

Criminal(x)

People(z)Programmer(x) Emulator(y)

Software(y)

Runs(U64, N64 games)

Provide(reality man,

U64,friends)

Use(friends, U64)

Page 40: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 40

Backward Chaining

• Backward Chaining benefits from the fact that it is directed toward proving one statement or answering one question.

• In a focused, specific knowledge base, this greatly decreases the amount of superfluous work that needs to be done in searches.

• However, in broad knowledge bases with extensive information and numerous implications, many search paths may be irrelevant to the desired conclusion.

• Unlike forward chaining, where all possible inferences are made, a strictly backward chaining system makes inferences only when called upon to answer a query.

Page 41: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 41

Completeness

• As explained earlier, Generalized Modus Ponens requires sentences to be in Horn form:• atomic, or

• an implication with a conjunction of atomic sentences as the antecedent and an atom as the consequent.

• However, some sentences cannot be expressed in Horn form.• e.g.: x bored_of_this_lecture (x)

• E.g.: x P(x) => Q(x)• Cannot be expressed in Horn form due to presence of negation.

Generalized Modus PonensBackward chaining

Forward chaining

Page 42: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 42

Completeness

• A significant problem since Modus Ponens cannot operate on such a sentence, and thus cannot use it in inference.

• Knowledge exists but cannot be used.

• Thus inference using Modus Ponens is incomplete.

Page 43: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 43

Completeness

• However, Kurt Gödel in 1930-31 developed the completeness theorem, which shows that it is possible to find complete inference rules.

• The theorem states:• any sentence entailed by a set of sentences can be

proven from that set.

=> Resolution Algorithm which is a complete inference method.

Page 44: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 44

Completeness in FOL

Page 45: Chương 3 Tri thức và lập luận

Resolution Procedure… Thủ tục hợp giải

• Proof by contradiction method

• Given a consistent set of axioms KB and goal sentence Q, we want to show that KB |= Q. This means that every interpretation I that satisfies KB, satisfies Q. But we know that any interpretation I satisfies either Q or ~Q, but not both. Therefore if in fact KB |= Q, an interpretation that satisfies KB, satisfies Q and does not satisfy ~Q. Hence KB union {~Q} is unsatisfiable, i.e., that it's false under all interpretations.

• In other words, (KB |- Q) <=> (KB ^ ~Q |- False)

• What's the gain? If KB union ~Q is unsatisfiable, then some finite subset is unsatisfiable

• Resolution procedure can be used to establish that a given sentence Q is entailed by KB; however, it cannot, in general, be used to generate all logical consequences of a set sentences. Also, the resolution procedure cannot be used to prove that Q is not entailed by KB.

Page 46: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 46

Resolution inference rule

Page 47: Chương 3 Tri thức và lập luận

Resolution Algorithm – Giải thuật hợp giải

procedure resolution-refutation(KB, Q)

;; KB is a set of consistent, true FOL sentences

;; Q is a goal sentence that we want to derive

;; return success if KB |- Q, and failure otherwise

KB = union(KB, ~Q)

while false not in KB do

pick 2 sentences, S1 and S2, in KB that contain literals that unify (if none, return "failure“)

resolvent = resolution-rule(S1, S2)

KB = union(KB, resolvent)

return "success"

Page 48: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 48

Kinship Example

KB:

(1) father (art, jon) (2) father (bob, kim) (3) father (X, Y) parent (X, Y)

Goal: parent (art, jon)?

Page 49: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 49

Refutation Proof/Graph

¬parent(art,jon) ¬ father(X, Y) \/ parent(X, Y) \ /

¬ father (art, jon) father (art, jon) \ / []

Page 50: Chương 3 Tri thức và lập luận

Problems yet to be addressed – Các vấn đề cần giải quyết khi áp dụng hợp giải.

• Resolution rule of inference is only applicable with sentences that are in the form P1 v P2 v ... v Pn, where each Pi is a negated or nonnegated predicate and contains functions, constants, and universally quantified variables, so can we convert every FOL sentence into this form?

• Resolution strategy• How to pick which pair of sentences to resolve?

• How to pick which pair of literals, one from each sentence, to unify?

Page 51: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 51

Remember: normal forms

“sum of products of simple variables ornegated simple variables”

“product of sums of simple variables ornegated simple variables”

Page 52: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 52

Conjunctive normal form

Page 53: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 53

Skolemization

Page 54: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 54

Examples: Converting FOL sentences to clause form…

Convert the sentence

1. (x){P(x) => [(y)(P(y) => P(f(x,y))) ^ ¬(y)(Q(x,y) => P(y)))]

(like A => B ^ C)(chú ý: để dễ nhìn, dùng “[“, “]”,”{“ và “}” thay cho”(“ và “)” trong một vài vị

trí)

2. Eliminate => (x){¬P(x) [(y)(¬P(y) P(f(x,y))) ^ ¬(y)(¬Q(x,y) P(y)))]

3. Reduce scope of negation(x){¬P(x) [(y)(¬P(y) P(f(x,y))) ^ (y)(Q(x,y) ^ ¬P(y)))]

4. Standardize variables(x){¬P(x) [(y)(¬P(y) P(f(x,y))) ^ (z)(Q(x,z) ^ ¬P(z)))]

Page 55: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 55

Examples: Converting FOL sentences to clause form…

5. Eliminate existential quantification(x){¬P(x) [(y)(¬P(y) P(f(x,y))) ^ (Q(x,g(x)) ^

¬P(g(x))))]

6. Drop universal quantification symbols{¬P(x) [(¬P(y) P(f(x,y))) ^ (Q(x,g(x)) ^

¬P(g(x))))]

7. Convert to conjunction of disjunctions[¬P(x) ¬P(y) P(f(x,y))] ^ [¬P(x) Q(x,g(x))]

^[¬P(x) ¬P(g(x))]

Page 56: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 56

Examples: Converting FOL sentences to clause form…

8. Create separate clauses¬P(x) ¬P(y) P(f(x,y)) ¬P(x) Q(x,g(x)) ¬P(x) ¬P(g(x))

9. Standardize variables¬P(x) ¬P(y) P(f(x,y)) ¬P(z) Q(z,g(z)) ¬P(w) ¬P(g(w))

Page 57: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 57

Resolution proof

Page 58: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 58

Resolution proof

Page 59: Chương 3 Tri thức và lập luận

Problems yet to be addressed – Các vấn đề cần giải quyết khi áp dụng hợp giải.

• Resolution rule of inference is only applicable with sentences that are in the form P1 v P2 v ... v Pn, where each Pi is a negated or nonnegated predicate and contains functions, constants, and universally quantified variables, so can we convert every FOL sentence into this form?

• Resolution strategy• How to pick which pair of sentences to resolve?

• How to pick which pair of literals, one from each sentence, to unify?

Page 60: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 60

Example of Refutation Proof(in conjunctive normal form)

(1) Cats like fish(2) Cats eat everything they

like(3) Josephine is a cat.(4) Prove: Josephine eats

fish.

cat (x) likes (x,fish)cat (y) likes (y,z) eats (y,z)cat (jo) eats (jo,fish)

Page 61: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 61

Backward Chaining – Hợp giải lùi

Negation of goal wff: eats(jo, fish)

eats(jo, fish) cat(y) likes(y, z) eats(y, z)

= {y/jo, z/fish}

cat(jo) likes(jo, fish) cat(jo)

=

cat(x) likes(x, fish) likes(jo, fish)

= {x/jo}

cat(jo) cat(jo)

(contradiction)

Page 62: Chương 3 Tri thức và lập luận

CS 561, Session 16-18 62

Forward chaining – Hợp giải tiến

cat (jo) cat (X) likes (X,fish) \ / likes (jo,fish) cat (Y) likes (Y,Z) eats (Y,Z)

\ / cat (jo) eats (jo,fish) cat (jo)

\ /eats (jo,fish) eats (jo,fish)

\ / [] []

Page 63: Chương 3 Tri thức và lập luận

Logic programming, Prolog

Language: Horn forms (at most 1 positive literal)• KB: definite clauses: (Horn forms with exact 1 positive

literal)

• Query (q): not(q) is a negative clause (Horn forms with 0 positive literal)

Inference procedure: Backward chaining resolution (actually, SLD Resolution – Selected literals Linear from Definite clauses Resolution)• Idea: Bắt đầu với Q=not(q), hợp giải với câu trong KB,

được câu hợp giải mới, câu mới này lại hợp giải với câu trong KB đến khi nào câu [] tìm thấy

• Đlý: Vơi KB gồm positive clauses và Q là negative clause:

KB Q |= [] khi và chỉ khi KB Q |-- SDL []