Passing function to YALMIP

378 views
Skip to first unread message

deafdino

unread,
Jan 7, 2013, 10:18:29 AM1/7/13
to yal...@googlegroups.com
Hello,

Is it possible to pass a function to use as a constraint? For example:

X = sdpvar(4,1,'full','complex');
Cons = [Func(B,X) <= 0.2];
Obj = norm(A*X - targ);

Many thanks

Johan Löfberg

unread,
Jan 7, 2013, 10:20:40 AM1/7/13
to yal...@googlegroups.com
You mean Func being your own MATLAB file, taking parameter B and argument X.

deafdino

unread,
Jan 7, 2013, 10:21:14 AM1/7/13
to yal...@googlegroups.com
Yes, exactly

Johan Löfberg

unread,
Jan 7, 2013, 10:37:21 AM1/7/13
to yal...@googlegroups.com
To begin with, unstructured black-box programs like this is not the primary target for YALMIP.

A hack that might work is to replace the function calls with

result = sdpfun(X,B,'Func')

Note that the first argument in your black-box function has to be the sdpvar, i.e., you have to change the order.

No derivatives will be computed, it will perform very bad in global solvers, and you might miss a lot of structure.

deafdino

unread,
Jan 7, 2013, 10:43:46 AM1/7/13
to yal...@googlegroups.com
Ah, I see. I suspect your hack may work as the function simply selects the max value of a matrix operation when given an input.

Thanks so much.

Johan Löfberg

unread,
Jan 7, 2013, 12:38:50 PM1/7/13
to yal...@googlegroups.com
If it is simple straight MATLAB operators, then you might just call the function directly. Problems occur if you have functions involving if-statements, external functions such as simulations etc. The MAX operator is for example supported in YALMIP. If you use it in a convex fashion, the corresponding conic models will be created. If you use them in a non-convex fashion, mixed-integer models will be created. If you simply hide the function through sdpfun, it will generate a nonlinear black-box model.

deafdino

unread,
Jan 8, 2013, 9:05:21 AM1/8/13
to yal...@googlegroups.com
Ok, it essentially just has a for-loop within it. I tried running it with sdpfun and keep getting the following error:

LMI #3 not available.

Error in compileinterfacedata>convertsocp2NONLINEAR (line
1232)
        z = sdpvar(Fsocp(i));

Error in compileinterfacedata (line 699)
                [F,changed] = convertsocp2NONLINEAR(F);

Error in solvesdp (line 241)
[interfacedata,recoverdata,solver,diagnostic,F,Fremoved] =
compileinterfacedata(F,[],logdetStruct,h,options,0,solving_parametric); 

Any ideas why?

Johan Löfberg

unread,
Jan 8, 2013, 9:06:46 AM1/8/13
to yal...@googlegroups.com
I think you are using an old version of YALMIP. That bug was fixed recently.

Johan Löfberg

unread,
Jan 8, 2013, 9:07:54 AM1/8/13
to yal...@googlegroups.com
A for-loop which changes behaviour based on X? Then sdpfun is reasonable, otherwise not most likely. If you post the function, it is easier to give you guidance on suitable modeling strategy.

deafdino

unread,
Jan 8, 2013, 9:15:04 AM1/8/13
to yal...@googlegroups.com
Ok, thanks. This is the optimisation problem:

X = sdpvar(8,1,'full','complex');
Fun = @(X,B) sdpfun(X,B,'@(X,B) Fun(X,B)');
Cons = [Fun(X,B) <= 0.001, X'*QG*X <= 0.04];               % Constrain against IEEE max SAR
Obj = norm(A*X - targ);
options = sdpsettings('verbose',0);

sol = solvesdp(Cons,Obj,options);

and the function is:

    function [S] = Fun(x,Q)

    % Preallocate
    S = zeros(size(B,1));

    % Compute all local S values
    for k=1:size(B,1)
        S(k) = ((x)'*squeeze(B(k,:,:))*(x));
    end

    % Output max local S
    [S] = abs(max(S(:)));

Johan Löfberg

unread,
Jan 8, 2013, 9:20:20 AM1/8/13
to yal...@googlegroups.com
No, you should absolutely not use sdpfun here. The constraints are essentially quadratic constraints, and YALMIP and the solver should know this.

However, the model is a bit problematic since it mixes the convex operators abs and max with the non-monotone convex quadratic operators, thus making it hard for YALMIP to derive a simple model.

Are the B-matrices positive semidefinite? Your indicates otherwise since you are taking absolute values, but if it is you can create a much much better model.

deafdino

unread,
Jan 8, 2013, 9:26:30 AM1/8/13
to yal...@googlegroups.com
The B matrices are positive semi-definite, yes. However it is only the final value of S that is important here.

Johan Löfberg

unread,
Jan 8, 2013, 9:28:29 AM1/8/13
to yal...@googlegroups.com
So why do you have an absolute value? Positive-semi-definite means all quadratic terms are non-negative, hence the maximum is non-negative

deafdino

unread,
Jan 8, 2013, 9:32:15 AM1/8/13
to yal...@googlegroups.com
Ah apologies. I mixed up my definitions. I though you meant Hermitian. All the matrices are Hermitian but I am unsure if they are PSD. I too the absolute value to ignore the phase of S and just get the magnitude.

Johan Löfberg

unread,
Jan 8, 2013, 9:37:47 AM1/8/13
to yal...@googlegroups.com
You should check, it makes a world of difference. And do you really mean abs(max)), don't you mean max(abs)), i.e., the worst-case is constrained

deafdino

unread,
Jan 8, 2013, 9:40:02 AM1/8/13
to yal...@googlegroups.com
Ok, thanks for all your help. I will look into this.
Reply all
Reply to author
Forward
0 new messages