Question about for loops vectorization

79 views
Skip to first unread message

B. Mattlet

unread,
Oct 6, 2015, 6:23:13 AM10/6/15
to YALMIP
Hi everyone !

So, I've this bilevel problem (which is not yet formulated in the way YALMIP allows it). This is my lower level function which returns a cost that is used by my upper level function. So this function will be called a lot of times
On my i5 it takes around 2sec to run (Time. T = 96 and this code will called for 200 houses by the upper level function). I already read that I need to vectorize my constraints and read examples on it but I don't get it. Can anyone recode just one of the below constraints to show me ? I'm not comfortable enough with MATLAB.

My other question, again for speeding up the code, is the difference between arrays of structure or arrays of table. Is there a reason I should use one instead of the other ? I use plenty arrays of table because it's easy to manipulate the cells since they were imported from excel files and sometimes the columns change. So I don't have to worry about the column number since I know the name of the column.

Thanks for your time !

%% Optimize House = Lower level function
function [Objective, EV_x, EV_E, EV_P] = OptimizeHouse(Time, Cost, House)

%% Decision variables
% Declaration of the decision variable
EV_E
= sdpvar(1,Time.T,'full');
EV_P
= sdpvar(1,Time.T,'full');
EV_x
= binvar(1,Time.T,'full');

%% General parameters
Constraints = [];
Objective = 0;

%% Constraints
   
% Initial conditions of the energy must be set
EV_E
(1) = House.SI{1,'SoC_ini'}*House.SI{1,'Energy'};
for t=1:Time.T
   
% The EV can only charge at home and without exceeding the connection power of the house
   
Constraints = [Constraints, EV_P(t) + House.occupancy(t)*House.p_baseload(t) <= House.occupancy(t)*House.data{1,'connection'}];
   
% The energy added in one time slot is bounded, ex for an EV : min and max charging power
   
Constraints = [Constraints, EV_x(t)*House.SI{1,'P_min'} <= EV_P(t) <= EV_x(t)*House.SI{1,'P_max'}];
end
for t=2:Time.T
   
% Evolution of the energy
   
Constraints = [Constraints, EV_E(t) == EV_E(t-1) + House.SI{1,'Eff_ch'}*EV_P(t)*Time.tau/60 - House.travel(t)];
   
% The total amount of energy is the storage is bounded, ex for an EV : min and max SoC
   
Constraints = [Constraints, House.SI{1,'SoC_min'} <= EV_E(t)/House.SI{1,'Energy'} <= House.SI{1,'SoC_max'}];
end
   
% The last time slot of energy must be greater than the first one for limit conditions
Constraints = [Constraints, EV_E(Time.T) >= EV_E(1)];


%% Objective
for t=1:Time.T
       
Objective = Objective + Cost.c(t)*EV_P(t);
end




Johan Löfberg

unread,
Oct 6, 2015, 6:37:52 AM10/6/15
to yal...@googlegroups.com
This is primarily a basic MATLAB question, and not YALMIP specific.

Vectorization would be, for example
t=2:Time.T
EV_E
(t) == EV_E(t-1) + House.SI{1,'Eff_ch'}*EV_P(t)*Time.tau/60 - House.travel(t)


similarily
Objective =  sum(Cost.c.*EV_P);

or something like that, depending on the dimensions (row vs columns) on your data


Message has been deleted

B. Mattlet

unread,
Oct 6, 2015, 7:43:39 AM10/6/15
to YALMIP
Ok, I get it. Sorry it must look like a ridiculous question :)

Considering that some constraints only apply for t > 2 and other don't should I use

t=1:time.T
Some constraints

t
=2:Time.T
EV_E
(t) == EV_E(t-1) + House.SI{1,'Eff_ch'}*EV_P(t)*Time.tau/60 - House.travel(t)


or 

t=1:Time.T
Some contraints
if t>1

  EV_E
(t) == EV_E(t-1) + House.SI{1,'Eff_ch'}*EV_P(t)*Time.tau/60 - House.travel(t)

end


Thanks a lot !

Johan Löfberg

unread,
Oct 6, 2015, 7:45:04 AM10/6/15
to YALMIP
Yes, I only showed one of the loops

Johan Löfberg

unread,
Oct 6, 2015, 7:47:35 AM10/6/15
to YALMIP
and your second alternative makes no sense for matlab (it runs, but what does it mean to apply if on a vector of logic values. Any? All?)

B. Mattlet

unread,
Oct 6, 2015, 7:53:14 AM10/6/15
to YALMIP
Yes of course ! Sorry I sent my response too soon, I was still thinking with my loops... It does not make any sense !

Last question (I hope) : why did you remove the 

Constraints = [Constraints, "some constraint"];

and wrote directly the constraint ?

Johan Löfberg

unread,
Oct 6, 2015, 7:56:19 AM10/6/15
to YALMIP
I just showed how to define the constraint, not the complete model

B. Mattlet

unread,
Oct 6, 2015, 8:02:13 AM10/6/15
to YALMIP
Ok, it was just to make sure I was not missing something :)

Thanks a lot ! It divided the time of calculus by 4 !

Johan Löfberg

unread,
Oct 6, 2015, 8:20:26 AM10/6/15
to YALMIP
If you are solving many problems with almost identical models, make sure to check up the optimizer command

B. Mattlet

unread,
Oct 6, 2015, 8:36:08 AM10/6/15
to YALMIP
I'm solving many problems because I have a bilevel problem that I wrote myself. Once I get it completely working, I'll work on the code to use the bilevel formulation for YALMIP and it won't be two different optimization problems.
Reply all
Reply to author
Forward
0 new messages