Ai Lab Manual 123
Ai Lab Manual 123
ARTIFICAL INTELLIGENCELABORATORY
BRANCH :-AIML
PREPARED BY
Queries Solution :
% Facts
% 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:
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:
Solution :
/* Description:
*/
% Production rules:
can_reach 🡪 clever,close.
get_on: 🡪 can_climb.
% 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:
Solution :
/* Description:
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:
Solution :
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).
% Utility predicates.
is_subset([H|T],
Set):- member(H,
Set), is_subset(T,
Set).
is_subset([], _).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H), nl.
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:
Solution :
/* Description:
*/
% Domains:
queen = q(integer,
integer) queens = queen*
freelist = integer*
board = board(queens, freelist, freelist, freelist, freelist)
% Predicates:
% 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:
Solution :
/* Description:
For example, there are four cities(Kansas City,Houston,Gordon
and Tamp a).
*/
% 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.
8. Aim:
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(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
|