0% found this document useful (0 votes)
44 views55 pages

Foundations of Artificial Intelligence: Logic

This document provides an overview of propositional logic and its use in artificial intelligence. It discusses: 1) Propositional logic involves assigning truth values to basic propositions and defining rules for combining propositions using operators like "and", "or", and "if-then". 2) Possible worlds in propositional logic assign true or false values to all basic propositions, and propositional formulas are evaluated as true or false in each possible world. 3) Propositional logic can be used to represent an agent's knowledge about its environment and reasoning about that knowledge using inference algorithms like theorem proving and model checking.

Uploaded by

林佳緯
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views55 pages

Foundations of Artificial Intelligence: Logic

This document provides an overview of propositional logic and its use in artificial intelligence. It discusses: 1) Propositional logic involves assigning truth values to basic propositions and defining rules for combining propositions using operators like "and", "or", and "if-then". 2) Possible worlds in propositional logic assign true or false values to all basic propositions, and propositional formulas are evaluated as true or false in each possible world. 3) Propositional logic can be used to represent an agent's knowledge about its environment and reasoning about that knowledge using inference algorithms like theorem proving and model checking.

Uploaded by

林佳緯
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Foundations of Artificial Intelligence

Logic

Shang-Tse Chen
Department of Computer Science
& Information Engineering
National Taiwan University

(slides adapted from Dan Klein, Pieter Abbeel)


Outline
§ Propositional Logic
§ Basic concepts of knowledge, logic, reasoning
§ Propositional logic: syntax and semantics, Pacworld example
§ Inference by theorem proving (briefly) and model checking
§ A Pac agent using propositional logic
§ First-Order Logic
Agents that know things
§ Agents acquire knowledge through perception, learning, language
§ Knowledge of the effects of actions (“transition model”)
§ Knowledge of how the world affects sensors (“sensor model”)
§ Knowledge of the current state of the world
§ Can keep track of a partially observable world
§ Can formulate plans to achieve goals
§ Can design and build gravitational wave detectors…..
Knowledge, contd.
§ Knowledge base = set of sentences in a formal language
§ Declarative approach to building an agent (or other system):
§ Tell it what it needs to know (or have it Learn the knowledge)
§ Then it can Ask itself what to do—answers should follow from the KB
§ Agents can be viewed at the knowledge level
i.e., what they know, regardless of how implemented
§ A single inference algorithm can answer any answerable question
Knowledge base Domain-specific facts

Inference engine Generic code


Logic
§ Syntax: What sentences are allowed?
§ Semantics:
§ What are the possible worlds?
§ Which sentences are true in which worlds? (i.e., definition of truth)

