Eight Queens Problem in Minizinc
There’s a few things that I’m really good at, and one of them is procrastination. I can find anything to do that will delay me getting inevitable tasks done. Today it’s because I have an exam tomorrow and still have a lot of studying that I should do for that. I have also been given a research task to do.
Instead of doing either of those tasks, I’ve knocked up a solution to the eight queens problem using the Minizinc constraints programming language. The eight queens problem simply is, how to arrange eight queens on a chessboard so that none of them are in check.
Not sure if it works correctly – did it in about two seconds flat – so bonus points if you can find problems with it.
include "globals.mzn";
array[1..8] of var 1..8: xpos;
array[1..8] of var 1..8: ypos;
%no two queens can have the same xvalue
constraint
forall (i in 1..8) (
forall (j in 1..8 where j!=i) (
xpos[i] != xpos[j]
)
);
%no two queens can have the same yvalue
constraint
forall (i in 1..8) (
forall (j in 1..8 where j!=i) (
ypos[i] != ypos[j]
)
);
%none on the diagonals
constraint
forall (i in 1..8) (
forall (j in 1..8 where j!=i) (
(xpos[i] - xpos[j] != ypos[i] - ypos[j]) /\
(xpos[i] - xpos[j] != ypos[j] - ypos[i]) /\
(xpos[j] - xpos[i] != ypos[i] - ypos[j])
)
);
solve satisfy;
output [ "X: " ++ show(xpos[i]) ++ " " ++
"Y: " ++ show(ypos[i]) ++ "\n" | i in 1..8 ];
The output that I get is:
X: 4 Y: 8
X: 2 Y: 7
X: 7 Y: 6
X: 3 Y: 5
X: 6 Y: 4
X: 8 Y: 3
X: 5 Y: 2
X: 1 Y: 1
Now to find something else to procrastinate with..

Well, seeing as you can’t check a queen does it matter where you put them?
Hah.. true that.. I meant.. well… getting a queen to amost be captured.. by another queen.. you know what I mean
So elegant! The same thing in X10 is horrible by comparison and like 5 times as long. Is there much like implicit parallelism in minizinc do you know? You have no how idea how impressive I find your coding skills. I wish I was as 1337 as you.
Actually, I’m pretty sure there’s no implicit parallelism in minizinc – I was trying to work out if there was the other day actually, and it appears there’s not – but it confuses me a little because it’s a different paradigm. It could be there but I’m just getting the syntax wrong? I dunno.
I am so not 1337. Seriously. There are people on the net who do crazy things with constraints! Not silly little problems like the ones I do
Ye, I was thinking about it more the other night. The presentation you did, how did you get minizinc to interface with Java for the graphics of the house design thing you did?
I didn’t get them to interface.. I’m lazy.. I just took the outputted values from the minizinc model and typed them into the java program.. would have been much more impressive if they did interface. I imagine it wouldn’t be too hard – like, outputting the output of the minizinc model into a text file and then changing the java program to accept input from a file, and tying it all together with a bash script or bat file? I could imagine something like that.
But yeah, too lazy, it worked without that anyway.
I love your pragmatism! xD Ye, interfacing them seems like it would be quite hard to do. Still very 1337!