informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...logical agents...

102
Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2015 Artificial Intelligence: A Modern Approach, 3 rd Edition, Chapter 7 Soleymani

Upload: others

Post on 29-Mar-2021

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logical AgentsCE417: Introduction to Artificial IntelligenceSharif University of TechnologySpring 2015

“Artificial Intelligence: A Modern Approach”, 3rd Edition, Chapter 7

Soleymani

Page 2: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Knowledge-based agents

2

Knowledge-based agents

Knowledge representation

Logic as a general class of representation

Propositional logic

First-order logic

Reasoning operates on internal representation of

knowledge

Page 3: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

A generic knowledge-based agent

3

function KB_AGENT(𝑝𝑒𝑟𝑐𝑒𝑝𝑡) returns an 𝑎𝑐𝑡𝑖𝑜𝑛

persistent:𝐾𝐵, a knowledge base

𝑡, 𝑎 counter for time, initially 0

TELL(𝐾𝐵,MAKE_PERCEPT_SENTENCE(𝑝𝑒𝑟𝑐𝑒𝑝𝑡, 𝑡))

𝑎𝑐𝑡𝑖𝑜𝑛 ← ASK(𝐾𝐵,MAKE_ACTION_QUERY(𝑡))

TELL(𝐾𝐵,MAKE_ACTION_SENTENCE(𝑎𝑐𝑡𝑖𝑜𝑛, 𝑡))

𝑡 ← 𝑡 + 1

return 𝑎𝑐𝑡𝑖𝑜𝑛

The agent must be able to: Represent states, actions, percepts,….

Incorporate new percepts

Update internal representations of the world

Deduce hidden properties of the world

Deduce appropriate actions

• Makes a sentence asserting that the agent

perceived the given percept at the given time.

• Makes a sentence that asks what action

should be done at the current time.

• Makes a sentence asserting that the chosen

action was executed.

Page 4: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Knowledge Base (KB)

4

KB = a set of sentences expressed in a knowledge

representation language

TELL: adds new sentences to the knowledge base

ASK: asks a question of KB

the answer follows from previously TELLed sentences to the KB

Inference: derives new sentences from old ones

Basis of TELL and ASK operations

Page 5: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world

5

Wumpus

Wumpus

Pitts

Gold

Agent

Page 6: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world PEAS description

Performance measure

+1000 for garbing gold

-1000 for death

-1 for each action

-10 for using up the arrow

Game ends when the agent dies or climbs out of the cave

Environment

4×4 grid

Agent starts in [1,1] while facing to the right

Gold and wumpus are located randomly in the squares except to [1,1]

Each square other than [1,1] can be a pit with probability 0.2

6

Page 7: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world PEAS description

7

Sensors: Stench, Breeze, Glitter, Bump, Scream

In the squares adjacent to wumpus, agent perceives a Stench

It the squares adjacent to a pit, agent perceives a Breeze

In the gold square, agent perceives a Glitter

When walking into a wall, agent perceives a Bump

When Wumpus is killed, agent perceives a Scream

Actuators: Forward, TurnLeft, TurnRight, Shoot, Grab, Climb

Forward,TurnLeft,TurnRight: moving and rotating face actions.

Moving to a square containing a pit or a live wumpus causes death.

If an agent tries to move forward and bumps into a wall then it does not move.

Shoot: to fire an arrow in a straight line in the facing direction of the agent

Shooting kills wumpus if the agent is facing it (o.w. the arrow hits a wall)

The first shoot action has any effect (the agent has only one arrow)

Grab: to pick up the gold if it is in the same square as the agent.

Climb: climb out of the cave but only from [1,1]

Page 8: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world characterization

Fully Observable? No – many aspects are not directly

perceivable

Episodic? No – sequential at the level of actions

Static? Yes

Discrete?Yes

Single-agent?Yes

8

Page 9: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

9

Page 10: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

A: agent

OK: safe square

B: Breeze

10

Page 11: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

11

Page 12: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

S: Stench

12

Page 13: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

S: Stench

13

Page 14: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

S: Stench

14

Page 15: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

S: Stench

15

Page 16: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example

A: agent

OK: safe square

B: Breeze

P: Pit

S: Stench

16

Page 17: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logic & Language

Logic includes formal languages for representing informationsuch that conclusions can be drawn

Syntax defines the well-formed sentences in a language

Semantics define the "meaning" of sentences i.e., define truth of a sentence with respect to each possible world

17

Page 18: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logics Types

18

Ontological commitment: what exists- facts? objects? time? beliefs?

Epistemological commitment: what states of knowledge?

Language Ontological

Commitment

Epistemological

Commitment

Propositional logic Facts True/false/unknown

First-order logic Facts, objects, relations True/false/unknown

Temporal logic Facts, objects, relations, times True/false/unknown

Probability logic Facts [0,1]

Fuzzy logic Facts, degree of truth Known interval values

Page 19: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Propositional logic: Syntax

Propositional logic is the simplest logic

To illustrate basic ideas about logic & reasoning

The proposition symbols 𝑃,𝑄,… are sentences.

If 𝑃 is a sentence,𝑃 is a sentence (negation)

If 𝑃 and 𝑄 are sentences,𝑃𝑄 is a sentence (conjunction)

If 𝑃 and 𝑄 are sentences,𝑃𝑄 is a sentence (disjunction)

If 𝑃 and 𝑄 are sentences,𝑃 𝑄 is a sentence (implication)

If 𝑃 and 𝑄 are sentences,𝑃 𝑄 is a sentence (biconditional)

19

Page 20: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Propositional logic (Grammar)

20

𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 → 𝐴𝑡𝑜𝑚𝑖𝑐𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 | 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒𝐴𝑡𝑜𝑚𝑖𝑐𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 → 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝑃 𝑄 𝑅 | …𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 → (𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒)

| ¬𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒| 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 ∧ 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒| 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 ∨ 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒| 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 ⟹ 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒| 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 ⇔ 𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒

𝑃𝑟𝑒𝑐𝑒𝑑𝑒𝑛𝑐𝑒: ¬, ∧, ∨, ⟹, ⇔

Page 21: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Truth tables for connectives

21

Page 22: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Propositional logic: Semantics

22

Each model specifies true/false of proposition symbols

e.g., Propistion symbols:𝑃1,2, 𝑃2,2, 𝑃3,1 𝑚1 = {𝑃1,2 = 𝑓𝑎𝑙𝑠𝑒, 𝑃2,2 = 𝑓𝑎𝑙𝑠𝑒, 𝑃3,1 = 𝑡𝑟𝑢𝑒}

