111
Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

  • Upload
    dohanh

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Logical AgentsCSC 411: AI

Fall 2013

NC State University 1 / 111Logical Agents

Page 2: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Outline

Knowledge-based agents

Wumpus world

Logic in general – models and entailment

Propositional (Boolean) logic

Equivalence, validity, satisfiability

Inference rules and theorem proving

• Forward chaining• Backward chaining• Resolution

NC State University 2 / 111Logical Agents

Page 3: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Knowledge basesInference engine ← Domain-independent algorithmsKnowledge base ← Domain-specific content

A knowledge base is a set of sentences in a formallanguage.

The declarative approach to building an agent (or othersystem):

• TELL it what it needs to know.• Then it can ASK itself what to do.

Knowledge level versus implementation level.

NC State University 3 / 111Logical Agents

Page 4: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

A simple knowledge-based agent

KB-AGENT(percept)static t = 0TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))action = ASK(KB, MAKE-ACTION-QUERY(t))TELL(KB, MAKE-ACTION-SENTENCE(action, t))t = t + 1return action

NC State University 4 / 111Logical Agents

Page 5: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

A simple knowledge-based agent

The agent must be able to

• Represent states, actions, etc.• Incorporate new percepts• Update internal representations of the world• Deduce hidden properties of the world• Deduce appropriate actions

NC State University 5 / 111Logical Agents

Page 6: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus world PEAS description

Performance measure:

Gold: +1000Death: −1000Taking a step: −1Using an arrow: −10

PIT

1 2 3 4

1

2

3

4

START

Stench

Stench

Breeze

Gold

PIT

PIT

Breeze

Breeze

Breeze

Breeze

Breeze

Stench

NC State University 6 / 111Logical Agents

Page 7: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus world PEAS descriptionEnvironment:Smelly when adjacent to Wumpus.Breezy when adjacent to pit.Glitter iff gold is in same square.Shooting kills Wumpus if you arefacing it.Shooting uses up only arrow.Grabbing picks up gold if in samesquare.Releasing drops the gold in samesquare.

PIT

1 2 3 4

1

2

3

4

START

Stench

Stench

Breeze

Gold

PIT

PIT

Breeze

Breeze

Breeze

Breeze

Breeze

Stench

NC State University 7 / 111Logical Agents

Page 8: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus world PEAS description

Sensors:Stench, Breeze, Glitter,Bump, ScreamActuators:Left turn, Right turn, For-ward, Grab, Release, Shoot

PIT

1 2 3 4

1

2

3

4

START

Stench

Stench

Breeze

Gold

PIT

PIT

Breeze

Breeze

Breeze

Breeze

Breeze

Stench

NC State University 8 / 111Logical Agents

Page 9: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus world characterization

Fully Observable? No, only local perception.

Deterministic? Yes, outcomes are exactly specified.

Episodic? No, sequential at the level of actions.

Static? Yes, the Wumpus and pits do not move.

Discrete? Yes.

Single-agent? Yes, the Wumpus is essentially a naturalfeature.

NC State University 9 / 111Logical Agents

Page 10: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 10 / 111Logical Agents

Page 11: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 11 / 111Logical Agents

Page 12: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 12 / 111Logical Agents

Page 13: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 13 / 111Logical Agents

Page 14: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 14 / 111Logical Agents

Page 15: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 15 / 111Logical Agents

Page 16: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 16 / 111Logical Agents

Page 17: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exploring a Wumpus world

NC State University 17 / 111Logical Agents

Page 18: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Logic in general

Logics are formal languages for representing informationsuch that conclusions can be drawn.

Syntax defines the sentences in the language; semanticsdefine the “meaning” (i.e., truth values) of sentences.

The language of arithmetic:

x + 2 ≥ y is a sentencex2 + y > () is not a sentencex + 2 ≥ y is true in a world where x = 7, y = 1x + 2 ≥ y is false in a world where x = 0, y = 6

NC State University 18 / 111Logical Agents

Page 19: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Entailment

The knowledge base KB entails sentence α if and only ifα is true in all worlds where KB is true: KB |= α.

Said differently, a set of premises KB logically entails aconclusion α if and only if every interpretation thatsatisfies the premises also satisfies the conclusion.

{p} |= (p ∨ q)

{p, q} |= (p ∧ q)

Entailment is a relationship between sentences (i.e.syntax), based on semantics.

NC State University 19 / 111Logical Agents

