0% found this document useful (0 votes)
48 views12 pages

Lab Manual: Sanskar College of Engineering & Technology

The document provides details of 8 experiments conducted on artificial intelligence concepts like Prolog, solving puzzles using search techniques, and other AI problems. The experiments cover concepts such as simple Prolog rules and queries, arithmetic in Prolog, solving puzzles like monkey banana problem, tower of Hanoi, 8-puzzle, 4-queen puzzle, traveling salesman problem, and water jug problem using search techniques in Prolog. Goals and sample outputs are provided for each experiment.

Uploaded by

Tabish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views12 pages

Lab Manual: Sanskar College of Engineering & Technology

The document provides details of 8 experiments conducted on artificial intelligence concepts like Prolog, solving puzzles using search techniques, and other AI problems. The experiments cover concepts such as simple Prolog rules and queries, arithmetic in Prolog, solving puzzles like monkey banana problem, tower of Hanoi, 8-puzzle, 4-queen puzzle, traveling salesman problem, and water jug problem using search techniques in Prolog. Goals and sample outputs are provided for each experiment.

Uploaded by

Tabish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sanskar College of Engineering & Technology

Department of Computer Science & Engineering

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

LAB MANUAL
Subject Code: KCS- 751A
Subject Name: Artificial Intelligence Lab

Submitted to: Submitted by:


Ms. Leena Chopra Shiv Kumar
(1902150109005)
INDEX

EXP. EXPERIMENT PAGE EXP. SIGN


NO. NO. DATE
EXPERIMENT NO. 1

Write a program in prolog to implement simple facts and Queries

1. Ram likes mango.


2. Bill likes Cindy.
3. Rose is red.
4. John owns gold.

Clauses:
likes(ram ,mango).
red(rose). likes(bill
,cindy). owns(john
,gold).

Goal:
?- likes (ram,What). What
= mango.
?-likes(Who,cindy).
Who= cindy
?-red(What). What=
rose ?-
owns(Who,What).
Who= john
What= gold

1
EXPERIMENT NO. 2

Write a program in prolog to implement simple arithmetic.

Production rules:
c_to_f :- f is c * 9
/ 5 +32 freezing :-
f < = 32

Rules:
c_to_f(C,F) :- F is
C * 9 / 5 + 32.
freezing(F) :- F
=< 32.

Queries :
?- c_to_f(100,X).
X = 212 Yes
?- freezing(15).
Yes
?- freezing(45).
No

2
EXPERIMENT NO. 3

Write a program to solve the Monkey Banana problem.

Production Rules: can_reachclever,close.


get_on: can_climb. underin room,in_room,
in_room,can_climb. Closeget_on,under| tall

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.
B = banana.
?- can_reach(monkey, banana).
Yes.

3
EXPERIMENT NO. 4

Write a program to solve Tower of Hanoi.