Semantics of a complex sentence in any model m:

𝑃 is true iff 𝑃 is false in 𝑚.

𝑃𝑄 is true iff 𝑃 is true and 𝑄 is true in 𝑚.

𝑃 ∨ 𝑄 is true iff 𝑃 is true or 𝑄 is true in 𝑚.

𝑃 ⟹ 𝑄 is true unless 𝑃 is true and 𝑄 is false in 𝑚.

𝑃 ⟺ 𝑄 is true iff 𝑃 and 𝑄 are both true or both false in 𝑚.

Page 23: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world sentences

𝑃𝑖,𝑗 is true if there is a pit in [𝑖, 𝑗].

𝑊𝑖,𝑗 is true if there is a wumpus in [𝑖, 𝑗], dead or alive.

𝐵𝑖,𝑗 is true if the agent perceives a breeze in [𝑖, 𝑗].

𝑆𝑖,𝑗 is true if the agent perceives a stench in [𝑖, 𝑗].

General rules (only related ones to the current agent position) 𝑅1: 𝑃1,1 (no pit in [1,1])

𝑅2: 𝐵1,1 ⇔ 𝑃1,2 ∨ 𝑃2,1 (Pits cause breezes in adjacent squares)

𝑅3: 𝐵2,1 ⇔ 𝑃1,1 ∨ 𝑃2,2 ∨ 𝑃3,1 (Pits cause breezes in adjacent squares)

Perception

𝑅4: 𝐵1,1 𝑅5: 𝐵2,1

23

Page 24: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Models

Models are mathematical abstraction of possible worlds Each model fixes the truth or falsehood of every relevant sentence.

We can consider a set for each logical sentence that specifies

all possible worlds in which 𝛼 is true

𝑀(𝛼): set of all models of the sentence 𝛼 (all satisfying 𝛼) 𝑚 ∈ 𝑀(𝛼) if 𝛼 is true in model 𝑚 E.g., For 𝛼: 𝑥 + 𝑦 = 4, all possible assignments of values to 𝑥, 𝑦 are

models.𝑀(𝛼) contains a subset of models satisfying 𝑥 + 𝑦 = 4.

24

Page 25: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

25

Agents starts at [1,1] with no active sensor,

then moves to [2,1] and senses Breeze in it

Possible models for pits in [1,2], [2,2], [3,1]:

Page 26: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logical reasoning: entailment

26

𝛼 ⊨ 𝛽 (entailment):𝛼 entails 𝛽 𝛼 ⇒ 𝛽 is a tautology or valid

A sentence is valid or tautology if it is T𝑟𝑢𝑒 in all models (e.g., 𝑃𝑃,

(𝑃(𝑃𝑄))𝑄)

𝛽 logically follows from 𝛼 or 𝛼 is stronger than 𝛽

𝛼 ⊨ 𝛽 iff 𝑀 𝛼 𝑀 𝛽 𝛼 entails 𝛽 iff 𝛽 is true in all worlds where 𝛼 is true

Page 27: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Entailment

𝐾𝐵 ⊨ 𝛼 iff 𝑀(𝐾𝐵) ⊆ 𝑀(𝛼)

Example: KB=“A is red” & “B is blue”

α = “A is red”

27

Page 28: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Entailment in the wumpus world

𝐾𝐵 (before perception): rules of the

wumpus world

Perception: After detecting nothing in

[1,1], moving right, a breeze in [1,2]

28

Page 29: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

29

Possible models for pits in [1,2], [2,2], [3,1]

Consider possible models for only

pits in neighboring squares 23 = 8possible models

Page 30: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

𝐾𝐵 = wumpus-world rules + perceptions

30

Page 31: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

𝐾𝐵 = wumpus-world rules + perceptions

𝛼1 = "[1,2] is safe"

𝑀(𝐾𝐵) ⊆ 𝑀(𝛼1) ⇒ 𝐾𝐵 ⊨ 𝛼1

31

Page 32: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

KB = wumpus-world rules + perceptions

32

Page 33: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus models

𝐾𝐵 = wumpus-world rules + perceptions

𝛼2 = "[2,2] is safe",𝐾𝐵 ⊭ 𝛼2

33

Page 34: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Inference

𝐾𝐵├𝑖𝛼:𝛼 can be derived from 𝐾𝐵 by an inference algorithm 𝑖

An inference algorithm 𝑖 is: sound if whenever 𝐾𝐵├𝑖𝛼, it is also true that 𝐾𝐵 ⊨ 𝛼

all provable statements by this algorithm are true

complete if whenever 𝐾𝐵 ⊨ 𝛼, it is also true that 𝐾𝐵├𝑖𝛼 all true statements are provable by this algorithm

34

Page 35: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Inference methods

Inference methods can be categorized into: Model checking (enumerating models)

truth table enumeration (always exponential in 𝑛)

improved backtracking, e.g., Davis-Putnam-Logemann-Loveland (DPLL)

heuristic search in model space (sound but incomplete)

e.g., min-conflicts-like hill-climbing algorithms

Theorem proving (searching proofs by applying inference rules)

Applying a sequence of inference rules on KB to find the desired sentence

Legitimate (sound) generation of new sentences from old ones.

35

Page 36: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Truth tables for inference

36

𝐾𝐵 ⊨ 𝛼?

Enumerate the models

“Is 𝛼 true in every model in which KB is true?”

𝑅1: 𝑃1,1𝑅2: 𝐵1,1 ⇔ 𝑃1,2 ∨ 𝑃2,1𝑅3: 𝐵2,1 ⇔ 𝑃1,1 ∨ 𝑃2,2 ∨ 𝑃3,1𝑅4: 𝐵1,1𝑅5: 𝐵2,1

KB

Page 37: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Entailment by enumeration (model checking)

37

function 𝑇𝑇_𝐸𝑁𝑇𝐴𝐼𝐿𝑆? (𝐾𝐵, 𝛼) returns 𝑡𝑟𝑢𝑒 or 𝑓𝑎𝑙𝑠𝑒

inputs: 𝐾𝐵, the knowledge base, a sentence in propositional logic

𝛼, the query, a sentence in propositional logic

𝑠𝑦𝑚𝑏𝑜𝑙𝑠 ← a list of the proposition symbols in 𝐾𝐵 and 𝛼

return 𝑇𝑇_𝐶𝐻𝐸𝐶𝐾_𝐴𝐿𝐿(𝐾𝐵, 𝛼 , 𝑠𝑦𝑚𝑏𝑜𝑙𝑠, {})

function 𝑇𝑇_𝐶𝐻𝐸𝐶𝐾_𝐴𝐿𝐿(𝐾𝐵, 𝛼, 𝑠𝑦𝑚𝑏𝑜𝑙𝑠, 𝑚𝑜𝑑𝑒𝑙) returns 𝑡𝑟𝑢𝑒 or 𝑓𝑎𝑙𝑠𝑒

if 𝐸𝑀𝑃𝑇𝑌? ( 𝑠𝑦𝑚𝑏𝑜𝑙𝑠) then

if 𝑃𝐿_𝑇𝑅𝑈𝐸? (𝐾𝐵,𝑚𝑜𝑑𝑒𝑙) then return 𝑃𝐿_𝑇𝑅𝑈𝐸? (𝛼,𝑚𝑜𝑑𝑒𝑙)

else return 𝑡𝑟𝑢𝑒

else

𝑃 ← 𝐹𝐼𝑅𝑆𝑇(𝑠𝑦𝑚𝑏𝑜𝑙𝑠)

𝑟𝑒𝑠𝑡 ← 𝑅𝐸𝑆𝑇(𝑠𝑦𝑚𝑏𝑜𝑙𝑠)

return 𝑇𝑇_𝐶𝐻𝐸𝐶𝐾_𝐴𝐿𝐿(𝐾𝐵, 𝛼, 𝑟𝑒𝑠𝑡,𝑚𝑜𝑑𝑒𝑙⋃{𝑃 = 𝑡𝑟𝑢𝑒}) and

𝑇𝑇_𝐶𝐻𝐸𝐶𝐾_𝐴𝐿𝐿(𝐾𝐵, 𝛼, 𝑟𝑒𝑠𝑡,𝑚𝑜𝑑𝑒𝑙⋃{𝑃 = 𝑓𝑎𝑙𝑠𝑒})

Page 38: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Entailment by enumeration: properties

38

Depth-first enumeration of all models is sound and complete

(when finite models).

For 𝑛 symbols, time complexity is 𝑂(2𝑛), space complexity is

𝑂(𝑛).

Page 39: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Entailment by using inference rule

39

Propositional theorem proving

Searching proofs can be more efficient than model

checking.

Can ignore irrelevant propositions.

When the number of models is large but the length of proof is

short, entailment by theorem proving is useful.

Page 40: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Inference rules

40

Some samples:

Modus Ponens:α⇒β, α

β

And elimination:𝛼∧𝛽

𝛽

Biconditional elimination:𝛼⇔𝛽

(𝛼⇒𝛽)∧(𝛽⇒𝛼)

Page 41: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Valid implication

41

Deduction theorem: 𝛼 ⊨ 𝛽 iff 𝛼 ⇒ 𝛽 is valid.

Every valid implication describes a legitimate inference.

Modus Ponens:α⇒β, α

βis equivalent to valid implication

α ⇒ β ∧ α ⇒ β is valid

All logical entailments can be used as inference rules.

Page 42: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logical equivalence

Using logical equivalence we can find many inference rules

Two sentences are logically equivalent iff they are true in the

same models

𝛼 ≡ 𝛽 iff 𝛼 ⊨ 𝛽 and 𝛽 ⊨ 𝛼

42

Page 43: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Example of inference

43

𝑅1: 𝑃1,1

𝑅2: 𝐵1,1 ⇔ 𝑃1,2 ∨ 𝑃2,1

𝑅3: 𝐵2,1 ⇔ 𝑃1,1 ∨ 𝑃2,2 ∨ 𝑃3,1 𝑅4: 𝐵1,1 𝑅5: 𝐵2,1

Can we infer from 𝑅1 through 𝑅5 to prove 𝑃1,2?

𝑅6: 𝐵1,1 ⇒ 𝑃1,2 ∨ 𝑃2,1 ∧ 𝑃1,2 ∨ 𝑃2,1 ⇒ 𝐵1,1 [biconditional elim. to 𝑅2]

𝑅7: 𝑃1,2 ∨ 𝑃2,1 ⇒ 𝐵1,1 [and elim. to 𝑅6]

𝑅8: ¬𝐵1,1 ⇒ ¬ 𝑃1,2 ∨ 𝑃2,1 [(𝑃 ⇒ 𝑄) ≡ (¬𝑄 ⇒ ¬𝑃)]

𝑅9: ¬ 𝑃1,2 ∨ 𝑃2,1 [Modus Ponens with 𝑅8 and 𝑅4]

𝑅10: ¬𝑃1,2 ∧ ¬𝑃2,1 [De Morgan’ rule]

Page 44: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Finding a proof as a search problem

44

𝐼𝑁𝐼𝑇𝐼𝐴𝐿_𝑆𝑇𝐴𝑇𝐸: the initial knowledge base

𝐴𝐶𝑇𝐼𝑂𝑁𝑆: all inference rules applied to all the sentences

that match their top line

𝑅𝐸𝑆𝑈𝐿𝑇: add the sentence in the bottom line of the used

inference rule to the current ones

𝐺𝑂𝐴𝐿: a state containing the sentence we are trying to

prove.

Page 45: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Monotonicity property

45

The set of entailed sentences can only grow as

information is added to 𝐾𝐵.

Inference rules can be applied where premises are found in KB

(regardless of what else is in KB)

If 𝐾𝐵 ⊨ 𝛼 then 𝐾𝐵 ∧ 𝛽 ⊨ 𝛼 (for any 𝛼 and 𝛽)

The assertion 𝛽 can not invalidate any conclusion 𝛼 already

inferred.

Page 46: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Resolution

46

Inference rules are sound.

Is a set of rules complete?

Are the available inference rules adequate?

Resolution: a single inference rule that yields a complete

inference algorithm (when couples with any complete

search algorithm).

Page 47: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Resolution

47

Unit resolution: 𝑙𝑖 and 𝑚 are complementary literals

𝑙1 ∨ 𝑙2 ∨ ⋯∨ 𝑙𝑘 , 𝑚

𝑙1 ∨ …∨ 𝑙𝑖−1 ∨ 𝑙𝑖+1 ∨ ⋯∨ 𝑙𝑘

Full resolution rule: 𝑙𝑖 and 𝑚𝑗 are complementary literals

