The Wumpus Game

Stench Breeze

Breeze Breeze
Stench
Gold
Stench Breeze

Start ⇒ Breeze Breeze

Cao Hoang Tru 1
CSE Faculty - HCMUT 02 November 2009

The Wumpus Game
• Stench in the square containing the wumpus and
in the directly adjacent squares
• Breeze in the squares directly adjacent to a pit
• Glitter in the square where the gold is
• Bump when an agent walks into a wall
• Scream when the wumpus is killed

Cao Hoang Tru 2
CSE Faculty - HCMUT 02 November 2009

The Wumpus Game
• Agent’s percept: [Stench, Breeze, Glitter, Bump, Scream]
• Agent’s actions:
– Go forward, turn right 90o, turn left 90o
– Grab to pick up an object in the same square
– Fire 1 arrow in a straight line in the faced direction
– Climb to leave the cave

• Death if entering a square of a pit or a live wumpus
• Agent’s goal: find and bring the gold back to the
start
Cao Hoang Tru 3
CSE Faculty - HCMUT 02 November 2009

The Wumpus Game Stench Breeze A = Agent B = Breeze G = Glitter/Gold Breeze Breeze Stench OK = Safe square Gold P = Pit Stench Breeze S = Stench OK V = Visited W = Wumpus Start ⇒ Breeze Breeze A OK Cao Hoang Tru 4 CSE Faculty .HCMUT 02 November 2009 .

The Wumpus Game Stench Breeze A = Agent B = Breeze G = Glitter/Gold Breeze Breeze Stench OK = Safe square Gold P = Pit Stench Breeze S = Stench OK P? V = Visited W = Wumpus Start ⇒ Breeze Breeze V A P? Cao Hoang Tru 5 CSE Faculty .HCMUT 02 November 2009 .

The Wumpus Game Stench Breeze A = Agent B = Breeze G = Glitter/Gold Breeze Breeze Stench OK = Safe square Gold P = Pit Stench Breeze S = Stench A OK V = Visited W = Wumpus Start ⇒ Breeze Breeze V V Cao Hoang Tru 6 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . The Wumpus Game Stench Breeze A = Agent B = Breeze G = Glitter/Gold Breeze Breeze Stench OK = Safe square Gold P = Pit Stench Breeze S = Stench V A V = Visited W = Wumpus Start ⇒ Breeze Breeze V V Cao Hoang Tru 7 CSE Faculty .

HCMUT 02 November 2009 . The Wumpus Game Stench Breeze A = Agent B = Breeze G = Glitter/Gold Breeze Breeze Stench OK = Safe square Gold A P = Pit Stench Breeze S = Stench V V V = Visited W = Wumpus Start ⇒ Breeze Breeze V V Cao Hoang Tru 8 CSE Faculty .

⇒. … • Logical connectives: ¬. Propositional Logic Syntax • Logical constants: true. ∨. and α ⇔ β Cao Hoang Tru 9 CSE Faculty . then so are ¬α and (α) – If α and β are sentences.HCMUT 02 November 2009 . α ∨ β. ⇔ • Sentences (formulas): – Logical constants – Proposition symbols – If α is a sentence. Q. false • Propositional symbols: P. ∧. then so are α ∧ β. α ⇒ β.

Propositional Logic Semantics • Interpretation: propositional symbol → true/false • The truth value of a sentence is defined by the truth table P Q ¬P P∧Q P∨Q P⇒Q P⇔Q false false true false false true true false true true false true true false true false false false true false false true true false true true true true Cao Hoang Tru 10 CSE Faculty .HCMUT 02 November 2009 .

Propositional Logic Semantics • Satisfiable: true under an interpretation • Valid: true under all interpretations P Q P ∧ ¬P (P ∨ Q) ∧ ¬Q ((P ∨ Q) ∧ ¬Q) ⇒ P false false false false true false true false false true true false false true true true true false false true unsatisfiable satisfiable valid Cao Hoang Tru 11 CSE Faculty .HCMUT 02 November 2009 .

Propositional Logic Semantics • Model: an interpretation under which the sentence is true P∧Q P∨Q   P  Q P Q P⇒Q P⇔Q   P Q P Q  Cao Hoang Tru 12 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . P {P ⇒ Q. Propositional Logic Semantics • Entailment: KB |= α iff every model of KB is a model of α α is a logical consequence of KB P Q P⇒Q P ⇒ Q. P} |= Q false false true false false true true false true false false false true true true true Cao Hoang Tru 13 CSE Faculty .

Propositional Logic Semantics • Equivalence: α ≡ β iff α |= β and β |= α P Q P⇒Q ¬P ∨ Q P ⇒ Q ≡ ¬P ∨ Q false false true true false true true true true false false false true true true true Cao Hoang Tru 14 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Propositional Logic Semantics • Theorems: – α |= β iff α ⇒ β is valid KB |= α can be proved by validity of KB ⇒ α – α |= β iff α ∧ ¬β is unsatisfiable KB |= α can be proved by refutation of KB ∧ ¬α Cao Hoang Tru 15 CSE Faculty .

α2.HCMUT 02 November 2009 ... αn  Conclusion: β • Soundness: R is sound iff {α1. αn} |= β Cao Hoang Tru 16 CSE Faculty . . . Inference Rules • Rule R: Premises: α1. .. . α2..

Inference Rules
• Modus Ponens:
α ⇒ β, α

β

• ∧-Elimination:
α1 ∧ α2 ∧ ... ∧ αn

αi

Cao Hoang Tru 17
CSE Faculty - HCMUT 02 November 2009

Inference Rules
• ∧-Introduction:
α1, α2, … , αn

α1 ∧ α2 ∧ … ∧ αn

• ∨-Introduction:
αi

α1 ∨ α2 ∨ … ∨ αn

Cao Hoang Tru 18
CSE Faculty - HCMUT 02 November 2009

Inference Rules
• Double-negation elimination:
¬¬α

α

Cao Hoang Tru 19
CSE Faculty - HCMUT 02 November 2009

Q1 ∨ … ∨ Qj ∨ … ∨ Qm Pi ≡ ¬Qj  P1 ∨ … ∨ Pi-1 ∨ Pi+1 ∨ … ∨ Pn ∨ Q1 … ∨ Qj-1 ∨ Qj+1 ∨ … ∨ Qm Cao Hoang Tru 20 CSE Faculty . Resolution • Unit resolution: P1 ∨ … ∨ Pi ∨ … ∨ Pn.HCMUT 02 November 2009 . Q Pi ≡ ¬Q  (all Pi and Q are literals) P1 ∨ … ∨ Pi-1 ∨ Pi+1 ∨ … ∨ Pn • Full resolution: P1 ∨ … ∨ Pi ∨ … ∨ Pn.

HCMUT 02 November 2009 . Resolution • Conjunctive normal form (CNF): conjunction of disjunctions of literals (L11 ∨ … ∨ L1k) ∧ … ∧ (Ln1 ∨ … ∨ Lnm) clause • k-CNF: each clause contains at most k literals • Every sentence can be written in CNF Cao Hoang Tru 21 CSE Faculty .

Resolution • Conjunctive normal form (CNF): conjunction of disjunctions of literals (P ∧ Q) ⇒ R ≡ ¬(P ∧ Q) ∨ R ≡ ¬P ∨ ¬Q ∨ R (S ∨ T) ⇒ Q ≡ ¬(S ∨ T) ∨ Q ≡ (¬S ∨ Q) ∧ (¬T ∨ Q) Cao Hoang Tru 22 CSE Faculty .HCMUT 02 November 2009 .

.HCMUT 02 November 2009 .e. Inference Algorithms • To derive/assert a sentence given a knowledge base (i. a set of sentences) • Inference algorithm = Inference rules + a searching algorithm Cao Hoang Tru 23 CSE Faculty .

