0% found this document useful (0 votes)
44 views

Name:-Lavkumar Narendrabhai Patel ENROLLMENT NO.: - 190160116087 Subject: - Ai Practicals Branch: - It (A3) Email

This document contains the code for 10 programs related to artificial intelligence practical assignments, including programs to find elements of lists, check if a number is prime, implement tic-tac-toe, and solve search problems using techniques like BFS, heuristic functions, and A*. It provides the Prolog code and output for each program to solve various AI problems as assignments.

Uploaded by

asd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Name:-Lavkumar Narendrabhai Patel ENROLLMENT NO.: - 190160116087 Subject: - Ai Practicals Branch: - It (A3) Email

This document contains the code for 10 programs related to artificial intelligence practical assignments, including programs to find elements of lists, check if a number is prime, implement tic-tac-toe, and solve search problems using techniques like BFS, heuristic functions, and A*. It provides the Prolog code and output for each program to solve various AI problems as assignments.

Uploaded by

asd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

AI 190160116087

NAME :- LAVKUMAR NARENDRABHAI PATEL


ENROLLMENT NO. :- 190160116087
SUBJECT :- AI PRACTICALS
BRANCH :- IT (A3)
EMAIL :- [email protected]

1 | Page
AI 190160116087

TABLE OF CONTENTS
Contents
PRACTICAL 1 ................................................................................................................ 3
(ii) : Find the last element of a list. ............................................................................. 3
(iii) : Find the K'th element of a list. ............................................................................ 3
(iv) : Find the number of elements of alist. ................................................................... 4
(v) : Reverse a given list ............................................................................................ 4
PRACTICAL 2 ............................................................................................................... 5
(i) : Find out whether a list is apalindrome. ............................................................... 5
(ii) : Duplicate the elements of a list agiven number of times. ...................................... 5
(iii) : Split a list into two parts; thelength of the first part is given. .................................... 6
(iv) : Remove the K'th element from alist. .................................................................... 7
(v) : Insert an element at a givenposition into a list. ..................................................... 7
PRACTICAL 3 ............................................................................................................... 8
(i) : Determine whether a given integernumber is prime or not. .................................... 8
(ii) : Determine the prime factors of agiven positive integer. .......................................... 9
(iii) : Determine whether two positiveinteger numbers are co-prime or not. ............... 10
PRACTICAL 4 ............................................................................................................. 11
Aim: Write a program to implementTic-Tac-Toe game problem. ...................................... 11
PRACTICAL 5 ............................................................................................................. 16
( i ) : Write a program to implement BFS(for 8 puzzle problem or Water Jug problem or any AI
search problem). ........................................................................................................ 16
( ii ): Write a program to implement BFS(for 8 puzzle problem or Water Jug problem or any AI
search problem). ........................................................................................................ 18
PRACTICAL 6 ............................................................................................................. 20
Aim: Write a program to implementSingle Player Game (Using Heuristic Function).......... 20
PRACTICAL 7 ............................................................................................................. 25
Aim: Write a program to solve A*Algorithm.................................................................. 25
PRACTICAL 8 ............................................................................................................. 31
Aim: Write a program to solve N-Queensproblem using Prolog. ................................. 31
PRACTICAL 9 ............................................................................................................. 33
Aim: Write a program to solve 8 puzzleproblem using Prolog. .................................... 33
PRACTICAL 10 ............................................................................................................ 36
Aim: Write a program to solve travellingsalesman problem using Prolog. ...................... 36
PRACTICAL 11 ............................................................................................................. 38
Aim: Develop an expert system for medical diagnosis of childhood diseasesusing prolog.38

2 | Page
AI 190160116087

PRACTICAL 1
(ii) : Find the last element of a list.
PROGRAM

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

OUTPUT

(iii) : Find the K'th element of a list.


PROGRAM

element_at(X,[X|_],1).
element_at(X,[_|L],K) :- K > 1, K1 is K - 1, element_a
t(X,L,K1).

OUTPUT

3 | Page
AI 190160116087

(iv) : Find the number of elements of a


list.
PROGRAM
my_length([],0).
my_length([_|L],N) :- my_length(L,N1), N is N1 + 1.

OUTPUT

(v) : Reverse a given list


PROGRAM
my_reverse(L1,L2) :- my_rev(L1,L2,[]).
my_rev([],L2,L2) :- !.
my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc]).
OUTPUT