𝑙1 ∨ 𝑙2 ∨ ⋯∨ 𝑙𝑘 , 𝑚1∨𝑚2 ∨ ⋯∨𝑚𝑛

𝑙1 ∨ …∨ 𝑙𝑖−1 ∨ 𝑙𝑖+1 ∨ ⋯∨ 𝑙𝑘 ∨ 𝑚1 ∨ …∨𝑚𝑗−1 ∨ 𝑚𝑗+1 ∨ ⋯∨𝑚𝑛

It resolution rule sound.Why?

A resolution-based theorem prover can (for any sentences 𝛼 and 𝛽 in

propositional logic) decide whether 𝛼 ⊨ 𝛽 Is 𝛼 ∧ ¬𝛽 unsatisfiable?

CNF (Conjunctive Normal Form)

Page 48: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Satisfiability

48

A sentence is satisfiable if it is true in some models

e.g.,𝑃𝑄

Determining the satisfiability of sentences in propositional

logic (SAT problem) is NP-complete

Satisfiability and validity are connected.

𝛼 is valid or tautology iff ¬𝛼 is unsatisfiable.

𝛼 is satisfiable iff ¬𝛼 is not valid.

𝛼 ⊨ 𝛽 iff (𝛼 ∧ ¬𝛽) is unsatisfiable

Proof by contradiction

Page 49: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

CNF Grammar

49

𝐶𝑁𝐹𝑆𝑒𝑛𝑡𝑒𝑛𝑐𝑒 → 𝐶𝑙𝑎𝑢𝑠𝑒1 ∧ ⋯∧ 𝐶𝑙𝑎𝑢𝑠𝑒𝑛

𝐶𝑙𝑎𝑢𝑠𝑒 → 𝐿𝑖𝑡𝑒𝑟𝑎𝑙1 ∨ ⋯∨ 𝐿𝑖𝑡𝑒𝑟𝑎𝑙𝑚

𝐿𝑖𝑡𝑒𝑟𝑎𝑙 → 𝑆𝑦𝑚𝑏𝑜𝑙 | ¬𝑆𝑦𝑚𝑏𝑜𝑙

𝑆𝑦𝑚𝑏𝑜𝑙 → 𝑃 𝑄 𝑅 | …

Page 50: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Example of resolution

50

𝑃1,1 ∨ 𝑃3,1, ¬𝑃1,1∨ ¬𝑃2,2𝑃3,1 ∨ ¬𝑃2,2

Page 51: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Resolution algorithm

To show 𝐾𝐵 ⊨ 𝛼, we show 𝐾𝐵 ∧ ¬𝛼 is unsatisfiable

51

function PL-RESOLUTION(KB,α) returns 𝑡𝑟𝑢𝑒 or 𝑓𝑎𝑙𝑠𝑒input: 𝐾𝐵, the knowledge base (a sentence in propositional logic)

𝛼, the query (a sentence in propositional logic)

𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ← the set of clauses in the CNF representation of 𝐾𝐵¬𝛼𝑛𝑒𝑤 ← {}

loop do

for each pair of clauses 𝐶𝑖 , 𝐶𝑗 in 𝑐𝑙𝑎𝑢𝑠𝑒𝑠 do

𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠 ← 𝑃𝐿_𝑅𝐸𝑆𝑂𝐿𝑉𝐸 (𝐶𝑖, 𝐶𝑗)

if 𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠 contains the empty clause then return 𝑡𝑟𝑢𝑒

𝑛𝑒𝑤 ← 𝑛𝑒𝑤 ∪ 𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠

if 𝑛𝑒𝑤 ⊆ 𝐶𝑙𝑎𝑢𝑠𝑒𝑠 then return 𝑓𝑎𝑙𝑠𝑒

𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ← 𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ∪ 𝑛𝑒𝑤

Page 52: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Resolution algorithm

To show 𝐾𝐵 ⊨ 𝛼, we show 𝐾𝐵 ∧ ¬𝛼 is unsatisfiable

52

function PL-RESOLUTION(KB,α) returns 𝑡𝑟𝑢𝑒 or 𝑓𝑎𝑙𝑠𝑒input: 𝐾𝐵, the knowledge base (a sentence in propositional logic)

𝛼, the query (a sentence in propositional logic)

𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ← the set of clauses in the CNF representation of 𝐾𝐵¬𝛼𝑛𝑒𝑤 ← {}

loop do

for each pair of clauses 𝐶𝑖 , 𝐶𝑗 in 𝑐𝑙𝑎𝑢𝑠𝑒𝑠 do

𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠 ← 𝑃𝐿_𝑅𝐸𝑆𝑂𝐿𝑉𝐸 (𝐶𝑖, 𝐶𝑗)

if 𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠 contains the empty clause then return 𝑡𝑟𝑢𝑒

𝑛𝑒𝑤 ← 𝑛𝑒𝑤 ∪ 𝑟𝑒𝑠𝑜𝑙𝑣𝑒𝑛𝑡𝑠

if 𝑛𝑒𝑤 ⊆ 𝐶𝑙𝑎𝑢𝑠𝑒𝑠 then return 𝑓𝑎𝑙𝑠𝑒

𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ← 𝑐𝑙𝑎𝑢𝑠𝑒𝑠 ∪ 𝑛𝑒𝑤

Each pair containing complementary literals is resolved and the

resulted clause is added to the set if it is not already present.

The process continues until one of these happens:

- No new clauses (𝐾𝐵 ⊭ 𝛼)

- Two clauses resolve to yield the empty clause (𝐾𝐵 ⊨ 𝛼)

Sound and complete inference algorithm

Page 53: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Resolution example

𝐾𝐵 = 𝐵1,1 𝑃1,2 𝑃2,1 𝐵1,1

𝛼 = ¬𝑃1,2

Convert 𝐾𝐵 ∧ ¬𝛼 into CNF(𝐵1,1 𝑃1,2 𝑃2,1) (𝑃1,2 𝐵1,1) (𝑃2,1 𝐵1,1)(𝐵1,1)𝑃1,2

53

Contradiction

OK?

Page 54: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Horn clauses & definite clauses

54

Some real-world KBs satisfy restrictions on the form of

sentences

We can use more restricted and efficient inference algorithms

Definite clause: a disjunction of literals of which exactly one is

positive literal.

¬𝑃1 ∨ ¬𝑃2 ∨…∨ ¬𝑃𝑘 ∨ 𝑃𝑘+1 ⇔ (𝑃1 ∧ 𝑃2 ∧… ∧ 𝑃𝑘) ⇒ 𝑃𝑘+1

