Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

25
Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College

Transcript of Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Page 1: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Logic

CPSC 386 Artificial Intelligence

Ellen Walker

Hiram College

Page 2: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Knowledge Based Agents

• Representation– How is the knowledge stored by the agent?– Procedural vs. Declarative

• Reasoning– How is the knowledge used…

• To solve a problem?• To generate more knowledge?

• Generic Functions– TELL (add a fact to the knowledge base)– ASK (get next action based on info in KB)

Page 3: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Hunt the Wumpus

• Game is played in a MxN grid• One player, one wumpus, one or more pits• Goal: find gold while avoiding wumpus and

pits• Percepts:

– Glitter (gold is in this square)– Stench (wumpus is within 1 square N,E,S,W)– Breeze (pit is within 1 square N, E, S, W)

Page 4: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Wumpus Example

stench [Wumpus] stench

Glitter

[gold]

stench,

breeze

[start] breeze [Pit] breeze

Page 5: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Examples of reasoning

• If the player is in square (1, 0) and the percept is breeze, then there must be a pit in (0,0) or a pit in (2,0) or a pit in (1,1).

• If the player is in (0,0) [and still alive], there is not a pit in (0,0).

• If there is no breeze in (0,0), there is no pit in (0,1)• If there is also no breeze in (0,1) then there is no pit

in (1,1).• Therefore, there must be a pit in (2,0)

Page 6: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Formalizing Reasoning

