Artificial Intelligence Notes

86
Unit II • Topics: Logical Agent. Propositional Logic. Inference. Resolution. Forward chaining & Backward chaining. First Order Logic. Syntax & Semantics of FOL. Quantifiers. Kinship Domain. Knowledge engineering Process.

Transcript of Artificial Intelligence Notes

Unit II• Topics:

Logical Agent. Propositional Logic. Inference. Resolution. Forward chaining & Backward chaining.

First Order Logic. Syntax & Semantics of FOL. Quantifiers. Kinship Domain. Knowledge engineering Process.

Inference In FOL. Unification. Forward Chaining. Backward Chaining. Logic programming: Prolog Resolution.

Logical Agents

Artificial Agent• Representation of knowledge and the reasoning process that brings knowledge to life.

• Knowledge and reasoning -artificial agent.

• Central component of the knowledge-based agent is its knowledge base or KB.

• Knowledge base = set of sentences.• Language of sentence – Knowledge representation language.

• Two standard tasks- TELL and ASK.

• Both task , involve – inference (deriving new sentence from old).

A simple knowledge-based agent

Wumpus World

Wumpus World PEAS description

• Performance measure + 1000 for picking gold, - 1000 for falling in to a pit or eaten by Wumpus ,-1 for each action and –10 for using up the Arrow.

• Environment: 4 x 4 grid of rooms.. Agent starts in the square [ 1 ,1 ]

• Actuators: Left turn, Right turn, Forward, Grab Release, Shoot.

Sensors: five sensors. -Stench(wumpus) -Breeze ( pit) - Glitter ( G old) -Bump ( Walks into a wall)

- Scream ( Wumpus is killed)

Wumpus world characterization

• Fully Observable• Deterministic• Episodic• Static• Discrete• Single-agent

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Exploring a wumpus world

Logic• 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;

Entailment• Entailment means that one thing follows from another:

α ╞ β• Knowledge base α entails sentence β if and only if β is true in all worlds where α is true

– E.g., x+y = 4 entails 4 = x+y– Entailment is a relationship between sentences (i.e., syntax) that is based on semantics

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 α if α is true in m

• M(α) is the set of all models of α

• Then KB ╞ α iff M(KB) M(α)

Entailment in the wumpus world

Situation after detecting nothing in [1,1], moving right, breeze in [2,1]

Consider possible models for KB assuming only pits

3 Boolean choices 8 possible models

Wumpus models

Wumpus models

• KB = wumpus-world rules + observations

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, S is a sentence (negation)– If S1 and S2 are sentences, S1 S2 is a sentence (conjunction)

– If S1 and S2 are sentences, S1 S2 is a sentence (disjunction)

– If S1 and S2 are sentences, S1 S2 is a sentence (implication)

– If S1 and S2 are sentences, S1 S2 is a sentence (biconditional)

Propositional logic: Semantics

Each model specifies true/false for each proposition symbolE.g. P1,2 P2,2 P3,1 false true false

With these symbols, 8 possible models, can be enumerated automatically.Rules for evaluating truth with respect to a model m:

S is true iff S is false S1 S2 is true iff S1 is true and S2 is trueS1 S2 is true iff S1is true or S2 is trueS1 S2 is true iff S1 is false or S2 is true i.e., is false iff S1 is true and S2 is falseS1 S2 is true iff S1S2 is true andS2S1 is true

Simple recursive process evaluates an arbitrary sentence, e.g.,P1,2 (P2,2 P3,1) = true (true false) = true true = true

Truth tables for connectives

Inference

• KB ├i α = sentence α can be derived from KB by procedure i

• Soundness: i is sound if whenever KB ├i α, it is also true that KB╞ α

• Completeness: i is complete if whenever KB╞ α, it is also true that KB ├i α

Wumpus world sentencesLet Pi,j be true if there is a pit in [i, j].Let Bi,j be true if there is a breeze in [i, j].R1: P1,1"Pits cause breezes in adjacent squares"R2: B1,1 (P1,2 P2,1)R3: B2,1 (P1,1 P2,2 P3,1)

• After visiting:

R4: B1,1

R5: B2,1

Truth tables for inference

Equivalence,Validity,Satisfiability• 3 concepts needed for logical inference

algorithm.• Logical equivalence• Validity: A sentence is valid if it is true in all models.

Valid sentence are also known as tautologies.

• Satisfiability: Sentence is satisfiable if it is true in some model.

Eg: 3 models in the sentence are satisfiable

(R1,R4,R5).

Logical equivalence• Two sentences are logically equivalent if true in same models:

• α ≡ ß if α╞ β and β╞ α

• Resolution. Derivations given in notes.• Forward chaining: Data Driven Reasoning.• Backward chaining: Goal Directed Reasoning.

First-Order Logic

• FOL: Expressive to represent a good deal of our commonsense knowledge.(natural language).

Syntax: noun and noun phrases- Objects (Pits,Squares)