Horn clause: a disjunction of literals of which at most one is

positive literal.

Closed under resolution

Page 55: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Grammar

55

𝑆𝑦𝑚𝑏𝑜𝑙 → 𝑃 𝑄 𝑅 | …

𝐻𝑜𝑟𝑛𝐶𝑙𝑎𝑢𝑠𝑒 → 𝐷𝑒𝑓𝑖𝑛𝑖𝑡𝑒𝐶𝑙𝑎𝑢𝑠𝑒 | 𝐺𝑜𝑎𝑙𝐶𝑙𝑎𝑢𝑠𝑒

𝐷𝑒𝑓𝑖𝑛𝑖𝑡𝑒𝐶𝑙𝑎𝑢𝑠𝑒 → 𝑆𝑦𝑚𝑏𝑜𝑙1 ∧ ⋯∧ 𝑆𝑦𝑚𝑏𝑜𝑙𝑙 ⇒ 𝑆𝑦𝑚𝑏𝑜𝑙

𝐺𝑜𝑎𝑙𝐶𝑙𝑎𝑢𝑠𝑒 → (𝑆𝑦𝑚𝑏𝑜𝑙1 ∧⋯∧ 𝑆𝑦𝑚𝑏𝑜𝑙𝑙) ⇒ 𝐹𝑎𝑙𝑠𝑒

Page 56: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward and backward chaining

Inference with Horn clauses can be done through forward

chaining or backward chaining.

These algorithms are very natural and run in time that is linear

in the size of KB.

They are bases of logic programming Prolog: backward chaining

56

Page 57: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining

Repeated application of Modus Ponens until reaching goal

Fire any rule whose premises are satisfied in the KB

add its conclusion to the KB, until query is found

57

Arcs show

conjunctions

Page 58: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining algorithm

58

function 𝑃𝐿_𝐹𝐶_𝐸𝑁𝑇𝐴𝐼𝐿𝑆? (𝐾𝐵, 𝑞) returns 𝑡𝑟𝑢𝑒 or 𝑓𝑎𝑙𝑠𝑒

inputs:𝐾𝐵, the knowledge base (a set of propositional definite clauses)

𝑞, the query (a propositional symbol)

𝑐𝑜𝑢𝑛𝑡 ← a table where 𝑐𝑜𝑢𝑛𝑡[𝑐] is the number of symbols in 𝑐’s premise

𝑖𝑛𝑓𝑒𝑟𝑟𝑒𝑑 ← a table, where 𝑖𝑛𝑓𝑒𝑟𝑟𝑒𝑑[𝑠] is initially false for all symbols

𝑎𝑔𝑒𝑛𝑑𝑎 ← a queue of symbols, initially symbols known to be true in 𝐾𝐵

while 𝑎𝑔𝑒𝑛𝑑𝑎 ≠ {} do𝑝 ← 𝑃𝑂𝑃(𝑎𝑔𝑒𝑛𝑑𝑎)

if 𝑝 = 𝑞 then return 𝑡𝑟𝑢𝑒

if 𝑖𝑛𝑓𝑒𝑟𝑟𝑒𝑑[𝑝] = 𝑓𝑎𝑙𝑠𝑒 then𝑖𝑛𝑓𝑒𝑟𝑟𝑒𝑑[𝑝] ← 𝑡𝑟𝑢𝑒

for each clause 𝑐 in 𝐾𝐵 where 𝑝 is in 𝑐. 𝑃𝑅𝐸𝑀𝐼𝑆𝐸 do

𝑐𝑜𝑢𝑛𝑡[𝑐]= 𝑐𝑜𝑢𝑛𝑡[𝑐]-1

if 𝑐𝑜𝑢𝑛𝑡[𝑐] = 0 then add 𝑐. 𝐶𝑂𝑁𝐶𝐿𝑈𝑆𝐼𝑂𝑁 to 𝑎𝑔𝑒𝑛𝑑𝑎

return false

Begin with known

facts (positive

literals) in KB

The process continues

until q is added or no

further inference

Page 59: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

59

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 60: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

60

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 61: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

61

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 62: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

62

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 63: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

63

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 64: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

64

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 65: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

65

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 66: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining: example

66

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 67: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward chaining

67

Assumption: KB of definite clauses

Sound?

Every inference is an application of Modus Ponens.

Complete?

Every entailed atomic sentence will be derived?

Humans keep forward chaining under careful control (to

avoid irrelevant consequences)

Page 68: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Proof of completeness

1. Consider the final state of the inferred table (fixed point

where no new atomic sentences are derived)

2. Consider the final state as a model 𝑚, assigning 𝑡𝑟𝑢𝑒 to

inferred symbols and 𝑓𝑎𝑙𝑠𝑒 to others

3. Every definite clause in the original KB is true in 𝑚If “𝑎1 ∧ ⋯∧ 𝑎𝑛 ⇒ 𝑏” is false then 𝑎1 ∧ ⋯∧ 𝑎𝑛 is true and 𝑏 is false. But it

contradicts with reaching a fixed point.

4. Thus,𝑚 is a model of KB

5. If 𝐾𝐵 ⊨ 𝑞, atomic sentence 𝑞 is true in every model of KB,

including 𝑚

68

Page 69: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining

69

Idea: work backwards from the query 𝑞:

To prove 𝑞:if 𝑞 is known to be true then

no work is needed

else

Find those rule concluding 𝑞

If all premises of one of them can be proved (by backward chaining)then 𝑞 is true

Avoid loops: check if new subgoal is already on the goal stack

Avoid repeated work: check if new subgoal has already been

proved true or failed

Page 70: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

70

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 71: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

71

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 72: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

72

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 73: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

73

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 74: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

74

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 75: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

75

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 76: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

76

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 77: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

77

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 78: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

78

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 79: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Backward chaining example

79

𝑃 ⇒ 𝑄

𝐿 ∧ 𝑀 ⇒ 𝑃

𝐵 ∧ 𝐿 ⇒ 𝑀

𝐴 ∧ 𝑃 ⇒ 𝐿

𝐴 ∧ 𝐵 ⇒ 𝐿

𝐴

𝐵

Page 80: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Forward vs. backward chaining

Forward chaining is data-driven, automatic, unconsciousprocessing

May do lots of work that is irrelevant to the goal

Backward chaining is goal-driven, appropriate for problem-solving

Only relevant facts are considered.