4 | Page
AI 190160116087

PRACTICAL 2
(i) : Find out whether a
list is apalindrome.
PROGRAM
is_palindrome(L) :- reverse(L,L).

OUTPUT

(ii) : Duplicate the elements of a list a


given number of times.
PROGRAM
dupli(L1,N,L2) :- dupli(L1,N,L2,N).
dupli([],_,[],_).
dupli([_|Xs],N,Ys,0) :- dupli(Xs,N,Ys,N).
dupli([X|Xs],N,[X|Ys],K) :- K > 0, K1 is K - 1, dupli(
[X|Xs],N,Ys,K1).

5 | Page
AI 190160116087

OUTPUT

(iii) : Split a list into two


parts; thelength of the first
part is given.
PROGRAM
split(L,0,[],L).
split([X|Xs],N,[X|Ys],Zs) :- N > 0, N1 is N - 1, split
(Xs,N1,Ys,Zs).

OUTPUT

6 | Page
AI 190160116087

(iv) : Remove the K'th element from a


list.
PROGRAM
remove_at(X,[X|Xs],1,Xs).
remove_at(X,[Y|Xs],K,[Y|Ys]) :- K > 1,
K1 is K - 1, remove_at(X,Xs,K1,Ys).

OUTPUT

(v) : Insert an element at


a givenposition into a list.
PROGRAM
:- ensure_loaded(p1_20).
insert_at(X,L,K,R) :- remove_at(X,R,K,L).

OUTPUT

7 | Page
AI 190160116087

PRACTICAL 3
(i) : Determine whether a given integer
number is prime or not.
PROGRAM : p1.pl
is_prime(2).
is_prime(3).
is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ ha
s_factor(P,3).

has_factor(N,L) :- N mod L =:= 0.


has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(
N,L2).

OUTPUT

8 | Page
AI 190160116087

(ii) : Determine the prime factors of a


given positive integer.
PROGRAM : p1.pl
prime_factors(N,L) :- N > 0, prime_factors(N,L,2).
prime_factors(1,[],_) :- !.
prime_factors(N,[F|L],F) :-
R is N // F, N =:= R * F, !, prime_factors(R,L,F).
prime_factors(N,L,F) :-
next_factor(N,F,NF), prime_factors(N,L,NF).
next_factor(_,2,3) :- !.
next_factor(N,F,NF) :- F * F < N, !, NF is F + 2.
next_factor(N,_,N).

p2.pl
:- ensure_loaded(p1).
prime_factors_mult(N,L) :- N > 0, prime_factors_mult(N
,L,2).
prime_factors_mult(1,[],_) :- !.
prime_factors_mult(N,[[F,M]|L],F) :- divide(N,F,M,R),
!,
next_factor(R,F,NF), prime_factors_mult(R,L,NF).
prime_factors_mult(N,L,F) :- !,
next_factor(N,F,NF), prime_factors_mult(N,L,NF).

divide(N,F,M,R) :- divi(N,F,M,R,0), M > 0.

divi(N,F,M,R,K) :- S is N // F, N =:= S * F, !,
K1 is K + 1, divi(S,F,M,R,K1).
divi(N,_,M,N,M).

OUTPUT

9 | Page
AI 190160116087

(iii) : Determine whether two positive


integer numbers are co-prime or not.
PROGRAM
gcd(X,0,X) :- X > 0.
gcd(X,Y,G) :- Y > 0, Z is X mod Y, gcd(Y,Z,G).
:- arithmetic_function(gcd/2).

coprime(X,Y) :- gcd(X,Y,1).

OUTPUT

10 | P a g e
AI 190160116087

PRACTICAL 4
Aim: Write a program to implement
Tic-Tac-Toe game problem.
PROGRAM
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
]).

11 | P a g e
AI 190160116087

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

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


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

12 | P a g e
AI 190160116087

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.

13 | P a g e
AI 190160116087

OUTPUT

14 | P a g e
AI 190160116087

15 | P a g e
AI 190160116087

PRACTICAL 5
( i ) : Write a program to implement BFS
(for 8 puzzle problem or Water Jug
problem or any AI search problem).
PROGRAM
%connected(+Start, +Goal, -Weight)
connected(1,7,1).
connected(1,8,1).
connected(1,3,1).
connected(7,4,1).
connected(7,20,1).
connected(7,17,1).
connected(8,6,1).
connected(3,9,1).
connected(3,12,1).
connected(9,19,1).
connected(4,42,1).
connected(20,28,1).
connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).
connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Path) :-


