CSE4108 Artificial Intelligence Lab Spring 2015 Session 1 Topic 1. Logic Programming and Backward Chaining. A simple knowledgebase (KB) from the Kinship Domain Object relationships: Hasib is a parent of Rakib. Rakib is a parent of Sohel. Rakib is a parent of Rebeka. If X is a parent of Y and Y is a parent of Z, then X is a grandparent of Z. Facts and Rules (KB) in ProLog: parent (‘Hasib’ , ’Rakib’). parent(‘Rakib’ , ’Sohel’). parent(‘Rakib’ , ’Rebeka’). grandparent (X, Z) :- parent(X, Y), parent(Y, Z). [2 2-place predicates; 3 variables; full stop (.) as the end marker of a clause/ sentence / statement; :- as ‘if’; comma (,) as logical AND. ] Queries to KB and Backward Chaining: Is Hasib a grandparent of Rebeka? grandparent (‘Hasib’, ‘Rebeka’). - - Is Hasib a parent of a person and that person is a parent of Rebeka? parent (‘Hasib’, X), parent(X, ‘Rebeka’). parent (‘Hasib’, ‘Rakib’). [X ← Rakib] Is Rakib a parent of Rebeka? parent (‘Rakib’, ‘Rebeka’). Yes. √ Various types of queries are possible. [Who is a parent? ‘parent (X, _).’ Who has a parent? ‘parent (_, X).’ Who is a parent and who has a parent? ‘parent (X, _), parent (_, X).’ Who is a parent or who has a parent? ‘parent (X, _); parent (_, X).’] √ Various rules may also be formulated. √ Nesting of the following type should be avoided. greatGrandParent (X, Z) :- parent(X, Y), grandparent(Y, Z). greatGreatGrandParet(X, Z) :- parent(X, Y), greatGrandParent(Y,Z),. Topic 2. Logic Programming and Recursion. i) Ancestor a. A parent is an ancestor. b. A parent of an ancestor is an ancestor. [X is an ancestor of Y, if X is a parent of an ancestor of Y.] ------------------------ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ii) Factorial of 0 is 1. If n is greater than 0, then factorial of n is the product of n and the factorial of n-1. [Factorial of N is F, if N is greater than 0 and F is the product of N and factorial of N-1.] ----------------------factorial(0, 1). factorial(N, F) :- N>0, N1 is N-1, factorial(N1, F1), F is N*F1. Exercise: 1) Enrich the KB discussed in Topic 1 with ‘brother’ and ‘sister’ rules. [Use not (X=Y) to avoid reflexivity and 1-place predicate ‘male’ where necessary.] 2) Define a recursive procedure in English and in ProLog to find the sum of 1st n odd numbers. 3) Define a recursive procedure to find the sum of the 1st n terms of the following series. 100+105+110+… 4) Define a recursive procedure to find the sum of the 1st n terms of the following series. 10x11 + 11x13 +12x15 + … 5) Define a recursive procedure to find the sum of 1st n terms of an equal-interval series given the 1st term and the interval. [Use a 4-place predicate.] 6) Develop a KB that represents a simple directed weighted graph, and provides a way for finding the length of a path between any two nodes. [Use three place predicates ‘neighbor’ and ‘path’.]