HCMUT 02 November 2009 . Inference Algorithms Algorithm PL-Resolution(KB. Cj) if Resolvents contains an empty clause then return true New = New ∪ Resolvents if New ⊆ Clauses then return false Clauses = Clauses ∪ New Cao Hoang Tru 24 CSE Faculty . α) input: KB and α are PL sentences output: true (to assert KB |= α) or false (otherwise) Clauses = set of clauses in the CNF representation of KB ∧ ¬α New = {} loop for each Ci. Cj in Clauses do Resolvents = PL-Resolve(Ci.

T} α=R Cao Hoang Tru 25 CSE Faculty . (P ∧ Q) ⇒ R.HCMUT 02 November 2009 . Inference Algorithms • Example: KB = {P. (S ∨ T) ⇒ Q.

Inference Algorithms • Soundness: every deduced answer is correct Cao Hoang Tru 26 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Inference Algorithms • Completeness: every correct answer is deducible Cao Hoang Tru 27 CSE Faculty .

Inference Algorithms • Example: Google search Cao Hoang Tru 28 CSE Faculty .HCMUT 02 November 2009 .

Inference Algorithms Soundness Completeness Complexity Cao Hoang Tru 29 CSE Faculty .HCMUT 02 November 2009 .

Inference Algorithms • Soundness: every deduced answer is correct if PL-Resolution returns true then KB |= α Cao Hoang Tru 30 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Inference Algorithms • Completeness: every correct answer is deducible if KB |= α then PL-Resolution returns true Cao Hoang Tru 31 CSE Faculty .

Inference Algorithms • PL-Resolution is sound and complete Cao Hoang Tru 32 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Q ⇒ R} α=P⇒R Cao Hoang Tru 33 CSE Faculty . Inference Algorithms • Modus Ponens with any searching algorithm is not complete KB = {P ⇒ Q.

Inference Algorithms • Horn clause: disjunction of literals of which at most one is positive ¬P1 ∨ ¬P2 ∨ … ∨ ¬Pn ∨ Q or P1 ∧ P2 ∧ … ∧ Pn ⇒ Q • Forward or backward chaining can be used for inference on Horn clauses Cao Hoang Tru 34 CSE Faculty .HCMUT 02 November 2009 .

Satisfiability Problem • SAT problem: to determine if a sentence is satisfiable (to determine if the symbols in the sentence can be assigned true/false as to make it evaluate to true) (¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E) ∧ (E ∨ ¬D ∨ B) ∧ (B ∨ E ∨ ¬C) Cao Hoang Tru 35 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Satisfiability Problem • SAT problem in propositional logic is NP-complete This does not mean all instances of PL inference has the exponential complexity Polynomial-time inference procedures for Horn clauses Cao Hoang Tru 36 CSE Faculty .

Satisfiability Problem • Many combinatorial problems in computer science can be reduced to SAT Cao Hoang Tru 37 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Loveland (1962) Cao Hoang Tru 38 CSE Faculty . Logemann. Satisfiability Problem • Backtracking algorithms • Davis and Putnam (1960) DPLL • Davis.

HCMUT 02 November 2009 . Satisfiability Problem • DPLL algorithm: – Recursive. depth-first enumeration of possible models Cao Hoang Tru 39 CSE Faculty .

HCMUT 02 November 2009 . Cao Hoang Tru 40 CSE Faculty . Satisfiability Problem • DPLL algorithm: – Early termination: a clause is true if any literal is true (A ∨ B) ∧ (A ∨ C) is true if A is true regardless of B and C.

HCMUT 02 November 2009 . Cao Hoang Tru 41 CSE Faculty . Satisfiability Problem • DPLL algorithm: – Pure symbol: has the same sign in all clauses (A ∨ ¬B) ∧ (¬B ∨ ¬C) ∧ (C ∨ A) A model would have the literals of pure symbols assigned true. Satisfied clause removal makes new pure symbols.

Satisfiability Problem • DPLL algorithm: – Unit clause: has just one literal (A ∨ B) ∧ ¬ B Unit clause satisfaction makes new unit clauses (unit propagation).HCMUT 02 November 2009 . Cao Hoang Tru 42 CSE Faculty .

value) = UNIT-CLAUSE(Clauses.P. Symbols .P. Symbols.HCMUT 02 November 2009 . Model)) (P. EXT(P. EXT(P. Statisfiability Problem Algorithm DPLL(Clauses. false.P. Symbols . Symbols .P. Model)) choose a P in Symbols return DPLL(Clauses. EXT(P. value. Symbols . EXT(P. Model) if P ≠ null then return DPLL(Clauses. Symbols . Model)) or DPLL(Clauses. Symbols . Model) if P ≠ null then return DPLL(Clauses. Model)) Cao Hoang Tru 43 CSE Faculty . true. value. value) = PURE-SYMBOL(Clauses. Model) if every clause in Clauses is true in Model then return true if some clause in Clauses is false in Model then return false (P.

HCMUT 02 November 2009 . Satisfiability Problem • DPLL algorithm: (¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E) ∧ (E ∨ ¬D ∨ B) ∧ (B ∨ E ∨ ¬C) Cao Hoang Tru 44 CSE Faculty .

HCMUT 02 November 2009 . Satisfiability Problem • Local search algorithms Evaluation function counts the number of unsatisfied clauses Cao Hoang Tru 45 CSE Faculty .

HCMUT 02 November 2009 . Satisfiability Problem • Local search algorithms Evaluation function counts the number of unsatisfied clauses • Randomness to escape local minima Flipping the truth value of one symbol at a time Cao Hoang Tru 46 CSE Faculty .

p. max_flips) /*1996*/ input: p is flipping probability. of satisfied clauses return failure Cao Hoang Tru 47 CSE Faculty .HCMUT 02 November 2009 . Statisfiability Problem Algorithm WalkSAT(Clauses. max_flips is number of flips allowed output: a model or failure Model = a random assignment of truth values to the symbols for i = 1 to max_flips do if Model satisfies Clauses then return Model C = a randomly selected clause from Clauses that is false in Model with probability p flip the value in Model of a symbol from C else flip whichever symbol in C to maximizes no.

Satisfiability Problem • WalkSAT algorithm: (¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ B ∨ E) ∧ (E ∨ ¬D ∨ B) ∧ (¬A ∨ E ∨ ¬C) Initial model: A = true B = false C = true D = true E = false Cao Hoang Tru 48 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Cao Hoang Tru 49 CSE Faculty . the less likely the clause is satisfiable. Satisfiability Problem • The larger the ratio clause/symbol is.

Satisfiability Problem • The probability of satisfiability drops sharply around the ratio of 4. Cao Hoang Tru 50 CSE Faculty .HCMUT 02 November 2009 .3.

Cao Hoang Tru 51 CSE Faculty . Satisfiability Problem • That is actually when the problem is hard.HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Satisfiability Problem • WalkSAT is much faster than DPLL. Cao Hoang Tru 52 CSE Faculty .

Inference Monotonicity • Monotonicity: the set of entailed sentences can only increase when information is added to the KB if KB1 |= α then (KB1∪KB2) |= α Cao Hoang Tru 53 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Inference Monotonicity • Propositional logic is monotone Cao Hoang Tru 54 CSE Faculty .

HCMUT 02 November 2009 . An Agent for the Wumpus Game • Knowledge Base: Xij = column i. row j Stench Breeze ¬S11 ¬B11 4 ¬S21 B21 Breeze Breeze S12 ¬B12 3 Stench Gold Stench Breeze 2 Start ⇒ Breeze Breeze 1 Cao Hoang Tru 1 2 3 4 55 CSE Faculty .

HCMUT 02 November 2009 . An Agent for the Wumpus Game • Knowledge Base: Xij = column i. row j R1: ¬S11 ⇒ ¬W11 ∧ ¬W12 ∧ ¬W21 R2: ¬S21 ⇒ ¬W11 ∧ ¬W21 ∧ ¬W22 ∧ ¬W31 R3: ¬S12 ⇒ ¬W11 ∧ ¬W12 ∧ ¬W22 ∧ ¬W13 R4: S12 ⇒ W13 ∨ W12 ∨ W22 ∨ W11 Cao Hoang Tru 56 CSE Faculty .

Agent for the Wumpus Game • Finding the wumpus: Modus Ponens (¬S11 + R1): ¬W11 ∧ ¬W12 ∧ ¬W21 ∧-Elimination: ¬W11 ¬W12 ¬W21 Modus Ponens (¬S21 + R2): ¬W11 ∧ ¬W21 ∧ ¬W22 ∧ ¬W31 ∧-Elimination: ¬W11 ¬W21 ¬W22 ¬W31 Modus Ponens (S12 + R4): W13 ∨ W12 ∨ W22 ∨ W11 Unit resolution (¬W11 + W13 ∨ W12 ∨ W22 ∨ W11): W13 ∨ W12 ∨ W22 Unit resolution (¬W22 + W13 ∨ W12 ∨ W22): W13 ∨ W12 Unit resolution (¬W12 + W13 ∨ W12): W13 Cao Hoang Tru 57 CSE Faculty .HCMUT 02 November 2009 .

Agent for the Wumpus Game • Translating knowledge into action: A11 ∧ EastA ∧ W21 ⇒ ¬Forward Cao Hoang Tru 58 CSE Faculty .HCMUT 02 November 2009 .

– Hard to deal with time and change – Not expressive enough to represent or answer a question like "What action should the agent take?". Agent for the Wumpus Game • Problems with the propositional agent: – Too many propositions to handle: "Don’t go forward if the wumpus is in front of you!" requires 16 x 4 = 64 propositions. … Cao Hoang Tru 59 CSE Faculty .HCMUT 02 November 2009 .

HCMUT 02 November 2009 . Homework • In Russell & Norvig’s AIMA (2nd ed.): Exercises of Chapter 7. Cao Hoang Tru 60 CSE Faculty .