The eight queens puzzle

22 views
Skip to first unread message

Brede Henriksen

unread,
Oct 31, 2022, 9:01:58 AM10/31/22
to am...@googlegroups.com
Hi!

Do you have an example on how to solve the eight queens puzzle using AMPL?
I have been searching, but only found solutions for other programming languages.

-Brede

AMPL Google Group

unread,
Oct 31, 2022, 11:00:54 AM10/31/22
to AMPL Modeling Language
Here are two examples from our website:

nqueens0.mod
is an integer programming model for the n-queens problem. The variables X[i,j] are 1 if there is a queen in row i and column j, and 0 otherwise. You can send this formulation to any solver that accepts integer variables.

nqueens.mod is a much more compact formulation that uses AMPL's alldiff operator. The variables Row[j] give the number of the row in which column j's queen is placed. Originally, this formulation could only be sent to constraint programming solvers, but now it is recognized by integer programming solvers that use our new MP interface; currently these include x-gurobi, copt, and highs (all available in our standard distribution from portal.ampl.com), with more to come.

For the 8-queens problem, just set parameter n to 8.


--
Robert Fourer
am...@googlegroups.com
{#HS:2053363833-112573#}
nQueens.mod
nQueens0.mod

Oliver Kvaløy Tiller

unread,
Nov 2, 2022, 12:04:22 PM11/2/22
to AMPL Modeling Language
Hey Robert!

Thanks for answering Bredes question, I was coincidentally looking for the same solution. 

I must admit that AMPL is new to me, so my skills with the language are somewhat limited. 
With the same original problem, I'm trying to display all possible solutions to the problem (of which there are 92 I believe), not just one. 

Do you know of a way to do this? I've tried changing the cplex-options, but I still only get one solution.  

Sincerely, Oliver

AMPL Google Group

unread,
Nov 2, 2022, 6:32:13 PM11/2/22
to AMPL Modeling Language
Using nqueens0.mod, try setting these CPLEX options:

option cplex_options 'poolstub=..\nq\nqueens populate=2 poolintensity=4';

Note that one AMPL .sol file is written for each solution found, and you will need to adjust poolstub to indicate the folder/directory where you want the files written. You can use Initial.npool to retrieve the number of solutions found. Then you can use a loop like this to display them (again adjusting the filename expression appropriately):

for {i in 1..Initial.npool} {
   solution ("..\nq\nqueens" & i & ".sol");
   display X;
}

CPLEX only gave me 34 solutions, however.


--
Robert Fourer
am...@googlegroups.com
{#HS:2053363833-112573#}
On Wed, Nov 2, 2022 at 4:04 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hey Robert!

Thanks for answering Bredes question, I was coincidentally looking for the same solution.

I must admit that AMPL is new to me, so my skills with the language are somewhat limited. With the same original problem, I'm trying to display all possible solutions to the problem (of which there are 92 I believe), not just one.

Do you know of a way to do this? I've tried changing the cplex-options, but I still only get one solution.

Sincerely, Oliver

On Mon, Oct 31, 2022 at 3:00 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Here are two examples from our website:

nqueens0.mod
is an integer programming model for the n-queens problem. The variables X[i,j] are 1 if there is a queen in row i and column j, and 0 otherwise. You can send this formulation to any solver that accepts integer variables.

nqueens.mod is a much more compact formulation that uses AMPL's alldiff operator. The variables Row[j] give the number of the row in which column j's queen is placed. Originally, this formulation could only be sent to constraint programming solvers, but now it is recognized by integer programming solvers that use our new MP interface; currently these include x-gurobi, copt, and highs (all available in our standard distribution from portal.ampl.com), with more to come.

For the 8-queens problem, just set parameter n to 8.


--
Robert Fourer
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages