Logical Agents - Santa Clara Universitytschwarz/COEN266/LogicalAgents.pdf · Logical Agents •...

53
Logical Agents Santa Clara University

Transcript of Logical Agents - Santa Clara Universitytschwarz/COEN266/LogicalAgents.pdf · Logical Agents •...

Logical AgentsSanta Clara University

Logical Agents• Humans know things • Humans use knowledge to make plans • Humans do not act completely reflexive, but reason

• AI: • Simple problem-solving agents have knowledge encoded

in their search spaces • CSP: Use a more generic approach by building generic

models of the problem and solving them • Logical agents: Have knowledge base and use logic in

order to make plans

Knowledge Based Agents• Knowledge base (KB)

• Set of sentences • Expressed in a knowledge representation

language• Need to add sentences and query

• TELL and ASK operations • Might involve inference

Knowledge Based Agents• Knowledge based agent

• forever: • TELL(MAKE-PERCEPT-SENTENCE(percept)) • ASK(MAKE-ACTION-QUERY) • TELL(MAKE-ACTION-SEQUENCE)

Wumpus World

PIT

1 2 3 4

1

2

3

4

START

Stench

Stench

Breeze

Gold

PIT

PIT

Breeze

Breeze

Breeze

Breeze

Breeze

Stench

The Wumpus World• Wumpus world

• Caves (4x4 grid) consisting of rooms connected by passage ways • One wumpus (smells, eats players, can be

shot dead) • Bottomless pits (cause drafts, player falls into

pit) • Gold (glitters, can be picked up)

The Wumpus World• Performance Measure

• +1000: getting out alive with gold • -1000: falling into a pit or being eaten by

wumpus • -1: each action taken • -10: shooting your only arrow

The Wumpus World• Actuators:

• Move, turn left, turn right • Percepts:

• Stench • Breeze • Glitter • Bump • Scream

Logic• Syntax • Semantics: defines truth depending on model • Model: possible world

Logic• If a sentence α is satisfied in the model m

• m satisfies α • m is a model for α M(↵)

Wumpus WorldABG

PS

W

= Agent = Breeze = Glitter, Gold

= Pit = Stench

= Wumpus

OK = Safe square

V = Visited

A

OK 1,1 2,1 3,1 4,1

1,2 2,2 3,2 4,2

1,3 2,3 3,3 4,3

1,4 2,4 3,4 4,4

OKOKB

P?

P?A

OK OK

OK 1,1 2,1 3,1 4,1

1,2 2,2 3,2 4,2

1,3 2,3 3,3 4,3

1,4 2,4 3,4 4,4

V

(a) (b)

Wumpus World

1 2 3

1

2 PIT

1 2 3

1

2 PIT

1 2 3

1

2 PIT PIT

PIT

1 2 3

1

2 PIT

PIT

1 2 3

1

2

PIT

1 2 3

1

2 PIT

PIT

1 2 3

1

2 PIT PIT

1 2 3

1

2

KB α1

Breeze

Breeze

Breeze

Breeze

Breeze

Breeze

Breeze

Breeze

1 2 3

1

2 PIT

1 2 3

1

2 PIT

1 2 3

1

2 PIT PIT

PIT

1 2 3

1

2 PIT

PIT

1 2 3

1

2

PIT

1 2 3

1

2 PIT

PIT

1 2 3

1

2 PIT PIT

1 2 3

1

2

KB

Breeze

α2

Breeze

Breeze

Breeze

Breeze

Breeze

Breeze

Breeze

Logic• Logical entailment

• α entails β

• The stronger assertion rules out more models

↵ ✏ � , M(↵) ⇢ M(�)

Logic• Inference mechanism

• A method to derive a statement from other statements

• Inference algorithm: • Sound: it derives only entailed sentences • Complete: it derives any sentence that is entailed

• Grounding: How do we know that the knowledge base is true in the world

Propositional Logic• Syntax

• Sentences are atomic or compound • To create compound statements, use logical

operations and, or, implies, equivalent (if and only if)

• Semantics • Give truth values to atomic sentences • Calculate truth values for compound statements

Propositional Logic• Logical equivalence

• Two sentences are logically equivalent if they are true in the same set of models

