π§© Constraint Solving POTD:Problem of the Day: N-Queens β A Gateway to Constraint Programming #35909
Closed
Replies: 1 comment
-
|
This discussion has been marked as outdated by Constraint Solving β Problem of the Day. A newer discussion is available at Discussion #36109. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The N-Queens Problem: A Gateway to Constraint Programming
Problem Statement
Place
nqueens on ann Γ nchessboard such that no two queens attack each other. Two queens attack if they share the same row, column, or diagonal.Concrete Instance (n = 4):
Here, queens are at positions (0,1), (1,3), (2,0), (3,2). Each row has exactly one queen; no two share a column or diagonal.
Input/Output:
n(board size)nnon-attacking queens (or "no solution" ifn β {2, 3})Why It Matters
The N-Queens problem appears in real-world scenarios:
Beyond applications, N-Queens is a canonical teaching tool in constraint programming: it illustrates search trees, pruning, variable ordering heuristics, and symmetry breaking in an intuitive visual setting.
Modeling Approaches
Approach 1: Constraint Programming (CP)
Variables:
queen[i] β {0..n-1}for rowi= column position of queen in rowi.Constraints:
Why it works: Column uniqueness is enforced by
AllDifferent, a global constraint with strong propagation. Diagonal constraints are posted as inequalities. Most CP solvers include specialized algorithms for these constraints.Trade-offs:
Approach 2: Integer Linear Programming (MIP)
Variables:
x[i,j] β {0,1}= 1 iff queen is at rowi, columnj.Constraints:
Why it works: Integer linear constraints model the attack structure directly. Standard branch-and-cut can solve it.
Trade-offs:
Example Model (MiniZinc)
Key Techniques
1. Variable Ordering Heuristics (First-Fail)
Assign queens from top row to bottom row. At each step, branch on the column with the fewest remaining consistent positions (minimum remaining values / MRV heuristic). This prunes the search tree dramatically by making hard decisions early and detecting conflicts quickly.
2. Arc Consistency & Constraint Propagation
c, removecfrom all subsequent rows' domains.iis at columnj, eliminate columnj Β± (k - i)for all future rowsk. Modern solvers achieve arc consistency on these diagonals in O(n2) or better.3. Symmetry Breaking
The N-Queens problem has 8-fold symmetry (4 rotations Γ 2 reflections). To count unique solutions or avoid redundant search:
queen[0] < queen[n-1](or other breaking constraints)n-1queens on the remainderChallenge Corner
Question for you:
Symmetry: The 8 symmetries of the board mean many solutions are rotations/reflections of others. How would you add the fewest constraints to ensure your solver finds only canonical solutions (one per equivalence class)?
Scaling: Can you model N-Queens such that the constraint propagation (without search) reduces the domains significantly before the solver even branches? What global constraints might help?
All-solutions: If you want to count all N-Queens solutions efficiently, would you use backtracking with symmetry breaking, or SAT enumeration? Why?
References
Rossi, F., van Beek, P., & Walsh, T. (2006). Handbook of Constraint Programming. Elsevier. Β§ 1 & 15 (foundational CP theory and CSP tutorial).
Gent, I. P., & Walsh, T. (1999). "CSPlib: a benchmark library for constraints." Technical report, available at csplib.org. Includes N-Queens as CSP benchmark
#4with extensive experimental data.Hooker, J. N. (2012). Integrated Methods for Optimization (2nd ed.). Springer. Chapter on constraint programming with N-Queens as case study.
OR-Tools & MiniZinc documentation: Both solvers ship with N-Queens examples and tutorials demonstrating CP vs. SAT modeling side-by-side.
Next Problem: Check back tomorrow for a routing or hybrid-optimization challenge!
Beta Was this translation helpful? Give feedback.
All reactions