connected2(Current, Next, _),
not(member(Next, Path)).

breadth_first(Goal, Goal, _,[Goal]).


breadth_first(Start, Goal, Visited, Path) :-
findall(X,
(connected2(X,Start,_),not(member(X,Visite
d))),
[T|Extend]),

16 | P a g e
AI 190160116087

write(Visited), nl,
append(Visited, [T|Extend], Visited2),
append(Path, [T|Extend], [Next|Path2]),
breadth_first(Next, Goal, Visited2, Path2).

OUTPUT

17 | P a g e
AI 190160116087

( ii ): Write a program to implement BFS


(for 8 puzzle problem or Water Jug
problem or any AI search problem).
PROGRAM
%connected(+Start, +Goal, -Weight)
connected(1,7,1).
connected(1,8,1).
connected(1,3,1).
connected(7,4,1).
connected(7,20,1).
connected(7,17,1).
connected(8,6,1).
connected(3,9,1).
connected(3,12,1).
connected(9,19,1).
connected(4,42,1).
connected(20,28,1).
connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).
connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Path) :-


connected2(Current, Next, _),
not(member(Next, Path)).

depth_first(Goal, Goal, _, [Goal]).


depth_first(Start, Goal, Visited, [Start|Path]) :-
next_node(Start, Next_node, Visited),
write(Visited), nl,
depth_first(Next_node, Goal, [Next_node|Visited],
Path).

18 | P a g e
AI 190160116087

OUTPUT

19 | P a g e
AI 190160116087

PRACTICAL 6
Aim: Write a program to implement
Single Player Game (Using Heuristic
Function).
PROGRAM
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
]).

20 | P a g e
AI 190160116087

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

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


strt(Brd) :- win(Brd, o), write('AI win!').

21 | P a g e
AI 190160116087

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.

22 | P a g e
AI 190160116087

OUTPUT

23 | P a g e
AI 190160116087

24 | P a g e
AI 190160116087

PRACTICAL 7
Aim: Write a program to solve A*
Algorithm.
PROGRAM
fluent(location(robbie, hallway)).
fluent(location(car-key, hallway)).
fluent(location(garage-key, hallway)).
fluent(location(vacuum-cleaner, kitchen)).
fluent(door(hallway-kitchen, unlocked)).
fluent(door(kitchen-garage, locked)).
fluent(door(garage-car, locked)).
fluent(holding(nothing)).
fluent(clean(car, false)).

fact(home(car-key, hallway)).
fact(home(garage-key, hallway)).
fact(home(vacuum-cleaner, kitchen)).

s0(Situation) :-
setof(S, fluent(S), Situation).

execute_process(S1, [], S1). % Nothing to do


execute_process(S1, [Action|Process], S2) :-
poss(Action, S1), % Ensure valid Process
result(S1, Action, Sd),
execute_process(Sd, Process, S2).

holds(Fluent, Situation) :-
ground(Fluent), ord_memberchk(Fluent, Situation),
!.

holds(Fluent, Situation) :-
member(Fluent, Situation).

25 | P a g e
AI 190160116087

replace_fluent(S1, OldEl, NewEl, S2) :-


ord_del_element(S1, OldEl, Sd),
ord_add_element(Sd, NewEl, S2).

poss(goto(L), S) :-
% If robbie is in X and the door is unlocked
holds(location(robbie, X), S),
( holds(door(X-L, unlocked), S)
; holds(door(L-X, unlocked), S)
).
poss(pickup(X), S) :-

dif(X, robbie), % Can’t pickup itself!


holds(location(X, L), S),
holds(location(robbie, L), S),
holds(holding(nothing), S).
poss(put_away(X), S) :-

holds(holding(X), S),
fact(home(X, L)),
holds(location(robbie, L), S).
poss(drop(X), S) :-

dif(X, nothing),
holds(holding(X), S).
poss(unlock(R1-R2), S) :-

holds(door(R1-R2, locked), S),


holds(holding(R2-key), S),
( holds(location(robbie, R1), S)
; holds(location(robbie, R2), S)
).
poss(lock(R1-R2), S) :-
holds(door(R1-R2, unlocked), S),
holds(holding(R2-key), S),

