AI LAB Record (Final)
AI LAB Record (Final)
DATE:
AIM:
To study introduction about PROLOG. Prolog stands for Programming in Logic: The
language was originally developed in 1972 by Aliancolmerauer and Perouse at the University of
Marseilles in France.
HISTORY OF PROLOG:
Durin the 1970s prolog became popular in Europe for Artifical intelligence Applications
.During there early year both prolog and LISP were very slow in execution and consumed large
amounts of memory. Also user needs considerable programming experience to use a Prolog
program. The picture suddenly changed in 1981 at the Frist International conference on 5th
Generation System in Tokyo. The Japanese were having great difficulties competing with the
rapidly growing U.S. computer market. As a result prolog started in practical Use.
FEATURES OF PROLOG:
It is an object-oriented language.
It uses heuristics to solve problems.
It is most efficient at formal reasoning.
The Prolog system is developed & maintained by Knowledge Engineers.
The Prolog programming is interactive and cycle development process.
PROGRAMMING IN PROLOG:
Data object are the basic blocks. Terms are used to represent the basic data type.
Procedures, clauses and predicates are written in order to implement programming constructs.
Progressive Substitution is used to match the clauses and rules. Recursion Process can also be
implemented in the prolog.
APPLICATIONS OF PROLOG:
In expert systems.
In Natural Language Processing.
In Robotics.
In Gaming and Simulation.
FEATURES OF TURBO PROLOG:
Compile stand alone program that will execute on a machine that Is not running Turbo
Prolog.
A full complement of Standard predicates for many functions like string, file operation
are available.
A functional interface to other language is provides allowing procedural language support
to be added to any Prolog Program.
Declared variables are used to provide more secure development control.
Both integer and real arithmetic are provided.
An integrated editor is provided, making program development, compilation and
debugging very easy.
RESULT:
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
has(jack,apples).
has(ann,plums).
has(dan,money).
fruit(apples).
fruit(plums).
Output:
RESULT:
Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(B) ARITHMETIC ON WIN-PROLOG
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
Arithmetic in Win-Prolog
+ plus - minus
* multiplication ^ power
/ division // integer division
sqrt square root mod N mod M is the remainder N/M
Output:
?- X is 5+4, Y is 5-4.
X=9
Y=1
?- X is 5*4, Y is 2^3.
X = 20
Y=8
?- X is 234556^100.
Error 0:Arithmetic Overflow
?- X is 5/3, Y=5//3.
X=1.66666667
Y=1
?- X is sqrt(3),Y is 3^0.5.
X = 1.73205080756888
Y = 1.73205080756888
?- X is 8 mod 3.
X=2
?- Y is 10^3 * (1+1) + 3.
Y = 2003
?- halt. Exit from Prolog
RESULT:
Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(C) FACTORIAL OF A GIVEN NUMBER
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
fact(0,1).
fact(N,R):- fact(N1,R1),N is N1+1,R is R1*N.
?- fact(4,R).
R = 24
RESULT:
Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(D) ADD TWO NUMBERS
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
start:- sum,nl.
sum:- write('X= '),read(X),
write('Y= '),read(Y),
S is X+Y,
write('Sum is '),write(S).
Output:
?- start.
X= |: 1.
Y= |: 2.
Sum is 3
Yes
RESULT:
Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(E) ARITHMETIC MEAN
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
Arithmetic mean
?- start.
X= 4.
Y= 6.
R is 5
yes
RESULT:
Thus the basic prolog programs have been executed and the output is verified
successfully.
EX NO: 3 MISSIONARIES AND CANNIBALS PROBLEM
DATE:
AIM:
To implement three missionaries and three cannibals find themselves on one side of a
river. They have would like to get to the other side using prolog.
ALGORITHM:
STEP1: start
STEP2: Number of cannibals should lesser than the missionaries on either side.
STEP4: Only one or maximum of two people can go in the boat at a time.
STEP5: All the six have to cross the river from bank.
STEP6: There is no restriction on the number of trips that can be made to reach of the
goal.
STEP7: Both the missionaries and cannibals can row the boat.
STEP8: stop
CODE:
initial_state(mc,mc(left,bank(3,3),bank(0,0))).
final_state(mc(right,bank(0,0),bank(3,3))).
move(mc(left,L,_R),Boat):-
choose_passengers(L,Boat).
move(mc(right,_L,R),Boat):-
choose_passengers(R,Boat).
choose_passengers(bank(C,_M),boat(2,0)):-
C>1.
choose_passengers(bank(_C,M),boat(0,2)):-
M>1.
choose_passengers(bank(C,M),boat(1,1)):-
C>0,M>0.
choose_passengers(bank(C,_M),boat(1,0)):-
C>0.
choose_passengers(bank(_C,M),boat(0,1)):-
M>0.
update(mc(B,L,R),Boat,mc(B1,L1,R1)):-
update_boat(B,B1),
update_banks(Boat,B,L,R,L1,R1).
update_boat(left,right).
update_boat(right,left).
update_banks(alone,_B,L,R,L,R).
update_banks(boat(C,M),left,
bank(LC,LM),bank(RC,RM),
bank(LC1,LM1),bank(RC1,RM1)):-
LM1 is LM-M,
RM1 is RM+M,
LC1 is LC-C,
RC1 is RC+C.
update_banks(boat(C,M),right,
bank(LC,LM),bank(RC,RM),
bank(LC1,LM1),bank(RC1,RM1)):-
LM1 is LM+M,
RM1 is RM-M,
LC1 is LC+C,
RC1 is RC-C.
legal(mc(left,_L,R)):-
\+illegal(R).
legal(mc(right,L,_R)):-
\+illegal(L).
illegal(bank(C,M)):-
M>C.
solve_dfs(State,_History,[step(State)]):-
final_state(State).
solve_dfs(State,History,[step(State,Move)|States]):-
move(State,Move),
update(State,Move,State1),
legal(State1),
\+member(State1,History),
solve_dfs(State1,[State1|History],States).
print_step(step(mc(left,L,R),B)):-
format("~p* ~p~n",[L,R]),
print_step(step(mc(right,L,R),B)):-
format("~p * ~p~n",[L,R]),
print_step(step(mc(left,L,R))):-
format("~p* ~p~n",[L,R]).
print_step(step(mc(right,L,R))):-
format("~p * ~p~n",[L,R]).
print_steps([A|AS]):-
print_step(A),
print_steps(AS).
print_steps([]).
go:-
initial_state(mc,State),
solve_dfs(State,[State],Steps),
print_steps(Steps).
OUTPUT:
1 ?- go.
Bank( 3 , 3 )* bank( 0 , 0 )
->boat( 1 , 1 )->
Bank( 2 , 2 ) * bank( 1 , 1 )
<-boat( 0 , 1 )<-
Bank( 2 , 3 )* bank( 1 , 0 )
->boat( 0 , 2 )->
Bank( 2 , 1 ) * bank( 1 , 2 )
<-boat( 0 , 1 )<-
Bank( 2 , 2 )* bank( 1 , 1 )
->boat( 0 , 2 )->
Bank( 1 , 1 ) * bank( 2 , 2 )
<-boat( 0 , 1 )<-
Bank( 1 , 2 )* bank( 2 , 1 )
->boat( 0 , 2 )->
Bank( 1 , 0 ) * bank( 2 , 3 )
<-boat( 0 , 1 )<-
Bank( 1 , 1 )* bank( 2 , 2)
->boat( 1 , 1 )->
Bank( 0 , 0 ) * bank( 3 , 3 )
true
RESULT
Thus the program for MISSIONARIES AND CANNIBALS PROBLEMS to cross from
one to other with the help of a boat is being implemented using prolog is verified and executed
successfully.
EX NO: 4 GRAPH COLORING
DATE:
AIM:
To implement graph coloring using prolog.
ALGORITHM:
Step 1: Start
Step 7: x is colored for the following for loop if given constraint is satisfied the solution is found
in pair.
coloring_fd(Colors,Sol):-
findall([N1,N2],arc(N1,N2),Arcs),
findall(Node,vertex(Node),L),
length(L,NumNodes),
color2number(Colors,1,C2N),
length(DomainVars,NumNodes),
length(Colors,N),!,
domain(DomainVars,1,N),
contrain(DomainVars,Arcs),
labeling([],DomainVars),
pair(DomainVars,Sol,0,C2N).
print_coloring(Sol),
comp_statistics.
%utilities
print_coloring([]):-write('Done!').
print_soloring([[N,C]|L]):-
write('Node'),write(N),write('gets'),
write('color'),write(C),nl,
print_coloring(L).
color2number([],_,[]).
color2number([A|L],N,[[A,N]|L1]):-
M is N+1,
color2number(L,M,L1).
pair([],[],_,_):- !.
pair([A|L],[[N,C]|L1],N,C2N):-
number2color(C,A,C2N),
N1 is N+1,
pair(L,L1,N1,C2N).
number2color(C,N,[[C,N]|W]).
number2color(C,N,[[C1N1]|W]):-
N\==N1,
number2color(C,N,W).
comp_statistics:-
statistics(runtime,[_,X]),
T is X/1000,
nl,
write('run time:'),
write(T),write('sec.'),nl.
OUTPUT:
1 ?- comp_tatistics.
true.
RESULT:
Thus the program for GRAPH COLORING being implemented using prolog is verified
and executed successfully.
EX.NO: 5 BLOCKS WORLD PROBLEM
DATE:
AIM:
To execute the blocks world problem using SWI Prolog.
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
FORWARD APPROACH:
on(a,b).
on(b,c).
on(c,table).
on(a,table).
on(b,c).
on(c,table).
put_on(A,B) :-
A \== table,
A \== B,
on(A,X),
clear(A),
clear(B),
retract(on(A,X)),
assert(on(A,B)),
assert(move(A,X,B)).
clear(table).
clear(B) :-
not(on(_X,B)).
action :- preconditions,
retract(affected_old_properties),
assert(new_properties).
RECURSION:
r_put_on(A,B) :-
on(A,B).
r_put_on(A,B) :-
not(on(A,B)),
A \== table,
A \== B,
clear_off(A), /* N.B. "action" used as precondition */
clear_off(B),
on(A,X),
retract(on(A,X)),
assert(on(A,B)),
assert(move(A,X,B)).
clear_off(table). /* Means there is room on table */
clear_off(A) :- /* Means already clear */
not(on(_X,A)).
clear_off(A) :-
A \== table,
on(X,A),
clear_off(X), /* N.B. recursion */
retract(on(X,A)),
assert(on(X,table)),
assert(move(X,A,table)).
action :- preconditions or actions,
retract(affected_old_properties),
assert(new_properties).
on(a,b).
on(b,c).
on(c,table).
OUTPUT:
?- put_on(a,table).
yes
?- listing(on), listing(move).
on(b,c).
on(c,table).
on(a,table).
move(a,b,table).
yes
?- put_on(c,a).
no
?- put_on(b,table), put_on(c,a).
yes
CASE 2: RECURSION:
?- r_put_on(c,a).
yes
?- listing(on), listing(move).
on(a,table).
on(b,table).
on(c,a).
move(a,b,table).
move(b,c,table).
move(c,table,a).
Yes
RESULT:
Thus the blocks world problem has been executed and the output is verified
successfully.
EX NO: 6 WATER JUG PROBLEM
DATE:
AIM:
DESCRIPTION:
Given 2jugs, a 41 one and 31 one, neither having any measuring works on it. There is a
pump that can used to fill the jugs with water. Thereby achieving a final state of 21 in jug1 and
jug2.
ALGORITHM:
Step 3: With this successor, search is done successively, back tracking also done.
RULES:
a. (x,y/x<4)-> (4,y)
b. (x,y/y<3)-> (x,3)
c. (x,y/y>0)-> (0,y)
d. (x,y/y>0)-> (x,0)
e. (x,y/x+y>4 & y>0) -> (4,x+y-4)
f. (x,y/x+y>3 & x>0) -> (x+y-3,3)
g. (x,y/x+y<4 & y>0) -> (x+y,0)
h. (x,y/x+y<3 & x>0) -> (0,x+y)
i. (x,y/x>0) -> (x-d,y)
j. (x,y/x>0) -> (x,y-d)
k. (0,2) -> (2,0)
CODE:
min(X,Y,X):-X<Y,!.
min(_,Y,Y).
rev(L,R):-revacc(L,[],R).
revacc([],R,R):-!.
revacc([H|T],A,R):-revacc(T,[H|A],R).
%X,Y are initial contents, Nx,Ny are final contents of jug1 of capacity_and jug2 of capacity My
respectively after pouring from jug1 into jug2
%Given 3 jugs of capacities Mx,My,Mz and filled with X,Y,Z unit of a liquid respectively,give steps so
that finally they contain Fx,Fy,Fz units of the liquid respectively.
jug(Mx,My,Mz,X,Y,Z,Fx,Fy,Fz):-jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,[],['initially']).
jug(_,Fx,_,Fy,_,Fz,Fx,Fy,Fz,T,R):-
!,rev([[Fx,Fy,Fz],[Fx,Fy,Fz]|T],TR),rev(['Finally'|R],RR),display(TR,RR).
jug(Mx,x,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(Mx,X,My,Y,Nx,Ny),not(member([Nx,Ny,Z],T)),jug(Mx,Nx,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Po
ur liquid from jug1 into jug2'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(Mx,Y,Mz,Z,Nx,Nz),not(member([Nx,Y,Nz],T)),jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug1 into jug3'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(Mx,Y,Mz,Z,Ny,Nz),not(member([X,Ny,Nz],T)),jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug2 into jug3'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(My,Y,Mx,X,Ny,Nx),not(member([Nx,Ny,Z],T)),jug(Mx,Ny,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Po
ur liquid from jug2 into jug1'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(Mz,Z,Mx,X,Nz,Nx),not(member([Nx,Y,Nz],T)),jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug3 into jug1'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-
chk(Mz,Z,My,Y,Nz,Ny),not(member([X,Ny,Nz],T)),jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug3 into jug2'|R]).
display([],[]):-!.
display([T1|T],[R1|R]):-write(R1),write(':'),write(T1),nl,display(T,R).
OUTPUT:
1 ?- jug(8 , 5 , 7 , 4 , 2 , 0 , 4 , 4 , 0).
initially : [4 , 2 , 0]
Finally : [4 , 4 , 0].
true
RESULT:
Thus the water jug problem using prolog was executed and the output was verified
successfully.
EX NO: 7 Heuristic Algorithms(A* Algorithm)
DATE:
AIM:
DESCRIPTION:
The A* algorithm is a specialization of best first search it provides the general guide
lines with which to estimate the goal distances for general search graphs. It chooses a successor
with which the shortest estimated distance for expansion. The form of heuristic function for A*
is: f*(n)=g*(n)+h*(n). it then chooses the successor with the shortest estimated distance for
expansion and the process continuous and a goalis forced or the search ends failure.
ALGORITHM:
STEP3: Remove from open the node n that has the smallest value of f(n) if the node is a
good node, return success and stop.
STEP4: Otherwise expand n, generating all its successor’s n and place n on closed. For
every successor n if n is not already open or closed attack a back pointer to n, complete f
x(n) and place it in an open.
STEP6: The biggest solution is get from the user and the nodes are transversed to reach
the goal starts with shortest distance.
STEP7: if the search end fails, then stop and return failure.
CODE:
%, write( [N | P] ).
expand( P, l( N,F/ G),Bound, Tree1, Solved, Sol):- F=<Bound,(
bagof(M/C,(s(N,M,C),\+member(M,P )),Succ),!,
expand([N|P],T,Bound1,T1,Solved1,Sol),continue(p,t(N,F/G,[T1|Ts]),Bound,Tree1,Solved1,Sol
ved,Sol).
expand(_,t(_,_,[]),_,_,never,_):-!.
expand(_,Tree,Bound,Tree,no,_):-f(Tree,F),F>Bound.
continue(_,_,_,_,yes,yes, Sol).
continue(P,t(N,F/G,[T1|Ts]),Bound,Tree1,Solved1,Sol):-
(Solved1=no,insert(T1,Ts,NTs);Solved1=never,NTs=Ts),bestf(NTs,F1),expand(P,t(N,F1/G,NTs)
,Bound,Tree1,Solved,Sol).
succlist(_,[],[]).
succlist(GO,[N/C|NCs],Ts):-G is GO+C,h(N,H),
F is G+H,succlist(GO,NCs,Ts1),insert(l(N,F/G),Ts1,Ts).
insert(T,Ts,[T|Ts]):-f(T,F),bestf(Ts,F1),F=<F1,!.
insert(T,[T1|Ts],[T1|Ts1]):-insert(T,Ts,Ts1).
%Extract f-value
f( l(_,F/_),F).
f( t(_,F/_,_),F).
bestf([T|_],F):-f( T, F).
bestf([],Big):-biggest(Big).
min(X,Y,X):-X=<Y,!.
min(X,Y,Y).
biggest(9898).
goal('Bucharest').
s('Oradea','Zerind',71).
s('Oradea','Sibiu',151).
s('Zerind','Arad',75).
s('Arad','Sibiu',140).
s('Arad','Timisora',118).
s('Timisoara','Lugoj',111).
s('Lugoj','Mehadia',70).
s('Mehadia','Dobreta',75).
s('Dobreta','Craiova',120).
s('Craiova','RV',146).
s('Craiova','Pitesti',138).
s('RV','Sibiu',80).
s('RV','Pitesti',97).
s('Sibiu','Fagarus',90).
s('Fagarus','Bucharest',211).
s('Pitesti','Bucharest',101).
s('Bucharest','Giurgiu',90).
s('Bucharest','Urzium',85).
s('Urzium','Vaslui',142).
s('Vaslui','Iasi',92).
s('Iasi','Neamt',87).
s('Urzium','Hirsova',98).
s('Hirsova','Eforie',86).
s('Neamt','Eforie',2).
s('Eforie','Neamt',2).
s('Eforie','Hirsova',86).
s('Hirsova','Urzium',98).
s('Neamt','Iasi',87).
s('Iasi','Vaslui',92).
s('Vaslui','Urzium',142).
s('Urzium','Bucharest',85).
s('Giurgiu','Bucharest',90).
s('Zerind','Oradea',71).
s('Sibui','Oradea',151).
s('Arad','Zerind',75).
s('Sibiu','Arad',140).
s('Timisoara','Arad',118).
s('Lugoj','Timisoara',111).
s('Mehadia','Lugoj',70).
s('Dobreta','Mehadia',75).
s('Craiova','Dobreta',120).
s('RV','Craiova',146).
s('Pitesti','Craiova',138).
s('Sibiu','RV',80).
s('Pitesti','RV',97).
s('Fagarus','Sibiu',90).
s('Bucharest','Fagarus',211).
s('Bucharest','Pitesti',101).
h('Arad',366).
h('Bucharest',0).
h('Craiova',160).
h('Dobreta',242).
h('Fagarus',176).
h('Lugoj',244).
h('Mehadia',241).
h('Oradea',380).
h('Pitesti',100).
h('RV',193).
h('Sibiu',253).
h('Timisoara',329).
h('Zerind',374).
h('Giurgiu',77).
h('Urzium',80).
h('Vaslui',199).
h('Iasi',226).
h('Neamt',234).
h('Hirsova',151).
h('Eforie',161).
OUTPUT:
1 ?- astar(‘Hirsova’,solution).
[‘Hirsova’Urzium,Bucharest]
RESULT:
Thus the A* algorithm using prolog was executed and the output was verified
successfully.
EX.NO: 8 Representation of Knowledge using
DATE: Propositional Logic and Querying
AIM:
To represent the Knowledge using Propositional Logic concept in SWI Prolog.
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
happy(yolanda).
listens2Music(mia).
listens2Music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2Music(mia).
playsAirGuitar(yolanda):- listens2Music(yolanda).
happy(vincent).
listens2Music(butch).
playsAirGuitar(vincent):-
listens2Music(vincent),
happy(vincent).
playsAirGuitar(butch):-
happy(butch).
playsAirGuitar(butch):-
listens2Music(butch).
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
OUTPUT:
?- woman(mia).
Yes
?- playsAirGuitar(jody).
Yes
?- playsAirGuitar(mia).
No
?- playsAirGuitar(vincent).
No
?- tatooed(jody).
No
?- party.
yes
?- rockConcert.
No
?- playsAirGuitar(mia).
No
playsAirGuitar(mia):- listens2Music(mia).
No
?- playsAirGuitar(yolanda).
Yes
listens2Music(yolanda):- happy(yolanda).
Yes
playsAirGuitar(yolanda):- listens2Music(yolanda).
Yes
?- playsAirGuitar(vincent).
No
?- playsAirGuitar(butch).
Yes
?- woman(X).
X = mia ;
X = jody ;
X = yolanda
RESULT:
Thus the knowledge representation using Propositional Logic and Querying has been
executed and the output is verified successfully.
EX.NO:9 Representation of Knowledge using Predicate
DATE: Logic and Querying
AIM:
To represent the knowledge using Predicate logic in SWI Prolog.
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
eats(fox, chicken).
eats(chicken, corn).
safe(_, []).
safe(_, Others) :- member(human, Others).
safe(Item, [Other|Others]) :- eats(Other, Item),!,fail
;
safe(Item, Others).
allSafe([],_).
allSafe([Item|Items], Others) :- safe(Item, Others), allSafe(Items, Others).
allSafe(X) :- allSafe(X,X).
won([],Right) :-
member(fox, Right),
member(chicken,Right),
member(corn, Right),
member(human, Right).
move(X, [X|Left], Right, NewLeft, [X|Right], A) :- append(Left, A, NewLeft).
move(X, [L|Left], Right, NewLeft, NewRight, A) :- move(X, Left, Right, NewLeft, NewRight,
[L|A]).
move(X, Left, Right, NewLeft, NewRight) :- move(X, Left, Right, NewLeft, NewRight, []).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
move(human, OldLeft, OldRight, NewLeft, NewRight).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
move(human, OldLeft, OldRight, Left, Right),
move(_, Left, Right, NewLeft, NewRight).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
member(human, OldRight),
validMove(OldRight, OldLeft, NewRight, NewLeft).
safeMove(OldLeft, OldRight, NewLeft, NewRight) :-
validMove(OldLeft, OldRight, NewLeft, NewRight),
allSafe(NewLeft),
allSafe(NewRight).
triedAlready(Left, Right, [[L1,R1]|Others]) :-
permutation(Left, L1),
permutation(Right, R1)
;
triedAlready(Left, Right, Others).
tryMoving(Left, Right, Game, Game) :- won(Left, Right).
tryMoving(Left, Right, Game, FinalGame) :-
safeMove(Left, Right, NewLeft, NewRight),
not(triedAlready(NewLeft, NewRight, Game)),
tryMoving(NewLeft, NewRight, [[NewLeft, NewRight]|Game], FinalGame).
playGame(FinalGame) :-
tryMoving([fox, chicken,corn, human],[],[[[chicken, human, fox, corn], []]], Game),
reverse(Game, FinalGame).
printGame([]).
printGame([[Left, Right]|Game]) :-
write(Left),nl,
write(' '),
write(Right),nl,
printGame(Game).
go :- playGame(X),printGame(X).
OUTPUT:
?- go.
[chicken, human, fox, corn]
[]
[fox, corn]
[chicken, human]
[human, fox, corn]
[chicken]
[corn]
[fox, human, chicken]
[chicken, human, corn]
[fox]
[chicken]
[corn, human, fox]
[human, chicken]
[fox, corn]
[]
[chicken, human, fox, corn]
Yes
RESULT:
Thus the knowledge representation using Predicate Logic and Querying has been
executed and the output is verified successfully.
EX.NO:10 Backward Chaining
DATE:
AIM:
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
?- murderer(X).
murderer(X)
hair(X, brown) /* murderer(X) :- hair(X, brown). */
attire(X, pincenez) /* hair(X, brown) :- attire(X, pincenez). */
X = mr_woodley
attire(sir_raymond, tattered_cuffs)
room(sir_raymond, 16)
FAIL (no facts or rules)
FAIL (no alternative rules)
REDO (found one alternative rule)
attire(X, pincenez)
X = sir_raymond
attire(mr_woodley, tattered_cuffs)
room(mr_woodley, 16)
SUCCESS
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond
RESULT:
Thus the backward chaining program using SWI Prolog has been executed and the output
is verified successfully.
EX.NO:11 Unification
DATE:
AIM:
To implement the unification program using SWI Prolog.
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
?- ['unify.pl'].
% unify.pl compiled 0.00 sec, -48 bytes
true.
?- run.
Enter something: test.
The answer is this is a test
true.
?- run.
Enter something: 'test'.
The answer is this is a test
true.
RESULT:
Thus the unification program using SWI Prolog has been implemented and the output is
verified successfully.
EX.NO: 12 Minimax Algorithm
DATE:
AIM:
To implement the minimax algorithm using SWI Prolog.
PROCEDURE:
Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.
PROGRAM:
%column sum
sumcol1([X|_],[Y|_],[Z|_],S):- S is X+Y+Z.
sumcol2([_|[X|_]],[_|[Y|_]],[_|[Z|_]],S):- S is X+Y+Z.
sumcol3([_|[_|[X|[]]]],[_|[_|[Y|[]]]],[_|[_|[Z|[]]]],S):- S is X+Y+Z.
bestrow1(_,_,_,1,1).
bestrow1(_,_,_,2,2).
bestrow1(_,_,_,3,3).
bestrow1([0|_],_,_,4,1).
bestrow1(_,[0|_],_,4,2).
bestrow1(_,_,[0|_],4,3).
bestrow1([_|[0|_]],_,_,5,1).
bestrow1(_,[_|[0|_]],_,5,2).
bestrow1(_,_,[_|[0|_]],5,3).
bestrow1([_|[_|[0|_]]],_,_,6,1).
bestrow1(_,[_|[_|[0|_]]],_,6,2).
bestrow1(_,_,[_|[_|[0|_]]],6,3).
bestrow1([0|_],_,_,7,1).
bestrow1(_,[_|[0|_]],_,7,2).
bestrow1(_,_,[_|[_|[0|_]]],7,3).
bestrow1([_|[_|[0|_]]],_,_,8,1).
bestrow1(_,[_|[0|_]],_,8,2).
bestrow1(_,_,[0|_],8,3).
% os = overall sum
% can we win
% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,11),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.
% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,5),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.
% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,6),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.
makemoverownp([0|X],[5|X]).
makemoverownp([X|[0|Y]],[X|[5|Y]]).
makemoverownp([X|[Y|[0|[]]]],[X|[Y|[5|[]]]]).
makemoverow1p([0|X],[5|X]).
makemoverow2p([X|[0|Y]],[X|[5|Y]]).
makemoverow3p([X|[Y|[0|[]]]],[X|[Y|[5|[]]]]).
makemove1(X,Y,Z,X1,Y,Z,PRIND,1):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(X,X1).
makemove1(X,Y,Z,X,Y1,Z,PRIND,2):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,PRIND,3):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(Z,Z1).
makemove1(X,Y,Z,X1,Y,Z,4,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,4,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,4,3):-makemoverow1p(Z,Z1).
makemove1(X,Y,Z,X1,Y,Z,5,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,5,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,5,3):-makemoverow1p(Z,Z1).
makemove1(X,Y,Z,X1,Y,Z,6,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,6,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,6,3):-makemoverow1p(Z,Z1).
makemove1(X,Y,Z,X1,Y,Z,7,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,7,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,7,3):-makemoverow1p(Z,Z1).
makemove1(X,Y,Z,X1,Y,Z,8,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,8,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,8,3):-makemoverow1p(Z,Z1).
makemove(X,Y,Z,X1,Y1,Z1):-
compsums(X,Y,Z,OS),bestrow(X,Y,Z,OS,PRIND,MOVE),makemove1(X,Y,Z,X1,Y1,Z1,PRIND,MOV
E),!.
userrowmove([0|X],[6|X],1).
userrowmove([X|[0|Y]],[X|[6|Y]],2).
userrowmove([X|[Y|[0|[]]]],[X|[Y|[6|[]]]],3).
usermove(X,Y,Z,1,C,X1,Y,Z):-userrowmove(X,X1,C).
usermove(X,Y,Z,2,C,X,Y1,Z):-userrowmove(Y,Y1,C).
usermove(X,Y,Z,3,C,X,Y,Z1):-userrowmove(Z,Z1,C).
%game is a draw
tictactoe(_,_,_):-write('Game is a draw'),!.
OUTPUT:
| ?- tictactoe([0,0,0],[0,0,0],[0,0,0]).
enter position to mark :
1.
2.
Your move
[0,6,0]
[0,0,0]
[0,0,0]
COMPUTERS MOVE
[5,6,0]
[0,0,0]
[0,0,0]
enter position to mark :
2.
2.
Your move
[5,6,0]
[0,6,0]
[0,0,0]
COMPUTERS MOVE
[5,6,5]
[0,6,0]
[0,0,0]
enter position to mark :
2.
3.
Your move
[5,6,5]
[0,6,6]
[0,0,0]
COMPUTERS MOVE
[5,6,5]
[5,6,6]
[0,0,0]
enter position to mark :
3.
3.
Your move
[5,6,5]
[5,6,6]
[0,0,6]
COMPUTERS MOVE
[5,6,5]
[5,6,6]
[5,0,6]
[5,6,5]
[5,6,6]
[5,0,6]
COMPUTER WINS
RESULT:
Thus the tic-tac-toe game using minimax algorithm is implemented and the output is
verified successfully in SWI Prolog.
EX NO: 13 SPELL CHECKING
DATE:
AIM:
ALGORITHM:
Step 1: Start.
Step 4: The root products are given the sentences as a link & it repeat the status.
Step 5: Stop.
RULES/GRAMMAR:
S->NP VP
NP->the NP
NP-> PRO
NP->PNI
NP-> ADJS N
VP-> V
VP-> V NP
N-> File/Printer
% Adjectives
adj([short]).
adj(short).
adj([long]).
adj(long).
adj([fast]).
adj(fast).
% Verbs
v([printed]).
v(printed).
v([created]).
v(created).
v([want]).
v(want).
% Pronoun
pro([i]).
pro(i).
% Proper noun
pn([bill]).
pn(bill).
% Noun
n([file]).
n(file).
n([printer]).
n(printer).
% ADJS ->ADJADJS|E
adjs([]).
% NP1 ->ADJSN
% NP
np(X):- pro(X).
np(X):- pn(X).
np(X):- np1(X).
% VP
vp(X):-v(X).
%S
% check pred
1 ?- check([the,printer,printed]).
true.
2 ?- check([printer,the]).
true
3 ?- check([the,long,file,created]).
true.
4 ?-
RESULT:
Thus the program for performing syntax checking in prolog was executed and output was
verified successfully.
EX NO: 14 EXPERT SYSTEM FOR MEDICAL DIAGNOSIS
DATE:
AIM:
To design a rule based expert system in PROLOG for medical diagnosis.
DESCRIPTION:
The problem is that a given set of disease and the possible diagnosis are entered in the
database well in advance. Whenever the user enters the name, the symptoms are accepted. From
user add the correct diseases corresponding to the symptoms are given to the user.
ALGORITHM:
Step 1: Start
Step 6: Stop.
CODE:
read(HA),
read(TP),
read(ST),
read(TR),
disease(HA,TP,ST,TR).
OUTPUT:
| ?- run.
Do you have HEADACHE? n.
Do you have TEMPARATURE? y.
Do you have a SORE THROAT? n.
Are you feeling TIRED? n.
You are suffering from DENGU FEVER
true ?;
no
RESULT:
Thus the program for implementing the expert systems for medical diagnosis using
prolog has been executed and the output was verified successfully.