Problem implementing optimizer() function

48 views
Skip to first unread message

areynell

unread,
Sep 12, 2017, 11:45:11 AM9/12/17
to YALMIP

Instead of using the standard optimize() function, I am instead trying to use the optimizer() function in order to improve run times, but I'm having some trouble implementing it correctly. On line 146 of the attached code, I've tried using the optimizer() function, where the variables to optimize are the vectors y and u, and the variables that change after each solution iteration are the multidimensional matrix Akm1 and the vector zkm1. Currently I'm getting errors of the form "Dimensions of matrices being concatenated are not consistent.", but I feel that's because I don't understand how to use the function correctly. Using the standard optimize() function, it runs without error, but takes around 30-35 seconds to complete, with much of that time spend on YALMIP overhead. If I could get help on how to use optimizer() correctly that would be great. Thanks very much.
Optimizer_Problem.m

Johan Löfberg

unread,
Sep 12, 2017, 12:07:42 PM9/12/17
to YALMIP
 the whole code looks weird

1.  You have the optimizer command inside the for-loop and thus will make as many calls to optimizer as you would to optimize and thus no benefit but only slower 
2. The parameter arguments are doubles
3. You try to concatenate numerical data with inconsistent sizes
K>> [Akm1; zkm1]
Error using vertcat

Johan Löfberg

unread,
Sep 12, 2017, 12:23:29 PM9/12/17
to YALMIP
and if I interpret what you really want to do, you want to setup a parameteried problem where

(I-0.5*dt*Akm1(:,:,t))\((I+0.5*dt*Akm1(:,:,t-1))*y(:,t-1)+0.5*dt*B*(u(:,t)+u(:,t-1)))

and

T_max*exp(-zkm1(t))*(1-(z(t)-zkm1(t)))

are dealth with by paramterizing the problem in Akm1 and zkm1. However, that will not work, as the inverse operator isn't supported for sdpvars (the resulting expression would be horrible), and parameters in nonlinear functions isn't supported.

What you would have to do is to see the epxressions as H{t}*y(:,t-1)+G{t}*(u(:,t)+u(:,t-1)) and Q{t}*(1-(z(t)-zkm1(t))) where H, G and Q are the new parameters. However, there would be a massive amount of parameters as H{t} is 5x5, and I doubt it would be much faster than setting up from scratch in every iteration. 

Johan Löfberg

unread,
Sep 12, 2017, 12:33:20 PM9/12/17
to YALMIP
and you have very low hanging fruits to get faster code

The individual norms

  
           for t = 1:N
         constraints = [
             constraints;
             sig(t) >= 0;
             sig(t) >= norm(tau(:,t));          
             ];

can be vectorized by using the cone operator

         constraints = [
            constraints;
            sig >= 0;
            cone([sig;tau])];

and all you elementwise linear constraints can be vectorized such as  sig(t) <= T_max*exp(-z1(t))*(1-(z(t)-z1(t)))

Johan Löfberg

unread,
Sep 12, 2017, 12:40:01 PM9/12/17
to YALMIP
other trivial improvements are to define variables outside the loop, and not redefining constraints that never change

areynell

unread,
Oct 8, 2017, 12:19:26 AM10/8/17
to YALMIP
Thanks for those tips, they sped the code up very nicely. I'm still a little confused by your last statement where you said "other trivial improvements are to define variables outside the loop, and not redefining constraints that never change". Could you explain this a little further? Thanks Johan.

Johan Löfberg

unread,
Oct 8, 2017, 4:54:38 AM10/8/17
to YALMIP
Well, if you are using optimizer I don't think you have anything further to improve (unles you define the optimizer inside the for-loop which would be against the whole idea.

What I mean is that you never should do 

for i = ...
 x = sdpvar
Model = ...
 ...
end

when you can do

 x = sdpvar
for i = ...
Model = ...
 ...
end

...but of course, with optimizer that is not relevant as you now hopefully do

x = sdpvar...
P = optimizer...
for i = ...
 solve
end
Reply all
Reply to author
Forward
0 new messages