26 | P a g e
AI 190160116087

( holds(location(robbie, R1), S)
; holds(location(robbie, R2), S)
).
poss(clean_car, S) :-
holds(location(robbie, car), S),
holds(holding(vacuum-cleaner), S).

result(S1, goto(L), S2) :-


holds(location(robbie, X), S1),
replace_fluent(S1, location(robbie, X),
location(robbie, L), Sa),
dif(Item, nothing),
(
holds(holding(Item), S1),
replace_fluent(Sa, location(Item, X),
location(Item, L), S2)
; \+ holds(holding(Item), S1),
S2 = Sa
).
result(S1, pickup(X), S2) :-
replace_fluent(S1, holding(nothing),
holding(X), S2).
result(S1, drop(X), S2) :-
replace_fluent(S1, holding(X),
holding(nothing), S2).
result(S1, put_away(X), S2) :-
replace_fluent(S1, holding(X),
holding(nothing), S2).
result(S1, unlock(R1-R2), S2) :-
replace_fluent(S1, door(R1-R2, locked),
door(R1-R2, unlocked), S2).
result(S1, lock(R1-R2), S2) :-
replace_fluent(S1, door(R1-R2, unlocked),
door(R1-R2, locked), S2).
result(S1, clean_car, S2) :-
replace_fluent(S1, clean(car, false),

27 | P a g e
AI 190160116087

clean(car, true), S2).

goal(clean(car, true)).
goal(door(garage-car, locked)).
goal(door(kitchen-garage, locked)).
goal(location(X, L)) :- fact(home(X, L)).
goal(holding(nothing)).

goal_situation(S) :-
setof(G, goal(G), S).

reached_goal(GoalSituation, Situation) :-
ord_subtract(GoalSituation, Situation, []). % [] -
> no goals not in Situation

list([]).
list([_|T]) :-
list(T).

iterative_deepening_search(Process) :-
s0(S0),
goal_situation(GoalSituation),
list(Process),
execute_process(S0, Process, Result),
reached_goal(GoalSituation, Result).

:- use_module(library(heaps)).

heuristic_distance_to_goal(GoalSituation, Situation, D
istance) :-
ord_subtract(GoalSituation, Situation, Dif),
length(Dif, Distance).

add_to_open_nodes(AccCost, H1, Sit-Process, Goal, H2)


:-
heuristic_distance_to_goal(Goal, Sit, D),

28 | P a g e
AI 190160116087

succ(AccCost, ActCost), % one action has been take


n, so incr
Priority is ActCost + D, % Priority cost
add_to_heap(H1, Priority, ActCost-Sit-Process, H2)
.

open_add_pairs(_, Heap, _, [], _, Heap).


open_add_pairs(AccCost, H1, Sits, [S-P|T], G, H2) :-
( ord_memberchk(S, Sits)
-> add_to_open_nodes(AccCost, H1, S-P, G, Hd)
; Hd = H1
),
open_add_pairs(AccCost, Hd, Sits, T, G, H2).

get_from_open_nodes(H1, Sit-Process, H2) :-


get_from_heap(H1, _Priority, Sit-Process, H2).

a_star(Sit, Process) :-
s0(S0),
goal_situation(GoalSituation),
a_star(S0, GoalSituation, Sit-Answer),
reverse(Answer, Process).
a_star(StartSituation, GoalSituation, Answer) :-
% Create heap of open search nodes
heuristic_distance_to_goal(GoalSituation, StartSit
uation, D),
singleton_heap(Open, D, 0-StartSituation-[]),
% Do the search
a_star(Open, GoalSituation, [StartSituation], Answ
er).
a_star(Open, GoalSituation, Closed, Answer) :-
get_from_open_nodes(Open, AccCost-Sit-Process, Rem
ainingSearch),
( reached_goal(GoalSituation, Sit), Answer = Sit
-Process

29 | P a g e
AI 190160116087

; setof(S-[A|Process], (poss(A, Sit), result(Sit


, A, S)), AS_Pairs),
pairs_keys(AS_Pairs, Children),
ord_union(Closed, Children, Closed1, Sits),
open_add_pairs(AccCost, RemainingSearch, Sits,
AS_Pairs, GoalSituation, Open1),
a_star(Open1, GoalSituation, Closed1, Answer)
).

OUTPUT

