8 AI
8 AI
(i) Write a prolog program to find the rules for parent, child, male, female, son, daughter, brother,
sister, uncle, aunt, ancestor given the facts about father and wife only.
(ii) Write a program to find the length of a given list
(iii) Write a program to find the last element of a given list
(iv) Write a program to delete the first occurrence and also all occurrences of a particular element in a
given list.
(v) Write a program to find union and intersection of two given sets represented as lists.
(vi) Write a program to read a list at a time and write a list at a time using the well defined read & write
functions.
(vii) Write a program given the knowledge base,
If x is on the top of y, y supports x.
If x is above y and they are touching each other, x is on top of y.
A cup is above a book. The cup is touching that book. Convert the following into wff’s, clausal
form; Is it possible to deduce that `The book supports the cup’.
(viii) Write a program given the knowledge base,
If Town x is connected to Town y by highway z and bikes are allowed on z, you can get to y from
x by bike.
If Town x is connected to y by z then y is also connected to x by z.
If you can get to town q from p and also to town r from town q, you can get to town r from town p.
Town A is connected to Town B by Road 1. Town B is connected to Town C by Road 2.
Town A is connected to Town C by Road 3. Town D is connected to Town E by Road 4.
Town D is connected to Town B by Road 5. Bikes are allowed on roads 3, 4, 5.
Bikes are only either allowed on Road 1 or on Road 2 every day.Convert the following into wff’s,
clausal form and deduce that `One can get to town B from townD’.
(ix) Solve the classical Water Jug problem of AI.
(x) Solve the classical Monkey Banana problem of AI.
(xi) Solve the classical Crypt arithmetic problems such as DONALD + GERALD = ROBERT of AI.
(xii) Solve the classical Missionary Cannibals problem of AI.
(xiii) Solve the classical Travelling Salesman Problem of AI.
(xiv) Solve the classical Blocks World Problemof AI.
(xv) Write a program to search any goal given an input graph using AO* algorithm.
Introduction to prolog
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Page 2
(i) Prolog is a general purpose logic programming language associated with artificial
intelligence and computational linguistics.
(ii) Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog
is declarative: The program logic is expressed in terms of relations, represented as facts and rules. A
computation is initiated by running a query over these relations.[4]
(iii) The language was first conceived by a group around Alain Colmerauer in Marseille, France, in the early 1970s
and the first Prolog system was developed in 1972 by Colmerauer with Philippe Roussel. [5][6]
[7]
(iv) Prolog was one of the first logic programming languages, and remains among the most popular such
languages today, with many free and commercial implementations available. While initially aimed
at natural language processing, the language has since then stretched far into other areas
[8] [9]
like theorem proving, expert systems, games, automated answering systems, ontologies and
sophisticated control systems. Modern Prolog environments support creating graphical user
interfaces, as well as administrative and networked applications.
Data types
Prolog's single data type is the term. Terms are either atoms, numbers, variables or compound terms.
Prolog programs describe relations, defined by means of clauses. Pure Prolog is restricted to Horn clauses. There are
two types of clauses: Facts and rules. A rule is of the form
Head :- Body.
and is read as "Head is true if Body is true". A rule's body consists of calls to predicates, which are called the
rule's goals. The built-in predicate ,/2 (meaning a 2-arity operator with name ,) denotesconjunction of goals,
and ;/2 denotes disjunction. Conjunctions and disjunctions can only appear in the body, not in the head of a rule.
Clauses with empty bodies are called facts. An example of a fact is:
cat(tom).
cat(tom) :- true.
is tom a cat?
?- cat(tom).
Yes
?- cat(X).
X = tom
animal(X):- cat(X).
Due to the relational nature of many built-in predicates, they can typically be used in several directions. For
example, length/2 can be used to determine the length of a list (length(List, L), given a list List) as well as
to generate a list skeleton of a given length (length(X, 5)), and also to generate both list skeletons and their
lengths together (length(X, L)). Similarly, append/3can be used both to append two lists (append(ListA,
ListB, X) given lists ListA and ListB) as well as to split a given list into parts (append(X, Y, List), given a
list List). For this reason, a comparatively small set of library predicates suffices for many Prolog programs.
Lists
Lists are powerful data structures for holding and manipulating groups of things.
In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types,
including structures and other lists. Syntactically, a list is denoted by square brackets with the
terms separated by commas. For example, a list of things in the kitchen is represented as
Rather than having separate location predicates for each thing, we can have one location
predicate per container, with a list of things in the container.
Production rule:
?- length([1,2,3,4,5,6,7], A).
A= 7
?- length([a,b,c,d,e,f,g,h,i], B).
B= 9
?- length([“class”, “ram”, “school”],C).
C= 3
AIM: Write a program to find the position of an element into a given list.
Clauses:
position(P, [P|T], 0)
position(P, [H|T], N):-position(P, T, M),
N = M + 1.
Queries:
?- position(3, [1, 2, 3, 4, 5, 6],N)
N=3
?- position(8, [1, 2, 3, 4, 5],X)
No
?- position(4, [8, 6, 4],Y)
Y=3
AIM: Write a program to delete the first occurrence and also all Occurrences of a particular element
in a given list.
Solution
Clauses:
delete(X,[],[]).
delete(X,[X|L],M):-delete(X,L,M).
delete(X,[Y|L],[Y|M]):-not(X=Y),delete(X,L,M).
Queries:
?delete(a,[a,b,a,c],M).
M=[b,c];
M=[b,a,c];
M=[a,b,a,c];
No
AIM: Write a program to find union and intersection of two given sets represented as lists.
Solution
Clauses:
member(X,[_|R) :- member(X,R).
member(X,[X|_]).
union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W).
union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W).
union([],Z,Z).
intersection([X|Y],M,[X|Z]) :- member(X,M), intersection(Y,M,Z).
intersection([X|Y],M,Z) :- \+ member(X,M), intersection(Y,M,Z).
intersection([],M,[]).
Queries:
?- union([1,2,3,4],[1,a,b,4],A).
A = [2,3,1,a,b,4]
?- intersection([1,2,3,4],[1,a,b,4],B).
B = [1,4]
AIM: Write a program to read a list at a time and write a list at a time using the well defined read &
write functions.
Term I/O
read(Term)
Reads next full-stop (period)
delimited term from the current input stream, if eof then returns the
atom 'end_of_file'.
write(Term)
Writes a term to the current output stream.
print(Term)
Writes a term to the current output stream. Uses a user defined predicate portray/1 to write the
term, otherwise uses write.
writeq(Term)
Writes a term to the current output stream in a form aceptable as input to read.
Clauses:
writelist([]).
writelist([H|T]):-
write(H),
writelist(T).
readlist([]).
readlist([H|T]):-
read(H),
readlist(T).
Query:
?- readlist([‘The’,’ ‘man’,’ was called ‘,’Bob’,’.’]),writelist([]).
The man was called Bob.
Yes
"You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring markers on it.
There is a tap that can be used to fill the jugs with water. How can you get exactly 2 gallons of water
into the 4-gallon jug?"
Production Rules:-
R1: (x,y) --> (4,y) if x < 4
R2: (x,y) --> (x,3) if y < 3
R3: (x,y) --> (x-d,y) if x > 0
R4: (x,y) --> (x,y-d) if y > 0
R5: (x,y) --> (0,y) if x > 0
R6: (x,y) --> (x,0) if y > 0
R7: (x,y) --> (4,y-(4-x)) if x+y >= 4 and y > 0
R8: (x,y) --> (x-(3-y),y) if x+y >= 3 and x > 0
R9: (x,y) --> (x+y,0) if x+y =< 4 and y > 0
R10: (x,y) --> (0,x+y) if x+y =< 3 and x > 0
Solution:-
Clauses
water_jugs :-
SmallCapacity = 3,
LargeCapacity = 4,
Reservoir is SmallCapacity + LargeCapacity + 1,
volume( small, Capacities, SmallCapacity ),
volume( large, Capacities, LargeCapacity ),
volume( reservoir, Capacities, Reservoir ),
volume( small, Start, 0 ),
volume( large, Start, 0 ),
volume( reservoir, Start, Reservoir ),
volume( large, End, 2 ),
water_jugs_solution( Start, Capacities, End, Solution ),
phrase( narrative(Solution, Capacities, End), Chars ),
put_chars( Chars ).
water_jugs_solution( Start, Capacities, End, Solution ) :-
solve_jugs( [start(Start)], Capacities, [], End, Solution ).
Imagine a room containing a monkey, chair and some bananas. That has been hanged from the
center of ceiling. If the monkey is clever enough he can reach the bananas by placing the chair
directly below the
bananas and climb on the chair .The problem is to prove the monkey can reach the bananas.
Production Rules
can_reach�clever,close.
get_on:� can_climb.
under�in room,in_room, in_room,can_climb.
Close�get_on,under| tall
Solution:-
Clauses:
in_room(bananas).
in_room(chair).
in_room(monkey).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, bananas).
can_reach(X, Y):-clever(X),close(X, Y).
get_on(X,Y):- can_climb(X,Y).
under(Y,Z):-in_room(X),in_room(Y),
in_room(Z),can_climb(X,Y,Z).
close(X,Z):-get_on(X,Y), under(Y,Z);
tall(Y).
Queries:
?- can_reach(A, B).
A = monkey.
A
C
B
Solution
Clauses
on(X,Y)
on(a,b)
on_table(b)
on_table(c)
on_table(X)
clear(X)
clear(a)
clear(c)
holding(X)
arm(empty)
unstuck(X,Y):-arm(empty),clear(X),on(X,Y)
stack(X,Y):- holding(X),clear(Y)
pickup(X):-arm(empty),clear(X),on(X)
putdown(X):-holding(X)
Queries:
?- unstuck(a,b)
yes
?- unstuck(b,a)
No
AIM: Write a program to search any goal given an input graph using AO* algorithm.
Lets take an example of decorating a cake to understand the searching in a given graph.
Clauses:
rule([[have,smarties]])
rule([[have,eggs]])
rule([[have, flour]])
rule([[have, money]])
rule([[have, car]])
rule([[have, kitchen]])
rule([[decorate, cakes],[have,cake],[have,icing]])
rule([[decorate, cakes],[have,cake],[have,smarties]])
rule([[have, money],[in,bank]])
rule([[have,cake], [have,money],[in,store[])
rule([[have,cake],[in,kitchen],[have,phone]])
rule([[in,store],[have,car]])
rule([[in,bank],[have,car]])
ao_search([])
ao_search(Goal)
ao_search_tree([Goal|Goals],[[Goal|Tree]|Tree]):- rule([Goal|Subgoal]),
ao_search_tree(Subgoals,Tree),
ao_search_tree(Goals,Trees)
Query:
?- ao_search_tree([[decorate,cake]],[T]), showtree(T).
T = [[decorate,cake],
[[have,cake], [[have,money]],
[[in,store],[[have, car]]]],[[have, smarties]]]
Yes