0% found this document useful (0 votes)
68 views28 pages

Artificial Intelligence (2180703) : Practical: 1

The document describes programs to implement various artificial intelligence algorithms including: 1) A Tic-Tac-Toe game with rules to check for wins in rows, columns and diagonals. 2) Breadth-first search (BFS) and depth-first search (DFS) algorithms. 3) A single player Sudoku game using Prolog predicates to represent the puzzle and constraints. 4) The A* search algorithm implemented for the 8-puzzle problem domain.

Uploaded by

Vinay Padhiyar
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)
68 views28 pages

Artificial Intelligence (2180703) : Practical: 1

The document describes programs to implement various artificial intelligence algorithms including: 1) A Tic-Tac-Toe game with rules to check for wins in rows, columns and diagonals. 2) Breadth-first search (BFS) and depth-first search (DFS) algorithms. 3) A single player Sudoku game using Prolog predicates to represent the puzzle and constraints. 4) The A* search algorithm implemented for the 8-puzzle problem domain.

Uploaded by

Vinay Padhiyar
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/ 28

ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 1
AIM: Write a program to implement Tic-Tac-Toe game problem.
Input:
win(Brd, Plyr) :- rwin(Brd, Plyr);
cwin(Brd, Plyr);
dwin(Brd, Plyr).
rwin(Brd, Plyr) :- Brd = [Plyr,Plyr,Plyr,_,_,_,_,_,_];
Brd = [_,_,_,Plyr,Plyr,Plyr,_,_,_];
Brd = [_,_,_,_,_,_,Plyr,Plyr,Plyr].
cwin(Brd, Plyr) :- Brd = [Plyr,_,_,Plyr,_,_,Plyr,_,_];
Brd = [_,Plyr,_,_,Plyr,_,_,Plyr,_];
Brd = [_,_,Plyr,_,_,Plyr,_,_,Plyr].
dwin(Brd, Plyr) :- Brd = [Plyr,_,_,_,Plyr,_,_,_,Plyr];
Brd = [_,_,Plyr,_,Plyr,_,Plyr,_,_].
omove([a,B,C,D,E,F,G,H,I], Plyr, [Plyr,B,C,D,E,F,G,H,I]).
omove([A,a,C,D,E,F,G,H,I], Plyr, [A,Plyr,C,D,E,F,G,H,I]).
omove([A,B,a,D,E,F,G,H,I], Plyr, [A,B,Plyr,D,E,F,G,H,I]).
omove([A,B,C,a,E,F,G,H,I], Plyr, [A,B,C,Plyr,E,F,G,H,I]).
omove([A,B,C,D,a,F,G,H,I], Plyr, [A,B,C,D,Plyr,F,G,H,I]).
omove([A,B,C,D,E,a,G,H,I], Plyr, [A,B,C,D,E,Plyr,G,H,I]).
omove([A,B,C,D,E,F,a,H,I], Plyr, [A,B,C,D,E,F,Plyr,H,I]).
omove([A,B,C,D,E,F,G,a,I], Plyr, [A,B,C,D,E,F,G,Plyr,I]).
omove([A,B,C,D,E,F,G,H,a], Plyr, [A,B,C,D,E,F,G,H,Plyr]).

