Showing posts with label visual prolog. Show all posts
Showing posts with label visual prolog. Show all posts

Wednesday, June 27, 2012

Eight queen problem with solution in visual prolog (artificial intelligence)


Eight queens problem
Eight queens problem is a constraint satisfaction problem. The task is to place eight queens in the 64 available squares in such a way that no queen attacks each other. So the problem can be formulated with variables x1,x2,x3,x4,x5,x6,x7,x8 and y1,y2,y3,y4,y5,y6, y7,y8; the xs represent the rows and ys the column. Now a solution for this problem is to assign values for x and for y such that the constraint is satisfied.
The problem can be formulated as
P={(x1,y1),(x2,y2),……………………..(x8,y8)}
where (x1,y1) gives the position of the first queen and so on.

So it can be clearly seen that the domains for xi and yi are
Dx = {1,2,3,4,5,6,7,8}and Dy ={1,2,3,4,5,6,7,8} respectively.

The constraints are
i. No two queens should be in the same row,
i.e  yi≠yj for i=1 to 8;j=1 to 8;i≠j
ii. No two queens should be in the same column,
i.e  xi≠xj for i=1 to 8;j=1 to 8;i≠j
iii. There should not be two queens placed on the same diagonal line
i.e  (yi-yj) ≠ ±(xi-xj).

Now a solution to this problem is an instance of P wherein the above mentioned constraints are satisfied.

code in prolog

PREDICATES
DOMAINS
cell=c(integer, integer)
list=cell*
int_list=integer*

PREDICATES
solution(list)
member(integer,int_list)
nonattack(cell,list)

CLAUSES
solution([]).

solution([c(X,Y)|Others]):-
solution(Others),
member(Y,[1,2,3,4,5,6,7,8]),
nonattack(c(X,Y),Others).

nonattack(_,[]).
nonattack(c(X,Y),[c(X1,Y1)|Others]):-
Y<>Y1,
Y1-Y<>X1-X,
Y1-Y<>X-X1,
nonattack(c(X,Y),Others).

member(X,[X|_]).
member(X,[_|Z]):-
member(X,Z).


GOAL
solution([c(1,A),c(2,B),c(3,C),c(4,D),c(5,E),c(6,F),c(7,G),c(8,H)]).

Sunday, June 3, 2012

how to append two list in visual prolog programming


Visual Prolog Program to append two list.


Here the two list are taken by the predicate “append” and returns the appended list. To append two list a list is broken continuously until the last empty list is encountered and finally the other list is appended or joined at the end of first recursively. The code to append is given as follows:

DOMAINS
int_list=Integer *

PREDICATES
append(int_list,int_list,int_list)
CLAUSES

append([],X,Z):-
Z=X.
append([H|T],X,Y):-
append(T,X,Z),Y=[H|Z].
GOAL
append([1,2,3,4,5],[6,7,8,9],X).

how to find factorial in visual prolog programming


Visual Prolog Program to find the factorial of a number.




To find the factorial of a number in Visual Prolog, the number is decreased and the predicate “factorial” is continuously until a Zero is encountered when it returns a value 1. Then the predicate multiplies the returned value and the decremented number continuously. Thus, the factorial of a number is found. The visual prologue code to find the factorial of a number is given below:

PREDICATES
factorial(Integer,Integer)
Clauses
factorial(0,1).
factorial(X,Y):-
X<>0,S=X-1,factorial(S,Y1),Y=X*Y1.

Goal
factorial(5,X).

How to find the hcf using Visual Prolog programming

Visual prolog is a software of artificial intelligence(AI).Before  we start the programming in the visual prolog ,we should install the programand we must write the code in it.and the thing we should consider is wemust save thefile in .PRO file and be placed at the location where it is stored in C: drive.(C: VIP/BIN/WIN/32).

Here is the code  to find the HCF of two numbers in visual prolog:

%TO FIND THE HCF OF TWO NUMBERS



PREDICATES
hcf(integer, integer, integer)

CLAUSES
hcf(X, Y, X):-
Y mod X = 0.
hcf(X,Y,Z):-
S = Y mod X,S<>0, hcf(S, X, Z).

GOAL
hcf(5,10,X).

OUTPUT:
To find the output press the G button on the top of the software. the output of the above program looks like this


CONCEPT:
here the concept is that when the  number is divided exactly by  the smaller number or the divisor,then the divisor is the required hcf.
and if there is remainder in the division, then the hcf of the remainder and the previous divisor is found repeadtly until the remainder is 0,

in the above example,10 %5 is 0 so the HCF is 5.
in 20 and 30, 30%20=10
since remainder is not 0,we should find now HCF of 20 and 10 ,here remiander is 0.so HCF is 10.