Σχόλια 0

Το κείμενο του εγγράφου

The N-Queens Six-way Solver

Dackral Phillips

Auburn University

phillds@eng.auburn.edu

Abstract

The N-Queens problem is a classical artificialintelligence constraint satisfaction problem (CSP).Many methods have been proposed to find solutionsto the problem, and most of these involve searchingschemes. In my approach to solving this problem, Iused six different solution-finding algorithms, andmake a comparison as to which works the best. Thesix solutions implemented are an O(N) approach firstproposed in ACM SIGART Bulletin Vol. 2(2) page 7,a brute force backtracking algorithm, a backtrackingalgorithm with look-ahead capabilities, a steepest-ascent hill-climber, a next-ascent hill-climber, and a

The N-Queens problem is a classic artificialintelligence problem that has been used as abenchmark

for many artificially intelligentalgorithms. It’s combinatorial nature makes theproblem a perfect candidate for algorithmbenchmarking simply due to the shear amount oftime it takes to conduct an exhaustive search on thesolution space. In this paper, I present algorithmimplementations that solve the N-Queens problem.

2. Problem Description

2.1 General Problem Layout

The format of the N-Queens problem is asfollows. On a chessboard of dimensionsn

xn, a setofnQueens must be positioned on theboard, suchthat no queen is attacking any other queen. Queenscan attack according to normal rules of chess, namelyvia rows (referred to as ranks in chess terminology),columns (files), as well as along diagonals. Onlyboards of size four or higher have solutions, and thereis usually more than one solution per board, ofcourse, some of these solutions are mirrors of others.The solution to the four-queen problem is an exampleof one such board that has only one solution that canbe mirrored only. All

subsequent boards have morethan one arrangement. As queens are placed on theboard, the lines of force that they exhibit down theranks, files, and diagonals drastically decreases thesearch space as they are added, such that whenn–

1

queens have been

placed, only one position is leftopen for the remaining queen in the case of asolution. In the case of a board not having a solution,the entire board has been placed in danger when then–

1th

queen has been placed, if not before.

2.2 Mathematical Implications

As previously mentioned, the N-Queens problemis a combinatorial problem. It has a simple andregular structure, and because of this, it is frequentlyused to test new AI strategies [2]. Most researchdone on the N-Queens problem has been ona singleprobable solution. In my implementation, however, Idiversify by trying several different algorithms.

3. Problem Approach

My initial approach to the problem was to pullout my trusty chessboard and solve the problemsmanually forn<= 8. I was specifically looking forpatterns in the way the queens were placed on theboard. When I examined the solutions forn= 4, andn

= 6, I thought I had found the pattern for which Ihad searched, unfortunately the pattern broke forn=8. Below are figures demonstrating my patternsearching findings.

Figure 1. Solution to the 4-queen problem

Figure 2. Solution to the 6-queen problem

It would appear as if the solution to all evenboard problems would be to place a queen on row 1,column 2, andthe subsequent queens a knight’s jumpapart (2 squares down, 1 square right). Untiln/ 2queens have been placed, then repeat the processfrom (n

/ 2) + 1 ton, this time beginning with column1 rather than 2. This pattern does seem to hold withmost boards, however, the pattern breaks onn

= 8,due to queens being in conflict with each other alongdiagonals. This break in the pattern seems to occurwhenevern

Odd boards appear to work the same way as theireven counterparts, with then

–

1 pattern beingapplied for queens 1 ton. On queenn, the queen canbe placed on the last square of the column. Iapparently was not the first person to stumble uponthis pattern. On further research, I found that ACMSIGART Bulletin Vol. 2(2) page 7 gives an algorithmthat solves the N-Queens problem in O(N) time basedon these very patterns.

4. Program Setup and Layout

Java was my language of choice for thisassignment, namely for its platform independence,but also to help reinforce my learning of thelanguage. I began by creating three object classes forsolving the problem: ChessSquare, Board, andQueen.

4.1 ChessSquare Class

The ChessSquare class defines

the basicfundamental unit of the chessboard–

a chess square.Included in this class are two Boolean variable,checked and queenOn. The checked variable is usedto determine whether a square is in danger. If aqueen places a square in danger, this Booleanvariable is toggled to true. The queenOn variable isused to tell whether or not a queen has been placedon the square. Initially, both of these variables arefalse. The following methods are a part of this class:

checkSquare()–

sets the check variable to true.

placeQueen()–

sets the check variable to true and the

queenOn variable to true.

unCheckSquare()–

sets the check variable to false.

removeQueen()–

sets the queenOn and checked

variables to false.

getChecked()–

returns the value of checked.

getQueenOn()–

returns the value of queenOn.

toString()–

prints out variable values--

added for

debugging purposes.

4.2 Queen Class

The queen class is used to define some essentialqueen characteristics, as well as keep up with theposition on

which a queen currently is. This classincludes three variables and one array. The integervariables xCoord and yCoord are used to keep trackof the position at which a queen currently is. ABoolean variable, placed, tells whether or not a givenqueenhas been successfully placed on the board, andthe integer array free is used by the look-aheadalgorithm to keep track of the next free square for aqueen on a given file. The class contains thefollowing methods:

setQueen()–

places a queen at an x, ycoordinate and

marks placed as true.

removeQueen()–

removes a previously placed queen.

getX()–

returns the Queen’s x coordinate value.

getY()–

returns the Queen’s y coordinate value.

getPlaced()–

returns the value of placed.

resetFree()–

returns thefree array to an unused state.

setFree()–

sets a position in the free array.

getFree()–

returns the next free position in the free

array.

toString()–

prints out variable values--

added for

debugging purposes.

4.3 Board Class

The board class is the class that defines what achessboard is, as well as contains the algorithms Ihave developed. For this class, there is a singleinteger variable, n, which defines how large the boardis for a particular problem. A two-dimensional arrayof ChessSquares

called matrix and an array of queensare also defined in the Board class. The classcontains the following methods:

createLinesOfForce()–

marks squares as endangered

when a queen is placed.

removeLinesOfForce()–

removes marked lines of

force.

placeQueen()–

place a queen on a location.

removeQueen()–

remove a queen from a location.

isSolution()–

tests whether a given arrangement of

queens on the board is a solution.

fillFree()–

used in the look-ahead algorithm to place

free squares in the free array.

whoopie()–

function that performs genetic mating.

fitness()–

function to calculate how close to a

solution the current board is.

createBoardFromArray()–

creates a chess board

from a one dimensional

array.

orderN()–

solves the problem in O(N). Algorithm

adapted from [1].

modifiedBacktrack()–

brute force back-tracking

algorithm.

lookAhead()–

backtracking with arc-consistent look

ahead techniques implemented.

nextHillClimb()–

performs the next-ascent hill

climbing algorithm.

steepHillClimb()–

performs the steepest-ascent

hill-climbing algorithm.

geneticSearch()–

performs the genetic search

Algorithm.

toString()–

prints out variable values--

added for

debugging purposes.

main()–

gets the problem solving routine going. It

offers a menu whereby a user can input n

and the way in which he/she desires to solvethe problem.

5. Algorithms Implemented

I implemented six different solutions on thesystem I designed. The first is an adaptation of theO(N) solution mentioned. I found information aboutthis algorithm at a page created by Marty Hall at JohnHopkins University [1]. I tailored Hall’s solution togo along with the Java classes and methods that Iauthored. The second algorithm is a simple bruteforce backtracking algorithm with a step saving firstmove. The third is an extension of this simplebacktracking algorithm, which implements look-ahead arrays to keep track of free squares. These firstthreesolutions provide a solution quickly and easily,however, due to the nature of the algorithms, thesame solution is presented every time. Because thereare multiple random solutions to larger boards, Iwanted to write a set of algorithms that would givedifferent solutions to the same size problems. Toaccomplish this goal, I created a next-ascent hillclimber, a steepest-ascent hill climber, and a geneticsearch algorithm.

5.1 O(N) Algorithm

As previously stated, this algorithm comesdirectly from an article that appeared in ACMSIGART Bulletin Vol. 2(2) on page 7. I found animplementation of the algorithm by Marty Hall atJohns Hopkins University, and modified his publicdomain source code to work with my classes andalgorithms. The solution given is done in O(N),however, only one solution can be presented for agiven board size due to the formulas used to computethe position at which the queen should be placed [1].

5.2 Simple Backtracking

I created this algorithm to go through the boarduntil asolution is reached, or until a dead-end isfound. When a dead-end occurs, the algorithmbacktracks to a previous file and moves the queen toa different rank. One thing I noticed about thesolutions to the N-Queens problems I have examinedis that a solution generally cannot be found on thefirst try when the first queen is placed on the firstrank of the first file. For this reason, I added a smalltime saving step by forcing the queen to begin at themiddle of the board or the (n/ 2)th square for boardswheren

is even, and the ((n+ 1) / 2)th square for oddsized boards. Like the O(N) solution, however, thisonly gives the same solution for a board over andover again.

5.3 Simple Backtracking with Look-Ahead

This algorithm is an extension of the last one,however, instead of trying every square by bruteforce repeatedly, I use an array to keep track of thefree ranks in a given file. When a backtrack occurs,the next free square is used, instead of brute forcechecking each square. On larger-sized boards, thissaves processing time. This solution gives the exactsame answer to a given board as the previousalgorithm, which again, does not lend itself to muchoriginality.

5.4 Next-Ascent Hill-Climbing

In order to generate more diverse solutionsto theN-Queens problem, rather than the same solutionrepeatedly, I created the last three algorithms withrandomization in mind. This algorithm randomlygenerates a parent array, as well as (n

* 2) childrenarrays. A fitness function is run on the parent arrayand the array is then copied to all the children arrays.One bit is flipped in per child to make it differentfrom the parent. A new fitness is computed for thechild, and the first child that is better than the originalparent becomes the new parent. The fitness of aparticular solution is measured by placing all thequeens on a board and then counting the number ofqueens that are in conflict with each other.

One potential problem with this algorithm is theability for the parents and children to reach a localmaximum. This occurs when none of the childrenare better than the parent, yet a solution has not beenreached (isSolution() returns false). In order to dealwith this possibility, I have added a breakout routine,which practically guarantees a solution will bereached. I tried three different solutions.

First, I multiplied the parent fitness by a randomconstant between one andn, and the algorithm isautomatically run again. I started thinking that it waspractically useless to multiply the fitness by one, asthe exact same predicament is reached.

In order to simplify matters, my second attemptwas to simply multiply the parent by two. In all ofmy experimental results, a solution was obtainedafter both of these breakout routineswereimplemented, however, I feared that cycles may be apossibility in this implementation, due to the fact thatthey appeared in the next algorithm I describe and soI decided to do a little further work.

For my third and final attempt, I did not try to

manipulate the fitness of the parent at all. Instead, Idecided to cast lots for the next parent, and pseudo-randomly select the new parent, such that if nochildren turned out to be better than the parent, Irandomly picked one and made it the new parent.This seems to work quite well, and so it is thebreakout implementation that is currently in mysource code.

5.5 Steepest-Ascent Hill-Climbing

The same basic setup is used as in the next-ascent hill-climbing algorithm. The only differencebeing that

instead of taking the first child that isbetter than the parent, the best overall child is takento be the next parent.

Again, the main problem with theimplementation has been the capability of a localmaximum to be reached, thus causing the solution-setto be “stuck.” I implemented the same breakoutroutines as in the previous hill-climber.

The first two attempts appeared to haveabsolutely no effect on the algorithm. More thanlikely, what occurred is that an infinite cycle wascreated, in whicha child was picked to be the newparent, after the original parent’s fitness waschanged. The children of this new parent includedthe previous parent, whose fitness value was betterthan all of the other children in the group, causing acycle.

To deal with this problem, I again decided to letthe computer pseudo-randomly pick which child touse as the new parent in order to break out of thelocal maximum. This appeared to have very goodresults. The algorithm terminates successfully almostalways.

5.6 Genetic Algorithm