Complexity of BC can be much less than linear in size of KB

80

Page 81: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Efficient propositional inference

Efficient algorithms for general propositional inference:

DPLL algorithm (Davis, Putnam, Logemann, Loveland)

Backtracking search algorithms

WalkSAT algorithm

Incomplete local search algorithms

They test entailment 𝐾𝐵 ⊨ 𝛼 by testing unsatisfiability of 𝐾𝐵∧ ¬𝛼.

81

Page 82: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

The DPLL algorithm

82

Improvements over truth table enumeration (simple DFS): Early termination

A clause is true if any literal is true.

A sentence is false if any clause is false.

Pure symbol heuristic

Pure symbol: always appears with the same "sign" in all clauses.

e.g., In (𝐴𝐵) ∧ (¬𝐵 𝐶) ∧ (𝐶𝐴),𝐴 and 𝐵 are pure,𝐶 is impure.

Make a pure symbol literal true.

In determining purity, we can ignore clauses already known to be true

Unit clause heuristic

Unit clause: all literals except to one already assigned false by model.

The only literal in a unit clause will be assigned true.

Many tricks can be used to enable SAT solvers to scale up.

Page 83: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

The DPLL algorithm

83

Determining satisfiability of an input propositional logic sentence (in CNF)

Page 84: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

WalkSAT

Local search algorithms (hill climbing, SA,…)

state space: complete assignments

evaluation function: number of unsatisfied clauses

WalkSat

Simple & effective

Balance between greediness and randomness

To escape from local minima

84

Page 85: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

WalkSAT

85

If max _𝑓𝑙𝑖𝑝𝑠 = ∞ and 𝑝 > 0, WalkSAT will find a model (if any exists)

If max _𝑓𝑙𝑖𝑝𝑠 = ∞, it never terminates for unsatisfiable sentences.

Page 86: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

WalkSAT

86

Failure may mean:

1) The sentence is unsatisfiable.

2) The algorithm needs more time.

It can not detect unsatisfiability.

Cannot be reliably used for deciding entailment.

Page 87: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Random SAT problems

Under-constrained problems: relatively few clauses

A large portion of possible assignments are solutions

e.g., (𝐷𝐵𝐶) ∧ (𝐵𝐴𝐶) ∧ (¬𝐶𝐵𝐸) ∧ (𝐸𝐷𝐵) ∧ (𝐵𝐸𝐶)

Over-constrained problems: many clauses relative to the

number of variables

Likely to have no solution

87

Page 88: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Hard satisfiability problems

88

𝑚 = number of clauses

𝑛 = number of symbols

m/n = 4.3 (critical point)

3 − 𝐶𝑁𝐹𝑛 = 50

Hard problems around

critical point

Page 89: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example:

propositional symbols

89

Atemporal variables that may be needed

𝑃𝑖,𝑗 is true if there is a pit in [𝑖, 𝑗].

𝑊𝑖,𝑗 is true if there is a wumpus in [𝑖, 𝑗], dead or alive.

𝐵𝑖,𝑗 is true if the agent perceives a breeze in [𝑖, 𝑗].

𝑆𝑖,𝑗 is true if the agent perceives a stench in [𝑖, 𝑗].

Some of the temporal variables that may be needed Variables for percepts and actions

𝐿𝑖,𝑗𝑡 , 𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡𝑡 , 𝐹𝑎𝑐𝑖𝑛𝑔𝑊𝑒𝑠𝑡𝑡 , 𝐹𝑎𝑐𝑖𝑛𝑔𝑁𝑜𝑟𝑡ℎ𝑡 , 𝐹𝑎𝑐𝑖𝑛𝑔𝑆𝑜𝑢𝑡ℎ𝑡

𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤𝑡

𝑊𝑢𝑚𝑝𝑢𝑠𝐴𝑙𝑖𝑣𝑒𝑡

Page 90: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example:

axioms on atemporal aspect of the world

90

Axioms: general knowledge about how the world works

General rules on atemporal variables

𝑃1,1

𝑊1,1

𝐵𝑥,𝑦 (𝑃𝑥,𝑦+1 𝑃𝑥,𝑦−1 𝑃𝑥+1,𝑦 𝑃𝑥−1,𝑦)

𝑆𝑥,𝑦 (𝑊𝑥,𝑦+1 𝑊𝑥,𝑦−1 𝑊𝑥+1,𝑦 𝑊𝑥−1,𝑦)

𝑊1,1 𝑊1,2 … 𝑊4,4

𝑊1,1 𝑊1,2

𝑊1,1 𝑊1,3

A distinct rule is needed

for each square

Page 91: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example:

perceptions & actions

91

Perceptions are converted to facts and are Telled to KB

𝑀𝐴𝐾𝐸_𝑃𝐸𝑅𝐶𝐸𝑃𝑇_𝑆𝐸𝑁𝑇𝐸𝑁𝐶𝐸 𝐵𝑟𝑒𝑒𝑧𝑒, 𝑆𝑡𝑒𝑛𝑐ℎ, 𝑁𝑜𝑛𝑒, 𝑁𝑜𝑛𝑒, 𝑁𝑜𝑛𝑒 , 𝑡

𝐵𝑟𝑒𝑒𝑧𝑒𝑡, 𝑆𝑡𝑒𝑛𝑐ℎ𝑡 are added to KB

Selected action is converted to a fact and is Telled to KB

𝑀𝐴𝐾𝐸_𝐴𝐶𝑇𝐼𝑂𝑁_𝑆𝐸𝑁𝑇𝐸𝑁𝐶𝐸 𝐹𝑜𝑟𝑤𝑎𝑟𝑑, 𝑡

𝐹𝑜𝑟𝑤𝑎𝑟𝑑𝑡 is added to KB

Page 92: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example: transition model

92

Changing aspect of world (Temporal variables)

They are also called as fluents or state variables

Initial KB includes the initial status of some of the temporal variables:

𝐿1,10 , 𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡0, 𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤0,𝑊𝑢𝑚𝑝𝑢𝑠𝐴𝑙𝑖𝑣𝑒0

Percepts are connected to fluents describing the properties of

squares

𝐿𝑥,𝑦𝑡 ⇒ (𝐵𝑟𝑒𝑒𝑧𝑒𝑡 ⇔ 𝐵𝑥,𝑦)

𝐿𝑥,𝑦𝑡 ⇒ 𝑆𝑡𝑒𝑛𝑐ℎ𝑡 ⇔ 𝑆𝑥,𝑦

