97
CS 541: Artificial Intelligence Lecture IV: Logic Agent and First Order Logic

CS 541: Artificial Intelligence

  • Upload
    parson

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 541: Artificial Intelligence. Lecture IV: Logic Agent and First Order Logic. Announcement. CA Office Hour: 3:00pm-4:00pm, Lieb 319. Homework assignment should be checked at Moodle. Schedule. Re-cap Lecture III. Constraint Satisfaction Problem - PowerPoint PPT Presentation

Citation preview

Page 1: CS 541: Artificial Intelligence

CS 541: Artificial Intelligence

Lecture IV: Logic Agent and First Order Logic

Page 2: CS 541: Artificial Intelligence

Announcement CA Office Hour: 3:00pm-4:00pm, Lieb 319.

Homework assignment should be checked at Moodle.

Page 3: CS 541: Artificial Intelligence

ScheduleWeek Date Topic Reading Homework**

1 08/29/2012 Introduction & Intelligent Agent Ch 1 & 2 N/A

2 09/05/2012 Search: search strategy and heuristic search Ch 3 & 4s HW1 (Search)

3 09/12/2012 Search: Constraint Satisfaction & Adversarial Search Ch 4s & 5 & 6  Teaming Due

4 09/19/2012 Logic: Logic Agent & First Order Logic Ch 7 & 8s HW1 due, Midterm Project  (Game)

5 09/26/2012 Logic: Inference on First Order Logic Ch 8s & 9  6 10/03/2012 No class     7 10/10/2012 Uncertainty and Bayesian Network  Ch 13 &

Ch14s HW2 (Logic)

8 10/17/2012 Midterm Presentation   Midterm Project Due

9 10/24/2012 Inference in Baysian Network Ch 14s HW2 Due, HW3 (Probabilistic Reasoning)

10 10/31/2012 Probabilistic Reasoning over Time Ch 15  11 11/07/2012 Machine Learning    HW3 due,

12 11/14/2012 Markov Decision Process Ch 18 & 20 HW4 (Probabilistic Reasoning Over Time)

 13 11/21/2012 No class Ch 16  14 11/29/2012 Reinforcement learning Ch 21 HW4 due

15 12/05/2012 Final Project Presentation   Final Project Due

Page 4: CS 541: Artificial Intelligence

Re-cap Lecture III Constraint Satisfaction Problem

Constraint satisfaction problem (CSP) examples Backtracking search for CSPs Problem structure and problem decomposition Local search for CSPs

Adversarial Search and Games Games Perfect play

Minimax decisions α - β pruning

Resource limits and approximate evaluation Games of chance Games of imperfect information

Page 5: CS 541: Artificial Intelligence

Logic Agent

Lecture IV: Part I

Page 6: CS 541: Artificial Intelligence

Outline Knowledge-based agents Wumpus world Logic in general—models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem proving

Forward chaining Backward chaining Resolution

Page 7: CS 541: Artificial Intelligence

Knowledge bases

Knowledge base Set of sentences in a formal language

Declarative approach to building an agent (or other system) TELL it what is needs to know

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

Or at the implementation level i.e., data structures in KB and algorithms that manipulate them

Page 8: CS 541: Artificial Intelligence

A simple knowledge-based agent

The agent must be able to: Represent states, actions, etc. Incorporate new percepts Update internal representations of the world Deduce hidden properties of the world Deduce appropriate actions

Page 9: CS 541: Artificial Intelligence

Wumpus world PEAS description Performance measure

Gold +1000; Death -1000 -1 per step; -10 for using the arrow

Environment Squares adjacent to wumpus are smelly Squares adjacent to pit are breezy Glitter iff gold is in the same square Shooting kills wumpus if you are facing it Grabbing picks up gold if in same square Releasing drops the gold in same square

Actuators Left turn, right turn, forward, grab, release,

shoot Sensors

Breeze, glitter, smell

Page 10: CS 541: Artificial Intelligence

Wumpus world characterization Observable??

No, only local perception Deterministic??

Yes, outcomes exactly specified Episodic??

No, sequential at the level of actions Static??

Wumpus and pits do not move Discrete??

Yes Single-agent??

Yes, Wumpus is essentially a natural feature

Page 11: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 12: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 13: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 14: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 15: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 16: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 17: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 18: CS 541: Artificial Intelligence

Exploring a wumpus world

Page 19: CS 541: Artificial Intelligence

Other tight spots Breeze in (1,2) and (2, 1)

No safe actions

