A classical question that has been asked in years past is the -queens problem, which asks what a configuration of queens on an chess board is such that no two queens attack each other.
It is always possible, for , to generate a solution to the -queens problem.1
The -queens board is not a board that would come up in a real game, since there are no black or white kings on the board! A more interesting question to ask might be:
Given that there are both white and black kings on the board, what is the maximum number of black queens that may be placed on the board such that on white's turn, white is not in checkmate and the black king is not attacked?
Short of trying all possible configurations of black queens, and the white and black kings on the board, there really is no way to try to manually come up with a maximal configuration by oneself.
A popular way of modeling integer optimization questions subject to integer constraints is by formulating these problems as integer-programming (IP) problems. Integer-programming is a problem that is classically known to be -hard. For small problem sizes, modern SAT solvers should be able to employ state-of-the-art SAT solving techniques to breeze through finding solutions to IP problems.
A short refresher on Integer Programming
Integer programming is a specification of values , and . A solution to an integer programming problem maximizes where such that the following conditions are true
We will now attempt to provide an integer programming formulation for the problem at hand.
Variables
We first declare the variables that will be needed for expressing our constraints and setting up the function we will eventually try to optimize for.
Let be the set of all 64 squares on the board. We use the classical (rank, file) notation as we do in chess, where the bottom left square is called , and the top right square is called .
We first define a set of indicators for queens on the board, for each square .
Then, we go on to defining indicators for white and black kings on the board, for each square .
Constraints
With only the variables defined above, we may model the constraints that are required for us to express the constraints of Chess with respect to Kings, Queens, and checks as follows.
Square Occupancy
There can be at most one piece on a square at any given time, and this constraint may be expressed, for each square , as follows.
Kings on the Board
Both the white and black kings should be present on the board, which is a constraint that may be expressed, without loss of generality for the black king, as the following.
Repeating the same constraint as above but for white will express the fact that there is one white king on the board.
We must also encode the constraint that the black and white kings do not sit on adjacent squares. Let be a function that computes the set of adjacent squares for a given square.
We simply require the following constraint for all squares .
You may take issue with the fact that since the implication is predicated on the value of a particular variable, this does not really respect the constraints of an IP problem since we do not necessarily have constant coefficients for variables on the left hand side of an inequality (or equality).
To this, I say that it is possible to reformulate the implication as such
If the white king is present on a square, then the neighborhood indicators for the black king must sum to zero, which is the implication that we want. If the white king is not present in the square, then we know that there can be at most one black king in the neighborhood (or none!) which still passes the inequality.
Queen Attacks
To model queen attacks, we need to create some sort of construct to denote the notion of attacking and blocking attacks. An interesting thing about queens of the same color is that if one queen blocks another queen, even though the ray of attack is blocked, the blocking queen has its own ray of attack that makes up for the blocked rays. Hence, blocking as a concept is only relevant for the black king who can block another black queen's attack.
Unfortunately, for modeling attacks, it would be very difficult to construct a linear integer programming constraint that is succinct (I've also not been able to come up with one in 20 minutes or so of thinking; please reach out to me if you come up with a good constraint for modeling attacks!). So, we fallback to expressing the constraints in a more expressive theory called SMT (Satisfiability modulo theories) that allows us to express constraints as the following, for the directions set , and for the function which is a function that returns the ray of squares from a specified square and in a provided direction:
To explain in english, basically denotes whether is attacked or not. It does so by checking the rays emanating from in all directions , and checks for all cells whether has a queen and whether there is a black king on the path from to (in which case the ray would be blocked).
Not in Checkmate
In order to denote the fact that the white king is not in checkmate, we basically check that either the king is not in check (which is easy to check with ), or that the there are escape squares available, which is also easy to check over the neighborhood.
This completes the specification of constraints for our problem. We finally need to specify the objective function to maximize over!
Objective Function
Since the objective is to maximize the number of queens placed, we simply want to maximize the following quantity
The 8×8 solution
Below is a solution to the 8x8 board, where, even the white king is attacked by both queens on and , is able to capture the queen on on the next turn and escape all checks. The black king is able to block some queens' attacks along two diagonals and the file.
Growth with board size
I was able to plot the maximum number of non-checkmating queens for all the way up to ( was taking multiple hours to compute so I gave up).
A clear pattern emerges, where the maximum number of non-checkmating queens appears to be for through . It should be possible to come up with a proof of this fact, but alas I couldn't get to it this weekend.
Footnotes
-
Explicit constructions for all are given in Hoffman, Loessi and Moore, "Constructions for the Solution of the m Queens Problem" (1969). ↩