How to Extract sdpvar variables from a Constraint?

383 views
Skip to first unread message

XB G

unread,
Sep 14, 2018, 4:48:32 PM9/14/18
to YALMIP
Dear all,
It is easier to understand my question through a simple example:

Capture.PNG

Currently the code above does not work since it cannot find variable `x`. Can I "extract" variable x from constr (in another function)? I guess the struct/variable constr already contains the variable information (I saw sth related in the robustify() function, but that was too complicated to understand for me..)


Clearly, if I put the content of add_y() function into the main function, everything works out. But this example is a simplification of a much more complicated case, in which it is better to define functions.


The code not in a figure:


clear

sdpvar x

constr = [x >= 1];

objective = x; 

% function add_y() adds an additional constraint

% x + y >= 1, y <= -1

constr_new = add_y(constr);


optimize(constr, obj)


function f_new = add_y(f)

    sdpvar y

    f_new = [f, x+y >= 1, y <=-1];

end









Johan Löfberg

unread,
Sep 14, 2018, 5:00:50 PM9/14/18
to YALMIP
YALMIP is 99.9% standard MATLAB, and in MATLAB you have to pass variables to the function

Johan Löfberg

unread,
Sep 14, 2018, 5:02:19 PM9/14/18
to YALMIP
and the horrible version would be 

function f_new = add_y(f)

    sdpvar y

    x = recover(depends(f));

XB G

unread,
Sep 15, 2018, 9:08:39 AM9/15/18
to YALMIP
Hi Johan,
Thanks for your prompt reply and suggestion! 

Actually the "horrible version" was what I was looking for....I saw you utilized the same code in some of YALMIP functions. Could you please explain why this is "horrible" (efficiency? code hard to maintain? etc).

Another related question: for my add_y() function, if I use the "horrible version", how can I make sure that the output constraint `f_new` are still naming variables as "x"? For example: the same code above excepting replacing `x` with `z` in the add_y() function. If we solve the new problem (objective and constr_new), we need to use `value(z)` and `value(y)` to get optimal solutions.

clear

sdpvar x

constr = [x >= 1];

objective = x; 

% function add_y() adds an additional constraint

% x + y >= 1, y <= -1

constr_new = add_y(constr);


optimize(constr, obj)


function f_new = add_y(f)

    sdpvar y

    z = recover(depends(f));

    f_new = [f, z+y >= 1, y <=-1];

end




Thanks!

Johan Löfberg

unread,
Sep 15, 2018, 9:15:00 AM9/15/18
to YALMIP
horrible as hard to understand what is happening, x will be all variables in the object, in the order they were defined in yalmip, with no coupling to what is called x outside etc

XB G

unread,
Sep 15, 2018, 9:35:33 AM9/15/18
to YALMIP
So can I understand this way: 
- I define variables and constraints using `x` and `w` in the main function, I add variables `y` and `z` and additional constraints in functions (e.g. add_y())
- YALMIP treats everything as `x` internally
- after YALMIP solved the problem: in the main function, I can only get the optimal values of `x` and `w`, but values of 'y' and 'z' cannot be obtained
I tested it in matlab, it seems so.

Sorry a new question arises: within a function I use `v= recover(depends(f));` to get every variable depending on constraint f, how can I know which part of v corresponds to `x` and which part is `w`?

Thanks 


XB G

unread,
Sep 15, 2018, 9:37:15 AM9/15/18
to YALMIP
In other words, how can I know the indices for `x` and `w`? Should I maintain a function record the order of defining variables? Or YALMIP has internal functions doing so...?

Johan Löfberg

unread,
Sep 15, 2018, 9:46:03 AM9/15/18
to YALMIP
YALMIP has no notion of a name or knowledge thereof, everything is references to internal indicies

>> yalmip('clear')
>> x = sdpvar(1);
>> y = sdpvar(1);
>> getvariables(x)

ans =

     1

>> getvariables(y)

ans =

     2

>> x = sdpvar(1);
>> getvariables(x)

ans =

     3

>> x = y;
>> getvariables(x)

ans =

     2



However, if you have to start thinking about these things, it smells like you are doing something the wrong way (unless you are writing a very low-level library of some sort)

Johan Löfberg

unread,
Sep 15, 2018, 9:47:56 AM9/15/18
to YALMIP
for instance, if you want to write a function which adds slacks to constraints, you could do

f = sdpvar(F);
sdpvar slack
F = [f + s >= 0, s >= 0]

etc

XB G

unread,
Sep 15, 2018, 9:52:40 AM9/15/18
to YALMIP
Got it.

I'm working on some code to converting some optimization to equivalent formulations, in which I need to reformulate some existing constraints (defined in the main function) and add auxiliary variables (somewhat similar to Robust optimization)


XB G

unread,
Sep 15, 2018, 10:44:52 AM9/15/18
to YALMIP
Where can I find the usage of sdpvar() that takes LMI objects/structs as input?

I want to be cautious to avoid potential bugs, I'm not very clear about the following example:

sdpvar x, y
F = [ x + y == 1]

a = sdpvar(F)
sdpvar z
F1 = [a+z == 1]
F2 = [a+z >= 1]
F3 = [a+z <= 1]

What do F1~F3 mean? 

F1: x+y-1+z == 1?
F2: x+y-1+z >= 1?
F3: x+y-1+z <= 1?

Johan Löfberg

unread,
Sep 15, 2018, 10:55:51 AM9/15/18
to yal...@googlegroups.com
All inequalities are written as f>=0, and f is what you get. Similarly, all equalities are written as f==0

Note equalities really aren't defined as a == b either could be a-b == 0 or b-a == 0. I think a-b, but you should confirm by comapring it with a-b
Reply all
Reply to author
Forward
0 new messages