N-Queens problem with Yalmip

69 views
Skip to first unread message

Asher Metzger

unread,
Mar 26, 2015, 6:21:01 AM3/26/15
to yal...@googlegroups.com
Hey Johan,
I've been trying to solve the 8-queen (placing 8 queens on a chess board without having them threaten each other)  placement problem with Yalmip.
I run in to 2 problems:
What should be my objective function?
and why are the constraints trivally false?

thanks,
Asher (attaching the code I tried)
Queens.m

Johan Löfberg

unread,
Mar 26, 2015, 3:17:02 PM3/26/15
to yal...@googlegroups.com
row(1) ~= row(1) sounds like a generally bad idea.

Well, if you don't know what you want to have as objective, why do you want an objective?

Asher Metzger

unread,
Mar 28, 2015, 6:08:49 AM3/28/15
to yal...@googlegroups.com
Of course.
I didn't upload the updated file. 
now row (i) and row (j) don't have the same index. yet the answer I get is a vector of four's which is against the first constraint. 
Queens.m

Johan Löfberg

unread,
Mar 28, 2015, 1:38:41 PM3/28/15
to yal...@googlegroups.com
The logical code is in a state of constant development (i.e. buggy)

Change line 30 in @sdpvar/ne to

elseif isa(X,'sdpvar') & is(X,'integer')

and it might work

Asher Metzger

unread,
Mar 30, 2015, 10:46:06 AM3/30/15
to yal...@googlegroups.com
Now it runs. But it seems to run forever.
I put a 5000 iteration limit but it still goes.

Johan Löfberg

unread,
Mar 30, 2015, 10:48:12 AM3/30/15
to yal...@googlegroups.com
Which solver? Solved in 0s with all solvers here

Asher Metzger

unread,
Mar 30, 2015, 11:23:07 AM3/30/15
to yal...@googlegroups.com
without C2 or C3 it finds a solution by 100 iter's but not with both

Johan Löfberg

unread,
Mar 30, 2015, 11:24:04 AM3/30/15
to yal...@googlegroups.com
Which solver?

Asher Metzger

unread,
Mar 31, 2015, 12:56:34 AM3/31/15
to yal...@googlegroups.com
With BNB.

Attached is the file with the corrected indicing.
Queens.m

Johan Löfberg

unread,
Mar 31, 2015, 3:01:39 AM3/31/15
to yal...@googlegroups.com

Asher Metzger

unread,
Apr 1, 2015, 1:08:36 PM4/1/15
to yal...@googlegroups.com
I tried with mosek, Works like a charm. 
Thank you

Mark L. Stone

unread,
Apr 1, 2015, 10:12:38 PM4/1/15
to yal...@googlegroups.com
With the change to @sdpvar/n, it seemed to run correctly with several solvers using YALMIP R20141218 (with not surprisingly, different valid solutions depending on the solver), but produces a vector of ones using R20150204.  But as you say, perhaps that code is still buggy.

But my bigger discovery was an apparent bug in the interface to baron when there is no objective function or a fake objective function such as 0 which has no sdpvar.
Here is the simplest example of that bug:

sdpvar x;optimize(x>=0,[],sdpsettings('solver','baron','debug',1))

which produces

Error using callbaron (line 23)
Error: Expression or statement is incomplete or incorrect.
Error in solvesdp (line 329)
    eval(['output = ' solver.call '(interfacedata);']);
Error in optimize (line 31)
varargout{:} = solvesdp(varargin{:});

Use of 0 instead of [] for the objective gives the same error.

sdpvar x;optimize(x>=0,x,sdpsettings('solver','baron','debug',1))
executes correctly

I am using matbar version 1.69.

Johan Löfberg

unread,
Apr 2, 2015, 2:40:32 AM4/2/15
to yal...@googlegroups.com
Thank you. Change line 22 in callbaron.m to

if length(obj)>0
    obj
= ['@(x) ' obj];
    obj
= eval(obj);
else
    obj
= @(x)0;
end


Mark L. Stone

unread,
Apr 2, 2015, 9:16:25 AM4/2/15
to yal...@googlegroups.com
Thanks.  I presume that is the replacement collectively for lines 22-23.

BTW, ignore my previous comment about getting the wrong answer with R20150204. I had a late night screw up with my file naming, so got the wrong result when using what turned out to be the uncorrected version of @sdpvar/ne.

Reply all
Reply to author
Forward
0 new messages