↵ ⌘ �

↵ ⌘ � if and only if ↵ ✏ � and � ✏ ↵

• Deduction Theorem

• Can prove the latter • by checking that it is true in every model • by proving that it is equivalent to true

Propositional Logic

↵ ✏ � if and only if (↵ ) �) is valid

Propositional Logic Syntax

• Atomic sentences • Denoted by capital letters

• Literals • Atomic sentences or their negation

• Compound statements • Using operators

) _ ^ ()

Propositional Logic Semantics

• Semantics of operations are defined by truth tables

Wumpus world logic• P(i,j) — a pit in location i, j • W(i,j) — a wumpus in positions i, j (dead or alive) • B(i,j) — a breeze in i, j • S(i,j) — a stench in i, j

Wumpus world logic

¬P1,1

Pi,j ) Bi�1,j ^Bi+1,j ^Bi,j+1 ^Bi,j�1

Wi,j ) Si�1,j ^ Si+1,j ^ Si,j+1 ^ Si,j�1

Bi,j ) Pi�1,j _ Pi+1,j _ Pi,j�1 _ Pi,j+1

Si,j ) Wi�1,j _Wi+1,j _Wi,j�1 _Wi,j+1

Wumpus world logic• Logical agent needs to decide whether this

knowledge base entails propositions • Good algorithm is

• sound if it only labels true propositions as true • complete if

• it labels every true proposition as true • it eventually decides

Propositional Theorem Proving

• Can do model checking • Generate the set of all allowable models • Check a proposition on whether it is valid in all

models • Can do mathematical proofs

• Use inference rules

Propositional Logic• Modus ponens

• And elimination

A ) B,A

B

A ^B

A

Wumpus World Logic• Can you inference rules in order to obtain

statements

P1,2 ) B1,1,¬B1,1

¬P1,2

P1,2 ) B1,1 ^B1,3 ^B2,2

P1,2 ) B1,1

Wumpus Logic• Can use search algorithms in order to find true

propositions using the knowledge base • States are true propositions • Actions are applications of an inference rule

Wumpus Logic• Can restrict to a single inference rule:

• Resolution

l1, l2, . . . ln literals, m literal that is the negative of literal li, then

l1 _ l2 _ . . . _ ln,ml1 _ l2 _ . . . li�1 _ li+1 _ . . . ln

Wumpus Logic• Example:

P1,1 _ P3,1,¬P1,1 _ ¬P2,2

P3,1 _ P2,2

Wumpus Logic• Transform knowledge base into CNF

• Use algebraic manipulations to move individual propositions to CNF

Propositional Logic• To prove a statement A

• Add ˜A to the KB • See whether you can obtain the empty clause with

resolution

• Ground resolution theorem: If a set of clauses is unsatisfiable, then the resolution closure outhouse clauses contains the empty clause.

• Can use fact that entailed statements only grow with the addition of information

Wumpus World Logic

¬P2,1 B1,1 ¬B1,1 P1,2 P2,1 ¬P1,2 B1,1 ¬B1,1 P1,2

¬P2,1 ¬P1,2P1,2 P2,1 ¬P2,1 ¬B1,1 P2,1 B1,1 P1,2 P2,1 ¬P1,2¬B1,1 P1,2 B1,1

^ ^ ^

^^ ^ ^ ^ ^ ^ ^

^

Propositional Logic• Resolution works, but is involved • Often, KB can be written in a more restricted form • Horn Clause

• Single, positive literals (the facts) • Implications of the form body implies head

• In conjunctive normal form: disjunction of literals where at most one is positive

B1 ^B2 ^ . . . ^Bn ) H

Propositional Logic• Deciding entailment with Horn clauses can be

done in time linear with the knowledge base

• Forward chaining: • Prove single literal • Start with all known literals • Try out Horn clauses to generate new known

literals

Propositional Logic

Q

P

M

L

BA

P ) Q

L ^M ) P

B ^ L ) M

A ^ P ) L

A ^B ) L

A

B

Propositional Logic• Backward chaining

• Sometimes uses much less inference • Start with goal • Look for all inferences with head goal • Include the body of these inferences in the goal

SAT• To check whether KB entails a proposition

• Hence:

