Artificial Intelligence (2180703) : Practical: 1
Artificial Intelligence (2180703) : Practical: 1
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]).
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]).
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.
OUTPUT:
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:
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:
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).
OUTPUT:
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.
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#_#_#_|_]).
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
goal(1/2/3/8/0/4/7/6/5).
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).
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
%%%
%%%
%%%
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).
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.
%%% 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.
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),
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).
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_row(_,_,[]).
draw_row(X,Y,[P|R]) :- cursor(X,Y),
write(P),
X1 is X + 1,
draw_row(X1,Y,R).
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:
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).
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:
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.
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#_#_#_|_]).
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
goal(1/2/3/8/0/4/7/6/5).
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).
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
%%%
%%%
%%%
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).
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.
%%% 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.
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),
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).
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_row(_,_,[]).
draw_row(X,Y,[P|R]) :- cursor(X,Y),
write(P),
X1 is X + 1,
draw_row(X1,Y,R).
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:
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)
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
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).
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
• ATRANS is one of the primitive acts used by the theory. it indicates transfer of possession
ACTs Actions
PPs Objects (picture producers)
AAs Modifiers of actions (action aiders)
PAs Modifiers of PPs (picture aiders)
QUERIES:
Po Mary
John ATRANS
↑o < John
book
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: