Académique Documents
Professionnel Documents
Culture Documents
APR 12 2013
3 COMMENTS
BY HAIRYCODE UNCATEGORIZED
Throughout this semester I have been thoroughly enjoying my artificial intelligence class. While the probability stuff can be a bit
much for me at times, I absolutely love learning various searching algorithms. For our first project I implemented a genetic
algorithm to find a solution for the Traveling Salesperson Problem in a language Id never used, Python. While I did have my ups
and downs, I learned to love the cleanliness of the language and the welcoming community it has built up. Naturally, I chose Python
for my second project.
The N-Queens problem is rather straightforward; how can we place N number of queens on a NxN board such that no queen can
directly attack another? This is rather simple, albeit monotonous, to do by hand on small board i.e.: 4 or 5 queens. But, what about
when we have 8, 10, or even 20 queens? Things get a bit more tricky. For simplicity, I chose to solve the problem with a backtracking
algorithm.
1. Starting from the first column, check every row for a threat from queens above, below, left, right, or diagonal to it. Once you find
a safe row for a given column, place a queen there.
2. Recurs incrementing one column until you have either placed a queen in every column or you are unable to safely place a queen
in a column. If latter occurs, you must backtrack to the last known good solution.
3. Annotate the location of the last queens row, remove the queen, and recurs calling on the previous column beginning one row
passed where the last queen was.
Before starting to code, I drew out a simple, 4-Queen problem on a white board, step by step, and really thought about what was
going on, how I wanted to attack the problem, and tricky cases which would need special care. Doing so greatly reduced the amount
of headaches I could have had by going straight to the old code and fix model. Now, only if the entry level C.S. students in my lab
would do the same.
class NQueens:
print board
After developing a working solution I started receiving: RuntimeError: maximum recursion depth exceeded in cmp on boards
with more than 13 queens. I found this surprising and did some research. After some digging, I discovered Python has a rather
conservative default recursion limit to help avoid infinite recursive calls. My resolution was rather hackish but allowed me to bump
the solvable board size to 17 queens by manually setting the recursion limit. This was more than good enough for my class project.
If time permits, I would like to go back and mimic the recursive nature of this with a stack and see how large of a solution would be
able to solve. If time permits.
-H.
Ne says:
November 8, 2014 at 1:48 pm
Awesome
Reply
truongkhanhit says:
March 16, 2015 at 5:55 am
I tried to find all the solutions but can solve N = 11 in 40 minutes. http://www.capacode.com/?p=682
Reply
truongkhanhit says:
January 20, 2016 at 9:47 pm
I just add some heuristic and now it solves N = 15 in less than 30 seconds. Read here http://www.capacode.com/recursion
/solving-n-queen-problem-with-recursive-algorithm/
Reply
Blog at WordPress.com.