a1
a2 a3
Syntaxland Semanticsland
Different kinds of logic
§ Propositional logic
§ Syntax: P Ú (¬Q Ù R); X1 Û (Raining Þ ¬Sunny)
§ Possible world: {P=true,Q=true,R=false,S=true} or 1101
§ Semantics: a Ù b is true in a world iff is a true and b is true (etc.)
§ First-order logic
§ Syntax: "x $y P(x,y) Ù ¬Q(Joe,f(x)) Þ f(x)=f(y)
§ Possible world: Objects o1, o2, o3; P holds for <o1,o2>; Q holds for <o3>;
f(o1)=o1; Joe=o3; etc.
§ Semantics: f(s) is true in a world if s=oj and f holds for oj; etc.
Different kinds of logic, contd.
§ Relational databases:
§ Syntax: ground relational sentences, e.g., Sibling(Ali,Bo)
§ Possible worlds: (typed) objects and (typed) relations
§ Semantics: sentences in the DB are true, everything else is false
§ Cannot express disjunction, implication, universals, etc.
§ Query language (SQL etc.) typically some variant of first-order logic
§ Often augmented by first-order rule languages, e.g., Datalog
§ Knowledge graphs (roughly: relational DB + ontology of types and relations)
§ Google Knowledge Graph: 5 billion entities, 500 billion facts, >30% of queries
§ Facebook network: 2.93 billion people, trillions of posts, maybe quadrillions of facts
Inference: entailment
§ Entailment: a |= b (“a entails b” or “b follows from a”) iff
in every world where a is true, b is also true
§ I.e., the a-worlds are a subset of the b-worlds [models(a) Í models(b)]
§ In the example, a2 |= a1
§ (Say a2 is ¬Q Ù R Ù S Ù W
a1 is ¬Q )
a1
a2
Inference: proofs
§ A proof is a demonstration of entailment between a and b
§ Sound algorithm: everything it claims to prove is in fact entailed
§ Complete algorithm: every that is entailed can be proved
Inference: proofs
§ Method 1: model-checking
§ For every possible world, if a is true make sure that is b true too
§ OK for propositional logic (finitely many worlds); not easy for first-order logic
§ Method 2: theorem-proving
§ Search for a sequence of proof steps (applications of inference rules) leading
from a to b
§ E.g., from P and (P Þ Q), infer Q by Modus Ponens
Propositional logic syntax
§ Given: a set of proposition symbols {X1,X2,…, Xn}
§ (we often add True and False for convenience)
§ Xi is a sentence
§ If a is a sentence then ¬a is a sentence
§ If a and b are sentences then a Ù b is a sentence
§ If a and b are sentences then a Ú b is a sentence
§ If a and b are sentences then a Þ b is a sentence
§ If a and b are sentences then a Û b is a sentence
§ And p.s. there are no other sentences!
Propositional logic semantics
§ Let m be a model assigning true or false to {X1,X2,…, Xn}
§ If a is a symbol then its truth value is given in m
§ ¬a is true in m iff a is false in m
§ a Ù b is true in m iff a is true in m and b is true in m
§ a Ú b is true in m iff a is true in m or b is true in m
§ a Þ b is true in m iff a is false in m or b is true in m
§ a Û b is true in m iff a Þ b is true in m and b Þ a is true in m
Propositional logic semantics in code
function PL-TRUE?(a,model) returns true or false
if a is a symbol then return Lookup(a, model)
if Op(a) = ¬ then return not(PL-TRUE?(Arg1(a),model))
if Op(a) = Ù then return and(PL-TRUE?(Arg1(a),model),
PL-TRUE?(Arg2(a),model))
etc.

(Sometimes called “recursion over syntax”)