The last algorithm, a genetic search implementsDarwinian survival of the fittest. At the outset of thealgorithm, (n* 2) parents are randomly generated.Two parents are then selected at random and a childis produced by randomly taking bits from each parenton a probabilistic basis. Each bit has a 50% chanceof being extracted from the father, and 50% from themother. After the bit has been set, I have included achance for random mutation such that each bit has a10% chance of being reversed. This insures that evenif two parents are exactly the same, a child has someprobability of being diverse from the parent.

At the outset of this problem, I wanted to try toget the best possible child to be spawned from theoverall genepool. I found, however, that theselection of only the best parents tends to have thechildren clump at local maxima, and so I quicklyabandoned this scheme for a more interesting one.Instead of picking the best parents, I switched myfocus to have fate, again, decide which parentsshould mate. I began selecting two parents at randomand mating them. The results were very good, withlittle sticking at local maxima.

I had considered stopping the algorithm after aset number of generations, but some ofthe largerboards I tried (n>= 8) were taking several hundredsof thousands of generations to complete, and so Idecided to remove this constraint.

6. Experimentation

In order to test my algorithms, I conducted aseries of tests on boards that range insize from fourto 10, as well as a 20-queen board. The test system isdescribed below as well as the results I obtained.

6.1 Architecture Used

The platform used to benchmark the algorithmswas an AMD Athlon Thunderbird processor runningat 1 GHz with 512 MB of RAM. The program wasexecuted on the hard drive, a Western Digital 60 GBCaviar drive running at 5400 RPMs. Theenvironment used to run the Java program wasjGRASP, a programming environment developed byJames Cross and Larry Barowski of AuburnUniversity. The operating system used was Windows2000 Advanced Server.

6.2 Experimental Setup

I recorded the time taken to execute a givenalgorithm, as well as the total number of backtracks,iterations, or generations required to find a solution inaMicrosoft Excel spreadsheet. I also used Excel tocalculate the averages of several runs, to give anoverall view of how efficient the system is. Alltiming was done on the stopwatch function of mywristwatch (a Casio G-Shock), and nothing wasrunning at

the time of testing except for backgroundprocesses and Winamp MP3 player, which did notseem to have an effect on my results. On the O(N),Backtracking, and Look Ahead algorithms, I did 10executions of each board ranging from four to 10queens. I also

did a larger 20-queen board to see howwell the algorithms would perform.

Due to efficiency constraints, I could notcomplete as much testing for the last threealgorithms. So, for the hill-climbing algorithms, Icompleted 10 executions of each board ranging fromfour to 10 queens, and dispensed with the larger 20-queen board.

The genetic algorithm turned out to be extremelytime consuming. Boards larger than the 6-queenboard took longer than one minute per execution. Iwas under some time constraints during my testingperiod so I decided to end my testing of the geneticalgorithm after the 6-queen board.

6.3 O(N) Algorithm Results

This algorithm executed very quickly, asexpected. About the only real speed consideration onthis algorithm was the time taken to print the boardonto the screen. Boards from four to 10 queensprinted out in approximately the same amount of time(0.22 seconds), and the board of twenty tookapproximately twice as long to print out (0.53seconds) , though it appeared thecalculations tookapproximately the same amount of time as theprevious runs. The drawback to this algorithm, aspreviously mentioned, was only one solution to eachproblem was produced. The discrepancies in thetimes recorded are probably due to human error (notstarting and stopping the stop watch at the precisetimes), rather than the computer taking longer toexecute one run than another.

6.4 Backtracking Algorithm Results

The modified backtracking algorithm workedvery well in test results, running

head to head withthe O(N) solution up until the board of size 10(approximately 0.22 seconds on each run). On the10-queen board, there was a small deviation in theswiftness at which a solution was delivered (0.22seconds on O(N) as compared to 0.305 seconds forthe look ahead algorithm). On the 20-queen board,there was a significant increase in deviation, with thesolution being found by the O(N) algorithm over fivetimes faster (0.524 seconds for the O(N) algorithm,as compared with 2.703 seconds for the backtrackalgorithm). Still the results seem to indicate that thisalgorithm is fairly efficient.

6.5 Look-Ahead Algorithm Results

There were varied results with this algorithm. Itappears that on smaller boards (n < 10), the arrayoverhead makes

this algorithm run somewhat slowerthan the previous algorithm.

On larger boards, however, the steps saved fromthe previous algorithm cause a performance jump.On the 10-queen board, for example, the look-aheadalgorithm gave an average execution timeof 0.278seconds, as opposed to the backtracking algorithmresult of 0.306 seconds on average. This can also beseen on a greater scale for the 20-queen board wherethe average total seconds to run was 2.315, comparedwith the backtracking result of 2.703.

6.6 Next-Ascent Hill-Climbing Results

The next-ascent hill-climbing algorithm had asharp increase in running time as the number ofqueens increased. For this reason, I did not conduct atest of the 20-queen board. For the first few runs, thetime it

took for individual runs was fairly close. Onthe 8-queen board, variations began to creep in, asruns took anywhere from 0.69 to 3.63 seconds to run.Similar deviation occurs on the 9 and 10-queenboards. The longest run measured occurred on the10-queen board and took 27.66 seconds to computean answer. Though the solutions took some time togenerate, they were normally of better quality thanthe previous algorithms, in that unique solutions weregenerated a majority of the time, instead of finding a

particular solution repeatedly.

6.7 Steepest-Ascent Hill-Climbing Results

I was quite surprised by the performance of thisalgorithm. I thought that by taking the best child outof a neighborhood of children that a solution wouldbe reached faster. Instead it appears as though thenext-ascent hill-climber actually performs better inmy experimentation. I am not sure of the exactreason for this, unless the randomly picked childrenmoved the solution generation in a completelyopposite direction. Theonly board where thisalgorithm showed better results than the next-ascenthill-climber was the 9-queen board where the averagenext-ascent hill-climbing run was 4.901 seconds withan average of 888.9 iterations to find a solution. Thesteepest-ascent algorithm took 3.641 seconds with anaverage of 548.5 iterations to generate an answer. Onall other boards, the performance of the next-ascenthill-climber either equaled or was better than theperformance of the steepest-ascent hill-climber.

6.8 Genetic Search Algorithm Results

The efficiency of the genetic algorithm was farfrom what I had hoped. I abandoned several of theruns after the time taken to find a solution was inexcess of one minute. The average amount of time ittook to find a solution tothe 4-queen board was1.367 seconds, which is over six times longer thanany of the previous solutions. It also took an averageof 66406 generations to converge on an answer. Icompleted testing on the 4, 5, and 6-queen boards,but about this point was when solutions required inexcess of one minute to complete, and I becameimpatient, and ended the testing prematurely.

7. Conclusions

From all of my testing, I have concluded that thealgorithm that performs best in terms of speed is theO(N) solution.

The modified-backtracking, and look-ahead backtracking algorithms are a close second,with solutions taking somewhat less than O(N^2) tocomplete. In addition, the look-ahead algorithmappears to run slightly more efficiently on largerboards than the modified backtracking algorithm.Unfortunately, for these first three algorithms, thequality of the solutions generated is not as good asthe last three algorithms, as the same solution isgenerated on each run of the program.

The efficiency and speed atwhich the last threealgorithms runs, is not as good as the performance ofthe previous three algorithms, as these latteralgorithms are highly dependent on the element ofchance. The next-ascent hill-climber runs the best ofthese three, which is far slower than any of the threeprevious algorithms. The steepest-ascent hill-climbercomes in fifth place, with results averaging to beworse than that of the next-ascent hill-climbingalgorithm. In last place is the genetic algorithm,which runs horribly inefficiently for boards wheren>= 7. The only consolation for the inefficiency ofthese algorithms is the fact that different solutions areproduced after each run, rather than the same solutionas on the first three algorithms.

8. Future Work

In the future, I would like to be able to have amore efficient means of calculating which squares areendangered by a queen. At the moment, myalgorithms for finding out such information aresomewhat inefficient, which does not allow me to dotests on larger boards (greater than 20).

I would also like to investigate other alternativesfor breakout methods for the hill-climbingalgorithms, as well as improve the efficiency of thegenetic algorithm, due to its poor performance duringmy evaluations. It might also be interesting to see theeffects that viral infection has on the geneticalgorithm.