Smell in (1,1) Cannot move

Can use a strategy of coercion Shoot straight ahead Wumpus was theredeadsafe Wumpus wasn’t there safe

Page 20: CS 541: Artificial Intelligence

Logic in general Logics are formal languages for representing

information Such that conclusions can be drawn

Syntax defines the sentences in the language Semantics define the “meaning” of sentences

i.e., define truth of a sentence in a world E.g., the language of arithematics

x+2≥y is a sentence; x2+2> is not a sentence x+2≥y is true iff the number x+2 is no less than the number y x+2≥y is true in a world where x=7, y=1 x+2≥y is false in a world where x=0, y=6

Page 21: CS 541: Artificial Intelligence

Entailment Entailment means that one thing follows from

another

Knowledge base KB entails sentence If and only if is true in all worlds where KB is true E.g., the KB containing “the giants won” and “the

Reds won” entails “Either the Giants won or the Reds won”

E.g., x+y=4 entails 4=x+y Entailment is a relationship between sentences

(i.e., syntax) that is based on semantics Note: brains process syntax (of some sort)

Page 22: CS 541: Artificial Intelligence

Models Logicians typically think in terms of models,

which are formally structured worlds with respect to which truth can be evaluated

We say m is a model of a sentence is the set of all models of

Then if and only if E.g, KB=Giants won and Reds won, and =Giants won

Page 23: CS 541: Artificial Intelligence

Entailment in the wumpus world Situation after detecting nothing in

[1,1], moving right, breeze in [2,1] Consider possible models for ?s

assuming only pits 3 Boolean choices 8 possible

models

Page 24: CS 541: Artificial Intelligence

Wumpus models

Page 25: CS 541: Artificial Intelligence

Wumpus models

KB=wumpus-world rules+observations

Page 26: CS 541: Artificial Intelligence

Wumpus models

KB=wumpus-world rules+observations =“[1,2] is safe”, , proved by model checking

Page 27: CS 541: Artificial Intelligence

Wumpus models

KB=wumpus-world rules+observations

Page 28: CS 541: Artificial Intelligence

Wumpus models

KB=wumpus-world rules+observations =“[2,2] is safe”,

Page 29: CS 541: Artificial Intelligence

Inference = sentence can be derived from KB by procedure i Consequences of KB are a haystack; is a needle. Entailment = needle in haystack; inference=finding it! Soundness:

i is sound if whenever , it is also true that Completeness:

i is complete if whenever , it is also true that Preview:

We will define a logic (first-order logic) which is expressive enough to say almost anything of interest, and for which there exists a sound and complete inference procedure

i.e., the procedure will answer any question whose answer follows from what is known about KB

Page 30: CS 541: Artificial Intelligence

Propositional logic: Syntax Propositional logic is the simplest logic—

illustrates basic ideas The proposition symbols P1, P2 etc. are

sentences If S 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 (bicondition)

Page 31: CS 541: Artificial Intelligence

Propositional logic: Semantics Each model specifies true/false for each proposition symbol

E.g., true true true With these symbols, 8 possible models, can be enumerated

automatically Rules for evaluating truth with respect to a model m:

is true iff is false is true iff is true and is true is true iff is true or is true is true iff is false or is truei.e., is false iff is true and is false is true iff is true or is true

Simple recursive process evaluates an arbitrary sentence, e.g.,

Page 32: CS 541: Artificial Intelligence

Truth tables for connectives

Page 33: CS 541: Artificial Intelligence

Wumpus world sentences Let be true if there is a pit in [i,j] Let be true if there is a breeze in [i,j]

“Pits cause breezes in adjacent squares”

“A square is breezy if and only if there is an adjacent pit”

Page 34: CS 541: Artificial Intelligence

Truth tables for inference

Enumerate rows (different assignments to symbols)

If KB is true in row, check that is too

Page 35: CS 541: Artificial Intelligence

Inference by enumeration Depth-first enumeration of all models in sound and complete

for n symbols; problem is co-NP-complete

Page 36: CS 541: Artificial Intelligence

Logical equivalence Two sentences are logically equivalent iff true in

same models: if and only if and

Page 37: CS 541: Artificial Intelligence

Validity and satisfiability A sentence is valid if it is true in all models

E.g., Validity is connected to inference via the Deduction

Theorem if and only if is valid

A sentence is satisfiable if it is true in some models E.g.,

A sentence is unsatisfiable if it is true in no model E.g.,

Satisfiability is connected to inference via the following: if and only if is unsatisfiable i.e., prove by reductio ad absurdum

