How to define the "WantedVariables" in optimizer(Con,Obj,Options,Parameters,WantedVariables) in most efficient way?

48 views
Skip to first unread message

Xinwei Shen

unread,
May 17, 2018, 5:54:44 AM5/17/18
to YALMIP
Dear Johan,

If I built a stochastic programming model with lots of variables in different names (x,y,z,...) and only have one uncertain parameter that belongs to normal distribution, e.g. 'w'. Do I need to write 
optimizer(Con,Obj,Options,w,[x,y,z,...]);
is there a efficient way to represent the [x,y,z,...] (just want to simplify the work of listing all the created variables).



Johan Löfberg

unread,
May 17, 2018, 6:03:29 AM5/17/18
to YALMIP
recover(setdiff(depends(Con),depends(w)) perhaps

Xinwei Shen

unread,
May 17, 2018, 8:05:02 AM5/17/18
to YALMIP
Thx a lot. Problem solved. 
Another question: how to recover the solutions in 'sol' to all the named variables?
sol=optimizer(Con,Obj,Options,w,recover(setdiff(depends(Con),depends(w)) );

Johan Löfberg

unread,
May 17, 2018, 8:12:00 AM5/17/18
to YALMIP
allvariables = recover(setdiff(depends(Con),depends(w)) ;

sol=optimizer(Con,Obj,Options,w,allvariables);

assign(allvariables,sol{1})

Xinwei Shen

unread,
May 17, 2018, 8:26:54 AM5/17/18
to YALMIP
Emmm.... I guess this time it doesn't work. 
I was wondering the meaning of 
allvariables = recover(setdiff(depends(Con),depends(w)) ;
just btw, the link to "depends" is "Page Not Found"

Now I see that recover() is used to "create an sdpvar variable based on variable indicies", so what if there is integer variables in my model?

and, what is "sol{1}" ?

Johan Löfberg

unread,
May 17, 2018, 8:36:50 AM5/17/18
to YALMIP
depends gives the internal indicies to used variables, and recover creates a variable which maps to that variable

>> yalmip('clear');
>> sdpvar x
>> sdpvar y
>> z = 2*x+x^2+y;
>> getvariables(z)

ans =

     1     2     3

>> depends(z)

ans =

     1     2

>> depends(x)

ans =

     1

>> recover(1)-x

0


sol{1} means the first solution returned from the optimizer object (or just sol if you haven't a cell list of outputs in optimizer)

Xinwei Shen

unread,
May 17, 2018, 8:47:41 AM5/17/18
to YALMIP
so, it doesn't matter if I have binvar() defined and used in my model, and "depends()" and "recover()" still work well, right?

Johan Löfberg

unread,
May 17, 2018, 8:49:01 AM5/17/18
to YALMIP
yes

Xinwei Shen

unread,
May 17, 2018, 9:19:00 AM5/17/18
to YALMIP
Sorry for bothering u again. I'm just wondering if I already use following commands to define variables

    X_a=binvar(1,a,'full');
    X_b
=binvar(1,b,'full');
    X_c
=binvar(1,c,'full');
....
how can I make values in sol{1} assigned to "X_a", "X_b", "X_c"...?

Johan Löfberg

unread,
May 17, 2018, 9:37:43 AM5/17/18
to YALMIP
optimizer outputs aren't really supposed to be used in this way. Maybe you should stick with optimize

You will have know where the associated variables are in the returned object, something like assign(X_b,sol{1}(1 + a:a+b)) etc. 

Xinwei Shen

unread,
May 17, 2018, 10:30:52 PM5/17/18
to YALMIP
Thanks for the help. 

another question: what did "sample" do? (the link to "sample" is "page not found", again...)
        allvariables = recover(setdiff(depends(Constraints),depends(PR_Gas)));
        sol
=optimizer(Constraints,Obj,ops,PR_Gas,allvariables);
        S
= sample(sol,100); % Sample ten instances and concatenate models
it's expected that 100 solutions are stored in "S", however, I found "S" is only a "Optimizer object with 0 inputs (0 blocks) and 10974 outputs (1 blocks)."  That's weird.

Johan Löfberg

unread,
May 18, 2018, 2:21:52 AM5/18/18
to YALMIP
sample is illustrated here


it replaces the sampled variable with a sample, and then concatenates all those sampled models.
Reply all
Reply to author
Forward
0 new messages