0% found this document useful (0 votes)
16 views17 pages

Ai Lab Manual 123

Uploaded by

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

Ai Lab Manual 123

Uploaded by

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

SHADAN COLLEGE OF ENGINEERING & TECHNOLOGY)

(NAAC WITH A+ GRADE, NBA ACCREDITED AND ISO 9001:2015 CERTIFICATE


INSTITUTION)
Peerancheru, Hyderabad -500086

AI LAB MANUAL (R18)


B.TECH III year II sem

ARTIFICAL INTELLIGENCELABORATORY
BRANCH :-AIML
PREPARED BY

MRS. RAFIA MATEEN

ASSISSTANT PROFESSOROF CSE DEPARTMENT


Aim:

Write a program in prolog to implement simple facts and

Queries Solution :

% Facts

1. Ram likes mango.


2. Seema is a girl.
3. Bill likes Cindy.
4. Rose is red.
5. John owns gold.

% Clauses

likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy)
. owns(john
,gold).

OUTPUT:

% Queries

?-likes(ram,What).
What= mango
?-
likes(Who,cindy).
Who= cindy

?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold
Aim:

2. Write a program in prolog to implement simple arithmetic

Solution :

% 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.

Output:

% Queries :

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

Write a program in prolog to solve Monkey banana problem

Solution :

/* Description:

Imagine a room containing a monkey, chair and some bananas. That


have 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 bana nas and climb on the chair .

The problem is to prove the monkey can reach the

bananas. The monkey can perform the following actions:


1) Walk on the floor
2) Climb the box
3) Push the box around(if it is beside the box).
4) Grasp the banana if it is standing on the box directly under
the ba nana.

*/

% Production rules:

can_reach 🡪 clever,close.
get_on: 🡪 can_climb.

under 🡪 in room,in_room, in_room,can_climb.


Close 🡪 get_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).

Output:

% Queries:

?- can_reach(A, B).
A = monkey.
B = banana.

?- can_reach(monkey,
banana). Yes.

4. Aim:

Write a program in prolog to solve Tower of Hanoi

Solution :

/* Description:

This object of this famous puzzle is to move N disks from the


left peg to the right peg using the center peg as an auxiliary
holding peg. At no time can a larger disk be placed upon a
smaller disk. The followin g diagram depicts the starting setup
for N=3 disks.
*/
% 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).

Output:

% 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

5. Aim:

Write a program in prolog to solve 8 Puzzle problems

Solution :

% Simple Prolog Planner for the 8 Puzzle Problem

/* This predicate initialises the problem states. The first


argument of solve is the initial state, the 2nd the goal state,
and the third t he plan that will be produced.*/

test(Plan):-
write('Initial state:'),nl,
Init= [at(tile4,1), at(tile3,2), at(tile8,3), at(empty,4),
at(tile 2,5), at(tile6,6), at(tile5,7), at(tile1,8), at(tile7,9)],
write_sol(Init),
Goal= [at(tile1,1), at(tile2,2), at(tile3,3), at(tile4,4),
at(empt y,5), at(tile5,6), at(tile6,7), at(tile7,8), at(tile8,9)],
nl,write('Goal state:'),nl,
write(Goal),nl,nl,
solve(Init,Goal,Plan).

solve(State, Goal, Plan):-


solve(State, Goal, [], Plan).

% Determines whether Current and Destination tiles are a valid


move. is_movable(X1,Y1) :- (1 is X1 - Y1) ; (-1 is X1 - Y1) ; (3
is X1 - Y1)
; (-3 is X1 - Y1).
/* This predicate produces the plan. Once the Goal list is a
subset of the current State the plan is complete and it is
written to the scree n using write_sol */

solve(State, Goal, Plan, Plan):-


is_subset(Goal, State), nl,
write_sol(Plan).

solve(State, Goal, Sofar, Plan):-


act(Action, Preconditions, Delete,
Add), is_subset(Preconditions,
State),
\+ member(Action, Sofar),
delete_list(Delete, State, Remainder),
append(Add, Remainder, NewState),
solve(NewState, Goal, [Action|Sofar],
Plan).

/* The problem has three


operators. 1st arg = name
2nd arg =
preconditions 3rd arg
= delete list 4th arg
= add list. */

% Tile can move to new position only if the destination tile is


empty & Manhattan distance = 1
act(move(X,Y,Z),
[at(X,Y), at(empty,Z), is_movable(Y,Z)],
[at(X,Y), at(empty,Z)],
[at(X,Z), at(empty,Y)]).

% Utility predicates.

% Check is first list is a subset of the second

is_subset([H|T],
Set):- member(H,
Set), is_subset(T,
Set).
is_subset([], _).

% Remove all elements of 1st list from second to create third.

delete_list([H|T], Curstate, Newstate):-


remove(H, Curstate, Remainder),
delete_list(T, Remainder, Newstate).
delete_list([], Curstate, Curstate).

remove(X, [X|T], T).


remove(X, [H|T], [H|R]):-
remove(X, T, R).

write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H), nl.

