About vectorization and optimizer

180 views
Skip to first unread message

B. Mattlet

unread,
Mar 2, 2017, 5:18:33 AM3/2/17
to YALMIP
Hi !

I've a MIP optimization problem that needs to be run several times and which can get quite big. I have some questions regarding the efficiency of the code.

1. Declaration of variables
Say I have three decision variables and all these are H by T matrices (H households = 200 and time running up to an horizon T = 96) :

P = semivar(Block.H,Time.T,3,'full');   % P(1) = Electric power from the Heat Pump
                                        % P(2) = Electric power from the Extra Heater 
                                        % P(3) = Heat power from/to the radiators

I declared these variables as a H x T x 3 decision variable. What is the fastest ? To declare it the way I did or to declare three H x T decision variables ? Does it even make any difference ?

P1 = semibar(Block.H,Time.T,'full');
P2 = semibar(Block.H,Time.T,'full');
P3 = semibar(Block.H,Time.T,'full');

2. Vectorization
Of course, to fasten the code, I try to vectorize as much as possible. Does the function bsxfun count as Functions Commonly Used in Vectorization ? Or would you just write "A - B" instead of bsxfun(@minus, A, B) ? I can't tell what's happening behind.

3. Optimizer
Since my code will be called several times, it want to run the optimizer. In the definition ;

P = optimizer(Con,Obj,Options,Parameters,WantedVariables)

How do I pass multiple parameters and variables in the input of optimizer ? 

Should I pass
  
optimizer(..., param1, param2, ..., var1, var2, ...)

or should I create an array containing all my parameters and variables ?

parameters = [param1 param2 ...]
% or ?
parameters = [param1;param2;...]

optimizer(..., parameters, ...)

The final point is to call the optimization several times and just change a parameter delta = 0:0.1:15. How do I put that in the input arguments ? I get that my problem becomes a parametric problem. Should I keep my formulation the way I did, i.e.calling the optimization and changing the value of my parameter ? Because I saw that MPT calls a specific solver and I have CPLEX (for free since I work in an university) and which I believe to be the fastest solver for MIP problems like mine.

Thanks for the help !

Best regards.


Johan Löfberg

unread,
Mar 2, 2017, 5:37:37 AM3/2/17
to YALMIP
1. Should be no major difference. Code for 2d is perhaps slightly more optimized

2. bsxfun is not overloaded, you you have to loop or do reshape(A(:)-B(:),size(A))

3. {param1, param2},{var1, var2}

B. Mattlet

unread,
Mar 2, 2017, 11:50:27 AM3/2/17
to YALMIP
Thanks a lot !

I just have to work on the optimizer now. 

B. Mattlet

unread,
Dec 5, 2017, 9:46:14 AM12/5/17
to YALMIP
Hi ! 

So, I'm using the optimizer. 

I'm working on a minimization of a classical problem : 
Objective = sum(x)

With a constraint that is a function of a parameter
Constraints =[Constraints, cost*x <= some_value*(1 + delta)]

The idea is to see the result of the optimization if delta is equal to 0, 1%, 2%, 3%, and so on, until, say, 50%
Naturally, I created a simple for loop using delta = 0 : 0.01 : 0.5 and called my optimization 50 times. 

for delta = 0:0.01:0.5
     some other constraints

     
Constraints =[Constraints, cost*x <= some_value*(1 + delta)]

     rest of the code
(objective, constraints, etc.)

     solution
= optimize(Constraints, Objective, Options);
end

Besides the time needed that is quite long, my code runs just fine.
This is a classical use for your tool 'optimizer'. Actually, there are more parameters and I want to run around 1000 times the previous optimization that takes around 30-60 sec to be solved.
So I worked on the optimizer : 

delta = sdpvar(1,1,'full');

some other constraints

Constraints = [Constraints, cost*x <= some_value*(1 + delta)];
Constraints = [Constraints, 0<= delta <= 0.5];

rest of the code (objective, constraints, etc.)

solution = optimizer(Constraints, Objective, Options, delta, x);

Two questions arise: 
1. how many values will delta take from 0 to 0.5 ? If I want to optimize for different values of delta by step of 0.1% can I declare delta as an intvar then use delta/100 ?
2. I don't have an error when I call the problem but all I get are "0" as values for my variables. Solution.info gives me a 'successfully solved (IBM CPLEX)'. What am I missing there ?

Thanks for the help

Johan Löfberg

unread,
Dec 5, 2017, 10:32:43 AM12/5/17
to yal...@googlegroups.com
You seem to have misunderstood how the optimizer is used. Your optimizer based approach would be

delta = sdpvar(1,1);
some other constraints
Constraints = [Constraints, cost*x <= some_value*(1 + delta)];
rest of the code (objective, constraints, etc.)
solution = optimizer(Constraints, Objective, Options, delta, x
);

for deltavalue = 0:0.01:0.5
 xsol
= solution(deltavalue)
end



B. Mattlet

unread,
Dec 5, 2017, 1:23:02 PM12/5/17
to YALMIP
OK, I forgot that it's a precompiled object but it actually needs to be solved at a certain value of delta.

Got it to work now.

Thanks a lot.
Reply all
Reply to author
Forward
0 new messages