• Information is represented in sentences, which must have correct syntax( 1 + 2 ) * 7 = 21 vs. 2 ) + 7 = * ( 1 21

• The meaning of a sentence (semantics) defines its truth in each model (possible world)

• One sentence entails another sentence if the second one follows logically from the first; i.e. every model that has the first true, also has the second true.

• Inference is the process of deriving a specific sentence from a KB (where the sentence must be entailed by the KB)

Page 7: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Desirable properties of an inference algorithm

• Soundness– Only sentences that are entailed by a KB will be derived by

the inference algorithm

• Completeness– Every sentence that is entailed by a KB will be derived by

the inference algorithm (eventually)

• If these properties are true, then every sentence derived from a true KB will be true in the world. – All reasoning can be done in a model, not the world!

Page 8: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Propositional Logic

• Syntax: Sentence ->true, false, P, Q, R … primitive sent.

sentence not

( sentence sentence ) and

( sentence sentence ) or

( sentence sentence ) implies (if)

( sentence sentence ) if & only if (iff)

( sentence )

• Note: propositional logic can be directly implemented in hardware using logic gates for operations

Page 9: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Propositional Logic Sentences

• If there is a pit at [1,1], there is a breeze at [1,0]P11 B10

• There is a breeze at [2,2], if and only if there is a pit in the neighborhoodB22 ( P21 P23 P12 P32 )

• There is no breeze at [2,2]B22

Page 10: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Propositional Logic Inference

• Question: Does KB entail S?• Method 1: Truth Table Entailment

– Construct a truth table whose columns are all propositions used in the sentences in KB.

– If S is true everywhere all sentences in KB are true, then KB entails S (otherwise not)

• Method 2: Proof– Proof by deduction– Proof by contradiction– Etc.

Page 11: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Truth Table Entailment

A B C A B A C B CF F F F F F

F F T F F F

F T F F F F

F T T F F T

T F F F F F

T F T F T F

T T F T F F

T T T T T T

A,B, EntailsAB

A^C, Cdoes not entailBC

Page 12: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Truth Table Entailment is…

• Sound– by definition, since it directly implements the

definition of entailment

• Complete– Only when the KB (and therefore the truth table) is

finite

• Time consuming – The truth table size is 2number of statements

, and we have to check every row!

Page 13: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Rules for Deductive Proofs

• Modus Ponens– Given: S1 S2 and S1, derive S2

• And-elimination– Given: S1 S2, derive S1– Given: S1 S2, derive S2

• DeMorgan’s Law– Given ( A B) derive A B– Given ( A B) derive A B

• More on p. 210

Page 14: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Example Proof by Deduction

• KnowledgeS1: B22 ( P21 P23 P12 P32 ) rule

S2: B22 observation

• InferencesS3: (B22 (P21 P23 P12 P32 ))

((P21 P23 P12 P32 ) B22) [S1,bi elim]

S4: ((P21 P23 P12 P32 ) B22) [S3, and elim]

S5: (B22 ( P21 P23 P12 P32 )) [contrapos]

S6: (P21 P23 P12 P32 ) [S2,S6, MP]

S7: P21 P23 P12 P32 [S6, DeMorg]

Page 15: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Evaluation of Deductive Inference (using p. 110 rules)

• Sound– Yes, because the inference rules themselves are sound.

(This can be proven using a truth table argument).

• Complete– If we allow all possible inference rules, we’re searching in an

infinite space, hence not complete– If we limit inference rules, we run the risk of leaving out the

necessary one…

• Monotonic– If we have a proof, adding information to the DB will not

invalidate the proof

Page 16: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Resolution

• Resolution allows a complete inference mechanism (search-based) using only one rule of inference

• Resolution rule:– Given: P1 P2 P3 … Pn, and P1 Q1 … Qm

– Conclude: P2 P3 … Pn Q1 … Qm

Complementary literals P1 and P1 “cancel out”

• Why it works:– Consider 2 cases: P1 is true, and P1 is false

Page 17: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Resolution in Wumpus World

• There is a pit at 2,1 or 2,3 or 1,2 or 3,2– P21 P23 P12 P32

• There is no pit at 2,1 P21

• Therefore (by resolution) the pit must be at 2,3 or 1,2 or 3,2– P23 P12 P32

Page 18: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Proof using Resolution

• To prove a fact P, repeatedly apply resolution until either:– No new clauses can be added, (KB does not entail P)– The empty clause is derived (KB does entail P)

• This is proof by contradiction: if we prove that KB P derives a contradiction (empty clause) and we know KB is true, then P must be false, so P must be true!

• To apply resolution mechanically, facts need to be in Conjunctive Normal Form (CNF)

• To carry out the proof, need a search mechanism that will enumerate all possible resolutions.

Page 19: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Conjunctive Normal Form for B22 ( P21 P23 P12 P32 )

1. Eliminate , replacing with two implications(B22 ( P21 P23 P12 P32 )) ((P21 P23 P12 P32 ) B22)

• Replace implication (A B) by A B

(B22 ( P21 P23 P12 P32 )) ((P21 P23 P12 P32 ) B22)

1. Move “inwards” (unnecessary parens removed)(B22 P21 P23 P12 P32 ) ( (P21 P23 P12 P32 ) B22)

4. Distributive Law(B22 P21 P23 P12 P32 ) (P21 B22) (P23 B22) (P12

B22) (P32 B22)

(Final result has 5 clauses)

Page 20: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Resolution Example

• Given B22 and P21 and P23 and P32 , prove P12

• (B22 P21 P23 P12 P32 ) ; P12

• (B22 P21 P23 P32 ) ; P21

• (B22 P23 P32 ) ; P23

• (B22 P32 ) ; P32

• (B22) ; B22

• [empty clause]

Page 21: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Mechanical Approach to Resolution

• Use DFS in “clause space”– Initial state is Not (whatever is being proven)– Goal state is Empty clause– Next state generator finds all clauses in the KB

[that include negations of one or more propositions in the current state]; next states are resolutions of those clauses with current state

• Like all DFS, this is worst-case exponential (search all possible clauses)

Page 22: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Evaluation of Resolution

• Resolution is sound– Becase the resolution rule is true in all cases

• Resolution is complete– Provided a complete search method is used to find the

proof, if a proof can be found it will– Note: you must know what you’re trying to prove in order to

prove it!

• Resolution is exponential– The number of clauses that we must search grows

exponentially…– If it didn’t we could use resolution to solve 3SAT in

polynomial time!

Page 23: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Horn Clauses

• A Horn Clause is a CNF clause with exactly one positive literal– The positive literal is called the head– The negative literals are called the body– Prolog: head:- body1, body2, body3 …– English: “To prove the head, prove body1, …”– Implication: If (body1, body2 …) then head

• Horn Clauses form the basis of forward and backward chaining

• The Prolog language is based on Horn Clauses• Deciding entailment with Horn Clauses is linear in the

size of the knowledge base

Page 24: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

Reasoning with Horn Clauses

• Forward Chaining– For each new piece of data, generate all new

facts, until the desired fact is generated– Data-directed reasoning

• Backward Chaining– To prove the goal, find a clause that contains the

goal as its head, and prove the body recursively– (Backtrack when you chose the wrong clause)– Goal-directed reasoning

Page 25: Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.

First Order Logic (ch. 8)

• Has a greater expressive power than propositional logic– We no longer need a separate rule for each

square to say which other squares are breezy/pits

• Allows for facts, objects, and relations– In programming terms, allows classes, functions

and variables