append([H|T], L1, [H|L2]):-


append(T, L1, L2).
append([], L, L).

member(X, [X|_]).
member(X, [_|T]):-
member(X, T).

Output:

% Queries:

?-
test(Plan).
Initial
state:
at(tile7,9
)
at(tile1,8
)
at(tile5,7
)
at(tile6,6
)
at(tile2,5
)
at(empty,4
)
at(tile8,3
)
6. Aim:

Write a program in prolog to solve 4-Queens problem

Solution :

/* Description:

In the 4 Queens problem the object is to place 4 queens on a


chessboar d in such a way that no queens can capture a piece.
This means that no two queens may be placed on the same row,
column, or diagonal.

*/

% 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),Fina
l), write(Final).

placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!.
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]).

Output:

% 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. Aim:

Write a program in prolog to solve Traveling salesman


problem

Solution :

/* Description:
For example, there are four cities(Kansas City,Houston,Gordon
and Tamp a).

-> The distance between Kansas City and Houston is 120.


-> The distance between Kansas City and Tampa is 80.
-> The distance between Houston and Gordon is 100.

*/

% 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,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
!.

Output:

% 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. Aim:

Write a program in prolog for Water jug problem

Solution :

/* Description:
"You are given two jugs, a 4-gallon one and a 3-gallon one.
Neither ha ve any measuring markers on it. There is a tap that
can be used to fil l 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
*/

%database
visited_state(integer,integer).

%predicates
state(integer,integer).

%clauses
state(2,0).

state(X,Y):- X < 4,
not(visited_state(4,Y)),
assert(visited_state(X,Y)),
write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\
n")
,
state(4,Y).

state(X,Y):- Y < 3,
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Fill the 3-Gallon Jug: (", X,",",Y,") --> (",
X,","
,3,")\n"),
state(X,3).

state(X,Y):- X > 0,
not(visited_state(0,Y)),
assert(visited_state(X,Y)),
write("Empty the 4-Gallon jug on ground: (", X,",",Y,")
--
> (", 0,",",Y,")\n"),
state(0,Y).

state(X,Y):- Y > 0,
not(visited_state(X,0)),
assert(visited_state(X,0)),
write("Empty the 3-Gallon jug on ground: (", X,",",Y,")
--
> (", X,",",0,")\n"),
state(X,0).

state(X,Y):- X + Y >= 4,
Y > 0,
NEW_Y = Y - (4 - X),
not(visited_state(4,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour water from 3-Gallon jug to 4-gallon until
it i s full: (", X,",",Y,") --> (", 4,",",NEW_Y,")\n"),
state(4,NEW_Y).

state(X,Y):- X + Y >=3,
X > 0,
NEW_X = X - (3 - Y),
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Pour water from 4-Gallon jug to 3-gallon until
it i s full: (", X,",",Y,") --> (", NEW_X,",",3,")\n"),
state(NEW_X,3).

state(X,Y):- X + Y>=4,
Y > 0,
NEW_X = X + Y,
not(visited_state(NEW_X,0)),
assert(visited_state(X,Y)),
write("Pour all the water from 3-Gallon jug to 4-
gallon: ( ", X,",",Y,") --> (", NEW_X,",",0,")\n"),
state(NEW_X,0).

state(X,Y):- X+Y >=3,


X > 0,
NEW_Y = X + Y,
not(visited_state(0,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour all the water from 4-Gallon jug to 3-
gallon: ( ", X,",",Y,") --> (", 0,",",NEW_Y,")\n"),
state(0,NEW_Y).

state(0,2):-
not(visited_state(2,0)),
assert(visited_state(0,2)),
write("Pour 2 gallons from 3-Gallon jug to 4-gallon:
(", 0
,",",2,") --> (", 2,",",0,")\n"),
state(2,0).

state(2,Y):- not(visited_state(0,Y)),
assert(visited_state(2,Y)),
write("Empty 2 gallons from 4-Gallon jug on the ground:
("
, 2,",",Y,") --> (", 0,",",Y,")\n"),
state(0,Y).

goal:-
makewindow(1,2,3,"4-3 Water Jug

Output:

% Goal:-
makewindow(1,2,3,"4-3 Water Jug
Problem",0,0,25,80), state(0,0).
+ 4-3 Water Jug Problem
+
| Fill the 4-Gallon Jug: (0,0) --> (4,0)
|
| Fill the 3-Gallon Jug: (4,0) --> (4,3)
|
| Empty the 4-Gallon jug on ground: (4,3) --> (0,3)
|
| Pour all the water from 3-Gallon jug to 4-gallon: (0,3) -->
(3,0)
|
| Fill the 3-Gallon Jug: (3,0) --> (3,3)
|
| Pour water from 3-Gallon jug to 4-gallon until it is full: (3,3)
--> (4,2) |
| Empty the 4-Gallon jug on ground: (4,2) --> (0,2)
|
| Pour all the water from 3-Gallon jug to 4-gallon: (0,2) -->
(2,0)
|
|
|
| Press the SPACE bar
|

You might also like