Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect n-Queens Constraints? #344

Open
EthanJamesLew opened this issue Apr 8, 2023 · 1 comment
Open

Incorrect n-Queens Constraints? #344

EthanJamesLew opened this issue Apr 8, 2023 · 1 comment

Comments

@EthanJamesLew
Copy link

Describe the bug
I was able to get the solution
[[0 0 Q 0]
[0 Q 0 0]
[0 0 0 Q]
[Q 0 0 0]]
For a 4x4 4-Queens problem. I believe this is because the "diagonal /" constraints are slightly off.

I believe the correct code is

diff --git a/benchmarks/queens-pulp.py b/benchmarks/queens-pulp.py
index 0768dbf..f6d5323 100644
--- a/benchmarks/queens-pulp.py
+++ b/benchmarks/queens-pulp.py
@@ -45,7 +45,7 @@ def gen_model(n):
                         if i - j == k) <= 1, 'diag1({})'.format(p)
 
     # diagonal /
-    for p, k in enumerate(range(3, n + n)):
+    for p, k in enumerate(range(1, n + n - 2)):
         queens += lpSum(x[i][j] for i in range(n) for j in range(n)
                         if i + j == k) <= 1, 'diag2({})'.format(p)

At least, this seemed to produce correct solutions to me.

To Reproduce
Solve the n-Queens problem with PuLP and the example will be present in the solutions.

Expected behavior
I was expecting no queens to share diagonals, like
[[0 Q 0 0]
[0 0 0 Q]
[Q 0 0 0]
[0 0 Q 0]]

Desktop (please complete the following information):

  • Operating System, version: Ubuntu 22.04
  • Python version: Python 3.9
  • Python-MIP version (we recommend you to test with the latest version): Latest commit 6044fc8

Additional context
I was looking for a PuLP n-Queens problem to test my SMT solvers backend.

@EthanJamesLew EthanJamesLew changed the title $n$-Queens Incorrect Constraints $n$-Queens Incorrect Constraints? Apr 8, 2023
@EthanJamesLew EthanJamesLew changed the title $n$-Queens Incorrect Constraints? Incorrect n-Queens Constraints? Apr 8, 2023
@sebheger
Copy link
Collaborator

sebheger commented May 4, 2023

@EthanJamesLew more than happy if you do a PR with the corrected test and ideally an additional assertation for the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants