SIR CHHOTU RAM INSTITUTE OF ENGINEERING & TECHNOLOGY
CCSU , Meerut
DEPARTMENT OF INFORMATION TECHNOLOGY
(7th SEMESTER)
Subject Code : BT-764
ARTIFICIAL INTELLIGENCE LAB
Submitted To: Submitted By:
Er. Priyank Sirohi Sandeep Chaudhary
(100200556)
Table Of Content :
1. Write a program in prolog to solve Monkey Banana Problem.
2. Write a program in prolog to solve 8 Puzzle Problem.
3. Write a program in prolog to solve 4-Queens Problem.
4. Write a program in prolog to solve Travelling Salesman Problem.
5. Write a program in prolog to solve Water Jug Problem.
1. Write a program in prolog to solve Monkey Banana Problem.
/* 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 bananas 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 banana.
*/
% 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.
2. Write a program in prolog to solve 8 Puzzle Problem.
% 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 the 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(tile2,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(empty,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 screen 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)
at(tile3,2)
at(tile4,1)
Goal state:
[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)]
false.
3. Write a program in prolog to solve 4-Queens Problem.
/* Description:
In the 4 Queens problem the object is to place 4 queens on a chessboard 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),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).
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
4. Write a program in prolog to solve Travelling Salesman Problem.
/* Description:
For example, there are four cities(Kansas City,Houston,Gordon and Tampa).
-> 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,Town2,Distance).
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
5. Write a program in prolog to solve Water Jug Problem.
/* Description:
"You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring markers on
it. There is a tap that can be used to fill 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 is 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 is 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 Problem",0,0,25,80),
state(0,0).
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
+ +