xmove([a,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
xmove([A,a,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
xmove([A,B,a,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
xmove([A,B,C,a,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
xmove([A,B,C,D,a,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
xmove([A,B,C,D,E,a,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
xmove([A,B,C,D,E,F,a,H,I], 7, [A,B,C,D,E,F,x,H,I]).
xmove([A,B,C,D,E,F,G,a,I], 8, [A,B,C,D,E,F,G,x,I]).
xmove([A,B,C,D,E,F,G,H,a], 9, [A,B,C,D,E,F,G,H,x]).
xmove(Brd, _, Brd) :- write('Illegal move.'), nl.

disp([A,B,C,D,E,F,G,H,I]) :-
write('|'),
write([A,B,C]),write('|'),nl,
write('|'),
write([D,E,F]),write('|'),nl, write('|'),
write([G,H,I]),write('|'),nl,nl.

go :- how_to_play, strt([a,a,a,a,a,a,a,a,a]).

KJIT/IT/8TH SEM/ Page 1


ARTIFICIAL INTELLIGENCE (2180703)

how_to_play :-
write('You are x player, enter positions followed by a period.'),
nl,
disp([1,2,3,4,5,6,7,8,9]).

strt(Brd) :- win(Brd, x), write('You win!').


strt(Brd) :- win(Brd, o), write('AI win!').
strt(Brd) :- read(N),
xmove(Brd, N, NewBrd),
disp(NewBrd),
oplay(NewBrd, NewnewBrd),
disp(NewnewBrd),
strt(NewnewBrd).

can_x_win(Brd) :- omove(Brd, x, NewBrd), win(NewBrd, x).

oplay(Brd,NewBrd) :-
omove(Brd, o, NewBrd),
win(NewBrd, o),!.
oplay(Brd,NewBrd) :-
omove(Brd, o, NewBrd),
not(can_x_win(NewBrd)).
oplay(Brd,NewBrd) :-
omove(Brd, o, NewBrd).
oplay(Brd,NewBrd) :-
not(member(a,Brd)),!,
write('Game Ended without Winner!'), nl,
NewBrd = Brd.

KJIT/IT/8TH SEM/ Page 2


ARTIFICIAL INTELLIGENCE (2180703)

OUTPUT:

KJIT/IT/8TH SEM/ Page 3


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 2
AIM: Write a program to implement BFS
Input:
bfs(s,Path,Path):-goal(s).

bfs(s,Checked,Path):-
%try a move
move(s,s2),
%ensure the resulting state is new

\+member(s2,Checked),
%annd that this state leads to the goal
bfs(s2,[s2|Checked],Path).
Output:

KJIT/IT/8TH SEM/ Page 4


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 3
AIM: Write a program to implement DFS
Input:
dfs(s,Path,Path):-goal(s).

dfs(s,Checked,Path):-
%try a move
move(s,s2),
%ensure the resulting state is new
\+member(s2,Checked),
%annd that this state leads to the goal
dfs(s2,[s2|Checked],Path).

Output:

KJIT/IT/8TH SEM/ Page 5


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 4
AIM: Write a program to implement Single Player Game(Sudoku).
Input:
:- use_rendering(sudoku).
:- use_module(library(clpfd)).
sudoku(Rows) :-
length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs), Vs ins 1..9,
maplist(all_distinct, Rows),
transpose(Rows, Columns),
maplist(all_distinct, Columns),
Rows = [A,B,C,D,E,F,G,H,I],
blocks(A, B, C), blocks(D, E, F), blocks(G, H, I).

blocks([], [], []).


blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
all_distinct([A,B,C,D,E,F,G,H,I]),
blocks(Bs1, Bs2, Bs3).

problem(1, [[_,_,_, _,_,_, _,_,_],


[_,_,_, _,_,3, _,8,5],
[_,_,1, _,2,_, _,_,_],

[_,_,_, 5,_,7, _,_,_],


[_,_,4, _,_,_, 1,_,_],
[_,9,_, _,_,_, _,_,_],

[5,_,_, _,_,_, _,7,3],


[_,_,2, _,1,_, _,_,_],
[_,_,_, _,4,_, _,_,9]]).

OUTPUT:

KJIT/IT/8TH SEM/ Page 6


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 5
AIM: Write a program to Implement A* Algorithm.(8- PUZZLE).
Input:
:- op(400,yfx,'#'). /* Node builder notation */

solve(State,Soln) :- f_function(State,0,F),
search([State#0#F#[]],S), reverse(S,Soln).

f_function(State,D,F) :- h_function(State,H),
F is D + H.

search([State#_#_#Soln|_], Soln) :- goal(State).


search([B|R],S) :- expand(B,Children),
insert_all(Children,R,Open),
search(Open,S).

insert_all([F|R],Open1,Open3) :- insert(F,Open1,Open2),
insert_all(R,Open2,Open3).
insert_all([],Open,Open).

insert(B,Open,Open) :- repeat_node(B,Open), ! .
insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .
insert(B,[B1|R],[B1|S]) :- insert(B,R,S), !.
insert(B,[],[B]).

repeat_node(P#_#_#_, [P#_#_#_|_]).

cheaper( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.

expand(State#D#_#S,All_My_Children) :-
bagof(Child#D1#F#[Move|S],
(D1 is D+1,
move(State,Child,Move),
f_function(Child,D1,F)),
All_My_Children).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%
%%% 8-puzzle solver
%%%
%%%
%%% State have form A/B/C/D/E/F/G/H/I

KJIT/IT/8TH SEM/ Page 7


ARTIFICIAL INTELLIGENCE (2180703)

%%% where {A,...,I} = {0,...,8}


%%% 0 represents the empty tile
%%%

goal(1/2/3/8/0/4/7/6/5).

%%% The puzzle moves

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

%%% the heuristic function


h_function(Puzz,H) :- p_fcn(Puzz,P),
s_fcn(Puzz,S),
H is P + 3*S.

%%% the move

KJIT/IT/8TH SEM/ Page 8


ARTIFICIAL INTELLIGENCE (2180703)

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

%%% the Manhattan distance function


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,1). 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),
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) :- !.
s_aux(_,1).

s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%
%%% 8-puzzle animation -- using VT100 character graphics
%%%
%%%
%%%

KJIT/IT/8TH SEM/ Page 9


ARTIFICIAL INTELLIGENCE (2180703)

puzzle(P) :- solve(P,S),
animate(P,S),
message.

animate(P,S) :- initialize(P),
cursor(1,2), write(S),
cursor(1,22), write('Hit ENTER to step solver.'),
get0(_X),
play_back(S).

:- dynamic location/3. %%% So that location of a tile


%%% can be retracted/asserted.
%%% Location(s) asserted and retracted
%%% by puzzle animator below

initialize(A/B/C/D/E/F/H/I/J) :-
cls,
retractall(location(_,_,_)),
assert(location(A,20,5)),
assert(location(B,30,5)),
assert(location(C,40,5)),
assert(location(F,40,10)),
assert(location(J,40,15)),
assert(location(I,30,15)),
assert(location(H,20,15)),
assert(location(D,20,10)),
assert(location(E,30,10)), draw_all.

draw_all :- draw(1), draw(2), draw(3), draw(4),


draw(5), draw(6), draw(7), draw(8).

%%% play_back([left,right,up,...]).
play_back([M|R]) :- call(M), get0(_X), play_back(R).
play_back([]) :- cursor(1,24). %%% Put cursor out of the way

message :- nl,nl,
write(' ********************************************'), nl,
write(' * Enter 8-puzzle goals in the form ... *'), nl,
write(' * ?- puzzle(0/8/1/2/4/3/7/6/5). *'), nl,
write(' * Enter goal ''message'' to reread this. *'), nl,
write(' ********************************************'), nl, nl.

KJIT/IT/8TH SEM/ Page 10


ARTIFICIAL INTELLIGENCE (2180703)

cursor(X,Y) :- put(27), put(91), %%% ESC [


write(Y),
put(59), %%% ;
write(X),
put(72). %%% M

%%% clear the screen, quickly


cls :- put(27), put("["), put("2"), put("J").

%%% video attributes -- bold and blink not working


plain :- put(27), put("["), put("0"), put("m").
reverse_video :- put(27), put("["), put("7"), put("m").

%%% Tile objects, character map(s)


%%% Each tile should be drawn using the character map,
%%% drawn at 'location', which is asserted and retracted
%%% by 'playback'.
character_map(N, [ [' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ', N ,' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' '] ]).

%%% move empty tile (spot) to the left


left :- retract(location(0,X0,Y0)),
Xnew is X0 - 10,
location(Tile,Xnew,Y0),
assert(location(0,Xnew,Y0)),
right(Tile),right(Tile),right(Tile),
right(Tile),right(Tile),
right(Tile),right(Tile),right(Tile),
right(Tile),right(Tile).

up :- retract(location(0,X0,Y0)),
Ynew is Y0 - 5,
location(Tile,X0,Ynew),
assert(location(0,X0,Ynew)),
down(Tile),down(Tile),down(Tile),down(Tile),down(Tile).

right :- retract(location(0,X0,Y0)),
Xnew is X0 + 10,
location(Tile,Xnew,Y0),

KJIT/IT/8TH SEM/ Page 11


ARTIFICIAL INTELLIGENCE (2180703)

assert(location(0,Xnew,Y0)),
left(Tile),left(Tile),left(Tile),left(Tile),left(Tile),
left(Tile),left(Tile),left(Tile),left(Tile),left(Tile).

down :- retract(location(0,X0,Y0)),
Ynew is Y0 + 5,
location(Tile,X0,Ynew),
assert(location(0,X0,Ynew)),
up(Tile),up(Tile),up(Tile),up(Tile),up(Tile).

draw(Obj) :- reverse_video, character_map(Obj,M),


location(Obj,X,Y),
draw(X,Y,M), plain.

%%% hide tile


hide(Obj) :- character_map(Obj,M),
location(Obj,X,Y),
hide(X,Y,M).

hide(_,_,[]).
hide(X,Y,[R|G]) :- hide_row(X,Y,R),
Y1 is Y + 1,
hide(X,Y1,G).

hide_row(_,_,[]).
hide_row(X,Y,[_|R]) :- cursor(X,Y),
write(' '),
X1 is X + 1,
hide_row(X1,Y,R).

%%% draw tile


draw(_,_,[]).
draw(X,Y,[R|G]) :- draw_row(X,Y,R),
Y1 is Y + 1,
draw(X,Y1,G).

draw_row(_,_,[]).
draw_row(X,Y,[P|R]) :- cursor(X,Y),
write(P),
X1 is X + 1,
draw_row(X1,Y,R).

KJIT/IT/8TH SEM/ Page 12


ARTIFICIAL INTELLIGENCE (2180703)

%%% Move an Object up


up(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
Y1 is Y - 1,
assert(location(Obj,X,Y1)),
draw(Obj).

down(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
Y1 is Y + 1,
assert(location(Obj,X,Y1)),
draw(Obj).

left(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
X1 is X - 1,
assert(location(Obj,X1,Y)),
draw(Obj).

right(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
X1 is X + 1,
assert(location(Obj,X1,Y)),
draw(Obj).

:- message.

OUTPUT:

KJIT/IT/8TH SEM/ Page 13


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 6
AIM: Write a program to solve N-Queens problem using Prolog.
Input:
:- use_rendering(chess).

queens(N, Queens) :-
length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).

board([], [], N, N, _, _).


board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-
Col is Col0+1,
functor(Vars, f, N),
constraints(N, Vars, VR, VC),
board(Queens, Board, Col, N, VR, [_|VC]).

constraints(0, _, _, _) :- !.
constraints(N, Row, [R|Rs], [C|Cs]) :-
arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).

queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).

OUTPUT:

KJIT/IT/8TH SEM/ Page 14


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 8
AIM: Write a program to solve 8 puzzle problem using Prolog.
Input:
:- op(400,yfx,'#'). /* Node builder notation */

solve(State,Soln) :- f_function(State,0,F),
search([State#0#F#[]],S), reverse(S,Soln).

f_function(State,D,F) :- h_function(State,H),
F is D + H.

search([State#_#_#Soln|_], Soln) :- goal(State).


search([B|R],S) :- expand(B,Children),
insert_all(Children,R,Open),
search(Open,S).

insert_all([F|R],Open1,Open3) :- insert(F,Open1,Open2),
insert_all(R,Open2,Open3).
insert_all([],Open,Open).

insert(B,Open,Open) :- repeat_node(B,Open), ! .
insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .
insert(B,[B1|R],[B1|S]) :- insert(B,R,S), !.
insert(B,[],[B]).

repeat_node(P#_#_#_, [P#_#_#_|_]).

cheaper( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.

expand(State#D#_#S,All_My_Children) :-
bagof(Child#D1#F#[Move|S],
(D1 is D+1,
move(State,Child,Move),
f_function(Child,D1,F)),
All_My_Children).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%
%%% 8-puzzle solver
%%%
%%%
%%% State have form A/B/C/D/E/F/G/H/I

KJIT/IT/8TH SEM/ Page 15


ARTIFICIAL INTELLIGENCE (2180703)

%%% where {A,...,I} = {0,...,8}


%%% 0 represents the empty tile
%%%

goal(1/2/3/8/0/4/7/6/5).

%%% The puzzle moves

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

%%% the heuristic function


h_function(Puzz,H) :- p_fcn(Puzz,P),
s_fcn(Puzz,S),
H is P + 3*S.

%%% the move

KJIT/IT/8TH SEM/ Page 16


ARTIFICIAL INTELLIGENCE (2180703)

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

%%% the Manhattan distance function


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,1). 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),
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) :- !.
s_aux(_,1).

s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%
%%% 8-puzzle animation -- using VT100 character graphics
%%%
%%%
%%%

KJIT/IT/8TH SEM/ Page 17


ARTIFICIAL INTELLIGENCE (2180703)

puzzle(P) :- solve(P,S),
animate(P,S),
message.

animate(P,S) :- initialize(P),
cursor(1,2), write(S),
cursor(1,22), write('Hit ENTER to step solver.'),
get0(_X),
play_back(S).

:- dynamic location/3. %%% So that location of a tile


%%% can be retracted/asserted.
%%% Location(s) asserted and retracted
%%% by puzzle animator below

initialize(A/B/C/D/E/F/H/I/J) :-
cls,
retractall(location(_,_,_)),
assert(location(A,20,5)),
assert(location(B,30,5)),
assert(location(C,40,5)),
assert(location(F,40,10)),
assert(location(J,40,15)),
assert(location(I,30,15)),
assert(location(H,20,15)),
assert(location(D,20,10)),
assert(location(E,30,10)), draw_all.

draw_all :- draw(1), draw(2), draw(3), draw(4),


draw(5), draw(6), draw(7), draw(8).

%%% play_back([left,right,up,...]).
play_back([M|R]) :- call(M), get0(_X), play_back(R).
play_back([]) :- cursor(1,24). %%% Put cursor out of the way

message :- nl,nl,
write(' ********************************************'), nl,
write(' * Enter 8-puzzle goals in the form ... *'), nl,
write(' * ?- puzzle(0/8/1/2/4/3/7/6/5). *'), nl,
write(' * Enter goal ''message'' to reread this. *'), nl,
write(' ********************************************'), nl, nl.

KJIT/IT/8TH SEM/ Page 18


ARTIFICIAL INTELLIGENCE (2180703)

cursor(X,Y) :- put(27), put(91), %%% ESC [


write(Y),
put(59), %%% ;
write(X),
put(72). %%% M

%%% clear the screen, quickly


cls :- put(27), put("["), put("2"), put("J").

%%% video attributes -- bold and blink not working


plain :- put(27), put("["), put("0"), put("m").
reverse_video :- put(27), put("["), put("7"), put("m").

%%% Tile objects, character map(s)


%%% Each tile should be drawn using the character map,
%%% drawn at 'location', which is asserted and retracted
%%% by 'playback'.
character_map(N, [ [' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ', N ,' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' '] ]).

%%% move empty tile (spot) to the left


left :- retract(location(0,X0,Y0)),
Xnew is X0 - 10,
location(Tile,Xnew,Y0),
assert(location(0,Xnew,Y0)),
right(Tile),right(Tile),right(Tile),
right(Tile),right(Tile),
right(Tile),right(Tile),right(Tile),
right(Tile),right(Tile).

up :- retract(location(0,X0,Y0)),
Ynew is Y0 - 5,
location(Tile,X0,Ynew),
assert(location(0,X0,Ynew)),
down(Tile),down(Tile),down(Tile),down(Tile),down(Tile).

right :- retract(location(0,X0,Y0)),
Xnew is X0 + 10,
location(Tile,Xnew,Y0),

KJIT/IT/8TH SEM/ Page 19


ARTIFICIAL INTELLIGENCE (2180703)

assert(location(0,Xnew,Y0)),
left(Tile),left(Tile),left(Tile),left(Tile),left(Tile),
left(Tile),left(Tile),left(Tile),left(Tile),left(Tile).

down :- retract(location(0,X0,Y0)),
Ynew is Y0 + 5,
location(Tile,X0,Ynew),
assert(location(0,X0,Ynew)),
up(Tile),up(Tile),up(Tile),up(Tile),up(Tile).

draw(Obj) :- reverse_video, character_map(Obj,M),


location(Obj,X,Y),
draw(X,Y,M), plain.

%%% hide tile


hide(Obj) :- character_map(Obj,M),
location(Obj,X,Y),
hide(X,Y,M).

hide(_,_,[]).
hide(X,Y,[R|G]) :- hide_row(X,Y,R),
Y1 is Y + 1,
hide(X,Y1,G).

hide_row(_,_,[]).
hide_row(X,Y,[_|R]) :- cursor(X,Y),
write(' '),
X1 is X + 1,
hide_row(X1,Y,R).

%%% draw tile


draw(_,_,[]).
draw(X,Y,[R|G]) :- draw_row(X,Y,R),
Y1 is Y + 1,
draw(X,Y1,G).

draw_row(_,_,[]).
draw_row(X,Y,[P|R]) :- cursor(X,Y),
write(P),
X1 is X + 1,
draw_row(X1,Y,R).

KJIT/IT/8TH SEM/ Page 20


ARTIFICIAL INTELLIGENCE (2180703)

%%% Move an Object up


up(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
Y1 is Y - 1,
assert(location(Obj,X,Y1)),
draw(Obj).

down(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
Y1 is Y + 1,
assert(location(Obj,X,Y1)),
draw(Obj).

left(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
X1 is X - 1,
assert(location(Obj,X1,Y)),
draw(Obj).

right(Obj) :- hide(Obj),
retract(location(Obj,X,Y)),
X1 is X + 1,
assert(location(Obj,X1,Y)),
draw(Obj).

:- message.

OUTPUT:

KJIT/IT/8TH SEM/ Page 21


ARTIFICIAL INTELLIGENCE (2180703)

KJIT/IT/8TH SEM/ Page 22


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 8
AIM: Write a program to solve travelling salesman problem using Prolog.
The following is the simplified map used for the prototype:

Input:
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

KJIT/IT/8TH SEM/ Page 23


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 9
AIM: Convert following Prolog predicates into Semantic Net
cat(tom).
cat(cat1).
mat(mat1).
sat_on(cat1,mat1).
bird(bird1).
caught(tom,bird1).
like(X,cream) :– cat(X).
mammal(X) :– cat(X).
has(X,fur) :– mammal(X).
animal(X) :– mammal(X).
animal(X) :– bird(X).
owns(john,tom).
is_coloured(tom,ginger).

Input:
isa(tom,cat).
caught(tom,bird).
isownedby(john,tom).
iscoloured(tom,ginger).
like(cat,cream).
saton(cat,mat).
mammal(cat).
isa(bird,animal).
animals(mammals).
have(mammanls,fur).

KJIT/IT/8TH SEM/ Page 24


ARTIFICIAL INTELLIGENCE (2180703)

KJIT/IT/8TH SEM/ Page 25


ARTIFICIAL INTELLIGENCE (2180703)

PRACTICAL: 10
AIM: Write the Conceptual Dependency for following statements.
(a) John gives Mary a book
(b) John gave Mary the book yesterday.
WHAT IS CONCEPTUAL DEPENDENCY?
Conceptual dependency is a theory of how to represent the kind of knowledge about events
that is usually contained in language sentences. The goal is to represent the knowledge in a
way that
• Facilities drawing inferences from sentences.
• Is independent of the language in which the sentences were originally stated.
For example,
➔ I gave the man a book ,can be represented in conceptual dependency as follows:

p to
I ATRANSbook o R
man
from
I
where the symbols have the following meaning:
• Arrows indicate direction of dependency

• Double arrow indicates two may link between actor and the action

• P indicates past tense

• ATRANS is one of the primitive acts used by the theory. it indicates transfer of possession

• 0 indicates the object case relation

• R indicates the recipient case relation

➢ A typical set of primitive acts:


1. ATRANS :- Transfer of an abstract relationship (e.g., give)

2. PTRANS :- Transfer of the physical location of an object (e.g., go)

3. PROPEL :- Application of physical force to an object (e.g., push)

4. MOVE :- Movement of a body part by its owner (e.g., kick)

5. GRASP :- Grasping of an object by an actor (e.g., clutch)

6. INGEST :-Ingesting of an object by an animal (e.g., eat)

7. EXPEL :- Expulsion of something from the body of an animal (e.g., cry)

KJIT/IT/8TH SEM/ Page 26


ARTIFICIAL INTELLIGENCE (2180703)

8. MTRANS :- Transfer of mental information (e.g., tell)

9. MBUILD :- Building new information out of old (e.g., decide)

10. SPEAK :- Production of sounds (e.g., say)

11. ATTEND :- Focusing of sense organ toward a. stimulus (e.g., listen)

➢ A second set of concept dependency building blocks (primitive concept categories):

ACTs Actions
PPs Objects (picture producers)
AAs Modifiers of actions (action aiders)
PAs Modifiers of PPs (picture aiders)

➢ The rules shown in the Figure can be interpreted as follows:

Sr.No. Concept Dependency Concept Dependency English Sentence


building block representation representation of sentence
1. PP  ACT P John ran.
John  PTRANS
2. PP  PA John  height (> average) John is tall.

3. O p o John pushed the


ACT  PP John  PROPEL  cart cart.

4. o PP P oJohn John took the


ACT  John  ATRANS  book from Mary.
< PP ↑o< Mary
Book
5. yesterday John ran
  yesterday.
PP  ACT John  PTRANS
P

KJIT/IT/8TH SEM/ Page 27


ARTIFICIAL INTELLIGENCE (2180703)

QUERIES:

(a) John gives Mary a book.

Po Mary
John ATRANS
↑o < John
book

(b)John gave Mary the book yesterday.

yesterday
o Mary
John ATRANS
↑o < John
book

Input:
owner(john,book).
gives(john,mary,book).
atrans(book).
pp(john).
pp(mary).
gives(pp,atrans,pp).
gave(aa,atrans,pp.yeasterday).

Output:

KJIT/IT/8TH SEM/ Page 28

You might also like