Verbs and verb phrases – Relations among

objects.(is breezy,shoots)

some relations - functions

First-order logic• Whereas propositional logic assumes the world contains facts,

• First-order logic (like natural language) assumes the world contains– Objects: people, houses, numbers, colors, baseball games, wars, …

– Relations: red, round, brother of, bigger than, part of, comes between, …

– Functions one more than, plus, •

FOL & PL• Propositional Logic: Each fact can be True or False.

• First Order Logic: Consists of objects with certain relations

among them.

Syntax & Semantics of FOL

• Models: Domain of a model is the set of objects it contains.

Domain element – objects in the model.

Models for FOL: Example

• Model: Five objects Richard,Evil King John(younger brother),the left legs of both kings,and a crown.

• Relations: Binary relation: Objects related-Set of tuples of objects.

Eg: { < Richard,King john>,<King John,Richard><the crown,king john>

Unary relation: Richard’s left leg.<king kohn> -> john’s left leg

Symbols & Interpretation• Symbols: Constant symbol: Objects Predicate symbol: Relations Function symbol: Function• Interpretation: Specifies exactly which Object,relation,function are referred by constant, predicate and function symbol.

Eg: john- King john Brother- brotherhood relation. Left leg –Left leg function.

Syntax of FOL: Basic elements• Constants KingJohn,A,...

• Predicates Brother,Raining,Hascolor >,...

• Functions Sqrt, LeftLeg,Plus...• Variables x, y, a, b,...• Connectives, , , , • Equality = • Quantifiers , • Sentence- Atomic | Complex • Atomic- Predicate(Terms,…)• Term- Constant| Variable

Atomic sentencesAtomic sentence = predicate (term1,...,termn) or term1 = term2

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

• E.g., Brother(KingJohn,RichardTheLionheart) Married(Father(Richard),Mother(john))

Complex sentences• Complex sentences are made from atomic sentences using connectives

• Eg:King(Richard) King(John)

King(Richard) King(John)

Universal quantification• <variables> <sentence>

Everyone at NUS is smart: x At(x,NUS) Smart(x)A common mistake to avoid:• Common mistake: using as the main connective with :x At(x,NUS) Smart(x)means “Everyone is at NUS and everyone is smart”

• Typically, is the main connective with

…..cont Univercell Quantifier

• Variable x written in lowercase. (it is term & argument of function)

• x King(x) Person(x)King(john) Person(john)

Richard the lion heart is king Richard the lion Heart is a person

John left leg is a king John left leg is a personThe Crown is a king The crown is a personAll above statements are true under original

interpretation ,based on the truth table for

Existential quantification

• <variables> <sentence>

• King John Has a crown on his head• x Crown(x) OnHead(x,john)• “There exist an x”

• x P is true in a model m iff P is true with x, for atleast one object.

Apply Richard, john, crown for xRichard is a crown Richard is on john’s HeadKing john is crown king john is on john’s headThe crown is a crown the crown is on john’s head Is the natural Connectives used in

The Third assertion is true in the model.

Using FOLThe kinship domain:• Brothers are siblings

x,y Brother(x,y) Sibling(x,y)• One's mother is one's female parent

m,c Mother(c) = m (Female(m) Parent(m,c))• “Sibling” is symmetric

x,y Sibling(x,y) Sibling(y,x)

–––

Knowledge engineering in FOL

1. Identify the task.2. Assemble the relevant knowledge.3. Decide on a vocabulary of predicates,

functions, and constants.4. Encode general knowledge about the

domain.5. Encode a description of the specific

problem instance.6. Pose queries to the inference

procedure and get answers.7. Debug the knowledge base.

Inference in first-order logic

Universal instantiation (UI)• Every instantiation of a universally quantified sentence is

entailed by it:v α

SUBST({v/g}, α)

King(Richard) Greedy(Richard) Evil(Richard) King(Father(John)) Greedy(Father(John))

Evil(Father(John))...

E.g., x King(x) Greedy(x) Evil(x) yields: King(John) Greedy(John) Evil(John)

for any variable v and ground term g

Existential instantiation (EI)

• For any sentence α, variable v, and constant symbol k that does not appear elsewhere in the knowledge base:

v αSUBST({v/k}, α)

k is a constant symbol

• E.g., x Crown(x) OnHead(x,John) yields:

Crown(C1) OnHead(C1,John)provided C1 is a new constant symbol, called a Skolem constant, process is Skolemization

Generalized Modus Ponens (GMP)

p1', p2', … , pn', ( p1 p2 … pn q) -------------------------------------------------

p1' is King(John) p1 is King(x) p2' is Greedy(y) p2 is Greedy(x) θ is {x/John , y/John} q is

Evil(x) q θ is Evil(John)

Unification• Inference rule require finding substitution that make different logical expression is called Unification.

• Unify(α,β) = θ if αθ = βθ

p q θ Unify( Knows(John,x) Knows(John,Jane)

Unify( Knows(John,x) Knows(y,Bill) Unify( Knows(John,x) Knows(y,Mother(y)) Unify( Knows(John,x) Knows(x,Mark)

Unification p q θ Knows(John,x) Knows(John,Jane)

{x/Jane}}

Unification

Knows(John,x) Knows(John,Jane) {x/Jane}}

Knows(John,x) Knows(y,Bill) {x/Bill,y/John}}

• p q θ

Unification

p q θ Knows(John,x) Knows(John,Jane) {x/Jane}}Knows(John,x) Knows(y,Bill)

{x/Bill,y/John}}Knows(John,x) Knows(y,Mother(y))

{y/John,x/Mother(John)}}

Unification

p q θ Knows(John,x) Knows(John,Jane) {x/Jane}}Knows(John,x) Knows(y,Bill)

{x/Bill,y/John}}Knows(John,x) Knows(y,Mother(y))

{y/John,x/Mother(John)}}Knows(John,x) Knows(x,Mark) {fail}

• Standardizing apart eliminates overlap of variables, e.g.,

UnifyKnows(John,x) , Knows(Z,Mark) = {x/Mark,z/John}}

Unification• To unify Knows(John,x) and Knows(y,z),

θ = {y/John, x/z } or θ = {y/John, x/John, z/John}

• The first unifier is more general than the second.

• There is a single most general unifier (MGU) that is unique up to renaming of variables.

MGU = { y/John, x/z }–

The unification algorithm

Storage and Retrieval

• TELL & ASK Function – inform and interrogate a knowledge base.

• STORE & FETCH same as TELL & ASK.

Forward chaining• Propositional definite clause (disjunction With 1 positive literals)

• First order definite clause (conjunction With positive literals)

King(x) Greedy(x) Evil(x)

King(John) Greedy(y)

Example

• The law says that it is a crime for an American to sell weapons to hostile nation. The country Nono, an enemy of America, has some missiles and all of its missiles were sold to it by Colonel West, who is American

Forward chaining algorithm

Forward chaining proof

Forward chaining proof

Forward chaining proof

Backward chaining algorithm

SUBST(COMPOSE(θ1, θ2), p) = SUBST(θ2, SUBST(θ1, p))

Backward chaining example

Backward chaining example

Backward chaining example

Backward chaining example

Backward chaining example

Backward chaining example

Backward chaining example

Properties of backward chaining

• Incomplete due to infinite loops fix by checking current goal against every goal on stack

• Inefficient due to repeated subgoals (both success and failure) fix using caching of previous results (extra space)

• Widely used for logic programming

Logic programming: Prolog• Algorithm = Logic + Control

Prolog uses: Upper case letter ------- Variables Lower case letter ------- Constants Head(result) Preceding the body :- ----- Left implication comma ----- to separate the literals Period mark ----- End

• Program = set of clauses = head :- literal1, … literaln.

criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z).

Prolog• Appending two lists to produce a third:

append([],Y,Y). append([X|L],Y,[X|Z]) :- append(L,Y,Z).

:- append(x,y,z)

• query: append(A,B,[1,2]) ?

• answers: A=[] B=[1,2] A=[1] B=[2] A=[1,2] B=[]

Prolog-Standard Logical inference

• Depth-first, left-to-right backward chaining• Built-in functions for arithmetic etc., e.g., X is 4+3 & not 5 is x+y.

• Built-in predicates (e.g., input and output predicates, assert/retract predicates)

• Closed-world assumption ("negation as failure")– e.g., given alive(X) :- not dead(X).

Resolution• Apply resolution steps to CNF(KB α); complete for FOL

Conversion to CNF• Everyone who loves all animals is loved by someone:

x [y Animal(y) Loves(x,y)] [y Loves(y,x)]

1. Eliminate biconditionals and implicationsx [y Animal(y) Loves(x,y)] [y Loves(y,x)]

2. Move inwards: x p ≡ x p, x p ≡ x px [y (Animal(y) Loves(x,y))] [y Loves(y,x)] x [y Animal(y) Loves(x,y)] [y Loves(y,x)] x [y Animal(y) Loves(x,y)] [y Loves(y,x)]

–•

Conversion to CNF contd.3.Standardize variables: each quantifier should use a different

one

x [y Animal(y) Loves(x,y)] [z Loves(z,x)] 4.Skolemize: a more general form of existential instantiation.

Each existential variable is replaced by a Skolem function of the enclosing universally quantified variables:

x [Animal(F(x)) Loves(x,F(x))] Loves(G(x),x)5.Drop universal quantifiers:

[Animal(F(x)) Loves(x,F(x))] Loves(G(x),x)6.Distribute over :

[Animal(F(x)) Loves(G(x),x)] [Loves(x,F(x)) Loves(G(x),x)]

Example• Everyone who loves all animal is loved by someone.

• Anyone who kills an animal is loved by no one.

• Jack loves all animal.

Resolution proof: definite clauses