30 | P a g e
AI 190160116087

PRACTICAL 8
Aim: Write a program to solve N-Queens
problem using Prolog.
PROGRAM
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], V
C) :-
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
31 | P a g e
AI 190160116087

32 | P a g e
AI 190160116087

PRACTICAL 9
Aim: Write a program to solve 8 puzzle
problem using Prolog.
PROGRAM
goal([1,2,3,
4,0,5,
6,7,8]).
move([X1,0,X3, X4,X5,X6, X7,X8,X9],
[0,X1,X3, X4,X5,X6, X7,X8,X9]).
move([X1,X2,0, X4,X5,X6, X7,X8,X9],
[X1,0,X2, X4,X5,X6, X7,X8,X9]).

%% move left in the middle row


move([X1,X2,X3, X4,0,X6,X7,X8,X9],
[X1,X2,X3, 0,X4,X6,X7,X8,X9]).
move([X1,X2,X3, X4,X5,0,X7,X8,X9],
[X1,X2,X3, X4,0,X5,X7,X8,X9]).

%% move left in the bottom row


move([X1,X2,X3, X4,X5,X6, X7,0,X9],
[X1,X2,X3, X4,X5,X6, 0,X7,X9]).
move([X1,X2,X3, X4,X5,X6, X7,X8,0],
[X1,X2,X3, X4,X5,X6, X7,0,X8]).

%% move right in the top row


move([0,X2,X3, X4,X5,X6, X7,X8,X9],
[X2,0,X3, X4,X5,X6, X7,X8,X9]).
move([X1,0,X3, X4,X5,X6, X7,X8,X9],
[X1,X3,0, X4,X5,X6, X7,X8,X9]).

%% move right in the middle row


move([X1,X2,X3, 0,X5,X6, X7,X8,X9],
[X1,X2,X3, X5,0,X6, X7,X8,X9]).

33 | P a g e
AI 190160116087

move([X1,X2,X3, X4,0,X6, X7,X8,X9],


[X1,X2,X3, X4,X6,0, X7,X8,X9]).

%% move right in the bottom row


move([X1,X2,X3, X4,X5,X6,0,X8,X9],
[X1,X2,X3, X4,X5,X6,X8,0,X9]).
move([X1,X2,X3, X4,X5,X6,X7,0,X9],
[X1,X2,X3, X4,X5,X6,X7,X9,0]).

%% move up from the middle row


move([X1,X2,X3, 0,X5,X6, X7,X8,X9],
[0,X2,X3, X1,X5,X6, X7,X8,X9]).
move([X1,X2,X3, X4,0,X6, X7,X8,X9],
[X1,0,X3, X4,X2,X6, X7,X8,X9]).
move([X1,X2,X3, X4,X5,0, X7,X8,X9],
[X1,X2,0, X4,X5,X3, X7,X8,X9]).

%% move up from the bottom row


move([X1,X2,X3, X4,X5,X6, X7,0,X9],
[X1,X2,X3, X4,0,X6, X7,X5,X9]).
move([X1,X2,X3, X4,X5,X6, X7,X8,0],
[X1,X2,X3, X4,X5,0, X7,X8,X6]).
move([X1,X2,X3, X4,X5,X6, 0,X8,X9],
[X1,X2,X3, 0,X5,X6, X4,X8,X9]).

%% move down from the top row


move([0,X2,X3, X4,X5,X6, X7,X8,X9],
[X4,X2,X3, 0,X5,X6, X7,X8,X9]).
move([X1,0,X3, X4,X5,X6, X7,X8,X9],
[X1,X5,X3, X4,0,X6, X7,X8,X9]).
move([X1,X2,0, X4,X5,X6, X7,X8,X9],
[X1,X2,X6, X4,X5,0, X7,X8,X9]).

%% move down from the middle row


move([X1,X2,X3, 0,X5,X6, X7,X8,X9],
[X1,X2,X3, X7,X5,X6, 0,X8,X9]).

34 | P a g e
AI 190160116087

move([X1,X2,X3, X4,0,X6, X7,X8,X9],


[X1,X2,X3, X4,X8,X6, X7,0,X9]).
move([X1,X2,X3, X4,X5,0, X7,X8,X9],
[X1,X2,X3, X4,X5,X9, X7,X8,0]).

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),
% and that this state leads to the goal
dfs(S2, [S2|Checked], Path).

