Скачать презентацию CS 344 Introduction to Artificial Intelligence Pushpak Скачать презентацию CS 344 Introduction to Artificial Intelligence Pushpak

03ae21145068b605b204a78e0a3de37f.ppt

  • Количество слайдов: 27

CS 344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept. , IIT Bombay CS 344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept. , IIT Bombay Lecture 16 - Prolog

An example Prolog Program An example Prolog Program

Shows path with mode of conveyeance from city C 1 to city C 2 Shows path with mode of conveyeance from city C 1 to city C 2 n n n n : -use_module(library(lists)). by. Car(auckland, hamilton). by. Car(hamilton, raglan). by. Car(valmont, saarbruecken). by. Car(valmont, metz). by. Train(metz, frankfurt). by. Train(saarbruecken, frankfurt ). by. Train(metz, paris). by. Train(saarbruecken, paris). by. Plane(frankfurt, bangkok). by. Plane(frankfurt, singapore). by. Plane(paris, los. Angeles). by. Plane(bangkok, auckland). by. Plane(los. Angeles, auckland). n go(C 1, C 2) : - travel(C 1, C 2, L), show_path(L). n travel(C 1, C 2, L) : direct_path(C 1, C 2, L). n n n travel(C 1, C 2, L) : direct_path(C 1, C 3, L 1), travel(C 3, C 2, L 2), append(L 1, L 2, L). direct_path(C 1, C 2, [C 1, C 2, ' by car']): - by. Car(C 1, C 2). direct_path(C 1, C 2, [C 1, C 2, ' by train']): - by. Train(C 1, C 2). direct_path(C 1, C 2, [C 1, C 2, ' by plane']): - by. Plane(C 1, C 2). show_path([C 1, C 2, M|T]) : write(C 1), write(' to '), write(C 2), write(M), nl, show_p ath(T).

Questions based on facts n Answered by matching Two facts match if their predicates Questions based on facts n Answered by matching Two facts match if their predicates are same n (spelt the same way) and the arguments each are same. n n If matched, prolog answers yes, else no. No does not mean falsity.

Prolog does theorem proving n n n When a question is asked, prolog tries Prolog does theorem proving n n n When a question is asked, prolog tries to match transitively. When no match is found, answer is no. This means not provable from the given facts.

Variables n Always begin with a capital letter n n n ? - likes Variables n Always begin with a capital letter n n n ? - likes (john, X). ? - likes (john, Something). But not n ? - likes (john, something)

Example of usage of variable Facts: likes(john, flowers). likes(john, mary). likes(paul, mary). Question: ? Example of usage of variable Facts: likes(john, flowers). likes(john, mary). likes(paul, mary). Question: ? - likes(john, X) Answer: X=flowers and wait ; mary ; no

Conjunctions n n Use ‘, ’ and pronounce it as and. Example n Facts: Conjunctions n n Use ‘, ’ and pronounce it as and. Example n Facts: n n n likes(mary, food). likes(mary, tea). likes(john, mary) ? n n likes(mary, X), likes(john, X). Meaning is anything liked by Mary also liked by John?

Backtracking (an inherent property of prolog programming) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, Backtracking (an inherent property of prolog programming) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. First goal succeeds. X=food 2. Satisfy likes(john, food)

Backtracking (continued) Returning to a marked place and trying to resatisfy is called Backtracking Backtracking (continued) Returning to a marked place and trying to resatisfy is called Backtracking likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. Second goal fails 2. Return to marked place and try to resatisfy the first goal

Backtracking (continued) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. First Backtracking (continued) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. First goal succeeds again, X=tea 2. Attempt to satisfy the likes(john, tea)

Backtracking (continued) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. Second Backtracking (continued) likes(mary, X), likes(john, X) likes(mary, food) likes(mary, tea) likes(john, mary) 1. Second goal also suceeds 2. Prolog notifies success and waits for a reply

Rules n Statements about objects and their n Expess relationships n If-then conditions n Rules n Statements about objects and their n Expess relationships n If-then conditions n n n Generalizations n n n I use an umbrella if there is a rain use(i, umbrella) : - occur(rain). All men are mortal(X) : - man(X). Definitions n n An animal is a bird if it has feathers bird(X) : - animal(X), has_feather(X).

Syntax n n n <head> : - <body> Read ‘: -’ as ‘if’. E. Syntax n n n : - Read ‘: -’ as ‘if’. E. G. n n likes(john, X) : - likes(X, cricket). “John likes X if X likes cricket”. i. e. , “John likes anyone who likes cricket”. Rules always end with ‘. ’.

Another Example sister_of (X, Y): - female (X), parents (X, M, F), parents (Y, Another Example sister_of (X, Y): - female (X), parents (X, M, F), parents (Y, M, F). X is a sister of Y is X is a female and X and Y have same parents

Question Answering in presence of rules n Facts n n n male (ram). male Question Answering in presence of rules n Facts n n n male (ram). male (shyam). female (sita). female (gita). parents (shyam, gita, ram). parents (sita, gita, ram).

Question Answering: Y/N type: is sita the sister of shyam? ? - sister_of (sita, Question Answering: Y/N type: is sita the sister of shyam? ? - sister_of (sita, shyam) female(sita) parents(sita, M, F) parents(shyam, gita, ram) parents(sita, gita, ram) success

Question Answering: wh-type: whose sister is sita? ? - sister_of (sita, X) female(sita) parents(sita, Question Answering: wh-type: whose sister is sita? ? - sister_of (sita, X) female(sita) parents(sita, M, F) parents(Y, gita, ram) parents(sita, gita, ram) parents(shyam, gita, ram) Success Y=shyam

Exercise 1. From the above it is possible for somebody to be her own Exercise 1. From the above it is possible for somebody to be her own sister. How can this be prevented?

Prolog Program Flow, Back. Tracking and Cut Controlling the program flow Prolog Program Flow, Back. Tracking and Cut Controlling the program flow

Prolog’s computation n Depth First Search n n n Pursues a goal till the Prolog’s computation n Depth First Search n n n Pursues a goal till the end Conditional AND; falsity of any goal prevents satisfaction of further clauses. Conditional OR; satisfaction of any goal prevents further clauses being evaluated.

Control flow (top level) Given g: - a, b, c. (1) g: - d, Control flow (top level) Given g: - a, b, c. (1) g: - d, e, f; g. (2) If prolog cannot satisfy (1), control will automatically fall through to (2).

Control Flow within a rule Taking (1), g: - a, b, c. If a Control Flow within a rule Taking (1), g: - a, b, c. If a succeeds, prolog will try to satisfy b, succeding which c will be tried. For ANDed clauses, control flows forward till the ‘. ’, iff the current clause is true. For ORed clauses, control flows forward till the ‘. ’, iff the current clause evaluates to false.

What happens on failure n REDO the immediately preceding goal. What happens on failure n REDO the immediately preceding goal.

Fundamental Principle of prolog programming n Always place the more general rule AFTER a Fundamental Principle of prolog programming n Always place the more general rule AFTER a specific rule.

CUT n Cut tells the system that IF YOU HAVE COME THIS FAR DO CUT n Cut tells the system that IF YOU HAVE COME THIS FAR DO NOT BACKTRACK EVEN IF YOU FAIL SUBSEQUENTLY. ‘CUT’ WRITTEN AS ‘!’ ALWAYS SUCCEEDS.

Fail n n n This predicate always fails. Cut and Fail combination is used Fail n n n This predicate always fails. Cut and Fail combination is used to produce negation. Since the LHS of the neck cannot contain any operator, A ~B is implemented as B : - A, !, Fail.