Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Standard view
Full view
of .
×
0 of .
Results for:
P. 1
Lab 3 N_Queens Problem in Prolog

# Lab 3 N_Queens Problem in Prolog

Ratings: (0)|Views: 2,414|Likes:

### Availability:

See More
See less

04/15/2013

pdf

text

original

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCESecond Semester 2007-2008EA C461 Artificial IntelligenceLab 3: Solving N-Queens Problem in PROLOGDocument Prepared By:
Mukesh Kumar Rohil, CS & IS Group, BITS, Pilani – 333031 (Rajasthan), Indiarohil@bits-pilani.ac.in
To solve N-Queens problem let us first discuss solution to eight queens’ problem.
1.The eight queen’s problem:
The problem here is to place eight queens on the emptychessboard in such a way that no queen attacks any other queen. A queen can performhorizontal, vertical or diagonal attack.
1.1

Solution 1
: The board position of the queens is represented by their Y-coordinates (i.e.[Y1, Y2, Y3, …, Y8] since to prevent the horizontal attack no two queens cann be in the samerow. Each solution is represented as permutation of the list [1,2,3,4,5,6,7,8]. The program islisted below:
S  o   l  u   t   i  o  n    1
% solution(Queens) if Queens is a list of Y-coordinates of eight non-attacking queenssolution(Queens) :-permutaion([1,2,3,4,5,6,7,8], Queens),safe(Queens). permutation([], []). permutation([Head|Tail], PermList) :-permutation(Tail, PermTail),del(Head, PermList, PermTail). % Here insert Head in permuted Tail%del(Item, List, NewList): deleting Item from List gives NewListdel(Item, [Item|List], List).del(Item, [First|List], [First|List1]) :-del(Item, List, List1).% safe(Queens) if Queens is a list of Y-coordinates of non-attacking queenssafe([]).safe([Queen|Others]) :-safe(Others),noattack(Queen, Others,1).noattack(_, [], _).noattack(Y, [Y1|Ylist], Xlist) :-Y1 – Y =\= Xdist,Y – Y1 =\= Xdist,Dist1 is Xdist + 1,noattack(Y, Ylist, Dist1).

1.2

Solution 2
: Each queen has to be placed on some square; that is; into some column,some row, some upward diagonal, and some downward diagonal. So we will consider a richer representation with four coordinates: x: columns, y:rows, u: upward diagonal, and v: downwarddiagonal. The coordinates are not independent: given x and y, u and v can be computed as u = x –y, v = x + y. The domains for all four dimensions are: Dx = [1, 2, 3, 4, 5, 6, 7, 8], Dy = [1, 2, 3,4, 5, 6, 7, 8], Du = [-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7], and Dv = [2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16]. The program is listed below:
S  o   l  u   t   i  o  n    2
% solution(Queens) if Queens is a list of Y-coordinates of eight non-attacking queenssolution(Ylist) :-sol(Ylist,[1, 2, 3, 4, 5, 6, 7, 8],[1, 2, 3, 4, 5, 6, 7, 8],[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7],[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).sol([], [], Dy, Du, Dv).sol([Y|Ylist], [X|Dx1], Dy, Du, Dv) :-del(Y, Dy, Dy1),U is X – Y,del(U, Du, Du1),V is X + Y,del(V, Dv, Dv1),sol(Ylist, Dx1, Dy1, Du1, Dv1).%del(Item, List, NewList): deleting Item from List gives NewListdel(Item, [Item|List], List).del(Item, [First|List], [First|List1]) :-del(Item, List, List1).
2. The N queen’s problem:
The problem here is to place N queens on the empty N x N sizechessboard in such a way that no queen attacks any other queen.2.1 Solution 1: Use the
solution 1
given for eight queens problem to solve N queens problem.2.2 Solution 2: Use the
solution 2
given for eight queens problem to solve N queens problem.(
Hint:
For both you need a procedure gen(N1, N2, List) which will, for two given intergers N1 and N2, produces the list: List = [N1, N1 + 1, N1 + 2, …., N2 -1, N2]. For solution 2 youneed to define solution(N,S) where N is the size of board, and S is a solution represented asa list of Y-coordinates of N queens)
3.
Compare the time taken by each solution when you take 8, 16, 32 and 64 queens andcomplete the following table.

## Activity (5)

### Showing

AllMost RecentReviewsAll NotesLikes