Page 38: CS 541: Artificial Intelligence

Proof methods Proof methods divide into (roughly) two kinds: Application of inference rules

Legitimate (sound) generation of new sentences from old Proof = a sequence of inference rule applications

Can use inference rules as operators in a standard search algorithm

Typically require translation of sentences into a norm form Model checking

Truth table enumeration (always exponential in n) Improved backtracking, e.g., Davis-Putnam-Logemann-

Loveland (DPLL) Heurisitc search in model space (sound but incomplete)

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

Page 39: CS 541: Artificial Intelligence

Horn From (restricted) KB=conjunction of Horn clauses Horn clause =

Proposition symbol (Conjunction of symbols) symbol E.g.,

Modus Ponens (for Horn Form): complete for Horn KBs

Can be used with forward chaining or backward chaining

These algorithms are very natural and run in linear time

Forward and backward chaining

Page 40: CS 541: Artificial Intelligence

Idea: fire any rule whose premises are satisfied in the KB, add its conclusion to the KB, until query is found

Forward chaining

Page 41: CS 541: Artificial Intelligence

Forward chaining algorithm

Page 42: CS 541: Artificial Intelligence

Forward chaining example

Page 43: CS 541: Artificial Intelligence

Forward chaining example

Page 44: CS 541: Artificial Intelligence

Forward chaining example

Page 45: CS 541: Artificial Intelligence

Forward chaining example

Page 46: CS 541: Artificial Intelligence

Forward chaining example

Page 47: CS 541: Artificial Intelligence

Forward chaining example

Page 48: CS 541: Artificial Intelligence

Forward chaining example

Page 49: CS 541: Artificial Intelligence

Forward chaining example

Page 50: CS 541: Artificial Intelligence

Proof of completeness FC derives every atomic sentence that is entailed by KB

FC reaches a fixed point where no new atomic sentences are derived

Consider the final state as a model m, assigning true/false to symbols

Every clause in the original KB is true in m Proof: Suppose a clause is false in mThen is true in m and b is false in mTherefore the algorithm has not reached a fixed point

Hence m is a model of KB If , q is true in every model of KB, including m General idea: construct any model of KB by sound inference, check

Page 51: CS 541: Artificial Intelligence

Backward chaining Idea: work backwards from the query q:

To prove q by BC, check if q is known already, or, prove by BC all premises of some rule concluding q

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

Avoid repeated work: check if new sub goal Has already been proven true, or Has already failed

Page 52: CS 541: Artificial Intelligence

Backward chaining example

Page 53: CS 541: Artificial Intelligence

Backward chaining example

Page 54: CS 541: Artificial Intelligence

Backward chaining example

Page 55: CS 541: Artificial Intelligence

Backward chaining example

Page 56: CS 541: Artificial Intelligence

Backward chaining example

Page 57: CS 541: Artificial Intelligence

Backward chaining example

Page 58: CS 541: Artificial Intelligence

Backward chaining example

Page 59: CS 541: Artificial Intelligence

Backward chaining example

Page 60: CS 541: Artificial Intelligence

Backward chaining example

Page 61: CS 541: Artificial Intelligence

Backward chaining example

Page 62: CS 541: Artificial Intelligence

Backward chaining example

Page 63: CS 541: Artificial Intelligence

Forward vs. backward chaining FC is data-driven, cf. automatic, unconscious

processing E.g., object recognition, routine decisions

May do lots of work that is irrelevant to the goal BC is goal-driven, appropriate for problem-solving

E.g., where are my keys? How do I get into a PhD program? Complexity of BC can be much less than linear in size of KB

Page 64: CS 541: Artificial Intelligence

Resolution Conjunctive Normal Form (CNF—universal)

Conjunction of disjunctions of literals E.g.,

Resolution inference rule (for CNF): complete for propositional logic

where li and mj are complementary literals. E.g,

Resolution is sound and complete complete for propositional logic

Page 65: CS 541: Artificial Intelligence

Conversion to CNF

1. Eliminate , replacing with 2. Eliminate , replacing with 3. Move inwards using de Morgan’s rules and double-negation 4. Apply distributivity law ( over ) and flatten:

Page 66: CS 541: Artificial Intelligence

Resolution algorithm Proof of contradiction, i.e., show unsatisfiable

Page 67: CS 541: Artificial Intelligence

Resolution example

Page 68: CS 541: Artificial Intelligence

Summary Logical agents apply inference to a knowledge base to derive

new information and make decisions Basic concepts of logic:

Syntax: formal structure of sentences Semantics: truth of sentences wrt models Entailment: necessary truth of one sentence given another Inference: deriving sentences from other sentences Soundess: derivations produce only entailed sentences Completeness: derivations can produce all entailed sentences

Wumpus world requires the ability to represent partial and negated information, reason by cases, etc.

Forward, backward chaining are linear-time, complete for Horn clauses

Resolution is complete for propositional logic Propositional logic lacks expressive power

Page 69: CS 541: Artificial Intelligence

First-order Logic

Lecture IV: Part II

Page 70: CS 541: Artificial Intelligence

Outline Why first-order logic (FOL)? Syntax and semantics of FOL Fun with sentences Wumpus world in FOL

Page 71: CS 541: Artificial Intelligence

Pros and cons of propositional logic Propositional logic is declarative: pieces of syntax

correspond to facts Propositional logic allows partial/disjunctive/negated

information Unlike most data structures and databases

Propositional logic is compositional: Meaning of is derived from meaning of and of

Meaning in propositional logic is context-independent Unlike natural language, where meaning depends on context

Propositional logic has very limited expressive power Unlike natural language E.g., cannot say “pits cause breezes in adjacent squares” except by

writing one sentence for each square

Page 72: CS 541: Artificial Intelligence

First-order logic Whereas propositional logic assumes world contains facts, First-order logic (like natural language) assumes that the world

contains Objects: people, houses, numbers, theories, Ronald McDonald, colors,

baseball games, wars, centuries … Relations: red, round, bogus, prime, multistoried…, brother of, bigger

than, inside, part of, has color, occurred after, owns, comes between, … Functions: father of, best friend, third inning of, one more than, end of…

Page 73: CS 541: Artificial Intelligence

Logics in general

Page 74: CS 541: Artificial Intelligence

Syntax of FOL: Basic elements Constants: King John, 2, Stevens Predicates: Brother, >, … Functions: Sqrt, LeftLegOf,… Variables: x, y, a, b, … Connectives: Equality: = Quantifiers

Page 75: CS 541: Artificial Intelligence

Atomic sentences Atomic sentence = predicate(term1, …, termn) Or term1=term2

Term=function(term1,…,termn) or constant or variable

E.g., Brother(King John, RichardTheLinonHeart)

>(Length(LeftLegOf(Richard))), Length(LeftLegOf(KingJohn)))

Page 76: CS 541: Artificial Intelligence

Complex sentences Complex sentences are made from atomic sentences using

connectives E.g.

Page 77: CS 541: Artificial Intelligence

Truth in first-order logic Sentences are true with respect to a model

and an interpretation Model contains objects (domain elements)

and relations among them Interpretation specifies referents for

Constant symbols objects Predicate symbols relations Function symbols functional relations

An atomic sentence predicate(term1,…, termn) is true Iff the objects referred by term1, …, termn are in

the relations referred to by predicate

Page 78: CS 541: Artificial Intelligence

Models for FOL: example

Page 79: CS 541: Artificial Intelligence

Truth example Consider the interpretation in which

Richard Richard the Lionheart John the evil King John Brother The brotherhood relation

Under this interpretation, Brother(Richard, John) is true, just in case Richard the Liohheart and the evil King John are in the brotherhood relation in the model

Page 80: CS 541: Artificial Intelligence

Models of FOL: lots! Entailment in propositional logic can be computed by

enumerating models We can enumerate the FOL models for a given KB

vocabulary, i.e., For each number of domain elements n from 1 to ∞

For each k-ary predicate Pk in the vocabulary For each possible k-ary relation on n objects

For each constant symbol C in the vocabulary For each choice of referent for C from n Objects …

Computing entailment by enumerating FOL models is not easy!

Page 81: CS 541: Artificial Intelligence

Universal quantification

Everyone at Stevens is smart

is true in a model m iff P is true with x being each possible object in the model

Roughly speaking, equivalent to the conjunction of instantiations of P

Page 82: CS 541: Artificial Intelligence

A common mistake to avoid Typically, is the main connective with Common mistake: using as the main

connective with : means “Everyone is at Stevens and everyone is

smart”.

Page 83: CS 541: Artificial Intelligence

Existential quantification

Someone at RPI is smart:

is true in a model m iff P is true with x being some possible object in the model

Roughly speaking, equivalent to the disjunction of instantiations of P

Page 84: CS 541: Artificial Intelligence

