AI Practical Solution
AI Practical Solution
AI Practical Solution
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.
Enroll. No.-110440702007
1
Artificial Intelligence
ME CE-SEM-II
Practical 2 Aim :- 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" Enroll. No.-110440702007
2
Artificial Intelligence
ME CE-SEM-II
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, 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.
Enroll. No.-110440702007
3
Artificial Intelligence
ME CE-SEM-II
Rule Syntax 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 1, a Prolog rule has two parts: the head and the body. This is the generic syntax for a rule: HEAD :- <Subgoal>, <Subgoal>, ..., <Subgoal>. 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.
Enroll. No.-110440702007
4
Artificial Intelligence
ME CE-SEM-II
Practical 3 Aim :- 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)
Enroll. No.-110440702007
5
Artificial Intelligence
ME CE-SEM-II
Practical 4 Aim :- 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).
Enroll. No.-110440702007
6
Artificial Intelligence
ME CE-SEM-II
Practical 5 Aim :- 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).
Enroll. No.-110440702007
7
Artificial Intelligence
ME CE-SEM-II
Practical 6 Aim :- 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.
Enroll. No.-110440702007
8
Artificial Intelligence
ME CE-SEM-II
Practical 7 Aim :- 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 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").*/ X:"),readint(X),write("Enter
Goal : Readvalues.
Enroll. No.-110440702007
9
Artificial Intelligence
ME CE-SEM-II
n3 1 8.
m3
Goal : start.
Artificial Intelligence
ME CE-SEM-II
Aim :-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).
Artificial Intelligence
ME CE-SEM-II
Aim :-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|_]):-X<>H,!,del(X,T,_). del(_,[_|T],L):-insert(T,L). insert(L,L). Goal : del(11,[11,15,30,40,60,70,88],List).
Artificial Intelligence
ME CE-SEM-II
Aim :- 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).
Artificial Intelligence
ME CE-SEM-II
Aim :- Merge two given ordered list in such a way that the resulting List is ordered in ascending.
Goal : merge([2,3,15],[11,12,13],List).
Artificial Intelligence
ME CE-SEM-II
Aim :- 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):-Y<=X,!,minimum(Ys,Y,Z). minimum([_|Ys],X,Z):-minimum(Ys,X,Z). minimum([],X,X). efface([Y|Xs],Y,Xs):-!. efface([X|Xs],Y,[X|Zs]):-efface(Xs,Y,Zs).
Goal : seln_sort([1,3,45,5,14,3]X).
Artificial Intelligence
ME CE-SEM-II
Aim :-Write a prolog program to solve tower of Hanoi problem. domains loc=right;middle;left predicates hanoi(integer) move(integer,loc,loc,loc) inform(loc,loc) clauses hanoi(N):-move(N,left,middle,right). move(1,A,_,C):-inform(A,C),!. move(N,A,B,C):-N1=N-1,move(N1,A,C,B),inform(A,C),move(N,B,A,C). inform(Loc1,Loc2):-write("\n Move a disk from",Loc1,"to",Loc2).
Goal : Hanoi(4).
Artificial Intelligence
ME CE-SEM-II
Aim :- Write a prolog program to solve 8-queen problem. domains n=integer L=qun(n,n) LL=L* Lst=integer* predicates queen(LL) noattack(L,LL) member(integer,Lst) clauses queen([]). queen([qun(X,Y)|Rest]):queen(Rest),member(Y, [1,2,3,4,5,6,7,8]),noattack(qun(X,Y),Rest). noattack(qun(_,_),[]). noattack(qun(X,Y),[qun(X1,Y1)|R]):-Y<>Y1,Y1-Y<>X-X1,Y1-Y<>X1X,noattack(qun(X,Y),R). member(Y,[Y|_]). member(Y,[_|T]):-member(Y,T).
Goal : queen([qun(1,Y1),qun(2,Y2),qyn(3,Y3),qun(4,Y4),qun(5,Y5),qun(6,Y6),qun(7,Y7),qun(8,Y8)])
Artificial Intelligence
ME CE-SEM-II
Aim :- Write a C program to solve 8-puzzle problem. #include<iostream.h> #include<conio.h> #include<malloc.h> #include<stdlib.h> void swap(int *,int *); int no_node=1; class link { public: int node[3][3]; int eval; link *next; link *start; link(int a[10]); link(int a[3][3]); link *insert_at(link *,int node[3][3]); void evaluate(int b[3][3],int g[3][3]); link* generate_list(link *, int node[3][3]); }; link::link(int a[3][3]) { eval=0; int i,j; for(i=0;i<3;i++) {
Enroll. No.-110440702007
18
Artificial Intelligence for(j=0;j<3;j++) { node[i][j]=a[i][j]; } } void link::evaluate(int b[3][3],int g[3][3]) { int i,j,k,m; for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) {
ME CE-SEM-II
for(m=0;m<3;m++) { if(b[i][j]==g[k][m] && b[i][j]!=0) { eval+=abs(k-i)+abs(m-j); m=3; k=3; } } } } link* link::generate_list(link *q,int c[3][3]) { int t1[3][3],t2[3][3],t3[3][3],t4[3][3]; Enroll. No.-110440702007
19
Artificial Intelligence int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { t1[i][j]=c[i][j]; t2[i][j]=c[i][j]; t3[i][j]=c[i][j]; t4[i][j]=c[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(c[i][j]==0) {
ME CE-SEM-II
if((i+1)<3 && (i+1)>=0) { swap(&t1[i][j],&t1[i+1][j]); start=start->insert_at(q,t1); } if((j+1)<3 && (j+1)>=0) { swap(&t2[i][j],&t2[i][j+1]); start=start->insert_at(q,t2); } Enroll. No.-110440702007
20
Artificial Intelligence
ME CE-SEM-II
if((i-1)<3 && (i-1)>=0) { swap(&t3[i][j],&t3[i-1][j]); start=start->insert_at(q,t3); } if((j-1)<3 && (j-1)>=0) { swap(&t4[i][j],&t4[i][j-1]); start=start->insert_at(q,t4); } break; } } return start; } link * link::insert_at(link *p,int nod[3][3]) { link *temp=start; start=temp; int i,j; int c1=0; if(p!=NULL) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { Enroll. No.-110440702007
21
Artificial Intelligence
ME CE-SEM-II
if(p->node[i][j]==nod[i][j]) c1++; } } cout<<endl<<"c1 = "<<c1<<endl; if(c1==7) { return start; } else { while(temp->next!=NULL) { temp=temp->next; } link *temp1=new link(nod); no_node++; temp->next=temp1; temp->next->next=NULL; } return start; } void swap(int *a,int *b) { int temp; temp=*a; *a=*b; Enroll. No.-110440702007
22
ME CE-SEM-II
int ini[3][3]={{2,8,3},{1,6,4},{7,0,5}}; link l1(ini); int gal[3][3]={{1,2,3},{8,0,4},{7,6,5}}; int i=0,j=0; clrscr(); link goal(gal); link *ll=&l1; ll->next=NULL; link *start_open=ll; link *start_close=NULL; link *gl=&goal; gl->next=NULL; ll->evaluate(ll->node,gl->node); cout<<"evaluation = "<<ll->eval; int m_eval=ll->eval; start_open->start=ll; start_open->start=start_open->generate_list(NULL,ll->node); link *tl; tl=start_open->start; link *p,*q; q=tl; tl=tl->next; while(tl!=NULL) Enroll. No.-110440702007
23
Artificial Intelligence {
ME CE-SEM-II
tl->evaluate(tl->node,gl->node); cout<<"\neval = "<<tl->eval<<" "<<endl; p=tl; if(tl->eval<=m_eval && tl->eval!=0) { m_eval=tl->eval; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<tl->node[i][j]<<" "; } cout<<endl; } start_open->start=start_open->generate_list(q,tl->node); } else if(tl->eval==0) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<tl->node[i][j]<<" "; } cout<<endl; } Enroll. No.-110440702007
24
ME CE-SEM-II
Enroll. No.-110440702007
25