Page 20: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Models

Logicians typically think in terms of models, formallystructured worlds with respect to which truth can beevaluated.

We can think of a model as a possible world.

In the semantics of arithmetic, x + y = 4 is true in a worldwhere x is 2 and y is 2, but false in a world where x is 1and y is 10.

NC State University 20 / 111Logical Agents

Page 21: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Models

We say m is a model of a sentence α if α is true in m.

M(α) is the set of all models of α.

KB |= α iff M(KB) ⊆ M(α).

{p, q} |= p because the worlds in which p is true is asuperset of the worlds in which p is true and q is true.

NC State University 21 / 111Logical Agents

Page 22: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Entailment in the Wumpus world

You detect nothing in [1,1].

You move right.

You detect a breeze in [2,1].

Consider possible models forKB assuming only pits:

3 (Boolean) possibilities→8 possible models.

NC State University 22 / 111Logical Agents

Page 23: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus models

NC State University 23 / 111Logical Agents

Page 24: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus models

KB = Wumpus-world rules + observations

NC State University 24 / 111Logical Agents

Page 25: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus models

KB = Wumpus-world rules + observations

α1 = “[1,2] is safe.” Does KB |= α1?

NC State University 25 / 111Logical Agents

Page 26: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus models

KB = Wumpus-world rules + observations

NC State University 26 / 111Logical Agents

Page 27: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus models

KB = Wumpus-world rules + observations

α2 = “[2,2] is safe.” Does KB |= α2?

NC State University 27 / 111Logical Agents

Page 28: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Inference

KB `i α means that sentence α can be derived from KBby procedure i.

Soundness: i is sound if whenever KB `i α, it is also truethat KB |= α.