Transition model: fluents can change as the results of agent’s actions

Page 93: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world example: transition model

Frame problem

93

Transition model:

𝐿1,10 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡0 ∧ 𝐹𝑜𝑟𝑤𝑎𝑟𝑑0 ⇒ (𝐿2,1

1 ∧ ¬𝐿1,11 )

𝐿1,10 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡0 ∧ 𝑇𝑢𝑟𝑛𝐿𝑒𝑓𝑡0 ⇒ (𝐿1,1

1 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝑁𝑜𝑟𝑡ℎ1 ∧ ¬𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡1)

After doing 𝐹𝑜𝑟𝑤𝑎𝑟𝑑 action at time 0:

𝐴𝑆𝐾 𝐾𝐵,𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤1 = 𝑓𝑎𝑙𝑠𝑒

Why?

Frame problem: representing the effects of actions without having to

represent explicitly a large number of intuitively obvious non-effects

Page 94: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world exampleSolution to frame problem: successor-state axioms

94

A Solution to frame problem: Successor-state axioms

Instead of writing axioms about actions, we write axioms

about fluents

𝐹𝑡+1 ⇔ 𝐴𝑐𝑡𝑖𝑜𝑛𝐶𝑎𝑢𝑠𝑒𝑠𝐹𝑡 ∨ (𝐹𝑡 ∧ ¬𝐴𝑐𝑡𝑖𝑜𝑛𝐶𝑎𝑢𝑠𝑒𝑠𝑁𝑜𝑡𝐹𝑡)

𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤𝑡+1 ⇔ 𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤𝑡 ∧ ¬𝑆ℎ𝑜𝑜𝑡𝑡

𝐿1,1𝑡+1 ⇔ (𝐿1,1

𝑡 ∧ (¬𝐹𝑜𝑟𝑤𝑎𝑟𝑑𝑡 ∨ 𝐵𝑢𝑚𝑝𝑡+1))

∨ (𝐿1,2𝑡 ∧ (𝐹𝑎𝑐𝑖𝑛𝑔𝑆𝑜𝑢𝑡ℎ𝑡 ∧ 𝐹𝑜𝑟𝑤𝑎𝑟𝑑𝑡))

∨ (𝐿2,1𝑡 ∧ (𝐹𝑎𝑐𝑖𝑛𝑔𝑊𝑒𝑠𝑡𝑡 ∧ 𝐹𝑜𝑟𝑤𝑎𝑟𝑑𝑡))

Page 95: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

A hybrid agent for the wumpus world

95

Basic concepts:

Safeness of [𝑥, 𝑦] square:

𝑂𝐾𝑥,𝑦𝑡 ⇔ ¬𝑃𝑥,𝑦 ∧ ¬(𝑊𝑥,𝑦 ∧ 𝑊𝑢𝑚𝑝𝑢𝑠𝐴𝑙𝑖𝑣𝑒𝑡)

Set of safe squares:

𝑠𝑎𝑓𝑒 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵, 𝑂𝐾𝑥,𝑦𝑡 = 𝑡𝑟𝑢𝑒

Set of unvisited squares:

𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵, 𝐿𝑥,𝑦𝑡′ = 𝑓𝑎𝑙𝑠𝑒 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑡′ ≤ 𝑡

Page 96: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

96

function 𝐻𝑌𝐵𝑅𝐼𝐷_𝑊𝑈𝑀𝑃𝑈𝑆_𝐴𝐺𝐸𝑁𝑇(𝑝𝑒𝑟𝑐𝑒𝑝𝑡) returns an action sequence

inputs: 𝑝𝑒𝑟𝑐𝑒𝑝𝑡, a list, 𝑏𝑟𝑒𝑒𝑧𝑒, 𝑠𝑡𝑒𝑛𝑐ℎ, 𝑔𝑙𝑖𝑡𝑡𝑒𝑟, 𝑏𝑢𝑚𝑝, 𝑠𝑐𝑟𝑒𝑎𝑚

persistent: 𝐾𝐵, initially the atemporal “wumpus physics”

𝑡, a time counter, initially 0

𝑝𝑙𝑎𝑛, an action sequence, initially empty

TELL(𝐾𝐵,𝑀𝐴𝐾𝐸_𝑃𝐸𝑅𝐶𝐸𝑃𝑇_𝑆𝐸𝑁𝑇𝐸𝑁𝐶𝐸 𝑝𝑒𝑟𝑐𝑒𝑝𝑡, 𝑡 )

TELL the 𝐾𝐵 the temporal “physics” sentences for time 𝑡

𝑠𝑎𝑓𝑒 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵,𝑂𝐾𝑥,𝑦𝑡 = 𝑡𝑟𝑢𝑒

if 𝐴𝑆𝐾(𝐾𝐵, 𝐺𝑙𝑖𝑡𝑡𝑒𝑟𝑡) = 𝑡𝑟𝑢𝑒 then

𝑝𝑙𝑎𝑛 ← 𝐺𝑟𝑎𝑏 + 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸 𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 1,1 , 𝑠𝑎𝑓𝑒 + [𝐶𝑙𝑖𝑚𝑏]

if 𝑝𝑙𝑎𝑛 = {} then

𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵, 𝐿𝑥,𝑦𝑡′ = 𝑓𝑎𝑙𝑠𝑒 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑡′ ≤ 𝑡

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ∩ 𝑠𝑎𝑓𝑒, 𝑠𝑎𝑓𝑒)

if 𝑝𝑙𝑎𝑛 = {} and 𝐴𝑆𝐾 𝐾𝐵,𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤𝑡 = 𝑡𝑟𝑢𝑒 then

𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒_𝑤𝑢𝑚𝑝𝑢𝑠 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵,¬𝑊𝑥,𝑦 = 𝑓𝑎𝑙𝑠𝑒

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑆𝐻𝑂𝑇(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒_𝑤𝑢𝑚𝑝𝑢𝑠, 𝑠𝑎𝑓𝑒)

if 𝑝𝑙𝑎𝑛 = {} then

𝑛𝑜𝑡_𝑢𝑛𝑠𝑎𝑓𝑒 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵,¬𝑂𝐾𝑥,𝑦𝑡 = 𝑓𝑎𝑙𝑠𝑒

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ∩ 𝑛𝑜𝑡_𝑢𝑛𝑠𝑎𝑓𝑒, 𝑠𝑎𝑓𝑒)

if 𝑝𝑙𝑎𝑛 = {} then

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸 𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 1,1 , 𝑠𝑎𝑓𝑒 + [𝐶𝑙𝑖𝑚𝑏]

action ← 𝑃𝑂𝑃(𝑝𝑙𝑎𝑛)

𝑇𝐸𝐿𝐿(𝐾𝐵,𝑀𝐴𝐾𝐸_𝐴𝐶𝑇𝐼𝑂𝑁_𝑆𝐸𝑁𝑇𝐸𝑁𝐶𝐸(𝑎𝑐𝑡𝑖𝑜𝑛, 𝑡))

𝑡 ← 𝑡 + 1

return 𝑎𝑐𝑡𝑖𝑜𝑛

Page 97: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Hybrid agent (phases listed based on priority)

97

Simple-reflex (condition-action rule):

if 𝐴𝑆𝐾(𝐾𝐵, 𝐺𝑙𝑖𝑡𝑡𝑒𝑟𝑡) = 𝑡𝑟𝑢𝑒 then

𝑝𝑙𝑎𝑛 ← [𝐺𝑟𝑎𝑏] + 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸 𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 1,1 , 𝑠𝑎𝑓𝑒 + [𝐶𝑙𝑖𝑚𝑏]

Goal-based & reasoning:

if 𝑝𝑙𝑎𝑛 = {} then

𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵, 𝐿𝑥,𝑦𝑡′ = 𝑓𝑎𝑙𝑠𝑒 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑡′ ≤ 𝑡

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ∩ 𝑠𝑎𝑓𝑒, 𝑠𝑎𝑓𝑒)

if 𝑝𝑙𝑎𝑛 = {} and 𝐴𝑆𝐾 𝐾𝐵,𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤𝑡 = 𝑡𝑟𝑢𝑒 then

𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒_𝑤𝑢𝑚𝑝𝑢𝑠 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵,¬𝑊𝑥,𝑦 = 𝑓𝑎𝑙𝑠𝑒

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑆𝐻𝑂𝑇(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒_𝑤𝑢𝑚𝑝𝑢𝑠, 𝑠𝑎𝑓𝑒)

Page 98: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Hybrid agent (phases listed based on priority)

98

Exploration & reasoning

if 𝑝𝑙𝑎𝑛 = {} then

𝑛𝑜𝑡_𝑢𝑛𝑠𝑎𝑓𝑒 ← 𝑥, 𝑦 : 𝐴𝑆𝐾 𝐾𝐵,¬𝑂𝐾𝑥,𝑦𝑡 = 𝑓𝑎𝑙𝑠𝑒

𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 ∩ 𝑛𝑜𝑡_𝑢𝑛𝑠𝑎𝑓𝑒, 𝑠𝑎𝑓𝑒)

Goal-based:

if 𝑝𝑙𝑎𝑛 = {} then𝑝𝑙𝑎𝑛 ← 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸 𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 1,1 , 𝑠𝑎𝑓𝑒 + [𝐶𝑙𝑖𝑚𝑏]

Page 99: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Hybrid agent: Using A* to make a plan

99

function 𝑃𝐿𝐴𝑁_𝑅𝑂𝑈𝑇𝐸(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑔𝑜𝑎𝑙, 𝑎𝑙𝑙𝑜𝑤𝑒𝑑) returns an action sequence

inputs: 𝑐𝑢𝑟𝑟𝑒𝑛𝑡, the agent’s current position

𝑔𝑜𝑎𝑙𝑠, a set of squares; try to plan a route to one of them

𝑎𝑙𝑙𝑜𝑤𝑒𝑑, a set of squares that can form part of the route

𝑝𝑟𝑜𝑏𝑙𝑒𝑚 ← 𝑅𝑂𝑈𝑇𝐸_𝑃𝑅𝑂𝐵𝐿𝐸𝑀(𝑐𝑢𝑟𝑟𝑒𝑛𝑡, 𝑔𝑜𝑎𝑙𝑠, 𝑎𝑙𝑙𝑜𝑤𝑒𝑑)return 𝐴∗_𝐺𝑅𝐴𝑃𝐻_𝑆𝐸𝐴𝑅𝐶𝐻(𝑝𝑟𝑜𝑏𝑙𝑒𝑚)

Page 100: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Logical state estimation

100

Problem: Computational time of ASK grows by the length ofthe agent’s life We must save the results of inference to use them in the next time steps

Solution: Belief state instead of the whole past history ofpercepts and actions

Belief state: the agent’s knowledge about the state of the world

(the set of all possible current states of the world)

𝐵1,2 ∧ (𝑃1,3 ∨ 𝑃2,2)

∧ 𝐿1,21 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝑁𝑜𝑟𝑡ℎ1 ∧ 𝐻𝑎𝑣𝑒𝐴𝑟𝑟𝑜𝑤1 ∧𝑊𝑢𝑚𝑝𝑢𝑠𝐴𝑙𝑖𝑣𝑒1

State estimation: process of updating the belief state when doingactions and receiving new percepts

Page 101: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Approximate state estimation

101

Problem: The exact belief state estimation may require logical

formulas whose size is exponential in the number of symbols.

𝑛 fluent symbols, 2𝑛 possible physical states, 22𝑛

possible belief states

Solution: approximation

e.g., 1-CNF belief state: given the belief state at 𝑡 − 1, agent tries to

prove 𝑋𝑡 or ¬𝑋𝑡 for each 𝑋𝑡)

Approximate belief state estimation?

compact representation (open area for research)

Relation to model-based agents

Page 102: Informed search algorithmsce.sharif.edu/courses/93-94/2/ce417-1/resources/root...Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring

Wumpus world

Distinct rules “for each square [x,y]” and “for each time t”

𝐿𝑥,𝑦𝑡 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡𝑡 ∧ 𝑇𝑢𝑟𝑛𝐿𝑒𝑓𝑡𝑡 ⇒ (𝐿𝑥,𝑦

𝑡+1 ∧ 𝐹𝑎𝑐𝑖𝑛𝑔𝑈𝑝𝑡+1 ∧ ¬𝐹𝑎𝑐𝑖𝑛𝑔𝐸𝑎𝑠𝑡𝑡+1)

Many rules are needed to describe the world axioms

Rather impractical for bigger world

We can not express general knowledge about “physics” of the worlddirectly in propositional language

More expressive language is needed.

Qualification problem: specifying all the exceptions

Probability theory allows us to summarize all the exceptions (withoutexplicitly naming them)

Limitations of propositional logic

102