Logical Agents Santa 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 4 Stench Breez e PIT Breez e 3 Stench PIT Breez e Gold 2 Stench Breez e 1 Breez e PIT Breez e START 1 2 3 4
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 World 1,4 2,4 3,4 4,4 1,3 2,3 3,3 4,3 1,2 2,2 3,2 4,2 OK 1,1 2,1 3,1 4,1 A OK OK (a) A = Agent B = Breeze G = Glitter, Gold OK = Safe square P = Pit S = Stench V = Visited W = Wumpus 1,4 2,4 3,4 4,4 1,3 2,3 3,3 4,3 1,2 2,2 P? 3,2 4,2 OK 1,1 2,1 A 3,1 P? 4,1 V B OK OK (b)
Wumpus World 2 2 PIT 2 2 PIT 1 Breeze 1 PIT 1 2 3 KB α 1 Breeze 1 2 3 KB 1 Breeze PIT 1 2 3 1 Breeze 1 2 3 α 2 2 PIT 1 Breeze 1 2 3 2 1 Breeze 1 2 3 2 PIT 1 Breeze PIT 1 2 3 2 PIT 1 Breeze 1 2 3 2 1 Breeze 1 2 3 2 PIT 1 Breeze PIT 1 2 3 2 PIT 2 PIT PIT 1 Breeze PIT 1 2 3 2 PIT PIT 1 Breeze PIT 1 2 3 1 Breeze 1 2 3 2 PIT 1 Breeze PIT 1 2 3 2 PIT PIT 2 PIT PIT 1 Breeze 1 2 3 1 Breeze PIT 1 2 3
Logic Logical entailment α entails β, M( ) M( ) The stronger assertion rules out more models
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
Propositional Logic Deduction Theorem if and only if ( ) ) is valid Can prove the latter by checking that it is true in every model by proving that it is equivalent to true
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 P 1,1 P i,j ) B i 1,j ^ B i+1,j ^ B i,j+1 ^ B i,j 1 W i,j ) S i 1,j ^ S i+1,j ^ S i,j+1 ^ S i,j 1 B i,j ) P i 1,j _ P i+1,j _ P i,j 1 _ P i,j+1 S i,j ) W i 1,j _ W i+1,j _ W i,j 1 _ W i,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 A ) B,A And elimination A ^ B B A
Wumpus World Logic Can you inference rules in order to obtain statements P 1,2 ) B 1,1 ^ B 1,3 ^ B 2,2 P 1,2 ) B 1,1 P 1,2 ) B 1,1, B 1,1 P 1,2
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 l 1,l 2,...l n literals, m literal that is the negative of literal l i,then l 1 _ l 2 _..._ l n,m l 1 _ l 2 _...l i 1 _ l i+1 _...l n
Wumpus Logic Example: P 1,1 _ P 3,1, P 1,1 _ P 2,2 P 3,1 _ P 2,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 P 2,1 B 1,1 B 1,1 P 1,2 P 2,1 P 1,2 B 1,1 B 1,1 P 1,2 ^ ^ ^ ^ B 1,1 P 1,2 B 1,1 ^ ^ P 1,2 P 2,1 P 2,1 B 1,1 P 2,1 B 1,1 P 1,2 P 2,1 P 1,2 ^ ^ ^ ^ ^ ^ P 2,1 P 1,2
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 B 1 ^ B 2 ^...^ B n ) H In conjunctive normal form: disjunction of literals where at most one is positive
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 P ) Q L ^ M ) P B ^ L ) M A ^ P ) L A ^ B ) L A B L Q P A M 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 KB ) A () KB _ A Hence: KB ) A () KB ^ A not satisfiable Since the KB can be in CNF, this question is an instance of SAT (not 3-SAT yet)
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 A;((A _ B) ^ (A _ C)) Independent of other assignments
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 false A ^ B ^ C;(( A _ B _ C) ^...)
Davis Putnam Logemann Improvements Loveland Algorithm 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 1 0.8 P(satisfiable) 0.6 0.4 0.2 0 0 1 2 3 4 5 6 7 8 Clause/symbol ratio m/n
SAT Runtime 2000 1800 1600 1400 1200 1000 800 600 400 200 0 DPLL WalkSAT 0 1 2 3 4 5 6 7 8 Clause/symbol ratio m/n
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 L t x,y ) (Breeze t, B x,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 Forward t Effect axioms L 1 0,0 ^ FacingEast 0 ^ Forward 0 ) (L 1 2,1 ^ L 1 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 HaveArrow t+1 ) (HaveArrow t ^ Shoot t )
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: WumpusAlive 3 ^ L 3 2,1 ^ B 2,1 ^ (P 3,1 _ P 2,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 B 2,1 ^ P 1,1 ^ S 1,2 ^ P 1,2 ^ B 1,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