AI Lab Manual
AI Lab Manual
Lab Manual
Practical: 1
PROLOG PROGRAM:
male(shankar).
male(ulhas).
male(satish).
male(saurabh).
male(prashant).
female(umabai).
female(mrunal).
female(sadhana).
female(swati).
parent(shankar,umabai,ulhas).
parent(shankar,umabai,satish).
parent(ulhas,mrunal,prashant).
parent(satish,sadhana,saurabh).
parent(satish,sadhana,swati).
brother(ulhas,satish).
brother(satish,ulhas).
brother(prashant,saurabh).
brother(saurabh,prashant).
sister(swati,saurabh).
sister(swati,prashant).
father(X,Y) :- parent(X,Z,Y).
mother(X,Y) :- parent(Z,X,Y).
son(X,Y,Z) :- male(X),father(Y,X),mother(Z,X).
daughter(X,Y,Z) :- female(X),father(Y,X),mother(Z,X).
wife(X,Y) :- female(X),parent(Y,X,Z).
grandfather(X,Y) :- male(X),father(X,Z),father(Z,Y).
uncle(X,Y) :- male(X),brother(X,Z),father(Z,Y).
aunt(X,Y) :- wife(X,Z),uncle(Z,Y).
cousin(X,Y) :- father(Z,X),brother(Z,W),father(W,Y).
ancestor(X,Y,Z) :- parent(X,Y,Z).
ancestor(X,Y,Z) :- parent(X,Y,W),ancestor(W,U,Z).
Output:
Practical: 3
Output:
Practical: 4(a)
Aim: Give an opportunity to user to re-enter the password ‘n’ no. Of Times, on entering
wrong password.
PROLOG PROGRAM:
db(nirali,123).
OUTPUT:
Practical: 4(b)
Aim: Give an opportunity to user to re-enter the password three (03) Times, on
entering wrong password:
db(kelu,123).
db(patel,789).
login:-write('Invalid password'),nl,login.
Output:
Practical: 5
PROLOG PROGRAM:
hobby(kelu,gaming).
hobby(amita,dancing).
hobby(bhavini,playing).
hobby(poonam,painting).
hobby(nikunj,reading).
Output:
Practical: 6
PROLOG PROGRAM:
symtom(john,fever).
symtom(john,rashes).
symtom(john,stomachache).
symtom(john,running_nose).
symtom(john,body_ache).
dis(X,malaria):-symtom(X,fever),symtom(X,headache),symtom(X,running_nose).
dis2(X,jaundice):-symtom(X,fever),symtom(X,stomachache),symtom(X,body_ache).
Output:
Practical: 7
Aim: Write a PROLOG program To check if a given year is a Leap Year or not.
PROLOG PROGRAM:
Output:
Practical: 8
PROLOG PROGRAM:
factorial(0,1).
factorial(N,F):- N>0,N1 is N-1,factorial(N1,F1),F is N*F1.
Output:
Practical: 9
PROLOG PROGRAM:
Output:
Practical: 10
jug(integer, integer).
jug(2, _).
jug(0, 2):-
write('(0, 2)'), nl,
write('(2, 0)'), nl.
jug(4, 0):-
write('(4, 0)'), nl,
jug(0, 0).
jug(4, 3):-
write('(4, 3)'), nl,
jug(0, 0).
jug(3, 0):-
write('(3, 0)'), nl,
jug(3, 3).
jug(X, 0):-
write('('),write(X),write(', 0)'), nl,
jug(0, 3).
jug(0, 3):-
write('(0, 3)'), nl,
jug(3, 0).
jug(0, X):-
write('(0,'),write(X),write(' )'), nl,
jug(0, 0).
jug(3, 3):-
write('(3, 3)'), nl,
jug(4, 2).
jug(4, 2):-
write('(4, 2)'), nl,
write('(2, 0)'), nl,
jug(2, 0).
jug(X, Y):-
X>4,
fail,
Y>3,
fail.
Output:
Practical: 12
read('surat','valsad',100).
read('valsad','pune',150).
read('pune','mumbai',100).
read('chikhli','navsari',50).
find:-write('Source center:'),read(City1),write('Dest center:'),read(City2),route(City1,City2,Dist).
route(City1,City2,Dist):-read(City1,City2,Dist),write('Distance bet. '),write(City1),write(' and '),write(City2),write('
is '),write(Dist),nl,nl.
route(City1,City2,Dist):-read(City1,X,Dist1),route(X,City2,Dist2),Dist=Dist1+Dist2,write('Distance bet.
'),write(City1),write(' and '),write(City2),write(' is '),write(Dist),nl.
Output:
Practical: 13
Output:
Practical: 15
Aim: Write a program to find simple interest with the help of prolog.
go:-write('Enter principal :'),read(P), write('Enter rate: '),read(R),write('Enter year: '),read(N),
I is P*R*N, S is I/100,
write('Simple Intres is='),write(S).
Output:
Practical: 16
Answer:
Answer:
Practical: 18
Program:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1; while((a[k]<=n)&&!
place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}
Output:-
Practical: 19
Program:-
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#define DOWN 0
#define UP 1
#define LEFT 2
#define RIGHT 3
#define H2
struct elementstruct
{
int block[9];
char* str;
int pathcost;
int valid;
int totalcost;
elementstruct* next;
};
int main()
{
int block[9];
clrscr();
printf("\nThe Eight Puzzle!\n");
printf("\nPlease Enter the initial state of the game \n"
" [Represent tiles with numbers 1 to 8, and the blank space as 'x'.\n"
" Start writing them from left to right for each row. Start from the topmost row to the bottommost
row.\n"
" Your final string will look similar to this '1 4 2 3 x 6 7 8 5'.\n"
" Do not forget the spaces in between the characters]\n");
int i = 0;
while(i<9)
{
char chr;
chr = fgetc(stdin);
if (chr==32) continue;
if (chr=='x') block[i] = 8;
else if (chr >= '1' && chr <= '9') block[i] = chr - '1';
else { printf("Invalid Input. Example of valid input...2 1 3 4 7 5 6 8 x.", chr); return 1;
} i+
+;
}
printf("\nWorking...");
top = newelement();
for(i=0; i<9; i++)
top->block[i] = block[i];
top->totalcost = heur(block);
elementstruct* newnode = newelement();
while (1)
{
elementstruct* node = bestnodefromqueue();
if (node == NULL)
{
printf("done!\n");
printf("There is no solution to this of less than %d depth.\n", maxdepth);
printf("Try increasing the depth by 5.\n");
printf("If there is no solution within 35-40 depth, the pattern is usually unsolvable.\n\n");
break;
}
else if (goal(node->block))
{
char chr[15];
printf("done. \nFound the solution of least number of steps (%d).", node->pathcost);
printf("\nWant a graphical display of each step? (Y/N)?");
scanf("%s", chr);
if(chr[0] =='n' || chr[0]=='N') {
printf("\n (Move Blank u=up, d=down, l=left, r=right)\n");
printf(node->str);
printf("\n");
break;
}
int block2[9];
for (i=0; i<node->pathcost; i++)
{
print_block(block);
apply(block2, block, op(node->str[i]));
for(int j=0; j<=8; j++)
block[j] = block2[j];
}
print_block(block);
printf("\nGraphical Display Complete.\nThe steps taken were: (Move blank u=up, d=down, l-left,
r=right)\n");
printf(node->str);
printf("\n");
break;
}
if (node->totalcost > maxdepth) continue;
for(i=0; i<=3; i++)
{
if (apply(newnode->block, node->block, i) == -1)
continue;
if (notonqueue(newnode->block))
{
prepend(newnode, node, i);
newnode = newelement();
if (newnode==NULL) { printf ("ERROR!! insufficient memory!! Try decreasing depth!");
return 1;
}
}
}
}
return 0;
}
while (t != NULL)
{
if (t->valid==1 && t->totalcost < min_totalpathcost)
{
min_totalpathcost = t->totalcost;
to_return = t;
}
t = t->next;
}
if (to_return != NULL) to_return->valid = 0;
return to_return;
}
int apply (int* newstate, int* oldstate, int op)
{
int j;
int blank;
for (j=0; j<9; j++)
if (oldstate[j]==8) { blank=j; break; }
if (blank==notvalid1[op] || blank==notvalid2[op] || blank==notvalid3[op])
return -1;
for (j=0; j<9; j++)
newstate[j] = oldstate[j];
newstate[blank] = newstate[blank+applyparam[op]];
newstate[blank+applyparam[op]] = 8;
return 1;
}
elementstruct* newelement()
{
elementstruct* t = new elementstruct;
if (t==NULL) return NULL;
t->valid = 1;
t->str = new char[maxdepth+1];
if (t->str ==NULL) return NULL;
t->str[0] = 0;
t->pathcost = t->totalcost = 0;
t->next = NULL;
return t;
}
void print_block(int* block)
{
printf("\n");
printf ("\n------");
printf ("\n|%c|%c|%c|", to_char(block[0]), to_char(block[1]), to_char(block[2]));
printf ("\n------");
printf ("\n|%c|%c|%c|", to_char(block[3]), to_char(block[4]), to_char(block[5]));
printf ("\n------");
printf ("\n|%c|%c|%c|", to_char(block[6]), to_char(block[7]), to_char(block[8]));
printf ("\n------");
}
char to_char(int i)
{
if (i>=0 &&i<=7) return i+'1';
else if (i==8) return 'x';
else
{
printf("ERROR in Program!"); return -1;
}
}
int op(char i)
{
switch (i)
{
case 'd': return 0;
case 'u': return 1;
case 'l': return 2;
case 'r': return 3;
default: printf("ERROR!"); return -1;
}
}
Output:-
PRACTICAL 20
Write a PROLOG/C program to implement an Expert System of your choice.
Program:-
domains
disease,indication = symbol
Patient,name = string
predicates
hypothesis(string,disease)
symptom(name,indication)
response(char)
go
clauses
go :-
write("What is the patient's name? "),
readln(Patient),
hypothesis(Patient,Disease),
write(Patient,"probably has ",Disease,"."),nl.
go :-
write("Sorry, I don't seem to be able to"),nl,
write("diagnose the disease."),nl.
symptom(Patient,fever) :-
write("Does ",Patient," have a fever (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,rash) :-
write("Does ",Patient," have a rash (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,headache) :-
write("Does ",Patient," have a headache (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,runny_nose) :-
write("Does ",Patient," have a runny_nose (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,conjunctivitis) :-
write("Does ",Patient," have a conjunctivitis (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,cough) :-
write("Does ",Patient," have a cough (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,body_ache) :-
write("Does ",Patient," have a body_ache (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,chills) :-
write("Does ",Patient," have a chills (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,sore_throat) :-
write("Does ",Patient," have a sore_throat (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,sneezing) :-
write("Does ",Patient," have a sneezing (y/n) ?"),
response(Reply),
Reply='y'.
symptom(Patient,swollen_glands) :-
write("Does ",Patient," have a swollen_glands (y/n) ?"),
response(Reply),
Reply='y'.
hypothesis(Patient,measles) :-
symptom(Patient,fever),
symptom(Patient,cough),
symptom(Patient,conjunctivitis),
symptom(Patient,runny_nose),
symptom(Patient,rash).
hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).
hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,conjunctivitis),
symptom(Patient,chills),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,cough).
hypothesis(Patient,common_cold) :-
symptom(Patient,headache),
symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,chills).
hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).
hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,chills),
symptom(Patient,body_ache),
symptom(Patient,rash).
hypothesis(Patient,measles) :-
symptom(Patient,cough),
symptom(Patient,sneezing),
symptom(Patient,runny_nose).
response(Reply) :-
readchar(Reply),
write(Reply),nl.
Output :
PRACTICAL 21
Write a PROLOG/C program to solve Monkey-Banana problem.
Program:-
domains
state=state(symbol,symbol,symbol,sym
bol)
/*state=state(monkey horizontal monkey vertical, box location, has/has not banana) */
predicates
move(state,symbol,stat
e) canget(state)
clauses
move(state(middle,onbox,middle,hasn
ot),
grasp,state(middle,onbox,middle,has).
move(state(P,onfloor,P,hasnot),cli
mb, state(P,onbox,P,hasnot)).
move(state(P,onfloor,P,hasnot),p
ush, state(P1,onfloor,P1,hasnot)).
move(state(P1,onfloor,B,hasnot),w
alk, state(P2,onfloor,B,hasnot)).
canget(state(_,_,_,has))
:- write("get").
canget(State1) :-
move(State1,Move,State
2), canget(State2),
write(State2),nl.
goal
clearwindow,
canget(state(door,onfloor,window,hasn
ot)).
ARTIFICIAL INTELLIGENCE (3170716)
Output:-
MGITER/CE/2020-2021