• Since the KB can be in CNF, this question is an instance of SAT (not 3-SAT yet)

KB ) A () ¬KB _A

KB ) A () KB ^ ¬A not satisfiable

SAT• Every SAT can be changed to 3-SAT • Use a 3-SAT solver

• A cottage industry with annual competition • With huge theoretical importance • With many practical problems

Davis Putnam Logemann Loveland Algorithm

• Input is a sentence in conjunctive normal form • Recursive, depth-first enumeration of possible models • With improvements

• Early termination of evaluation • If a literal is true in all clauses, the sentence is

true

• Independent of other assignmentsA; ((A _B) ^ (A _ ¬C))

Davis Putnam Logemann Loveland Algorithm

• Improvements • Early termination

• One literal true in all clauses entails sentence is true

• One clause is false then sentence is falseA ^B ^ ¬C; ((¬A _ ¬B _ C) ^ . . .)

Davis Putnam Logemann Loveland Algorithm

• Improvements • Pure symbols

• Literal appears always either positive or negative in all clauses

• Pure symbols lead to simple truth assignment • Unit clause

• A clause with just one literal • A clause where only one literal has not yet been

assigned • Unit clauses also force assignment

DPLL algoritmo

SAT• Not all SAT problems are equally hard

• Convert to 3-SAT • It turns out that there needs to be a ratio between

number of clauses and number of variables for SAT to be hard

SAT

0

0.2

0.4

0.6

0.8

1

0 1 2 3 4 5 6 7 8

P(sa

tisfia

ble)

Clause/symbol ratio m/n

SAT

0200400600800

100012001400160018002000

0 1 2 3 4 5 6 7 8

Runt

ime

Clause/symbol ratio m/n

DPLLWalkSAT

Agents Based on Propositional Logic

• Collect many rules about breezes, stenches, wumpus

• Agent percepts: • Need to depend on the time • Connected to rules via location variables

Lt

x,y

) (Breezet , Bx,y

)

Agents Based on Propositional Logic

• Percepts only say something about the current time • Example of fluents:

• Aspect of the world that changes • Symbols associated with permanent aspects of the

world are atemporal variables

Agents Based on Propositional Logic

• Transition model • Propositions for the occurrences of actions

• Effect axioms

Forward

t

L10,0 ^ FacingEast

0 ^ Forward

0 ) (L12,1 ^ ¬L1

1,1)

Agents Based on Propositional Logic

• Frame problem • Many states are unchanged by the action

• The “frame of reference” • Can add frame axioms

• But these proliferate • Representational frame problem

• How to represent the frame • Inferential frame problem

• Calculating the results of an action takes longer

Agents Based on Propositional Logic

• Frame problem • Solution is to not write axioms about actions but

about fluents

HaveArrowt+1 ) (HaveArrowt ^ ¬Shoott)

Agents Based on Propositional Logic

• Hybrid agent for the wumpus world • Maintain and update knowledge base • Construct plan

• If glitter, grab gold, walk out • If not:

• Explore the world, avoiding Wumpus and pits

• Use A* for route planning

Agents Based on Propositional Logic

• Logical state estimation • Recalculating inferences becomes unsustainable

as the KB increases • Need to cache results • Past history of percepts can be accumulated in a

belief state • Process of updating belief state is called state

estimation • Example:

WumpusAlive3 ^ L32,1 ^B2,1 ^ (P3,1 _ P2,2)

Agents Based on Propositional Logic

• Logical State Estimation • Exact belief state is large

• n fluents: • 2**n physical states

• 2**2**n belief states • Alternative: Use approximate belief state

• Only use 1-CNF (conservatively) • State estimation:

• See whether literals about fluents can be ascertained • Example: Wumpus world

• Belief state B2,1 ^ ¬P1,1 ^ S1,2 ^ ¬P1,2 ^ ¬B1,1

Agents Based on Propositional Logic

• Making plans by propositional inference • Construct a sentence that includes:

• Assertions about initial state • Successor-state axioms for all possible actions up to some

maximum time t • Goal assertion for time t

• Present the sentence to the SAT solver • If the solver finds a satisfying model, then the goal is

achievable • If not, then planning is impossible

• If there is a model, extract the action variables that are true as the plan