In this post, I am going to explain the code I wrote for solving the "N-queens Problem" using the "Google or-tools".
Google Optimization Tools is a software suit which makes it easy to solve many types of combinatorial optimization problems.
N-queens problem asks this question: "How can N queens be placed on an NxN chessboard so that no two of them attack each other?". Here is a solution for 4 queens:
No two queens are on the same row, column, or diagonal. |
We are going to:
- Create the solver,
- Define our variables,
- Add constraints,
- Create the search tree,
- Iterate through the solutions.
For creating our solver, we need the "pywrapcp" module from or-tools:
First we create our solver:
We should now declare our queens' coordinates as variables. We have a NxN board. So we should keep the x and y coordinates. While the first thing came to mind for storing them is to create a 2-D array, a smarter way to do is to store them in a 1-D array: with the index representing the y coordinate(row), and the value representing the x coordinate(column).
We want our variables to be in range of 0..N. For this purpose, we are going to use "solver.IntVar". "IntVar" is an expression which allows us to define a variable's bound. Let's create our variables in one line:
That was easy, right? Now we have named our variables as "q0, q1, .., q(N-1)".
It is time to add our constraints to our solver. Basically, we have 2 constraints. We have to ensure that no two queens are:
- On the same column: No two values of the array can be the same.
- On the same diagonal: The values plus(and minus) the indices should be all different.
We can add the first constraint like this:
"solver.AllDifferent" enforces a set of variables to take distinct values. We can use it for the diagonal constraints too:
We are now ready for solving the problem. First, we should build our search tree. Using the "solver.Phase", we tell the solver what to solve. The first parameter will be our set of variables. The second parameter specifies how to choose the next "IntVar" variable to be selected in the search. The third parameter indicates what value to assign to the selected "IntVar". Since for this post the speed is not important, the default behaviors are chosen.
After creating the search tree, we can now begin our search.
We can print our solutions while iterating through them as:
We have reached the end of our search.
The documentation says that it is just better practice to finish the search with the method "EndSearch".
That's it. I hope you enjoyed this walk through.
You can find the full code here:
See you next post.