Queens game using CP
Alireza Soroudi, PhD
Lead Data Scientist @ bluecrux || SMIEEE || Optimization expert || Healthcare management || Lab Digitalization || Power and Energy systems || Developer || Author / Speaker || (views are mine)
This is an exciting installment of our newsletter!
I recently came across a fascinating challenge on LinkedIn that caught my attention. It's a perfect example of the kind of problem that can be tackled using constraint programming. Let’s explore how we can solve it with this powerful approach.
Why is this important?
Constraint programming is more than just a tool—it's a game-changer in solving complex problems efficiently. Whether you're managing supply chains
Game rules:
Data generation
The data associated with board should be created:
data = {
(0, 0): 0, (0, 1): 0, (0, 2): 0, (0, 3): 0, (0, 4): 0, (0, 5): 0, (0, 6): 0, (0, 7): 0, (0, 8): 0,
(1, 0): 0, (1, 1): 0, (1, 2): 5, (1, 3): 5, (1, 4): 5, (1, 5): 5, (1, 6): 5, (1, 7): 5, (1, 8): 0,
(2, 0): 0, (2, 1): 0, (2, 2): 2, (2, 3): 2, (2, 4): 2, (2, 5): 2, (2, 6): 2, (2, 7): 5, (2, 8): 0,
(3, 0): 0, (3, 1): 0, (3, 2): 2, (3, 3): 2, (3, 4): 4, (3, 5): 4, (3, 6): 2, (3, 7): 1, (3, 8): 0,
(4, 0): 0, (4, 1): 0, (4, 2): 2, (4, 3): 3, (4, 4): 3, (4, 5): 4, (4, 6): 2, (4, 7): 1, (4, 8): 0,
(5, 0): 0, (5, 1): 6, (5, 2): 6, (5, 3): 6, (5, 4): 3, (5, 5): 2, (5, 6): 2, (5, 7): 1, (5, 8): 0,
(6, 0): 8, (6, 1): 7, (6, 2): 7, (6, 3): 6, (6, 4): 2, (6, 5): 2, (6, 6): 2, (6, 7): 1, (6, 8): 0,
(7, 0): 8, (7, 1): 8, (7, 2): 7, (7, 3): 6, (7, 4): 0, (7, 5): 0, (7, 6): 0, (7, 7): 0, (7, 8): 0,
(8, 0): 8, (8, 1): 8, (8, 2): 8, (8, 3): 0, (8, 4): 0, (8, 5): 0, (8, 6): 0, (8, 7): 0, (8, 8): 0
}
Now we know the color and the coordinates of each cell.
The question we need to answer is a To be Or Not To be for each cell --> Binary variable (0/1)
So let's skip to the good part,
Python Code
Model
Let's create a model
model = cp_model.CpModel()
solver = cp_model.CpSolver()
Sets
Now we need a couple of sets
nodes = range(1,N+1)
colors = info_color.keys()
nodes: all cells
领英推荐
colors: all distinct colors
Decision variables
Now a number of binary variables are required to find the location of Queens
U = {c:model.NewBoolVar(f"queen_{c}") for c in nodes}
Constraints
For each colored region we should have only one Queen (use AddExactlyOne)
how to detect ? The
for c in colors:
expressions = [U[n] for n in nodes if info[n][2] == c]
model.AddExactlyOne(expressions)
"""
each color has exactly one queen
"""
For each row we should have only one Queen (use AddExactlyOne)
for x in range(1,10):
expressions = [U[n] for n in nodes if info[n][0] == x]
model.AddAtMostOne(expressions)
"""
each row has at most one queen
"""
For each column we should have only one Queen (use AddExactlyOne)
for y in range(1,10):
expressions = [U[n] for n in nodes if info[n][1] == y]
model.AddAtMostOne(expressions)
"""
each column has at most one queen
"""
No two adjacent cells should have both queens
for n in nodes:
expressions = [U[n]]+[U[c] for c in nodes if n!=c and (info[n][0] - info[c][0])**2 +(info[n][1] - info[c][1])**2<2]
model.AddAtMostOne(expressions)
"""
any two adjacent nodes can not both host queen
"""
Done !
Final result
Code is available on Git Repo
Importance:
In summary, this isn't just a fun exercise; it's a practical demonstration of how powerful tools like constraint programming can be applied to solve real challenges, driving efficiency and success in various fields.
Director of Financial Operations at Baltimore Convention Center
6 个月This is amazing. Way to go.
Dipl.Math., Data Scientist at Garmin Würzburg GmbH, Germany
6 个月nice problem again! The actual underlying problem is maximal independent set, https://en.wikipedia.org/wiki/Independent_set_(graph_theory) i.e. to color the maximal number of vertices of the conflict graph black under the constraint that no edge isadjacent to two black vertices. Normally that problem requires an integer linear program with a constraint for every edge but it is also possible reduce the number of constraints to the number vertices.. The problem is NP complete and dual to the vertex cover problem that asks for the minimal number of vertices that must be black so that every edge is adjacent to at least one black vertex.
Senior IT Project Manager, MD Anderson Cancer Center
6 个月Thanks for sharing!
I believe the N Queens problem is the type of problem where Constraints Programming easily beats Linear Programming, thanks to its AllDiff features. Especially for large instances!
Software developer (retired) / Independent researcher
6 个月Fun puzzle!