This action might not be possible to undo. Are you sure you want to continue?

Problem in PROLOG Document Prepared By:

Mukesh Kumar Rohil, CS & IS Group, BITS, Pilani – 333031 (Rajasthan), India rohil@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 empty chessboard in such a way that no queen attacks any other queen. A queen can perform horizontal, 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 same row. Each solution is represented as permutation of the list [1,2,3,4,5,6,7,8]. The program is listed below: Solution 1 % solution(Queens) if Queens is a list of Y-coordinates of eight non-attacking queens solution(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 NewList del(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 queens safe([]). 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: downward diagonal. 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: Solution 2 % solution(Queens) if Queens is a list of Y-coordinates of eight non-attacking queens solution(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 NewList del(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 size chessboard 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 you need to define solution(N,S) where N is the size of board, and S is a solution represented as a list of Y-coordinates of N queens) 3. Compare the time taken by each solution when you take 8, 16, 32 and 64 queens and complete the following table.

Queens 8 16 32 64

Time taken by Solution 1 Solution 2

4. Name your files as L3_YourIDNo_N_S.pl, where YourIDNo is your 11 character BITS id.no. N is number of queens 8 or N, and S is solutions number either 1 or 2. The four files to zipped as L3_YourIDNo.zip and should emailed at rohil@bits-pilani.ac.in before 07:00 PM today.

- m18 Learning
- m14-bayesian
- m13 Uncertainty
- m09 Inference
- m08-fol
- m07-logic
- m6-game-pub
- m06-game
- m5-csp-pub
- m05-csp
- m04-Heuristics Informed Search
- m03 Uninformed Search
- m02 Agents Technology
- m01 Introduction
- Lab 6 Lisp Programming and Working With Lisp Studio
- Lab 5 Best First Search for Scheduling
- Lab 4 Best First Heuristic Search
- Lab 2 Tree Traversal in Prolog
- Lab 1 Introduction to PROLOG
- krepresentation

Sign up to vote on this title

UsefulNot useful- Lab 5 Best First Search for Scheduling
- m01 Introduction
- m07-logic
- m03 Uninformed Search
- m5 Heuristics
- Prolog Lab Sheets
- m14-bayesian
- m18 Learning
- AI_Lab-3
- Lab 6 Lisp Programming and Working With Lisp Studio
- AI_Lab-1
- Lab 2 Tree Traversal in Prolog
- AI-FINAL
- Building Expert Systems in Prolog
- Prolog Lab Manual
- Prolog - Probleme
- Turbo Prolog Owners Handbook
- 99 prolog problems
- AI_Lab_2
- brna
- Prolog
- 8 Queens - Case Study
- DelManzano Hector paper N-Queens's Problem
- Mth 221 Entire Course
- Chad Heiser Annotated Bibliography
- Bab4-How to Use Buttons
- Two-Stage Allocations and the Double Q -Function
- mca
- 01. AI_MEETS_NS_2017.pdf
- Interpreting Wait Events
- 30277062 Lab 3 N Queens Problem in Prolog

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.