Example: Partially observable Pacman
§ Pacman knows the map but perceives just wall/gap to NSEW
§ Formulation: what variables do we need?
§ Wall locations
§ Wall_0,0 there is a wall at [0,0]
§ Wall_0,1 there is a wall at [0,1], etc. (N symbols for N locations)
§ Percepts
§ Blocked_W (blocked by wall to my West) etc.
§ Blocked_W_0 (blocked by wall to my West at time 0) etc. (4T symbols for T time steps)
§ Actions
§ W_0 (Pacman moves West at time 0), E_0 etc. (4T symbols)
§ Pacman’s location
§ At_0,0_0 (Pacman is at [0,0] at time 0), At_0,1_0 etc. (NT symbols)
How many possible worlds?
§ N locations, T time steps => N + 4T + 4T + NT = O(NT) variables
§ O(2NT) possible worlds!
§ N=200, T=400 => ~1024000 worlds
§ Each world is a complete “history”
§ But most of them are pretty weird!
Pacman’s knowledge base: Map
§ Pacman knows where the walls are:
§ Wall_0,0 Ù Wall_0,1 Ù Wall_0,2 Ù Wall_0,3 Ù Wall_0,4 Ù Wall_1,4 Ù …
§ Pacman knows where the walls aren’t!
§ ¬Wall_1,1 Ù ¬Wall_1,2 Ù ¬Wall_1,3 Ù ¬Wall_2,1 Ù ¬Wall_2,2 Ù …
Pacman’s knowledge base: Initial state
§ Pacman doesn’t know where he is
§ But he knows he’s somewhere!
§ At_1,1_0 Ú At_1,2_0 Ú At_1,3_0 Ú At_2,1_0 Ú …
Pacman’s knowledge base: Sensor model
§ State facts about how Pacman’s percepts arise…
§ <Percept variable at t> Û <some condition on world at t>
§ Pacman perceives a wall to the West at time t
if and only if he is in x,y and there is a wall at x-1,y
§ Blocked_W_0 Û ((At_1,1_0 Ù Wall_0,1) v
(At_1,2_0 Ù Wall_0,2) v
(At_1,3_0 Ù Wall_0,3) v …. )
§ 4T sentences, each of size O(N)
§ Note: these are valid for any map
Pacman’s knowledge base: Transition model
§ How does each state variable at each time gets its value?
§ Here we care about location variables, e.g., At_3,3_17
§ A state variable X gets its value according to a successor-state axiom
§ X_t Û [X_t-1 Ù ¬(some action_t-1 made it false)] v
[¬X_t-1 Ù (some action_t-1 made it true)]
§ For Pacman location:
§ At_3,3_17 Û [At_3,3_16 Ù ¬((¬Wall_3,4 Ù N_16) v (¬Wall_4,3 Ù E_16) v …)]
v [¬At_3,3_16 Ù ((At_3,2_16 Ù ¬Wall_3,3 Ù N_16) v
(At_2,3_16 Ù ¬Wall_3,3 Ù N_16) v …)]
How many sentences?
§ Vast majority of KB occupied by O(NT) transition model sentences
§ Each about 10 lines of text
§ N=200, T=400 => ~800,000 lines of text, or 20,000 pages
§ This is because propositional logic has limited expressive power
§ Are we really going to write 20,000 pages of logic sentences???
§ No, but your code will generate all those sentences!
§ In first-order logic, we need O(1) transition model sentences
§ (State-space search uses atomic states: how do we keep the
transition model representation small???)
Some reasoning tasks
§ Localization with a map and local sensing:
§ Given an initial KB, plus a sequence of percepts and actions, where am I?
§ Mapping with a location sensor:
§ Given an initial KB, plus a sequence of percepts and actions, what is the map?
§ Simultaneous localization and mapping:
§ Given …, where am I and what is the map?
§ Planning:
§ Given …, what action sequence is guaranteed to reach the goal?
§ ALL OF THESE USE THE SAME KB AND THE SAME ALGORITHM!!
Summary
§ One possible agent architecture: knowledge + inference
§ Logics provide a formal way to encode knowledge
§ A logic is defined by: syntax, set of possible worlds, truth condition
§ A simple KB for Pacman covers the initial state, sensor model, and
transition model
§ Logical inference computes entailment relations among sentences,
enabling a wide range of tasks to be solved
Simple theorem proving: Forward chaining
§ Forward chaining applies Modus Ponens to generate new facts:
§ Given X1 Ù X2 Ù … Xn Þ Y and X1, X2, …, Xn, infer Y
§ Forward chaining keeps applying this rule, adding new facts, until
nothing more can be added
§ Requires KB to contain only definite clauses:
§ (Conjunction of symbols) Þ symbol; or
§ A single symbol (note that X is equivalent to True Þ X)
§ Runs in linear time using two simple tricks:
§ Each symbol Xi knows which rules it appears in
§ Each rule keeps count of how many of its premises are not yet satisfied
Forward chaining algorithm: Details
function PL-FC-ENTAILS?(KB, q) returns true or false
count ← a table, where count[c] is the number of symbols in c’s premise
inferred ← a table, where inferred[s] is initially false for all s
agenda ← a queue of symbols, initially symbols known to be true in KB
while agenda is not empty do
p ← Pop(agenda)
if p = q then return true
if inferred[p] = false then
inferred[p]←true
for each clause c in KB where p is in c.premise do
decrement count[c]
if count[c] = 0 then add c.conclusion to agenda
return false
Properties of forward chaining
§ Theorem: FC is sound and complete for definite-clause KBs
§ Soundness: follows from soundness of Modus Ponens (easy to check)
§ Completeness proof:
1. FC reaches a fixed point where no new atomic sentences are derived
2. Consider the final set of known-to-be-true symbols as a model m (other ones false)
3. Every clause in the original KB is true in m
Proof: Suppose a clause a1Ù... Ùak Þ b is false in m
Then a1Ù... Ùak is true in m and b is false in m
Therefore the algorithm has not reached a fixed point!
4. Hence m is a model of KB
5. If KB |= q, q is true in every model of KB, including m
Resolution (briefly)
§ The resolution inference rule takes two implication sentences (of
a particular form) and infers a new implication sentence:
§ Example: A Ù B Ù C Þ U Ú V
DÙE ÙU ÞXÚY
AÙBÙCÙDÙE ÞVÚXÚY

§ Resolution is complete for propositional logic