Production Rules:
hanoi(N)move(N,left,middle,right). move(1,A,_,C)inform(A,C),fail.
move(N,A,B,C)N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).

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(N1,B,A,C). inform(Loc1,
Loc2):- write("\nMove a disk from ", Loc1, " to
", Loc2).

Goal: hanoi(3).
Move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
Yes

4
EXPERIMENT NO. 5

Write a program to solve 8-Puzzle problem.

Production Rules :
h_function(Puzz,H) p_fcn(Puzz,P), s_fcn(Puzz,S),H is P + 3*S.
The 'move' productions are defined as follows. move(P,C,left) left(P,C).
move(P,C,up) up(P,C). move(P,C,right) right(P,C). move(P,C,down)
down(P,C). p_fcn(A/B/C/D/E/F/G/H/I, P) a(A,Pa), b(B,Pb),
c(C,Pc),d(D,Pd), e(E,Pe), f(F,Pf),g(G,Pg), h(H,Ph), i(I,Pi),
P is Pa+Pb+Pc+Pd+Pe+Pf+Pg+Ph+Pg+Pi.
s_fcn(A/B/C/D/E/F/G/H/I, S) 1 s_aux(A,B,S1), s_aux(B,C,S2),s_aux(C,F,S3),
s_aux(F,I,S4),s_aux(I,H,S5), s_aux(H,G,S6),s_aux(G,D,S7), s_aux(D,A,S8),
s_aux(E,S9),
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.
s_aux(0,0) cut s_aux(X,Y,0)
:- Y is X+1, !. s_aux(8,1,0) :-
!.
h_function(Puzz,H) :- p_fcn(Puzz,P), s_fcn(Puzz,S), H
is P + 3*S.
The 'move' predicate is defined as follows. move(P,C,left) :- left(P,C).
move(P,C,up) :- up(P,C). move(P,C,right) :- right(P,C). move(P,C,down) :-
down(P,C). Here is the code for p and s. %%% Manhattan distance
p_fcn(A/B/C/D/E/F/G/H/I, P) :- a(A,Pa), b(B,Pb), c(C,Pc),d(D,Pd), e(E,Pe),
f(F,Pf),g(G,Pg), h(H,Ph), i(I,Pi), P is Pa+Pb+Pc+Pd+Pe+Pf+Pg+Ph+Pg+Pi.
a(0,0). a(1,0). a(2,1). a(3,2). a(4,3). a(5,4). a(6,3). a(7,2). a(8,1). b(0,0).
b(1,0). b(2,0). b(3,1). b(4,2). b(5,3). b(6,2). b(7,3). b(8,2). c(0,0). c(1,2).
c(2,1). c(3,0). c(4,1). c(5,2). c(6,3). c(7,4). c(8,3). d(0,0). d(1,1). d(2,2).
d(3,3). d(4,2). d(5,3). d(6,2). d(7,2). d(8,0). e(0,0). e(1,2). e(2,1). e(3,2).
e(4,1). e(5,2). e(6,1). e(7,2). e(8,1). f(0,0). f(1,3). f(2,2). f(3,1). f(4,0). f(5,1).
f(6,2). f(7,3). f(8,2).
g(0,0). g(1,2). g(2,3). g(3,4). g(4,3). g(5,2). g(6,2). g(7,0). g(8,1).
h(0,0). h(1,3). h(2,3). h(3,3). h(4,2). h(5,1). h(6,0). h(7,1). h(8,2). i(0,0).
i(1,4). i(2,3). i(3,2). i(4,1). i(5,0). i(6,1). i(7,2). i(8,3).
%%% the out-of-cycle function
s_fcn(A/B/C/D/E/F/G/H/I, S) :-
s_aux(A,B,S1), s_aux(B,C,S2), s_aux(C,F,S3),
s_aux(F,I,S4), s_aux(I,H,S5), s_aux(H,G,S6),

5
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.
s_aux(0,0) :- !. s_aux(_,1). s_aux(X,Y,0) :- Y
is X+1, !. s_aux(8,1,0) :- !. s_aux(_,_,2).
?- solve(0/8/1/2/4/3/7/6/5, S).

Solution:
left( A/0/C/D/E/F/H/I/J , 0/A/C/D/E/F/H/I/J ).
left( A/B/C/D/0/F/H/I/J , A/B/C/0/D/F/H/I/J ).
left( A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/0/H/J ).
left( A/B/0/D/E/F/H/I/J , A/0/B/D/E/F/H/I/J ).
left( A/B/C/D/E/0/H/I/J , A/B/C/D/0/E/H/I/J ).
left( A/B/C/D/E/F/H/I/0 , A/B/C/D/E/F/H/0/I ).
up( A/B/C/0/E/F/H/I/J , 0/B/C/A/E/F/H/I/J ). up(
A/B/C/D/0/F/H/I/J , A/0/C/D/B/F/H/I/J ). up(
A/B/C/D/E/0/H/I/J , A/B/0/D/E/C/H/I/J ). up(
A/B/C/D/E/F/0/I/J , A/B/C/0/E/F/D/I/J ). up(
A/B/C/D/E/F/H/0/J , A/B/C/D/0/F/H/E/J ). up(
A/B/C/D/E/F/H/I/0 , A/B/C/D/E/0/H/I/F ). right(
A/0/C/D/E/F/H/I/J , A/C/0/D/E/F/H/I/J ). right(
A/B/C/D/0/F/H/I/J , A/B/C/D/F/0/H/I/J ). right(
A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/H/J/0 ). right(
0/B/C/D/E/F/H/I/J , B/0/C/D/E/F/H/I/J ). right(
A/B/C/0/E/F/H/I/J , A/B/C/E/0/F/H/I/J ). right(
A/B/C/D/E/F/0/I/J , A/B/C/D/E/F/I/0/J ). down(
A/B/C/0/E/F/H/I/J , A/B/C/H/E/F/0/I/J ). down(
A/B/C/D/0/F/H/I/J , A/B/C/D/I/F/H/0/J ). down(
A/B/C/D/E/0/H/I/J , A/B/C/D/E/J/H/I/0 ). down(
0/B/C/D/E/F/H/I/J , D/B/C/0/E/F/H/I/J ). down(
A/0/C/D/E/F/H/I/J , A/E/C/D/0/F/H/I/J ). down(
A/B/0/D/E/F/H/I/J , A/B/F/D/E/0/H/I/J ).

6
EXPERIMENT NO. 6

Write a program to solve 4-Queen problem.


Domains:
queen = q(integer, integer)
queens = queen* freelist =
integer*
board = board(queens, freelist, freelist, freelist, freelist)

Predicates:
nondeterm placeN(integer, board, board)
nondeterm place_a_queen(integer, board, board)
nondeterm nqueens(integer) nondeterm
makelist(integer, freelist)
nondeterm findandremove(integer, freelist, freelist) nextrow(integer,
freelist, freelist)

Clauses: nqueens(N):- makelist(N,L), Diagonal=N*2-1,


makelist(Diagonal,LL), placeN(N,board([],L,L,LL,LL),Final),
write(Final). placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!.
placeN(N,Board1,Result):- place_a_queen(N,Board1,Board2),
placeN(N,Board2,Result).
place_a_queen(N,board(Queens,Rows,Columns,Diag1,Diag2),
board([q(R,C)|Queens],NewR,NewC,NewD1,NewD2)):- nextrow(R,Rows,NewR),
findandremove(C,Columns,NewC),
D1=N+C-R,findandremove(D1,Diag1,NewD1), D2=R+C-
1,findandremove(D2,Diag2,NewD2).
findandremove(X,[X|Rest],Rest).
findandremove(X,[Y|Rest],[Y|Tail]):- findandremove(X,Rest,Tail).
makelist(1,[1]). makelist(N,[N|Rest]) :- N1=N-
1,makelist(N1,Rest). nextrow(Row,[Row|Rest],Rest).

Goal:
nqueens(4),nl.
board([q(1,2),q(2,4),q(3,1),q(4,3),[],[],[7,4,1],[7,4,1]) yes

7
EXPERIMENT NO. 7

Write a program to solve traveling salesman problem.

Production Rules:
route(Town1,Town2,Distance) road(Town1,Town2,Distance).
route(Town1,Town2,Distance) road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,

Domains: town
= symbol
distance = integer

Predicates: nondeterm
road(town,town,distance) nondeterm
route(town,town,distance)

Clauses:
road("tampa","houston",200).
road("gordon","tampa",300).
road("houston","gordon",100).
road("houston","kansas_city",120).
road("gordon","kansas_city",130).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-
road(Town1,X,Dist1), route(X,Town2,Dist2),
Distance=Dist1+Dist2,
!.

Goal:
route("tampa", "kansas_city", X), write("Distance
from Tampa to Kansas City is ",X),nl.
Distance from Tampa to Kansas City is 320
X=320
1 Solution

8
EXPERIMENT NO. 8

Write a program for water jug problem.


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

Goal:
?- water_jugs.
Given three jugs with capacities of:
3 gallons in the small jug, 4 gallons in the large jug and 8 gallons in the reservoir;
To obtain the result:
0 gallons in the small jug, 2 gallons in the large jug and 6 gallons in the reservoir;
Starting with:
0 gallons in the small jug, 0 gallons in the large jug and 8 gallons in the reservoir;
Do the following:
- Fill the small jug from the reservoir giving:
3 gallons in the small jug, 0 gallons in the large jug and 5 gallons in the reservoir -
Empty the small jug into the large jug giving:
0 gallons in the small jug, 3 gallons in the large jug and 5 gallons in the reservoir
- Fill the small jug from the reservoir giving:
3 gallons in the small jug, 3 gallons in the large jug and 2 gallons in the reservoir -
Fill the large jug from the small jug giving:
2 gallons in the small jug, 4 gallons in the large jug and 2 gallons in the reservoir -
Empty the large jug into the reservoir giving:
2 gallons in the small jug, 0 gallons in the large jug and 6 gallons in the reservoir -
Empty the small jug into the large jug giving:
0 gallons in the small jug, 2 gallons in the large jug and 6 gallons in the reservoir

9
EXPERIMENT NO. 9

Write a python program to implement simple Chatbot.

pattern_where_is(S, X), !,
(info(D, X); next(X,_,_,_,_)),
print_prompt(me),
responses_db(location, D),
random_pick(D, R), write_list(R),
write('Where are you at the moment?\n'),
get_location(2), loc(Y), find_route(Y, D,
R), !, responses_db(location, [ ['Where
are you at the moment?'],
['What room are you in?'],
['Where are you?'],
['I\'m not telling!'] ['Where
are you?']
]).

10

You might also like