Completeness: i is complete if whenever KB |= α, it isalso true that KB `i α.

NC State University 28 / 111Logical Agents

Page 29: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Propositional logic: Syntax

The proposition symbols P1, P2, etc. are sentences.

If S is a sentence, ¬S is a sentence (negation).

If S1 and S2 are sentences. . .

S1 ∧ S2 is a sentence (conjunction).

S1 ∨ S2 is a sentence (disjunction).

S1 ⇒ S2 is a sentence (implication).

S1 ⇔ S2 is a sentence (biconditional).

NC State University 29 / 111Logical Agents

Page 30: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Propositional logic: Semantics

Each model specifies true or false for each propositionsymbol.

P1,2 P2,2 P3,1False True False

With these symbols, 8 possible models can be enumeratedautomatically.

NC State University 30 / 111Logical Agents

Page 31: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Propositional logic: Semantics

Rules for evaluating truth with respect to a model m:¬S is true iff S is false.

S1 ∧ S2 is true iff S1 is true and S2 is true.S1 ∨ S2 is true iff S1 is true or S2 is true.

S1 ⇒ S2 is true iff S1 is false or S2 is true.S1 ⇔ S2 is true iff S1 ⇒ S2 is true and S2 ⇒ S1 is true.

NC State University 31 / 111Logical Agents

Page 32: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Propositional logic: Semantics

A simple recursive process can evaluate an arbitrarysentence:¬P1,2 ∧ (P2,2 ∨ P3,1) = not false ∧ ( true ∨ false )

= true ∧ ( true ∨ false )= true ∧ true= true

NC State University 32 / 111Logical Agents

Page 33: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Truth tables for connectives

P Q ¬P P ∧ Q P ∨ Q P⇒ Q P⇔ QFalse False True False False True TrueFalse True True False True True FalseTrue False False False True False FalseTrue True False True True True True

NC State University 33 / 111Logical Agents

Page 34: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Wumpus world sentences

Let Pi,j be true if there is a pit in [i, j].

Let Bi,j be true if there is a breeze in [i, j].

Suppose we know

P1,1 and B1,2 and B2,1.

We also know that pits cause breezes in adjacent squares.Then

B1,2 ⇔ (P1,1 ∨ P2,2 ∨ P1,3).

B2,1 ⇔ (P1,1 ∨ P2,2 ∨ P3,1).

NC State University 34 / 111Logical Agents

Page 35: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Truth tables for inference

B1,1 B2,1 P1,1 P1,2 P2,1 P2,2 P3,1 KB α1

F F F F F F F F TF F F F F F T F T

. . .F T F F F F F F T

. . .F T F F F F F T TF T F F F T F T T

. . .F T F F T F F F T

. . .T T T T T T T F F

NC State University 35 / 111Logical Agents

Page 36: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Inference by enumerationTT-ENTAILS?(KB, α)

symbols = a list of symbols in KB and αreturn TT-CHECK-ALL(KB, α, symbols, {})

TT-CHECK-ALL(KB, α, symbols,model)if EMPTY?(symbols)

if PL-TRUE?(KB, model)return PL-TRUE?(α,model)

else return Trueelse p = First(symbols)

rest = Rest(symbols)return TT-CHECK-ALL(KB, α, rest, EXTEND(p,True,model))

and TT-CHECK-ALL(KB, α, rest, EXTEND(p,False,model))

NC State University 36 / 111Logical Agents

Page 37: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Inference by enumeration

Depth-first enumeration of all models is sound andcomplete.

For n symbols, time complexity is O(2n); spacecomplexity is O(n).

NC State University 37 / 111Logical Agents

Page 38: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Logical equivalence

Two sentences are logically equivalent iff they are true insame models:

α ≡ β iff α |= β and β |= α.

NC State University 38 / 111Logical Agents

Page 39: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Logical equivalence(α ∧ β) ≡ (β ∧ α) Commutativity of ∧(α ∨ β) ≡ (β ∨ α) Commutativity of ∨

((α ∧ β) ∧ γ) ≡ (α ∧ (β ∧ γ)) Associativity of ∧((α ∨ β) ∨ γ) ≡ (α ∨ (β ∨ γ)) Associativity of ∨

¬(¬α) ≡ α Double negation(α⇒ β) ≡ (¬β ⇒ ¬α) Contraposition¬(α ∧ β) ≡ (¬α ∨ ¬β) de Morgan¬(α ∨ β) ≡ (¬α ∧ ¬β) de Morgan

(α ∧ (β ∨ γ)) ≡ ((α ∧ β) ∨ (α ∧ γ)) Distributivity, ∧/∨(α ∧ (β ∨ γ)) ≡ ((α ∨ β) ∧ (α ∨ γ)) Distributivity, ∨/∧

NC State University 39 / 111Logical Agents

Page 40: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Validity

A sentence is valid if it is true in all models, e.g.,

TrueA ∨ ¬AA⇒ A

(A ∧ (A⇒ B))⇒ B

Validity is connected to inference via the DeductionTheorem:

KB |= α iff (KB⇒ α) is valid.

NC State University 40 / 111Logical Agents

Page 41: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Satisfiability

A sentence is satisfiable if it is true in some model.

A sentence is unsatisfiable if it is true in no model.

Satisfiability is also connected to inference:

KB |= α iff (KB ∧ ¬α) is unsatisfiable.

NC State University 41 / 111Logical Agents

Page 42: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Proof methods: Inference rules

One kind of proof method involves the application ofinference rules.

• Rules give sound generation of new sentences fromold.

• A proof is a sequence of inference rule applications.(We can use rules as operators in search.)

• This typically requires transformation of sentencesinto some normal form.

NC State University 42 / 111Logical Agents

Page 43: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Proof methods: Model checking

Another approach is model checking, e.g.,

• Truth table enumeration (as we’ve seen).• Smarter backtracking (potentially more efficient).• Heuristic search or optimization in model space

(sound but incomplete).

NC State University 43 / 111Logical Agents

Page 44: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

“You often ride your bike, but only on days when theweather is nice. Whenever you ride, you wear a helmet.Wearing your helmet always messes up your hair beyondeasy repair, but you like to be presentable: you neverarrive at work with messy hair, and you never go out ondinner dates with messy hair.”

What propositions should we define for this story?

NC State University 44 / 111Logical Agents

Page 45: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

“You often ride your bike, but only on days when theweather is nice. Whenever you ride, you wear a helmet.Wearing your helmet always messes up your hair beyondeasy repair, but you like to be presentable: you neverarrive at work with messy hair, and you never go out ondinner dates with messy hair.”

One possible set: NiceOut, BikeRide, Helmet, MessyHair,Work, DinnerDate.

Develop sentences to represent this story.

NC State University 45 / 111Logical Agents

Page 46: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

“You call me on your cell phone and tell me you’re out ona dinner date. If I wonder whether you rode your bike,should I ask you or figure it out on my own?”

How can I express this as a sentence in logic?

NC State University 46 / 111Logical Agents

Page 47: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution

We start with Conjunctive Normal Form (CNF).

Call a logical variable or its complement a literal.

A, B, ¬A, ¬B

Call a disjunction of literals (including a literal standingalone) a clause.

A, (B ∨ ¬A)

A sentence consisting of a conjunction of clauses is inCNF.

NC State University 47 / 111Logical Agents

Page 48: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution

The resolution rule for CNF:

a1 ∨ . . . ∨ ak b1 ∨ . . . ∨ bn

a1 ∨ . . . ∨ aj−1 ∨ aj+1 ∨ . . . ∨ ak ∨ b1 ∨ . . . ∨ bm−1 ∨ bm+1 ∨ . . . ∨ bn

where aj and bm are complementary literals.

Resolution is sound and complete for propositional logic.

NC State University 48 / 111Logical Agents

Page 49: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution in Wumpus world

P1,3 ∨ P2,2 ¬P2,2

P1,3

NC State University 49 / 111Logical Agents

Page 50: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution in Wumpus world

P1,3 ∨ P2,2 ¬P2,2

P1,3

NC State University 50 / 111Logical Agents

Page 51: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Conversion to CNF

B1,1 ⇔ (P1,2 ∨ P2,1)

Eliminate⇔, replacing α⇔ β with (α⇒ β) ∧ (β ⇒ α).

(B1,1 ⇒ (P1,2 ∨ P2,1)) ∧ ((P1,2 ∨ P2,1)⇒ B1,1)

Eliminate⇒, replacing α⇒ β with ¬α ∨ β.

(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1)

. . .

NC State University 51 / 111Logical Agents

Page 52: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Conversion to CNF

. . .

(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1)

Move ¬ inwards, using de Morgan’s rule:

(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ ((¬P1,2 ∧ ¬P2,1) ∨ B1,1)

Apply distributivity law (∧ over ∨) and flatten:

(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬P1,2 ∨ B1,1) ∧ (¬P2,1 ∨ B1,1)

NC State University 52 / 111Logical Agents

Page 53: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRideBikeRide⇒ HelmetHelmet⇒ MessyHair¬(MessyHair ∧ DinnerDate)¬(MessyHair ∧Work)

NC State University 53 / 111Logical Agents

Page 54: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideBikeRide⇒ Helmet Helmet ∨ ¬BikeRideHelmet⇒ MessyHair ¬Helmet ∨MessyHair¬(MessyHair ∧ DinnerDate) ¬DinnerDate ∨ ¬MessyHair¬(MessyHair ∧Work) ¬Work ∨ ¬MessyHair

NC State University 54 / 111Logical Agents

Page 55: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideBikeRide⇒ Helmet Helmet ∨ ¬BikeRideHelmet⇒ MessyHair ¬Helmet ∨MessyHair¬(MessyHair ∧ DinnerDate) ¬DinnerDate ∨ ¬MessyHair¬(MessyHair ∧Work) ¬Work ∨ ¬MessyHair

NC State University 55 / 111Logical Agents

Page 56: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideBikeRide⇒ Helmet Helmet ∨ ¬BikeRideHelmet⇒ MessyHair ¬Helmet ∨MessyHair¬(MessyHair ∧ DinnerDate) ¬DinnerDate ∨ ¬MessyHair¬(MessyHair ∧Work) ¬Work ∨ ¬MessyHair

NC State University 56 / 111Logical Agents

Page 57: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideBikeRide⇒ Helmet Helmet ∨ ¬BikeRideHelmet⇒ MessyHair ¬Helmet ∨MessyHair¬(MessyHair ∧ DinnerDate) ¬DinnerDate ∨ ¬MessyHair¬(MessyHair ∧Work) ¬Work ∨ ¬MessyHair

NC State University 57 / 111Logical Agents

Page 58: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

In CNF:

¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideBikeRide⇒ Helmet Helmet ∨ ¬BikeRideHelmet⇒ MessyHair ¬Helmet ∨MessyHair¬(MessyHair ∧ DinnerDate) ¬DinnerDate ∨ ¬MessyHair¬(MessyHair ∧Work) ¬Work ∨ ¬MessyHair

NC State University 58 / 111Logical Agents

Page 59: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

A truth table, for enumeration

NiceOut BikeRide ¬NiceOut⇒ ¬BikeRide NiceOut ∨ ¬BikeRideT T T TT T T TF F F FF T T T

NC State University 59 / 111Logical Agents

Page 60: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution algorithm

The strategy is proof by contradiction.

To prove α, we show that there exist no models in whichKB ∧ ¬α is true.

That is, we prove KB ∧ ¬α is unsatisfiable.

NC State University 60 / 111Logical Agents

Page 61: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Resolution algorithmPL-RESOLUTION(KB, α)

clauses = clauses in CNF representation of KB ∧ ¬αempty = the empty clausenew = the empty setwhile True

for Ci,Cj in clausesresolvent = PL-RESOLVE(Ci,Cj)if resolvent is the empty set

return Truenew = new ∪ resolvent

if new ⊆ clausesreturn False

clauses = clauses ∪ new

NC State University 61 / 111Logical Agents

Page 62: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

“You call me on your cell phone and tell me you’re out ona dinner date. . . ”

NiceOut ∨ ¬BikeRideHelmet ∨ ¬BikeRide¬Helmet ∨MessyHair¬DinnerDate ∨ ¬MessyHair¬Work ∨ ¬MessyHairDinnerDate

I’ll guess you didn’t ride your bike (¬BikeRide). Can Iprove it?

NC State University 62 / 111Logical Agents

Page 63: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

Recall. . .

a1 ∨ . . . ∨ ak b1 ∨ . . . ∨ bn

a1 ∨ . . . ∨ aj−1 ∨ aj+1 ∨ . . . ∨ ak ∨ b1 ∨ . . . ∨ bm−1 ∨ bm+1 ∨ . . . ∨ bn

Prove ¬BikeRide by resolution.

To start: Negate ¬BikeRide. . .

NC State University 63 / 111Logical Agents

Page 64: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Exercise: Bike riding

BikeRide Helmet ∨ ¬BikeRideHelmet

Helmet ¬Helmet ∨MessyHairMessyHair

MessyHair ¬DinnerDate ∨ ¬MessyHair¬DinnerDate

¬DinnerDate DinnerDate

NC State University 64 / 111Logical Agents

Page 65: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward and backward chaining

Horn Form: KB is a conjunction of Horn clauses.

A Horn clause is an implication of a conjunction to apositive literal:

C, (B⇒ A), (C ∧ D⇒ B)

Modus Ponens (for Horn Form) is complete for HornKBs.

α1 . . . αn α1 ∧ . . . ∧ αn ⇒ ββ

NC State University 65 / 111Logical Agents

Page 66: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining

AB

P ⇒ QL ∧M ⇒ PB ∧ L ⇒ MA ∧ P ⇒ LA ∧ B ⇒ L

Q

P

M

L

BA

NC State University 66 / 111Logical Agents

Page 67: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining

Fire any rule whose premises are satisfied in the KB.

Add its conclusion to the KB.

Continue until the query is found.

NC State University 67 / 111Logical Agents

Page 68: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining algorithmPL-FC-ENTAILS?(KB, q)

count = a table, where count[c] is # of symbols in c’s premiseinferred = a table, where inferred[s] is initially falseagenda = a list of symbols known to be true in KBwhile not Empty?(agenda)

p = Pop(agenda)if p is q return Trueif inferred[p] is False

inferred[p] = Truefor c such that p ∈ c.premise

count[c] = count[c]− 1if count[c] is 0

Push(c.conclusion, agenda)return False

Forward chaining is sound and complete for Horn KBs.NC State University 68 / 111Logical Agents

Page 69: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

Agenda: A, B.

P⇒ Q : 1L ∧M ⇒ P : 2B ∧ L⇒ M : 2A ∧ P⇒ L : 2A ∧ B⇒ L : 2Pop A.

NC State University 69 / 111Logical Agents

Page 70: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

After processing A.

Agenda: B.

P⇒ Q : 1L ∧M ⇒ P : 2B ∧ L⇒ M : 2A ∧ P⇒ L : 1A ∧ B⇒ L : 1Pop B.

NC State University 70 / 111Logical Agents

Page 71: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

After processing B.

P⇒ Q : 1L ∧M ⇒ P : 2B ∧ L⇒ M : 1A ∧ P⇒ L : 1A ∧ B⇒ L : 0

Agenda: L.

Pop L.

NC State University 71 / 111Logical Agents

Page 72: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

After processing L.

P⇒ Q : 1L ∧M ⇒ P : 1B ∧ L⇒ M : 0A ∧ P⇒ L : 1A ∧ B⇒ L : 0

Agenda: M.

Pop M.

NC State University 72 / 111Logical Agents

Page 73: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

After processing M.

P⇒ Q : 1L ∧M ⇒ P : 0B ∧ L⇒ M : 0A ∧ P⇒ L : 1A ∧ B⇒ L : 0

Agenda: P.

Pop P.

NC State University 73 / 111Logical Agents

Page 74: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

After processing P.

P⇒ Q : 0L ∧M ⇒ P : 0B ∧ L⇒ M : 0A ∧ P⇒ L : 0A ∧ B⇒ L : 0

Agenda: Q, L.

Pop Q.

NC State University 74 / 111Logical Agents

Page 75: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 75 / 111Logical Agents

Page 76: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 76 / 111Logical Agents

Page 77: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 77 / 111Logical Agents

Page 78: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 78 / 111Logical Agents

Page 79: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 79 / 111Logical Agents

Page 80: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 80 / 111Logical Agents

Page 81: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 81 / 111Logical Agents

Page 82: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward chaining example

NC State University 82 / 111Logical Agents

Page 83: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Proof of completeness

FC derives every atomic sentence that is entailed by KB.

FC reaches a fixed point where no new atomic sentencesare derived.

Consider the final state to be a model m, assigningtrue/false to symbols.

Every clause in the original KB is true in m

a1 ∧ . . . ∧ ak ⇒ b

Then m is a model of KB.

If KB |= q, q is true in every model of KB, including m.

NC State University 83 / 111Logical Agents

Page 84: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining

Work backward from query q.

To prove q by BC,

• Check if q is known already, or• Prove by BC all premises of some rule concluding q.

Avoid loops by checking if a new subgoal is already onthe goal stack.

Avoid repeated work by checking if a new subgoal hasalready been proved true or has already failed.

NC State University 84 / 111Logical Agents

Page 85: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 85 / 111Logical Agents

Page 86: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 86 / 111Logical Agents

Page 87: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 87 / 111Logical Agents

Page 88: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 88 / 111Logical Agents

Page 89: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 89 / 111Logical Agents

Page 90: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 90 / 111Logical Agents

Page 91: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 91 / 111Logical Agents

Page 92: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 92 / 111Logical Agents

Page 93: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 93 / 111Logical Agents

Page 94: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Backward chaining example

NC State University 94 / 111Logical Agents

Page 95: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Forward vs backward chaining

FC is data-driven, automatic processing, e.g., objectrecognition, routine decisions.

It may do lots of work that is irrelevant to the goal.

BC is goal-driven, appropriate for problem solving, e.g.,Where are my keys? How do I get CS degree?

The complexity of BC can be much less than linear in sizeof KB.

NC State University 95 / 111Logical Agents

Page 96: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Efficient propositional inference

Two families of efficient algorithms for propositionalinference are

• Complete backtracking search algorithms, such asDPLL, and

• Incomplete local search algorithms, such asWalkSAT.

NC State University 96 / 111Logical Agents

Page 97: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The DPLL algorithm: Concepts

A clause is true if any literal is true.

A sentence is false if any clause is false.

A unit clause contains only one literal.

A pure symbol always appears in the same positive ornegative form in all clauses.

(A ∨ ¬B), (¬B ∨ ¬C), (C ∨ A)

A and B are pure; C is not.

NC State University 97 / 111Logical Agents

Page 98: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The DPLL algorithm: Heuristics

Notice when a clause can be true if one of its literals istrue.

Notice that a sentence is false if one of its clauses is false.

Make the single literal in unit clauses true.

Make pure symbol literals true.

NC State University 98 / 111Logical Agents

Page 99: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The DPLL algorithm

DPLL-SATISFIABLE?(s)clauses = the set of clauses in the CNF representation of ssymbols = propositional symbols in sreturn DPLL(clauses, symbols, {})

NC State University 99 / 111Logical Agents

Page 100: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The DPLL algorithm

DPLL(clauses, symbols,model)if ALL-TRUE(clauses, model) return Trueif SOME-FALSE(clauses, model) return Falsep, value = FIND-PURE-SYMBOL(clauses, symbols, model)if p found

return DPLL(clauses, symbols \ p, [p = value | model])p, value = FIND-UNIT-CLAUSE(clauses, model)if p found

return DPLL(clauses, symbols \ p, [p = value | model])p = First(symbols)return DPLL(clauses, symbols \ p, [p = True | model]) or

DPLL(clauses, symbols \ p, [p = False | model])

NC State University 100 / 111Logical Agents

Page 101: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The WalkSAT algorithm

WalkSat is an incomplete local search algorithm.

Its evaluation function is the min-conflict heuristic ofminimizing the number of unsatisfied clauses.

It balances between greediness and randomness.

NC State University 101 / 111Logical Agents

Page 102: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

The WalkSAT algorithm

WALKSAT(clauses, p,max-flips)model = a random assignment of True/False to symbols in clausesfor j = 1 to max-flips

if SATISFIES?(model, clauses)return model

clause = CHOOSE-RANDOM-FALSE-CLAUSE(model)if Sample(0, 1) < p

model = FLIP(Random-Element(clause), clause,model)else symbol = ARG-MAX(clause, MAXIMIZE-SATISFIED)

model = FLIP(symbol, clause, model)return Failure

NC State University 102 / 111Logical Agents

Page 103: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Hard satisfiability problems

Consider random 3-CNF sentences.

(¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E)∨(E ∨ ¬D ∨ B) ∨ (B ∨ E ∨ ¬C)

Let

m = number of clauses andn = number of symbols.

Hard problems seem to cluster near a critical point,m/n = 4.3.

NC State University 103 / 111Logical Agents

Page 104: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Hard satisfiability problems

NC State University 104 / 111Logical Agents

Page 105: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Hard satisfiability problems

NC State University 105 / 111Logical Agents

Page 106: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Inference in the Wumpus world¬P1,1¬W1,1Bx,y ⇔ (Px,y+1 ∨ Px,y−1 ∨ Px+1,y ∨ Px−1,y)Sx,y ⇔ (Wx,y+1 ∨Wx,y−1 ∨Wx+1,y ∨Wx−1,y)W1,1 ∨W1,2 ∨ . . . ∨W4,4¬W1,1 ∨W1,2¬W1,1 ∨W1,3. . .

NC State University 106 / 111Logical Agents

Page 107: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

A propositional Wumpus world agent

PL-WUMPUS-AGENT(percept)static: KB, x, y, orientation, visited, action, planUPDATE(x, y, orientation, visited, action)if percept.stench

TELL(KB, stenchx,y)else TELL(KB, ¬stenchx,y)if percept.breeze

TELL(KB, breezex,y)else TELL(KB, ¬breezex,y)if percept.glitter

return action = Grab. . .

NC State University 107 / 111Logical Agents

Page 108: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

A propositional Wumpus world agent

PL-WUMPUS-AGENT(percept). . .else if not Empty?(plan)

return action = Pop(plan)else for j, k on FRINGE(visited)

if ASK(KB,Pj,k ∧ ¬Wj,k) is True orASK(KB,Pj,k ∨Wj,k) is False

plan = PLAN-PATH(x, y, orientation, j, k,visited)return action = Pop(plan)

else return action = CHOOSE-RANDOM-MOVE()

NC State University 108 / 111Logical Agents

Page 109: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Expressiveness limitation ofpropositional logic

KB contains “physics” sentences for every single square.

For every time t and every x, y location Lx,y,

Lx,y(t) ∧ FacingRight(t) ∧ Forward(t)⇒ Lx+1,y(t + 1),etc.

Rapid proliferation of clauses.

NC State University 109 / 111Logical Agents

Page 110: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Summary

Logical agents apply inference to a knowledge base toderive new information and make decisions.

• Syntax: Formal structure of sentences.• Semantics: Truth of sentences with respect models.• Entailment: Necessary truth of one sentence given

another.• Inference: Deriving sentences from other sentences.• Soundness: Derivations produce only entailed

sentences.• Completeness: Derivations can produce all entailed

sentences.NC State University 110 / 111

Logical Agents

Page 111: Logical Agents - CSC 411: AI Fall 2013 - Nc State Universitystamant/411/lectures/07-logic/logic-1.pdf · Logical Agents CSC 411: AI Fall 2013 NC State University 1 / 111 Logical Agents

Last observations

An agent, as in Wumpus world, needs to represent partialand negated information, reason by cases, etc.

Resolution is complete for propositional logic.

Forward and backward chaining run in linear time; theyare complete for Horn clauses.

Propositional logic lacks expressive power.

NC State University 111 / 111Logical Agents