Professional Documents
Culture Documents
AI Assignment PDF
AI Assignment PDF
AI Assignment PDF
W Laboratory Exercise
e
e
k
N
o
.
W Prolog Fundamentals
e Lab Experiments:
e 1. Write a Prolog program to find the sum of two numbers.
k
1 start:- sum,nl.
sum:- write('X= '),read(X),write('Y= '),read(Y),S is X+Y,write('Sum is '),write(S).
2 ?- start.
X= 26.
Y= |: 62.
Sum is 88
true.
6 ?- odd_even(19).
odd number
true.
7 ?- odd_even(16).
even number
true
?- X = 3+2. %just instantiate variable X to ?- 6>5.
value 3+2 X = 3+2. ?- 3+2 = X. X = 3+2. ?- 12<=12.
?- X is +(3,2). ?- 12 =< 7.
?- 5 is 3+2. ?- 3+4 =< 7.
?- 3+2 is 5. ?- 5=\=5. % 5 is not equal to 5.
?- X is 3*2. ?- 5=\=4.
?- X is 3-2. ?- 5=\=(3+2).
?- X is -(2,3). ?- 8=8.
?- X is 5-3-1. ?- 8=:=8. % 8 equal to 8.
?- X is -(5,3,1). %ERROR: is/2: Arithmetic: ?- 8=:=9.
`(-)/3' is not a function ?- (2+1)*10 = 30.
?- X is -(-(5,3),1). ?- (2+1)*10 =:= 30.
?- X is 5-3-1. X = 1 ?- X=2, X<3.
?- X is 3/5. X = 0.6. ?- X=4, X<3.
?- X is 3 mod 5. ?- *(2,3) = 2*3.
?- X is 5 mod 3. ?- X = Z.
?- X is 5^3. ?-(X>3)=(4>3).
?- X is (5^3)^2. ?- X = 3, C is X*X*X.
?- X = (5^3)^2. ?- X = 3, X*X*X is C.
?- 25 is 5^2. ?- is(Y,^(2,2)). % don't type space
?- Y is 3+2*4-1. between 'is' and bracket
W Prolog Fundamentals and Clauses
e Lab Experiments:
e 1. Write a Prolog program to find the maximum value among three values.
k 2. Write a Prolog program to find the factorial of a given number.
2
fact(0,1).
fact(X,Y):-X1 is X-1,fact(X1,Z),Y is X*Z.
5 ?- fact(6,X).
X = 720
3. Write a Prolog program to find the GCD and LCM of given two numbers.
gcd(X, Y, G) :- X = Y, G = X.
gcd(X, Y, G) :- X < Y, Y1 is Y-X,gcd2(X, Y1, G).
gcd(X, Y, G) :- X > Y, gcd(Y, X, G).
lcm(X,Y,LCM):-gcd2(X,Y,GCD), LCM is X*Y//GCD.
?- lcm(12,18,X).
X = 36 .
?- gcd(12,18,X).
X=6.
Lab Assignments:
1. Write a Prolog program to generate a Fibonacci series.
:- dynamic(stored/1).
memo(Goal) :-stored(Goal) -> true;
Goal, assertz(stored(Goal)).
2 ?- fib(7,X).
1, 1, 2,3,5,8,13,
X = 13.
[1] 14 ?- 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
7 ?- power(2,3,R).
R=8.
2. Write a Prolog program to find the circumference and area of a circle.
start:- radius(R),circ(R,C),write('Circumference is '),write(C),nl,area(R,A),write('Area is ') ,write(A).
radius(R):- write('Radius= '),read(R).
circ(R,C):- C is 2*3.14*R.
area(R,A):- A is 3.14*R*R.
3 ?- start.
Radius= 1.
Circumference is 6.28
Area is 3.14
true.
3. Write a Prolog program to exchange of two values of two variables without using 3rd variable.
?-swapfirsttwo([4,13],X).
X=13,4.
Lab Assignments:
1. Write a Prolog program to find the nth Fibonacci number.
fib(0,0).
fib(1,1).
fib(X, Ans) :- Xm1 is X-1 ,Xm2 is X-2 ,fib(Xm1,Sub1), fib(Xm2,Sub2),Ans is Sub1+Sub2.
?- fib(6,X).
X=8.
?- member(x,[w,x]).
true .
?- member(x,[]).
false.
?- range(10,50,L).
L = [10, 11, 12, 13, 14, 15, 16, 17, 18|...] .
[1] 17 ?- inc([24,26,35,68,99],X).
X = [25, 27, 36, 69, 100].
[1] 12 ?- nth(2,[a,b,c,d],R).
R=c.
5. Write a Prolog program to find before and after list value of particular element in list.
?- conc(Before,[may|After],[jan,feb,mar,apr,may,june,july,aug,sep,oct,nov,dec]).
Before = [jan, feb, mar, apr],
After = [june, july, aug, sep, oct, nov, dec] .
?- conc([a,b],[c,d],[a,b,c,d]).
true.
?- conc([a,b],[c,d],[a,b,a,c,d]).
false.
[1] 13 ?- perm([1,2,3],P).
P = [1, 2, 3] ;
P = [2, 1, 3] ;
P = [2, 3, 1] ;
P = [1, 3, 2] ;
P = [3, 1, 2] ;
P = [3, 2, 1] ;
False.
Lab Assignments:
1. Write a Prolog program to decompose a list.
writeall([]).
writeall([A|L]):- write(A),nl,writeall(L).
?- palindrome([a,b,c,b,a]).
true.
?- palindrome([a,b,c,b,e]).
false.
?- join2('Souvik',' Majumdar',S).
S = 'Souvik Majumdar'.
2. Write a Prolog program to count the number of vowels in a list of characters (word).
vowel(X):- member(X,[a,e,i,o,u]).
nr_vowel([],0).
nr_vowel([X|T],N):- vowel(X),nr_vowel(T,N1),N is N1+1,!.
nr_vowel([X|T],N):- nr_vowel(T,N).
[1] 8 ?- nr_vowel([a,r,e,d,i],X).
X = 3.
3. Write a Prolog program to find whether the node of the following graph is connected or not.
edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).
connected(X,Y) :- edge(X,Y) ; edge(Y,X).
Or
connected(X,Y) :- edge(X,Y).
connected(X,Y) :- edge(Y,X).
Lab Assignments:
1. Write a Prolog program to find the last element of a list.
last([X],X).
last([H|T],R):- last(T,R).
?- last([a],R)
R=a
?-last([a,b,1,c],X).
X=c
?-last([a,b,[c,[d]]],X).
X=[c,[d]]
?- my_length([a,b,[c,d],e],R).
R=4
?- my_length([[],[]],R).
R=2
?- my_length([[[]]],R).
R=1
b_sort([],Acc,Acc).
b_sort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),b_sort(NT,[Max|Acc],Sorted).
bubble(X,[],[],X).
bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).
[1] 11 ?- bubble_sort([25,3,65,1,12],A).
A = [1, 3, 12, 25, 65] .
insert_sort(List,Sorted):-i_sort(List,[],Sorted).
i_sort([],Acc,Acc).
i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted).
insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):-X=<Y.
insert(X,[],[X]).
[1] 10 ?- insert_sort([25,3,65,1,12],A).
A = [1, 3, 12, 25, 65] .
merge(A,[],A).
merge([],B,B).
merge([A|Ra],[B|Rb],[A|M]) :- A =< B, merge(Ra,[B|Rb],M).
merge([A|Ra],[B|Rb],[B|M]) :- A > B, merge([A|Ra],Rb,M).
mergesort([],[]). /* covers special case */
mergesort([A],[A]).
mergesort([A,B|R],S) :-split([A,B|R],L1,L2),mergesort(L1,S1), mergesort(L2,S2),merge(S1,S2,S).
split([],[],[]).
split([A],[A],[]).
split([A,B|R],[A|Ra],[B|Rb]) :- split(R,Ra,Rb).
[1] 9 ?- mergesort([4,3,6,5,9,1,7],S).
S = [1, 3, 4, 5, 6, 7, 9] .
Lab Assignments:
1. Write a Prolog program to sort a given list using quick sort.
/* [+,-] */
quicksort([], []).
quicksort([HEAD | TAIL], SORTED) :- partition(HEAD, TAIL, LEFT, RIGHT),
quicksort(LEFT, SORTEDL),
quicksort(RIGHT, SORTEDR),
append(SORTEDL, [HEAD | SORTEDR], SORTED).
/* [+,+,-,-] */
partition(PIVOT, [], [], []).
partition(PIVOT, [HEAD | TAIL], [HEAD | LEFT], RIGHT) :- HEAD @=< PIVOT,
partition(PIVOT, TAIL, LEFT, RIGHT).
partition(PIVOT, [HEAD | TAIL], LEFT, [HEAD | RIGHT]) :- HEAD @> PIVOT,
partition(PIVOT, TAIL, LEFT, RIGHT).
/* [+,+,-] */
append([], LIST, LIST).
append([HEAD | LIST1], LIST2, [HEAD | LIST3]) :- append(LIST1, LIST2, LIST3).
3 ?- quicksort([25,3,4,9],X).
X = [3, 4, 9, 25] .
6 ?- contains([56,8,90,98],98).
true .
7 ?- contains([56,8,90,98],9).
false.
W Tree family in Prolog
e Lab Experiments:
e 1. Write a Prolog program for below tree data relation.
k
7
:- op(500,xfx,'is_parent').
:- op(500,xfx,'is_sibling_of').
X is_sibling_of Y :- Z is_parent X,
Z is_parent Y,
X \== Y.
:-op(500,xfx,'is_same_level_as').
X is_same_level_as X .
X is_same_level_as Y :- W is_parent X,
Z is_parent Y,
W is_same_level_as Z.
:- op(500,xfx,'has_depth').
a has_depth 0 :- !.
Node has_depth D :- Mother is_parent Node,
Mother has_depth D1,
D is D1 + 1.
nl.
path(a). /* Can start at a. */
path(Node) :- Mother is_parent Node, /* Choose parent, */
path(Mother), /* find path and then */
write(Mother),
write(' --> ').
ht(Node,0) :- leaf(Node), !.
ht(Node,H) :- Node is_parent Child,
ht(Child,H1),
H is H1 +1.
max([],M,M).
max([X|R],M,A) :- (X > M -> max(R,X,A) ; max(R,M,A)).
?- h is_sibling_of S.
S=g ;
S=i ;
no
?- t has_depth D.
D=4
?- locate(n).
a --> c --> h --> n
2. Write a Prolog program to represent a family tree and write the predicates for different
relationships like father, mother, brother, sister, cousin, grandmother, grandfather etc.
i. Create a family database
ii. Display Father of?
iii. Display Mother of?
iv. List all brothers of?
v. List all sisters of?
?- start.
input a= 3.
input b= 4.
input c= 5.
These numbers are the edges of a triangle.
yes
Lab Assignments:
1. A binary tree can be defined in terms of 2 predicates:
emptyBT, the empty binary tree.
a. BTTree(N,T1,T2) that is true if N is the root of a binary tree with left subtree T1 and right
subtree T2, where all the items in T1 are less than or equal to N and all the items in T2 are
greater than N.
b. Write a Prolog program that implements the following predicates:
i. insert(I,T1,T2) is true if T2 is the binary tree resulting from I being inserted into binary
tree T1.
ii. preorder(T,L) is true if L is a list of nodes generated by a preorder traversal of the binary
tree T.
iii. inorder(T,L) is true if L is a list of nodes generated by a inorder traversal of the binary tree
T.
iv. postorder(T,L) is true if L is a list of nodes generated by a postorder traversal of the binary
tree T.
v. search(T,I) is true if I is contained in the binary tree T.
vi. height(T,H) is true if H is the height of the binary tree T. An empty tree has height 0 and a
tree with one item has height 1.
W AI Searching Based Problems Using Prolog
e Lab Experiments:
e 1. Write a Prolog program for Map colourings of following figure.
k
8
2. Write a Prolog program to solve the Chess queens challenge puzzle problem.
3. Write a program of the A* algorithm in Prolog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
%%%
%%% A* Algorithm
%%%
%%%
%%% Nodes have form S#D#F#A
%%% where S describes the state or configuration
%%% D is the depth of the node
%%% F is the evaluation function value
%%% A is the ancestor list for the node
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).
Lab Assignments :
1. Write a Prolog program to solve the 8-puzzle problem.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
%%%
%%% A* Algorithm
%%%
%%%
%%% Nodes have form S#D#F#A
%%% where S describes the state or configuration
%%% D is the depth of the node
%%% F is the evaluation function value
%%% A is the ancestor list for the node
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([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#_#_#_|_]).
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
%%% where {A,...,I} = {0,...,8}
%%% 0 represents the empty tile
%%%
goal(1/2/3/8/0/4/7/6/5).
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.
2. Write a Prolog program to solve the 8-Queen problem
%% To use, load in interpreter and call
%%
%% ?- queensSolution(A).
%%
%% To get all solutions at once, call
%%
%% ?- setof(A,queensSolution(A),B).
%%
rank(1).
rank(2).
rank(3).
rank(4).
rank(5).
rank(6).
rank(7).
rank(8).
queens([A,B,C,D,E,F,G,H]) :-queens__([A,B,C,D,E,F,G,H]),queens_([A,B,C,D,E,F,G,H]).
queens__([A]) :- rank(A).
queens__([A|B]) :-rank(A),queens__(B).
queens_([_]).
queens_([A|B]) :- \+ member(A,B),queens_(B).
?- setof(A,queensSolution(A),B), length(B,C).
B=
[[1,5,8,6,3,7,2,4],[1,6,8,3,7,4,2,5],[1,7,4,6,8,2,5,3],[1,7,5,8,2,4,6,3],[2,4,6,8,3,1,7,5],[2,5,7,1,3,8,6,4],[2,5,7,4,1,8,6,
3],[2,6,1,7,4,8,3,5],[2,6,8,3,1,4,7,5],[2,7,3,6,8,5,1,4],[2,7,5,8,1,4,6,3],[2,8,6,1,3,5,7,4],[3,1,7,5,8,2,4,6],[3,5,2,8,1,7,
4,6],[3,5,2,8,6,4,7,1],[3,5,7,1,4,2,8,6],[3,5,8,4,1,7,2,6],[3,6,2,5,8,1,7,4],[3,6,2,7,1,4,8,5],[3,6,2,7,5,1,8,4],[3,6,4,1,8,
5,7,2],[3,6,4,2,8,5,7,1],[3,6,8,1,4,7,5,2],[3,6,8,1,5,7,2,4],[3,6,8,2,4,1,7,5],[3,7,2,8,5,1,4,6],[3,7,2,8,6,4,1,5],[3,8,4,7,
1,6,2,5],[4,1,5,8,2,7,3,6],[4,1,5,8,6,3,7,2],[4,2,5,8,6,1,3,7],[4,2,7,3,6,8,1,5],[4,2,7,3,6,8,5,1],[4,2,7,5,1,8,6,3],[4,2,8,
5,7,1,3,6],[4,2,8,6,1,3,5,7],[4,6,1,5,2,8,3,7],[4,6,8,2,7,1,3,5],[4,6,8,3,1,7,5,2],[4,7,1,8,5,2,6,3],[4,7,3,8,2,5,1,6],[4,7,
5,2,6,1,3,8],[4,7,5,3,1,6,8,2],[4,8,1,3,6,2,7,5],[4,8,1,5,7,2,6,3],[4,8,5,3,1,7,2,6],[5,1,4,6,8,2,7,3],[5,1,8,4,2,7,3,6],[5,
1,8,6,3,7,2,4],[5,2,4,6,8,3,1,7],[5,2,4,7,3,8,6,1],[5,2,6,1,7,4,8,3],[5,2,8,1,4,7,3,6],[5,3,1,6,8,2,4,7],[5,3,1,7,2,8,6,4],[
5,3,8,4,7,1,6,2],[5,7,1,3,8,6,4,2],[5,7,1,4,2,8,6,3],[5,7,2,4,8,1,3,6],[5,7,2,6,3,1,4,8],[5,7,2,6,3,1,8,4],[5,7,4,1,3,8,6,2]
,[5,8,4,1,3,6,2,7],[5,8,4,1,7,2,6,3],[6,1,5,2,8,3,7,4],[6,2,7,1,3,5,8,4],[6,2,7,1,4,8,5,3],[6,3,1,7,5,8,2,4],[6,3,1,8,4,2,7,
5],[6,3,1,8,5,2,4,7],[6,3,5,7,1,4,2,8],[6,3,5,8,1,4,2,7],[6,3,7,2,4,8,1,5],[6,3,7,2,8,5,1,4],[6,3,7,4,1,8,2,5],[6,4,1,5,8,2,
7,3],[6,4,2,8,5,7,1,3],[6,4,7,1,3,5,2,8],[6,4,7,1,8,2,5,3],[6,8,2,4,1,7,5,3],[7,1,3,8,6,4,2,5],[7,2,4,1,8,5,3,6],[7,2,6,3,1,
4,8,5],[7,3,1,6,8,5,2,4],[7,3,8,2,5,1,6,4],[7,4,2,5,8,1,3,6],[7,4,2,8,6,1,3,5],[7,5,3,1,6,8,2,4],[8,2,4,1,7,5,3,6],[8,2,5,3,
1,7,4,6],[8,3,1,6,2,5,7,4],[8,4,1,3,6,2,7,5]]
C = 92
W Search Space roblems in Prolog
e Lab Experiments :
e 1. Write a Prolog program to solve the αβ search for tic-tac-toe game in Prolog.
k
9
:- set_prolog_flag(toplevel_print_options,
:- use_module(minimax).
% bestMove(+Pos, -NextPos)
bestMove(Pos, NextPos) :-
% State is equal to 'play' if not final posit., 'win' if win or 'draw' if draw
% (the first 3 elements are the first line of the board, ...).
% TODO :
% - Try to not test all lines/cols/diags for winning or draw after a move,
% play
play :-
nl,
write('********************'), nl,
playAskColor.
% playAskColor
% Ask the color for the human player and start the game with it.
playAskColor :-
read(Player), nl,
show(EmptyBoard), nl,
).
% play(+Position, +HumanPlayer)
% If next player to play in position is equal to HumanPlayer -> Human must play
show(NextBoard),
write('-> Bad Move !'), nl, % If humanMove fail -> bad move
).
% play(+Position, +HumanPlayer)
show(BestSuccBoard),
).
% nextPlayer(X1, X2)
nextPlayer(o, x).
nextPlayer(x, o).
% move(+Pos, -NextPos)
nextPlayer(X1, X2),
State = play
).
% min_to_move(+Pos)
min_to_move([o, _, _]).
% max_to_move(+Pos)
max_to_move([x, _, _]).
nextPlayer(X1, X2),
State = play
).
number(P),
P1 is P - 1,
% evalPos(+Pos, -Val) :-
% 0 otherwise.
% winningPos(+Player, +Board)
winningPos(P, [X1, X2, X3, X4, X5, X6, X7, X8, X9]) :-
% True if W = X = Y = Z.
equal(X, X, X, X).
% drawPos(+Player, +Board)
drawPos(Board) :-
\+ member(0, Board).
% show(+Board)
% show2(+Term)
show2(X) :-
X = 0, !,
write(' ').
show2(X) :-
write(X).
2. Write a Prolog program to solve the Depth First Search Algorithm in Prolog
3. Write a Prolog program for Hill climbing search.
Lab Assignments :
4. Write a Prolog program to solve the Minimax search and Alpha-Beta Pruning
W Knowledge Based Expert System Using Prolog
e Lab Experiments :
e 1. Write a Prolog program for Knowledge-Based medical diagnostic expert system that is capable of
k diagnosing one of the following medical situations:
1 HIV:
0 - sore_throat
- headache
- fever
- rash
Pregnancy
- fatigue
- vomiting
- light_headedness
- increased_waistline
Flu
- headache
- fever
- tiredness
- nasal_discharge
Your system should function correctly even if the user inputs a synonym of one of the symptoms.
Lab Assignments:
2. Write a Prolog program to solve the Knowledge-Based animal identification for
implementing expert systems.
go :- hypothesize(Animal),
write('I guess that the animal is: '),
write(Animal),
nl,
undo.
/* hypotheses to be tested */
hypothesize(cheetah) :- cheetah, !.
hypothesize(tiger) :- tiger, !.
hypothesize(giraffe) :- giraffe, !.
hypothesize(zebra) :- zebra, !.
hypothesize(ostrich) :- ostrich, !.
hypothesize(penguin) :- penguin, !.
hypothesize(albatross) :- albatross, !.
hypothesize(unknown). /* no diagnosis */
ostrich :- bird,
verify(does_not_fly),
verify(has_long_neck).
penguin :- bird,
verify(does_not_fly),
verify(swims),
verify(is_black_and_white).
albatross :- bird,
verify(appears_in_story_Ancient_Mariner),
verify(flys_well).
/* classification rules */
mammal :- verify(has_hair), !.
mammal :- verify(gives_milk).
bird :- verify(has_feathers), !.
bird :- verify(flys),
verify(lays_eggs).
carnivore :- verify(eats_meat), !.
carnivore :- verify(has_pointed_teeth),
verify(has_claws),
verify(has_forward_eyes).
ungulate :- mammal,
verify(has_hooves), !.
ungulate :- mammal,
verify(chews_cud).
:- dynamic yes/1,no/1.
2 ?- go.
Does the animal have the following attribute: has_hair? no.
3 ?- go.
Does the animal have the following attribute: has_hair? no.
4 ?- go.
Does the animal have the following attribute: has_hair? yes.