§ Exponential time in the worst case
Satisfiability and entailment
§ A sentence is satisfiable if it is true in at least one world
§ Suppose we have a hyper-efficient SAT solver (WARNING: NP-
COMPLETE 👿 👿 👿); how can we use it to test entailment?
§ a |= b
§ iff a Þ b is true in all worlds
§ iff ¬(a Þ b) is false in all worlds
§ iff a Ù ¬b is false in all worlds, i.e., unsatisfiable
§ So, add the negated conclusion to what you know, test for
(un)satisfiability; also known as reductio ad absurdum
§ Efficient SAT solvers operate on conjunctive normal form
Conjunctive normal form (CNF)
§ Every sentence can be expressed as a conjunction of clauses
Replace biconditional by two implications

§ Each clause is a disjunction of literals Replace a Þ b by ¬a v b

§ Each literal is a symbol or a negated symbol Distribute v over Ù

§ Conversion to CNF by a sequence of standard transformations:


§ At_1,1_0 Þ (Wall_0,1 Û Blocked_W_0)
§ At_1,1_0 Þ ((Wall_0,1 Þ Blocked_W_0) Ù (Blocked_W_0 ÞWall_0,1))
§ ¬At_1,1_0 v ((¬Wall_0,1 v Blocked_W_0) Ù (¬Blocked_W_0 v Wall_0,1))
§ (¬At_1,1_0 v ¬Wall_0,1 v Blocked_W_0) Ù
(¬At_1,1_0 v ¬Blocked_W_0 v Wall_0,1)
Efficient SAT solvers
§ DPLL (Davis-Putnam-Logemann-Loveland) is the core of modern solvers
§ Recursive depth-first search over models with some extras:
§ Early termination: stop if
§ all clauses are satisfied; e.g., (A Ú B) Ù (A Ú ¬C) is satisfied by {A=true}
§ any clause is falsified; e.g., (A Ú B) Ù (A Ú ¬C) is satisfied by {A=false,B=false}
§ Pure literals: if all occurrences of a symbol in as-yet-unsatisfied clauses have
the same sign, then give the symbol that value
§ E.g., A is pure and positive in (A Ú B) Ù (A Ú ¬C) Ù (C Ú ¬B) so set it to true
§ Unit clauses: if a clause is left with a single literal, set symbol to satisfy clause
§ E.g., if A=false, (A Ú B) Ù (A Ú ¬C) becomes (false Ú B) Ù (false Ú ¬C), i.e. (B) Ù (¬C)
§ Satisfying the unit clauses often leads to further propagation, new unit clauses, etc.
DPLL algorithm
function DPLL(clauses,symbols,model) returns true or false
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,value ←FIND-PURE-SYMBOL(symbols,clauses,model)
if P is non-null then return DPLL(clauses, symbols–P, model∪{P=value})
P,value ←FIND-UNIT-CLAUSE(clauses,model)
if P is non-null then return DPLL(clauses, symbols–P, model∪{P=value})
P ← First(symbols); rest ← Rest(symbols)
return or(DPLL(clauses,rest,model∪{P=true}),
DPLL(clauses,rest,model∪{P=false}))
Efficiency
§ Naïve implementation of DPLL: solve ~100 variables
§ Extras:
§ Smart variable and value ordering
§ Divide and conquer
§ Caching unsolvable subcases as extra clauses to avoid redoing them
§ Cool indexing and incremental recomputation tricks so that every step of the
DPLL algorithm is efficient (typically O(1))
§ Index of clauses in which each variable appears +ve/-ve
§ Keep track number of satisfied clauses, update when variables assigned
§ Keep track of number of remaining literals in each clause
§ Real implementation of DPLL: solve ~100000000 variables
SAT solvers in practice
§ Circuit verification: does this VLSI circuit compute the right answer?
§ Software verification: does this program compute the right answer?
§ Software synthesis: what program computes the right answer?
§ Protocol verification: can this security protocol be broken?
§ Protocol synthesis: what protocol is secure for this task?
§ Lots of combinatorial problems: what is the solution?
§ Planning: how can I eat all the dots???
A knowledge-based agent
function KB-AGENT(percept) returns an action
persistent: KB, a knowledge base
t, an integer, initially 0
TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
action ← ASK(KB, MAKE-ACTION-QUERY(t))
TELL(KB, MAKE-ACTION-SENTENCE(action, t))
t←t+1
return action
Reminder: Partially observable Pacman
§ Pacman perceives wall/no-wall in each direction
§ Variables:
§ Wall_0,0, Wall_0,1, …
§ Blocked_W_0, Blocked_N_0, …, Blocked_W_1, …
§ W_0 , N_0, …, W_1, …
§ At_0,0_0 , At_0,1_0, …, At_0,0_1 , …
Pacman’s knowledge base: Basic PacPhysics
§ Map: where the walls are and aren’t
§ Initial state: Pacman is definitely somewhere
§ Domain constraints:
§ Pacman does exactly one action at each step
§ Pacman is in exactly one location at each step
§ Sensor model: <Percept_t> Û <some condition on world_t>
§ Transition model:
§ <at x,y_t> Û [at x,y_t-1 and stayed put] v [next to x,y_t-1 and moved to x,y]
State estimation
§ State estimation means keeping track of what’s true now
§ A logical agent can just ask itself!
§ E.g., ask whether KB Ù <actions> Ù <percepts> |= At_2,2_6
§ This is “lazy”: it analyzes one’s whole life history at each step!
§ A more “eager” form of state estimation:
§ After each action and percept
§ For each state variable X_t
§ If KB Ù action_t-1 Ù percept_t |= X_t, add X_t to KB
§ If KB Ù action_t-1 Ù percept_t |= ¬X_t, add ¬X_t to KB
Example: Localization in a known map
§ Initialize the KB with PacPhysics for T time steps
§ Run the Pacman agent for T time steps:
§ After each action and percept
§ For each variable At_x,y_t
§ If KB Ù action_t-1 Ù percept_t |= At_x,y_t, add At_x,y_t to KB
§ If KB Ù action_t-1 Ù percept_t |= ¬ At_x,y_t, add ¬ At_x,y_t to KB
§ Choose an action
§ Pacman’s possible locations are those that are not provably false
Localization demo
§ Percept
§ Action
§ Percept
§ Action
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action SOUTH
§ Percept
§ Action
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action SOUTH
§ Percept
§ Action SOUTH
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action SOUTH
§ Percept
§ Action SOUTH
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action
§ Percept
§ Action
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
§ Action
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
Localization demo
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
§ Action WEST
§ Percept
Localization with random movement
Example: Mapping from a known relative location
§ Without loss of generality, call the initial location 0,0
§ The percept tells Pacman which actions work, so he always knows where he is
§ “Dead reckoning”
§ Initialize the KB with PacPhysics for T time steps, starting at 0,0
§ Run the Pacman agent for T time steps
§ At each time step
§ Update the KB with previous action and new percept facts
§ For each wall variable Wall_x,y
§ If Wall_x,y is entailed, add to KB
§ If ¬Wall_x,y is entailed, add to KB
§ Choose an action
§ The wall variables constitute the map
Mapping demo
§ Percept
§ Action NORTH
§ Percept
§ Action EAST
§ Percept
§ Action SOUTH
§ Percept
Example: Simultaneous localization and mapping
§ Often, dead reckoning won’t work in the real world
§ E.g., sensors just count the number of adjacent walls (0,1,2,3 = 2 bits)
§ Pacman doesn’t know which actions work, so he’s “lost”
§ So if he doesn’t know where he is, how does he build a map???
§ Initialize the KB with PacPhysics for T time steps, starting at 0,0
§ Run the Pacman agent for T time steps
§ At each time step
§ Update the KB with previous action and new percept facts
§ For each x,y, add either Wall_x,y or ¬Wall_x,y to KB, if entailed
§ For each x,y, add either At_x,y_t or ¬At_x,y_t to KB, if entailed
§ Choose an action
Planning as satisfiability
§ Given a hyper-efficient SAT solver, can we use it to make plans?
§ Yes, for fully observable, deterministic case:
§ planning problem is solvable iff there is some satisfying assignment
§ solution obtained from truth values of action variables
§ For T = 1 to ¥,
§ Initialize the KB with PacPhysics for T time steps
§ Assert goal is true at time T
§ Read off action variables from SAT-solver solution
Summary
§ Logical inference computes entailment relations among sentences
§ Theorem provers apply inference rules to sentences
§ Forward chaining applies modus ponens with definite clauses; linear time
§ Resolution is complete for PL but exponential time in the worst case
§ SAT solvers based on DPLL provide incredibly efficient inference
§ Logical agents can do localization, mapping, SLAM, planning (and
many other things) just using one generic inference algorithm on
one knowledge base

You might also like