Another common mistake to avoid Typically, is the main connective with Common mistake: using as the main

connective with :

is true if there is anyone who is not at RPI!

Page 85: CS 541: Artificial Intelligence

Properties of quantifiers is the same as is the same as is not the same as

“There is a person who loves everyone in the world”

“Everyone in the world is loved by at least one person” Quantifier duality: each can be expressed using

the other

Page 86: CS 541: Artificial Intelligence

Fun with sentences Brothers are siblings

“Sibling” is symmetric

One’s mother is one’s female parent

A first counsin is a child of a parent’s sibling

Page 87: CS 541: Artificial Intelligence

Equality term1=term2 is true under a given

interpretation if and only if term1 and term2 refer to the same object, e.g., and are satisfiable is valid Definition of (full) Sibling in terms of Parent:

Page 88: CS 541: Artificial Intelligence

Interacting with FOL KBs Suppose a wumpus-world agent is using an FOL KB and

perceives a smell and a breeze (but no glitter) at t=5: I.e., does KB entail any particular actions at t=5? Answer: Yes, {aShoot} substitution (binding list) Given a sentence S and a substitution , denotes the result of plugging into ; e.g.,

returns some/all such that

Page 89: CS 541: Artificial Intelligence

Knowledge base for the wumpus world “Perception”

Reflex: Reflex with internal state: do we have the

gold already? can not be observed

keeping track of changes is essential

Page 90: CS 541: Artificial Intelligence

Deducing hidden properties Properties of locations:

Squares are breezy near a pit: Diagnostic rule—infer cause from effect

Causal rule—infer from cause

Neither of these is complete—e.g., the causal rule doesn’t say whether squares far away from pits can be breezy

Definition for the Breezy predicate

Page 91: CS 541: Artificial Intelligence

Keeping track of change Facts hold in situations, rather than eternally

E.g., rather than just Situation calculus is one way to represent change in FOL:

Adds a situation argument to each non-eternal predicate E.g., Now in denotes a situation

Situation are connected by the Result function is the situation that results from doing a in s

Page 92: CS 541: Artificial Intelligence

Describing actions I “Effect” axiom—describe changes due to action “Frame” axiom—describe non-changes due to action Frame problem: find an elegant way to handle non-change

Representation –avoid frame axioms Inference—avoid repeated “copy-overs” to keep track of state

Quantification problem: true descriptions of real actions require endless caveats—what if gold is slippery or nailed down or …

Ramification problem: real actions have many secondary consequences—what about the dust on the gold, wear and tear on gloves, …

Page 93: CS 541: Artificial Intelligence

Describing actions II Successor-state axioms solve the representational

frame problem Each axiom is “about” a predicate (not an action

per se):

For holding the gold

Page 94: CS 541: Artificial Intelligence

Making plans Initial condition in KB:

Query: i.e., in what situation will I be holding the gold?

Answer: i.e., go forward and then grab the gold

This assumes that the agent is interested in plans starting at S0 and that S0 is the only situation described in the KB

Page 95: CS 541: Artificial Intelligence

Making plans: A better way Represent plans as action sequences is the result of executing p in s Then the query has the solution Definition of PlanResutlt in terms of Result: Planning systems are special-purpose reasoners

designed to do this type of inference more efficiently than a general-purpose reasoner

Page 96: CS 541: Artificial Intelligence

Summary First-order logic:

Objects and relations are semantic primitives Syntax: constants, functions, predicates, equality,

quantiers Increased expressive power: sufficient to define

wumpus world Situation calculus:

Conventions for describing actions and change in FOL Can formulate planning as inference on a situation

calculus KB

Page 97: CS 541: Artificial Intelligence

Midterm Mastermind (midterm)

http://en.wikipedia.org/wiki/Mastermind_%28board_game%29 Base problem: Guessing of 4 digit numbers without repetition What to submit?

A report summarizing the algorithm you adopted and implemented The report should come with related technical papers you read The report should include complexity analysis (worst case) The report should also include empirical performance.

Source code and executable with detailed ReadMe on how to run it Code implementation:

A starter program which generates a random 4 or n digit numbers without repetition A master program which can check the number of matched digits and positions of

your guessed number from your main program and the target number (hidden) The input is your guessed number, the output is of style mAnB stands for m digits are

guessed correctly with correct positions, with n other digits guessed correctly but with wrong positions.

A main program which guesses the target number and interacts with your master program to get feedback and re-guessing

Your main program should also be able to get feedback with dumb “keyboard input”.