Upload
sarah-wood
View
64
Download
7
Tags:
Embed Size (px)
DESCRIPTION
565656556
Citation preview
Artificial Intelligence
ME CE-SEM-II
Practical 1
Aim :- Introduction to Prolog.
The first, official version of Prolog was developed at the University of Marseilles, France by Alain Colmerauer in the early 1970s as a tool for Programming in Logic. The demand for more "user friendly" and intelligent programs is another reason for Prolog's growing popularity.
Prolog in general has moved out of the artificial intelligence labs, and PDC's Visual Prolog is a commercially competitive, general-purpose development environment. Visual Prolog has increasingly become the tool of choice for many developers, because of the intelligent features that can be so easily added programs or even web sites. . Not only that, Prolog encourages the programmer to start with a well-structured description of the problem, so that, with practice, Prolog can also be used as both a specification tool, and the implementation vehicle for the specified product.
Here's a short look at how Prolog differs from traditional programming languages.
Prolog is descriptive.
Instead of a series of steps specifying how the computer must work to solve a problem, a Prolog program consists of a description of the problem.
Conceptually, this description is made up of two components:
1.descriptions of the objects involved in the problem
2.facts and rules describing the relations between these objects
The rules in a Prolog program specify relations between the given input data and the output which should be generated from that input.
Practical 2Aim :- Introduction to Facts & Rules.Facts & Rules
Given a relation parent(X, Y) which represents relation between parent X and his child Y. Write a prolog program for the given relation. Store at least six relations and run following queries:
- list all parent child relations.
-display name of Ys parent.
-display name of Xs child.
Prolog permit you to descries facts as symbolic relationship. For example
Sun rises in the east. This same fact can be express in prolog as,
Rises (Sun,East).
This factual expression in prolog is called clauses.
father("John","Mary")
"John is the father to Mary"
father("John","Sally")
"John is the father to Sally"
father("John","Sam")
"John is the father to Sam"
mother("Jeanette","Mary")
"Jeanette is the mother to Mary""
mother("Mary","Tom")
"Mary is the mother to Tom"
or in the form of rules, such as
sister(X,Y) :- father(Z,X), father(Z,Y).
"X and Y are sisters if they have the same father"
X, Y, Z are here variables, which are used to specify bindings between the different relations. Variables can be any name starting with an uppercase letter.
Facts: What Is Known
In Prolog, a relation between objects is called a predicate. In natural language, a relation is symbolized by a sentence. In the predicate logic that Prolog uses, a relation is summarized in a simple phrase--a fact--that consists of the relation name followed by the object or objects (enclosed in parentheses). As with a sentence, the fact ends with a period (.).
Here are some more facts expressing "likes" relations in natural language:
Bill likes Cindy.
Cindy likes Bill.
Bill likes dogs.
Here are the same facts, written in Prolog syntax:
likes(bill, cindy).
likes(cindy, bill).
likes(bill, dogs).
A fact represents one single instance of either a property of an object or a relation between objects. A fact is self-standing; Prolog doesn't need to look any further for confirmation of the fact, and the fact can be used as a basis for inferences.
Rules: What You Can Infer from Given Facts
Rules enable you to infer facts from other facts. Another way to say this is that a rule,XE "rules" as conclusions is a conclusion that is known to be true if one or more other conclusions or facts are found to be true. Here are some rules concerning a "likes" relation:
Cindy likes everything that Bill likes.
Caitlin likes everything that is green.
Given these rules, you can infer from the previous facts some of the things that Cindy and Caitlin like:
Cindy likes Cindy.
Caitlin likes Kermit.
A rule is a property or relation known to be true when some set of other relations is known. Syntactically, these relations are separated by commas.
Rule Syntax
XE "rules: syntax"
XE "syntax: rules"Rules are used in Prolog when a fact depends upon the success (truth) of another fact or group of facts. As we explained in Chapter Error! Bookmark not defined., a Prolog rule has two parts: the head and the body. This is the generic syntax for a Visual Prolog rule:
HEAD :- , , ..., .
The body of the rule consists of one or more subgoals. Subgoals are separated by commas, specifying conjunction, and the last subgoal in a rule is terminated by a period.
Each subgoal is a call to another Prolog predicate, which may succeed or fail. In effect, calling another predicate amounts to evaluating its subgoals, and, depending on their success or failure, the call will succeed or fail. If the current subgoal can be satisfied (proven true), the call returns, and processing continues on to the next subgoal. Once the final subgoal in a rule succeeds, the call returns successfully; if any of the subgoals fail, the rule immediately fails.
Practical 3Aim :- Given a relation parent(X, Y) which represents relation between parent X and his child Y. Write a prolog program for the given relation. Store at least six relations and run following queries:
- list all parent child relations.
- Display name of Ys parent.
- Display name of Xs child.domains
X,Y,A,B=symbol
predicates
parent(X,Y)
likes(A,B)
clauses
parent(a,aa).
parent(b,bb).
parent(c,cc).
parent(d,dd).
likes(abc,def).
likes(ghi,jkl).
likes(abc,zzz):-likes(ghi,yyy).
Goal : parent(Who,Whom)Practical 4Aim :- Write a program to implement Monkey & Banana (M&B) Problem. M&B Problem: A Monkey is standing at the door of a Room. Banana is hanging in the middle of the Room. Monkey wants to eat the banana but banana is not reachable by the monkey. To get the banana monkey has to use a table lying in a corner of the Room. Monkey need to push the table in the middle of the Room. Then it has to get on the table and get the Banana.
domains
monkey,banana,table = symbol.
predicates
caneat(symbol,symbol)
position(symbol,symbol)
move(symbol,symbol)
clauses
position(table,corner):-
write("Table at corner"),nl.
position(monkey,table):-
move(table,center),
write("Push table to center"),nl.
position(table,center).
move(table,center):-
write("move monkey near table"),nl,
move(monkey,table),
write("Push"),nl,
position(table,center).
move(monkey,table).
caneat(monkey,banana):-
write("No. Not rechable"),nl,
fail.
caneat(monkey,banana):-
position(monkey,table),
position(table,center),
write("Yes. Bananas rechable.").
Goal :
Caneat(monkey,banana).Practical 5Aim :- Write a prolog program to check whether a person X can eat Food Y or not. A person X can eat food Y if X likes Y, X is available and X is eatable.
predicates
food(symbol)
test(symbol,symbol)
likes(symbol,symbol)
clauses
likes(mahesh,Food):-food(Food),test(Food,good).
food(pizza).
food(paubhaji).
test(pizza,good).
test(paubhaji,good).Goal : likes(Mahesh,what).Practical 6Aim :- Write a program to represent knowledge of 10 employees of a company, employee(EmpId, name, B_Date, Address). Where name of employee and B_Date are compound objects. Display list of Employees whose birthday is in current month.(Use Compound Obj and fail)
Domains
date=date(integer,integer,integer)
predicates
emp(symbol,date,symbol)
getlist(symbol,date,symbol)
loop1
clauses
getlist(Name,date(D1,M1,Y1),City):- emp(Name,date(D1,M1,Y1),City),date(_,M2,_),M1=M2.
emp(pranav,date(12,12,1982),ahmedabad).
emp(manish,date(12,6,1982),rajkot).
emp(paresh,date(12,7,1982),gandhinagar).
emp(deven,date(2,9,1982),anand).
emp(sanjay,date(12,7,1982),vvnagar).
emp(sunil,date(11,9,1982),ahmedabad).
emp(priyank,date(10,9,1982),ahmedabad).
emp(sandip,date(10,9,1982),ahmedabad).
emp(sohil,date(10,9,1982),ahmedabad).
emp(pratik,date(11,9,1982),ahmedabad).
loop1:-
getlist(Name,date(D,M,Y),City),writef("\n%-20%2/%2/%2%-30",Name,D,M,Y,City),fail.
loop1.
Goal :
Loop1.Practical 7Aim :- Write a program to perform different operations like add, mul, sub, div (Implementation of switch statement using cut).
predicates
op(integer,integer,integer,integer)
readvalues
clauses
readvalues:-
write("Enter Choice "),readint(CH),write("Enter X:"),readint(X),write("Enter Y:"),readint(Y),op(CH,X,Y,Ans),write("Answer is : ",Ans,"\n").
op(1,X,Y,Ans):-!,Ans=X+Y.
op(2,X,Y,Ans):-!,Ans=X-Y.
op(3,X,Y,Ans):-!,Ans=X*Y.
op(4,X,Y,Ans):-!,Ans=X/Y.
/*op(_,X,Y,Ans):-!,write("Invalid Choice").*/
Goal :
Readvalues.Practical 8Aim :- Display following table
n n31 12 8.
..
m m3
predicates
loop(integer)
start
clauses
loop(0):-!.
loop(N):-
CB=N*N*N,write(N),write("\t\t",CB,"\n"),NN=N-1,loop(NN).
start:-
write("Enter N :"),readint(M),loop(M).
Goal : start.Practical 9Aim :-Write a program to insert given item in a List in ascending order.
Domains
Lst=integer*
predicates
ins_order(integer,Lst,Lst)
insert(Lst,Lst)
clauses
ins_order(X,[],[X]):-!.
ins_order(X,[H|T],[H|T1]):-X>H,!,ins_order(X,T,T1).
ins_order(X,[H|T],L):-insert([X,H|T],L).
insert(L,L).
Goal : ins_order(15,[11,20,30,40,60,70,88],List).Practical 10Aim :-Write a program to (a) insert at start and (b)delete given item from a List.
(a).domains
Lst=integer*
predicates
insert_first(integer,Lst,Lst)
clauses
insert_first(Value,[H|T],[Value,H|T]).
Goal : insert_first(12,[1,2,3,4,6,7,8],List).
(b).domains
Lst=integer*
predicates
del(integer,Lst,Lst)
insert(Lst,Lst)
clauses
del(_,[],[]):-write("Element not found\n"),!.
del(X,[H|T],[H|_]):-XH,!,del(X,T,_).
del(_,[_|T],L):-insert(T,L).
insert(L,L).
Goal : del(11,[11,15,30,40,60,70,88],List).
Practical 11Aim :- Write a program to discard all negative numbers in a List.
domains
Lst=integer*
predicates
dis_ng(Lst,Lst)
clauses
dis_ng([],[]).
dis_ng([H|T],[H|T1]):-H>0,!,dis_ng(T,T1).
dis_ng([_|T],L):-dis_ng(T,L).
Goal : dis_ng([11,-15,30,-40,60,70,88],List).Practical 12Aim :- Merge two given ordered list in such a way that the resulting List is ordered in ascending.
domains
Lst=integer*
List=integer*
predicates
merge(Lst,Lst,Lst)
clauses
merge([],L2,L2):-!.
merge([H|T],L2,[H|T1]):-merge(T,L2,T1),!.
Goal : merge([2,3,15],[11,12,13],List).Practical 13Aim :- Write a prolog program to sort given list in ascending order.
domains
list=integer*
predicates
seln_sort(list,list)
minimum(list,integer,integer)
efface(list,integer,list)
clauses
seln_sort([X|Xs],[Y|Ys]):-minimum(Xs,X,Y),efface([X|Xs],Y,Zs),seln_sort(Zs,Ys).
seln_sort([],[]).
minimum([Y|Ys],X,Z):-Ynode,gl->node);
cout