OUTPUT

35 | P a g e
AI 190160116087

PRACTICAL 10
Aim: Write a program to solve travelling
salesman problem using Prolog.
PROGRAM
road(birmingham,bristol, 9).
road(london,birmingham, 3).
road(london,bristol, 6).
road(london,plymouth, 5).
road(plymouth,london, 5).
road(portsmouth,london, 4).
road(portsmouth,plymouth, 8).

get_road(Start, End, Visited, Result) :-


get_road(Start, End, [Start], 0, Visited, Result).
get_road(Start, End, Waypoints, DistanceAcc, Visited,
TotalDistance) :-
road(Start, End, Distance),
reverse([End|Waypoints], Visited),
TotalDistance is DistanceAcc + Distance.
get_road(Start, End, Waypoints, DistanceAcc, Visited,
TotalDistance) :-
road(Start, Waypoint, Distance),
\+ member(Waypoint, Waypoints),
NewDistanceAcc is DistanceAcc + Distance,
get_road(Waypoint, End, [Waypoint|Waypoints], NewD
istanceAcc, Visited, TotalDistance).

OUTPUT

36 | P a g e
AI 190160116087

37 | P a g e
AI 190160116087

PRACTICAL 11
Aim: Develop an expert system for
medical diagnosis of childhood diseases
using prolog.
PROGRAM
:- use_module(library(jpl)).
start :-sleep(0.4),
write('
'),nl,
sleep(0.4),
write('***************************************
**************************'),nl,
sleep(0.2),
write("###################||| EXPERT SYSTEM ||
|#########################"),nl,
sleep(0.4),
write('***************************************
**************************'),nl,
sleep(0.4),
write('
'),nl,nl,nl,

/*write("Hi. How are you? First of all tell me


your name Please : "),
read(Patient),*/

interface2.

/* hypothesis(Patient,Disease),
write(Patient),write(', you '), write(' probab
ly have '),write(Disease),write('.'),undo,
nl,nl,nl,
sleep(0.7),

38 | P a g e
AI 190160116087

write('***************************************
**************************'),nl,
sleep(0.4),
write("################||| THANK YOU FOR USE M
E |||#####################"),nl,
sleep(0.4),
write('***************************************
**************************'),nl.*/

symptom(Patient,fever) :- verify(Patient," have a


fever (y/n) ?").

symptom(Patient,rash) :- verify(Patient," have a r


ash (y/n) ?").

symptom(Patient,headache) :- verify(Patient," have


a headache (y/n) ?").

symptom(Patient,runny_nose) :- verify(Patient," ha
ve a runny_nose (y/n) ?").

symptom(Patient,conjunctivitis) :- verify(Patient,
" have a conjunctivitis (y/n) ?").

symptom(Patient,cough) :- verify(Patient," have a


cough (y/n) ?").

symptom(Patient,body_ache) :- verify(Patient," hav


e a body_ache (y/n) ?").

symptom(Patient,chills) :- verify(Patient," have a


chills (y/n) ?").

symptom(Patient,sore_throat) :- verify(Patient," h
ave a sore_throat (y/n) ?").

39 | P a g e
AI 190160116087

symptom(Patient,sneezing) :- verify(Patient," have


a sneezing (y/n) ?").

symptom(Patient,swollen_glands) :- verify(Patient,
" have a swollen_glands (y/n) ?").

/*symptom(_,"Sorry, I don't seem to be able to dia


gnose the disease.").*/

hypothesis(Patient,measles) :-
symptom(Patient,fever),
symptom(Patient,cough),
symptom(Patient,conjunctivitis),
symptom(Patient,runny_nose),
symptom(Patient,rash).

hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).

hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,conjunctivitis),
symptom(Patient,chills),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,cough).

hypothesis(Patient,common_cold) :-
symptom(Patient,headache),

40 | P a g e
AI 190160116087

symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,chills).

hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,chills),
symptom(Patient,body_ache),
symptom(Patient,rash).

hypothesis(Patient,measles) :-
symptom(Patient,cough),
symptom(Patient,sneezing),
symptom(Patient,runny_nose).

hypothesis(_,"disease. But I'm Sorry, I don't seem


to be able to diagnose the disease").

response(Reply) :-
read(Reply),
write(Reply),nl.

ask(Patient,Question) :-
write(Patient),write(', do you'),write(Question),
/*read(N),
( (N == yes ; N == y)
->
assert(yes(Question)) ;
assert(no(Question)), fail),*/

interface(', do you',Patient,Question),

41 | P a g e
AI 190160116087

write('Loading.'),nl,
sleep(1),
write('Loading..'),nl,
sleep(1),
write('Loading...'),nl,
sleep(1),
nl.

:- dynamic yes/1,no/1.

verify(P,S) :-
(yes(S)
->
true ;
(no(S)
->
fail ;
ask(P,S))).

undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.

pt(Patient):-

hypothesis(Patient,Disease),
interface3(Patient,', you probably have ',Dise
ase,'.'),
write(Patient),write(', you probably have '),w
rite(Disease),write('.'),undo,end.

end :-
nl,nl,nl,
sleep(0.7),
write('***************************************
**************************'),nl,
42 | P a g e
AI 190160116087

sleep(0.4),
write("################||| THANK YOU FOR USE M
E |||#####################"),nl,
sleep(0.4),
write('***************************************
**************************'),nl.

interface(X,Y,Z) :-
atom_concat(Y,X, FAtom),
atom_concat(FAtom,Z,FinalAtom),
jpl_new('javax.swing.JFrame', ['Expert System'], F
),
jpl_new('javax.swing.JLabel',['--- MEDICAL EXPERT
SYSTEM ---'],LBL),
jpl_new('javax.swing.JPanel',[],Pan),
jpl_call(Pan,add,[LBL],_),
jpl_call(F,add,[Pan],_),
jpl_call(F, setLocation, [400,300], _),
jpl_call(F, setSize, [400,300], _),
jpl_call(F, setVisible, [@(true)], _),
jpl_call(F, toFront, [], _),
jpl_call('javax.swing.JOptionPane', showInputDialo
g, [F,FinalAtom], N),
jpl_call(F, dispose, [], _),
write(N),nl,
( (N == yes ; N == y)
->
assert(yes(Z)) ;
assert(no(Z)), fail).

interface2 :-
jpl_new('javax.swing.JFrame', ['Expert System'], F
),
jpl_new('javax.swing.JLabel',['--- MEDICAL EXPERT
SYSTEM ---'],LBL),
jpl_new('javax.swing.JPanel',[],Pan),

43 | P a g e
AI 190160116087

jpl_call(Pan,add,[LBL],_),
jpl_call(F,add,[Pan],_),
jpl_call(F, setLocation, [400,300], _),
jpl_call(F, setSize, [400,300], _),
jpl_call(F, setVisible, [@(true)], _),
jpl_call(F, toFront, [], _),
jpl_call('javax.swing.JOptionPane', showInputDialo
g, [F,'Hi. How are you? First of all tell me your name
please'], N),
jpl_call(F, dispose, [], _),
/*write(N),nl,*/
( N == @(null)
-> write('you cancelled'),interface3('you can
celled. ','Thank you ','for use ','me.'),end,fail
; write("Hi. How are you? First of all tell
me your name please : "),write(N),nl,pt(N)
).

interface3(P,W1,D,W2) :-
atom_concat(P,W1, A),
atom_concat(A,D,B),
atom_concat(B,W2,W3),
jpl_new('javax.swing.JFrame', ['Expert System'], F
),
jpl_new('javax.swing.JLabel',['--- MEDICAL EXPERT
SYSTEM ---'],LBL),
jpl_new('javax.swing.JPanel',[],Pan),
jpl_call(Pan,add,[LBL],_),
jpl_call(F,add,[Pan],_),
jpl_call(F, setLocation, [400,300], _),
jpl_call(F, setSize, [400,300], _),
jpl_call(F, setVisible, [@(true)], _),
jpl_call(F, toFront, [], _),
jpl_call('javax.swing.JOptionPane', showMessageDia
log, [F,W3], N),

44 | P a g e
AI 190160116087

jpl_call(F, dispose, [], _),


/*write(N),nl,*/
( N == @(void)
-> write('')
; write("")
).

help :- write("To start the expert system please type


'start.' and press Enter key").

45 | P a g e
AI 190160116087

OUTPUT

46 | P a g e
AI 190160116087

47 | P a g e
AI 190160116087

48 | P a g e
AI 190160116087

49 | P a g e
AI 190160116087

50 | P a g e
AI 190160116087

51 | P a g e

You might also like