How to reduce yalmiptime?

440 views
Skip to first unread message

qi wang

unread,
Apr 2, 2019, 11:12:32 AM4/2/19
to YALMIP
    Hi! Now I encounter a problem about yalmip which is after many iterations, solvetime may not have changed much, but yalmiptime has multiplied several times. And I don't define new variables in the for loop, that is, the values of variables exclude sdpvar type variables in the for loop are previously defined.
    Is it because yalmip saves the values involved in the previous model building process?
    As far as I know, the possible solutions are the following two:
    1.yalmip('clear') 
    But the value of{u_p,u_q,Ph_log,Qh_log,Ps_log,Qs_log} solved in the previous iteration needs to be passed to the next iteration.
    So before each modeling of yalmip, is it only need to clear the sdpvar type variables, or all variables except{u_p,u_q,Ph_log,Qh_log,Ps_log,Qs_log} have to be cleared? If I do according to the second scheme, there is another problem. Because I have defined a lot of variables(more than 100), how to quickly clear these variables?What's more, those variables that have been cleared have to define in the for loop. I don’t think it can reduce yalmiptime in such way.
    2.optimizer
    Is it written like this?
    For the main problem, sol=optimizer(con,obj,ops,[],{Ps,Qs});
    For the sub problem, sol=optimizer(con,obj,ops,[],{Ph,Qh});
    But in this way, matlab displays the model is infeasible.
    I really don't know what to do. I would be grateful if you could make any suggestions.
*****************************************************
The code framework is as follows:

main problem:
1.Initialize variables and assign values
2.for k=1:MAX_ITER
3.con=con+[P=P_dg - Ps,Q=Q_dg - Qs];(Ps,Qs are the variables to be solved in the main problem, while Ps_log=double(Ps); Qs_log=double(Qs))
   con=con+[...];
4.obj=f(u_p(:, k),u_q(:, k),Ph_log(:, k),Qh_log(:, k),Ps,Qs);
5.ops=...;
   sol=solvesdp(con,obj,ops);
6. ... = double(...)
   Ps_log(:, k+1)=double(Ps)
   Qs_log(:, k+1)=double(Qs)
7.calling the subroutine
   Sub-problem finds the values of {Ph_log(:, k+1),Qh_log(:, k+1)}
8.u_p(:, k+1) = u_p(:, k) + rho*(Ps_log(:,k+1)-Ph_log(:,k+1))     (where rho is the penalty factor before the ADMM second penalty term)
   u_q(:, k+1) = u_q(:, k) + rho*(Qs_log(:,k+1)-Qh_log(:,k+1))
9.Calculate the residual and determine whether the program converges
   If the program converges,then end!If not, go to step 2

sub-problem:
1.Initialize variables and assign values
2.con=con+[...];
3.obj=f(u_p(:, k),u_q(:, k),Ps_log(:, k+1),Qs_log(:, k+1),Ph,Qh);(where {u_p(:, k),u_q(:, k),Ps_log(:, k+1),Qs_log(:, k+1)} come from the main problem)
4.ops=...;
   sol=solvesdp(con,obj,ops);
5. ... = double(...)
   Ph_log(:, k+1)=double(Ph)
   Qh_log(:, k+1)=double(Qh)


Appendix is the data of yalmiptime and solvetime.

****************************************************************************

Iteration 1:

The main program:

sol =  (Where sol is the return struct of solvesdp)

  contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 9.5957

       solvertime: 2.9813

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

      solverinput: [1×1 struct]

      yalmipmodel: [1×1 struct]

 

sub-problem:

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 1.9363

       solvertime: 0.0367

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

****************************************************************************

Iteration 32

The main program

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 168.7463

       solvertime: 2.2657

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

 

sub-problem

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 20.7593

       solvertime: 0.1087

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

****************************************************************************

Iteration 96

The main program

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 494.9447

       solvertime: 3.4363

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

 

sub-problem

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 57.9348

       solvertime: 0.0572

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

****************************************************************************

Iteration 112

The main program

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 624.9390

       solvertime: 7.2010

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]

 

sub-problem

sol =

  Contains the following struct:

    yalmipversion: '20181012'

       yalmiptime: 72.5015

       solvertime: 0.5995

             info: 'Successfully solved (CPLEX-IBM)'

          problem: 0

     solveroutput: [1×1 struct]






Johan Löfberg

unread,
Apr 2, 2019, 11:30:40 AM4/2/19
to YALMIP
you are asking about different things and it is not clear what the actual question is

First, this, as an example, is bad modelling

q = 1;
for i = 1:10000
 x
= sdpvar(1);
 y
= sdpvar(1);
 optimize
(x+y ==q,x^2+y^2)
 q
= value(x)*value(y)
end

This is better

q = 1;
for i = 1:10000
 yalmip
('clear')
 x
= sdpvar(1);
 y
= sdpvar(1);
 optimize
(x+y ==q,x^2+y^2)
 q
= value(x)*value(y)
end

this is even better
q = 1;
x
= sdpvar(1);
y
= sdpvar(1);
obj
= x^2+y^2;
for i = 1:10000
 optimize
(x+y == q,obj)
 q
= value(x)*value(y)
end

and this is the best

q = sdpvar(1);
x
= sdpvar(1);
y
= sdpvar(1);
P
= optimizer(x + y == q,x^2+y^2,sdpsettings('solver','cplex'),q,{x,y})
qval
= 1;

for i = 1:10000
 sol
= P(qval);
 qval
= sol{1}*sol{2};
end



qi wang

unread,
Apr 2, 2019, 10:26:57 PM4/2/19
to YALMIP
Thanks for your reply, Prof.Johan.
   But the problem of mine is more complicatedthe value of{u_p(:, k+1),u_q(:, k+1),Ph_log(:, k+1),Qh_log(:, k+1),Ps_log(:, k+1),Qs_log(:, k+1)} solved in the previous iteration needs to be passed to the objective function in the next iteration in both main problem and sub-problem.
*****************************************************
The modified code framework is as follows:

main problem:
1.Initialize variables of main problem and assign values
   u_p=zeros(...);u_q=zeros(...);Ps_log=zeros(...);Qs_log=zeros(...);
   Ps=sdpvar(...);Qs=sdpvar(...);P=sdpvar(...);Q=sdpvar(...);P_dg=sdpvar(...);Q_dg=sdpvar(...);
   ...
   obj_1=...   (the variables in obj_1 aren't the variables that need to be passed between adjacent iteration)
2.for k=1:MAX_ITER
3.con=con+[P=P_dg - Ps,Q=Q_dg - Qs];
   (Ps,Qs are the variables to be solved in the main problem, while Ps_log(:, k+1)=double(Ps); Qs_log(:, k+1)=double(Qs))
   con=con+[...];
4.sum_1=f(u_p(:, k),u_q(:, k),Ph_log(:, k),Qh_log(:, k),Ps,Qs);
   obj=obj_1 + sum_1;
5.ops=...;
   sol=solvesdp(con,obj,ops);
6.if (sol.problem==0)
    ... = double(...)
   Ps_log(:, k+1)=double(Ps)
   Qs_log(:, k+1)=double(Qs)
7.calling the subroutine
   Sub-problem finds the values of {Ph_log(:, k+1),Qh_log(:, k+1)}
8.u_p(:, k+1) = u_p(:, k) + rho*(Ps_log(:,k+1)-Ph_log(:,k+1))     (where rho is the penalty factor before the ADMM second penalty term)
   u_q(:, k+1) = u_q(:, k) + rho*(Qs_log(:,k+1)-Qh_log(:,k+1))
9.Calculate the residual and determine whether the program converges
   If the program converges,then end!If not, go to step 2
10.elseif (sol.problem==1)
     ...
     end
11.end


sub-problem:
1.Initialize variables of sub-problem and assign values
   Ph_log=zeros(...);Qh_log=zeros(...);
   Ph=sdpvar(...);Qh=sdpvar(...);
   P_a=sdpvar(...);Q_a=sdpvar(...);...;P_f=sdpvar(...);Q_f=sdpvar(...);
   ...
2.con=con+[...];
   con=con+[Ph=P_a+P_b+P_c+P_d+P_e+P_f,Qh=Q_a+Q_b+Q_c+Q_d+Q_e+Q_f];
3.obj=f(u_p(:, k),u_q(:, k),Ps_log(:, k+1),Qs_log(:, k+1),Ph,Qh);
   (where {u_p(:, k),u_q(:, k),Ps_log(:, k+1),Qs_log(:, k+1)} come from the main problem)
   (Ph,Qh are the variables to be solved in the sub-problem, while Ph_log(:, k+1)=double(Ph); Qh_log(:, k+1)=double(Qh))

qi wang

unread,
Apr 2, 2019, 10:34:02 PM4/2/19
to YALMIP
Futhermore, this is the detailed time of the main program and sub-problem.
捕获1.PNG
捕获2.PNG

Johan Löfberg

unread,
Apr 3, 2019, 2:02:27 AM4/3/19
to YALMIP
no difference, you have a problem parameterized by something, and something can be computed from the previous solution

q = sdpvar(1);
x
= sdpvar(1);
y
= sdpvar(1);

P
= optimizer(x + y == q,q*x^2+y*q + y^2,sdpsettings('solver','cplex'),q,{x,y})

qval
= 1;
for i = 1:10000
 sol
= P(qval);
 qval
= sol{1}*sol{2};
end

Your general problem is too complex and messy to discuss, no matter how much colors and bold you use

qi wang

unread,
Apr 3, 2019, 5:34:38 AM4/3/19
to YALMIP
Thank you, I understand.
When I write Outputs = {P,onoff}; as Outputs = {onoff,P}; as shown in the attachment I added.
The result of the program is wrong. Is there a sequential correspondence between Parameters and Outputs?
捕获1.PNG

Johan Löfberg

unread,
Apr 3, 2019, 6:52:34 AM4/3/19
to YALMIP
Note sure what you are saying. Are you saying if you switch the order in the definition of those variables in the example, the results from the simulation (updating that to the new order of course) is different?

qi wang

unread,
Apr 3, 2019, 7:25:42 AM4/3/19
to YALMIP
I made a mistake. After the order was exchanged, the variables corresponding  in the programs are not changed as well.
Thanks for your prompt reply. 

qi wang

unread,
Apr 3, 2019, 9:40:56 AM4/3/19
to YALMIP

Honorific Prof. johan.lofberg:

  I have encountered several problems now. 

  1.My program contains subroutines,so I have to put the variable definition and assignment of the subproblem before the for loop of the main program. However, the subroutine needs to use the variable of the sdpvar type defined in the main program. 

     So I want to ask if the variable of the sdpvar type can be defined as a global variable? Or I still have to define the variable of the sdpvar type in the subroutine.

  2.If I use the optimizer function, it means I only need to model the model once, and then i just  need to solve it with the optimization solver?

    Just like

    controller = optimizer(constraints, objective,sdpsettings('solver','gurobi'),parameters_in,solutions_out);

  ...

   [solutions,diagnostics] = controller{inputs};

  You give on https://yalmip.github.io/example/standardmpc/

qi wang

unread,
Apr 3, 2019, 11:27:26 AM4/3/19
to YALMIP
3.What should I do if I cannot find Parameters in the constraints and objective functions? Can it be an empty set? That is,can controller=optimizer(con,obj,ops,[],{Ps,Qs,...}); run?

Johan Löfberg

unread,
Apr 3, 2019, 2:06:35 PM4/3/19
to YALMIP
1. You can probably define it as global (never tried) or simply send them as arguments to the function

2. Feels like you are asking if the optimizer does what it is meant for. Yes.


Johan Löfberg

unread,
Apr 3, 2019, 2:07:10 PM4/3/19
to YALMIP
If you don't have any parameters, there is no reason to use optimizer

qi wang

unread,
Apr 4, 2019, 7:09:23 AM4/4/19
to YALMIP

Honorific Prof. johan.lofberg:

  I encountere several problems now. 

 1.How to check the optizer's solvetime? I looked at the code for the optimizer function, but I am not sure if I find it correctly.
 2.If the model is infeasiable, how can I get information about model errors?       
  
  just like
  sol = solvesdp(con,obj,ops);
  if (sol.problem~=0)
    sol.info
    yalmiperror(sol.problem)
  end

  3.If I comment out the code of this line, I get the wrong picture.  
  constraints = [constraints, x{k+1} == A*x{k}+B*u{k}+E*d];
  Aren't the two lines' effect I marked the same?
捕获2.PNG
捕获3.PNG

Johan Löfberg

unread,
Apr 4, 2019, 8:06:31 AM4/4/19
to YALMIP
1. There is no such info. optimizer is absolutely minimal support to reduce overhead. You will have to tic/toc manually

2. Second output from optimizer call

3. If you comment out that line, the model makes no sense, and I am not surpised the optimizer object won't work as, For instance, the parameter x{1} won't be part of the model

qi wang

unread,
Apr 5, 2019, 9:34:11 AM4/5/19
to YALMIP

Honorific Prof. johan.lofberg:

  I encounter a problem now. 

  1.In the first iteration, Controller can run. And Matlab does not report any errors. where Controller = optimizer(con,obj,ops,Parameters,Outputs);

While in the second iteration, Matlab reports  error, error is as follows.

when calling "optimizer/subsref",no assignment of output parameter "varargout{2}"(Other parameters may be included)


  Best regard!

qi wang

unread,
Apr 5, 2019, 10:00:17 AM4/5/19
to YALMIP
 2. Does sdpvar have the effect of initializing variables to 0?

qi wang

unread,
Apr 5, 2019, 10:22:05 AM4/5/19
to YALMIP
And for question 1, [solutions,problem] = Controller{u_p_log,u_q_log}; 
I updated the values of {u_p_log, u_q_log} between different iterations.

Johan Löfberg

unread,
Apr 5, 2019, 10:35:42 AM4/5/19
to YALMIP
then it sounds like the solver has crashed or something. Set 'debug' to 1 and set 'verbose' to 2 and see what it shows

Johan Löfberg

unread,
Apr 5, 2019, 10:36:25 AM4/5/19
to YALMIP
if the solver requires an initial guess, it will be 0 unless a initial guess has been specified (which cannot be done for the first iteration using optimizer)

qi wang

unread,
Apr 5, 2019, 10:22:22 PM4/5/19
to YALMIP
The reason for the problem one seems to be because NaN is included in {u_p_log, u_q_log}.
And NaN is coming back from the sub-problem, but I don't know why it is NaN instead of a number.

qi wang

unread,
Apr 5, 2019, 11:37:23 PM4/5/19
to YALMIP
And there is another problem!I just modified the model and the error occurred.But I don't know how to solve it.

Wrongly using compileinterfacedata (line 711)
Wrongly using createobjective (line 16)
Scalar expression to minimize please.

error in export (line 135)
    [interfacedata,recoverdata,solver,diagnostic,F]
    =
    compileinterfacedata(F,[],logdetStruct,h,options,findallsolvers);
    
error in optimizer (line 187)
    [aux1,aux2,aux3,model] = export((x ==
    repmat(pi,nIn*mIn,1))+Constraints,Objective,options,[],[],0);
    
error in both33_123_TZsocp_gai_20190404_optimizer
(line 1935)
    Controller =
    optimizer(con,obj,ops,Parameters,Outputs);

Johan Löfberg

unread,
Apr 6, 2019, 7:13:27 AM4/6/19
to YALMIP
well then you obviously have to dig into tht sub-problem and crank up verbose and turn on debug and check all error codes etc to see what happens there

Johan Löfberg

unread,
Apr 6, 2019, 7:14:05 AM4/6/19
to YALMIP
The error message is that you have a non-scalar objective, which makes no sense

qi wang

unread,
Apr 6, 2019, 8:04:39 AM4/6/19
to YALMIP
Thanks
And there is such a problem now.
The initial value of the objective function of the first iteration of my subproblem is 0.
But from the second iteration, the initial value of the objective function of my subproblem is the value of the objective function solved in the previous iteration.
For optimizer, I don't know how to solve the above problem.

Best regards

qi wang

unread,
Apr 6, 2019, 9:39:07 AM4/6/19
to YALMIP
Also, the optimizer modeling of my sub problem is also written in the main question.  

Contorller_house = optimizer(con_house,obj_house,ops_house,Parameters_house,Outputs_house);

After calling the Contorller_house in the main questionI pass the parameters(solutions_house,problem_house) back to the sub problem. 

where
[solutions_house,problem_house] = Contorller_house{u_p_log,u_q_log}; ——calling the optimizer generated by sub problem
[Ph_log_copy,Qh_log_copy] = House_pairwith_both33_123_TZsocp_gai_20190406_optimizer(k,hh,flag_1,solutions_house,problem_house);——calling sub problem, and passing parameters

Is there any problem with writing in this way?

Johan Löfberg

unread,
Apr 6, 2019, 10:24:51 AM4/6/19
to YALMIP
I have no idea what you are talking and/or asking about

Johan Löfberg

unread,
Apr 6, 2019, 10:28:48 AM4/6/19
to YALMIP
same here, no idea what the actual question is

You will have to set 'verbose' to 2, and then carefully look at the information displayed by the solver, and then carefully look at the diagnostics returned, and he solutiotion returned.

There is nothing special that can be said about an optimizer object. As long as you pass numerical values as parameters, and the solver terminates with no prolems and you get a solution, whatever you do between or send data back and forth in different functions etc etc, is not really interesting. If it fails all of a sudden, it simply means your algorithm isn't robust enough, or there is a flaw in your general algorithm.

qi wang

unread,
Apr 6, 2019, 10:57:26 AM4/6/19
to YALMIP
 1. such as
 if (h==1)
    obj_house=0;
 elseif ((h>=2)&&(h<=Npq))
     obj_house = obj_house_value(h-1, 1);
 end
 where Npq is the number of loops of the sub problem. While obj_house is the initial value of the objective function in sub problem.
 obj_house_value(h-1, 1) is the value of the objective function solved in the previous iteration.

 It seems that if I use optimizer, the initial value of obj_house is determined at the beginning of modeling, and it cannot be changed between iterations.

qi wang

unread,
Apr 6, 2019, 11:20:05 AM4/6/19
to YALMIP
2.Now my main problem can be solved with the optimizer.
 But when I applied the optimizer to the sub-problem, there was a problem which is the solutions{...} are all NaN.
 So I send you the optimizer writing framework of my sub-question to see if it’s right.

BTW, the optimizer modeling of my sub problem is also in the main question.  

The optimizer writing framework of my sub-question is as follows:

Contorller = optimizer(con,obj,ops,Parameters,Outputs);     where  Parameters={u_p{k},u_q{k}};

After calling the Contorller in the main questionI pass the parameters(solutions,problem) to the sub problem. 

which is
[solutions,problem] = Contorller{u_p_log,u_q_log}; ——calling the optimizer of sub problem in the main question

[..., ...] = Sub_problem(solutions_house,problem_house);——calling sub problem, and passing parameters to sub problem,then ruturn value solved in sub problem


Q:Is the framework of my sub-question right?

Johan Löfberg

unread,
Apr 6, 2019, 11:25:03 AM4/6/19
to YALMIP
then you have to have it as an output from optimizer, or compute it from the variables you output

Johan Löfberg

unread,
Apr 6, 2019, 11:26:15 AM4/6/19
to YALMIP
You get nan, but you still haven't said what the solver displays when this happens (after you set verbose to 2 and turn on debug), nor have you said what the returned diagnostic code is

qi wang

unread,
Apr 7, 2019, 7:11:52 AM4/7/19
to YALMIP
1.Thanks for your reply. Since I have not used the optimizer, Sorry for bothering you again.
   There is another problem.
 A=sdpvar(100,1,'full');
 B=sdpvar(100,1,'full');
   c=sdpvar(1);
 for t=1:T
    if(A(c)<=t)&&(t<=B(c)) 
      con=con+[...];
    else  
      con=con+[...];
    end  
 end

error:
The following error occurred when converting from constraint to logical:
Unable to convert from constraint to logical.
How can I solve this problem? If using the optimizer, can't the sdpvar variable appear in the condition of the if statement?

Johan Löfberg

unread,
Apr 7, 2019, 9:53:45 AM4/7/19
to YALMIP

qi wang

unread,
Apr 7, 2019, 10:22:53 AM4/7/19
to YALMIP
Thanks
I modified the code. But there is an error.

The function 'gethackflag' corresponding to the input parameter of type 'double' is not defined.

Error lmi (line 185)
TypeofConstraint(i) = gethackflag(Fi{i});
            
Error
Constraint/horzcat(line 17) H =lmi(varargin{i});
        
Error
main_20190407(line 2022)
Implies(d(3),[(A(B,1)+1)<=t<=24,C(B+(t-1)*Nbus*3),1)==0])];

qi wang

unread,
Apr 7, 2019, 10:36:56 AM4/7/19
to YALMIP
And the full codes are as follow:
d = binvar(3, 1);
sdpvar A,B,C,F
d=10;
e=20;
Nbus=33;
T=24;
for t=1:T
          con=con+[sum(d)==1,
                          implies(d(1), [1<=t<=(A(B,1)-1), F((B+(t-1)*Nbus*3), 1)==0]);
                          implies(d(2), [A(B,1)<=t<=C(B,1), d<=F((B+(t-1)*Nbus*3), 1)<=e]);
                          implies(d(3), [(C(B,1)+1)<=t<=T, F((B+(t-1)*Nbus*3), 1)==0])];
end

Johan Löfberg

unread,
Apr 7, 2019, 11:22:08 AM4/7/19
to YALMIP
Test code does not run

I hope you realize A(B,1) where both A and B are decision variables, leads to a lot of internal binary variables etc, as that is a very hard construction


qi wang

unread,
Apr 7, 2019, 11:19:23 PM4/7/19
to YALMIP
Sorry, I don't catch what you mean.

And the modified codes are as follows:

clc;clear;
Nbus=33;
T=24;
d = binvar(3, 1);
A=sdpvar(1);
B=sdpvar(1);
C=sdpvar(1);
F=sdpvar(Nbus*3*T, 1);
e=10;
g=20;

con=[];

for t=1:T
  con=con+[sum(d)==1,
          implies(d(1), [1<=t<=(A-1), F((B+(t-1)*Nbus*3), 1)==0]);
          implies(d(2), [A<=t<=C, e<=F((B+(t-1)*Nbus*3), 1)<=g]);
          implies(d(3), [(C+1)<=t<=T, F((B+(t-1)*Nbus*3), 1)==0])];
end

qi wang

unread,
Apr 7, 2019, 11:40:27 PM4/7/19
to YALMIP
And The following two lines have no error.
implies(d(1), [1<=t<=(A-1), F((B+(t-1)*Nbus*3), 1)==0]);
implies(d(2), [A<=t<=C, e<=F((B+(t-1)*Nbus*3), 1)<=g]);

Only the following line gives an error:
implies(d(3), [(C+1)<=t<=T, F((B+(t-1)*Nbus*3), 1)==0])];

I really don't know where the problem is.

Johan Löfberg

unread,
Apr 8, 2019, 2:01:36 AM4/8/19
to YALMIP
It is not clear what (C+1)<=t<=T is supposed to mean, as both t and T are constants. Do you mean (C+1)<=double(t<=T)

qi wang

unread,
Apr 8, 2019, 2:10:37 AM4/8/19
to YALMIP
24 hours(from1 to 24) a day is divided into three segments.
The third segment is from C to T(T=24). 
The value of C will be passed in when called. by Controller, where Controller = optimizer(...)

Johan Löfberg

unread,
Apr 8, 2019, 2:16:42 AM4/8/19
to YALMIP
I have no idea what that is supposed to say or answer

The bug in your code is (C+1)<=t<=T  since YALMIP cannot decipher

DECISIONVARIABLES <= Constant1 <= Constant2. 

It could either be that you mean 

DECISIONVARIABLES <= double(Constant1 <= Constant2)

or

DECISIONVARIABLES <= min(t,T)

qi wang

unread,
Apr 8, 2019, 2:33:00 AM4/8/19
to YALMIP
1.DECISIONVARIABLES refers to sdpvar variables?

2.The problem's background is
There are a total of Nbus*3 household. But each household’s electrical usage time preferences are different.
So for different households, C is different. My current solution is to set the household’s electrical usage time to a sdpvar variable.
Is this wrong?

Johan Löfberg

unread,
Apr 8, 2019, 2:37:23 AM4/8/19
to YALMIP
DECISIONVARIABLES means any expression involving sdpvars, such as your C+1

Once again, what do you mean when you write C+1 <= 1 <= 24 as you do now. Do you mean  C+1 <= 1 and C+1<= 24 i.e. C+1 <= min(1,24), or do you mean C+1 <= (1 <= 24) which is  C+1 <= 1 since 24>=1 is true and thus evaluates to 1.

qi wang

unread,
Apr 8, 2019, 2:44:30 AM4/8/19
to YALMIP
What I want to write is that t is a variable, C is equivalent to a constant, because the value of C can be entered through the parameters of the Controller.
For example, for household 1, the value of A is 17 and the value of C is 20.
1<=t<=16     F((B+(t-1)*Nbus*3), 1)==0
17<=t<=20  10<=F((B+(t-1)*Nbus*3), 1)<=20
21<=t<=24   F((B+(t-1)*Nbus*3), 1)==0

Johan Löfberg

unread,
Apr 8, 2019, 3:05:21 AM4/8/19
to YALMIP
Your code says something completely. In your code, t is a constant (as it is the counter in the for-loop)

the code you write here would simply be
for t = 1:24
 if t>=1 && t<=16
  add that constraint
elseif t>=17 && t<= 20
 ...
...
end
the use of implies is redundant as the logical condition is fixed



But yet again, C is a decision variable (although it ill be fixed later on as it is a parameter), so YALMIP will develop a model for indexing

f = x(y) where x is an sdpvar and y is an intvar is represented as

implies(case1, [C==1, f == x(1)])+implies(case2, [C==2, f == x(2)]) + ... + (case1+case2+... == 1)

The model will be very large, but as you then fix C, any good solver will presolve away almost everything here

Johan Löfberg

unread,
Apr 8, 2019, 3:35:47 AM4/8/19
to YALMIP
...but if you have A and C as parameters, and want to have the three cases parameterized, you still have to employ implies, but the crucial part is that you have to remove the redundant constant expressions 1<=t and t<=T. The model will be horribly large though, as a lot of effort is spent on modelling stuff that really isn't part of the optimiation model but simply defines stuff that will be fixed anyway at solvetime. Hence, it is not obvious thatit will be faster to use optimizer ocompared to optimize (or perhaps you simply should construct a bunch of optimizer objects instead)

Johan Löfberg

unread,
Apr 8, 2019, 3:36:43 AM4/8/19
to YALMIP
and note that by construction A,B,C can all be defined as intvars

qi wang

unread,
Apr 8, 2019, 3:43:19 AM4/8/19
to YALMIP
What does constructing a bunch of optimizer objects instead mean?

Johan Löfberg

unread,
Apr 8, 2019, 4:47:13 AM4/8/19
to YALMIP
You create one optimizer for the case A=1 and C=1, one for the case A=1, C=2, ....and then select among the optimizer objects when computing solutions

qi wang

unread,
Apr 8, 2019, 5:03:25 AM4/8/19
to YALMIP
1.When using implies, the following judgment conditions are right
sdpvar1 <=t<= sdpvar2    or    Const1 <=t<= Const2.
While     sdpvar1 <=t<= Const2    or    Const1  <=t<= sdpvar3      are wrong. Isn't it?
where sdpvar1 and sdpvar2 are sdpvar variables. Const1 and Const2 are constants. 

2.If there are 1000 households, then I have to write 1000 subroutines? While in each sub-problem, there is an optinizer. It's too troublesome.



Johan Löfberg

unread,
Apr 8, 2019, 5:17:13 AM4/8/19
to YALMIP
It is not clear as you use vague definitions of constant. Are both t and Const1 constants or is one actually  parameter

There are

DECISIONVARIABLES (sdpvars to be decided by solver)
PARAMETERS (sdpvars which will be fixed right before calling solver, but will enter the generic model as any other decision variable)
CONSTANTS (fixed numbers)

qi wang

unread,
Apr 8, 2019, 6:48:28 AM4/8/19
to YALMIP
for t=1:24 
In every loop, t is a constant

sdpvar1 and sdpvar2 are sdpvar variables. Const1 and Const2 are constants. 

When using implies, the following judgment conditions are right
sdpvar1 <=t<= sdpvar2    or    Const1 <=t<= Const2.
While     sdpvar1 <=t<= Const2    or    Const1  <=t<= sdpvar3      are wrong. 

Do I understand it rightly?

Johan Löfberg

unread,
Apr 8, 2019, 7:05:22 AM4/8/19
to YALMIP
sdpvar1 <=t<= sdpvar2  can be interpreted and means sdpvar1 <=t and t<= sdpvar2  , i.e two constraints in the decision variables

Const1 <=t<= Const2 has nothing to do with YALMIP as everything is numerical data, and that expression evaluates to (Const1<=t) <= Const2. The expression  (Const1<=t) will either be 0 or 1, and the final result then depends on whether Const2 is larger than 0 or 1. Hence [3 <= 2 <= 1] will generate the number 1.

sdpvar1 <= t <= Const2 makes no sense to YALMIP. t is fixed and const2 is fixed, so why are you telling the world that, .e.g, 5 <= 9 and appending that to your list of constraints.



qi wang

unread,
Apr 8, 2019, 8:17:22 AM4/8/19
to YALMIP
Thanks, I see.
Then there is another question.
you said sdpvar1 <=t<= sdpvar2  can be interpreted and means sdpvar1 <=t and t<= sdpvar2  , i.e two constraints in the decision variables
For Const1 <=t<= Const2 and sdpvar1 <= t <= Const2, What I originally meant to express is (Const1 <=t)&&(t<= Const2) and (sdpvar1 <= t)&&(t <= Const2).
This is a grammar problem, and it‘s my mistake.
Then how can I put two judgment statements in an implies statement?
   for t=1:24
  Model = [Model, sum(d) == 1,  
        implies(d(1), [ (3<=t)&&(t<=5), f == 8]); 
        implies(d(2), [ (sdpvar1<=t)&&(t<=24), f == 7]); 
        ...
 end

Does it right?

Johan Löfberg

unread,
Apr 8, 2019, 8:34:14 AM4/8/19
to yal...@googlegroups.com
First, t is always <=24 as that is the limit in your loop so that can be removed from the discussion

The constant condition inside the implication makes no sense. What you write now is (albeit it  is a flawed form with constants in the impliciation)

for t=1:24
 if (3<=t)&&(t<=5)
  Model = [Model, sum(d) == 1, 
           implies(d(1), [f == 8]); 
           implies(d(2), [sdpvar1<=t, f == 7]); 
 else
  Model = [Model,sdpvar1<=t, f == 7]); 
 end

If that is what you intend, this is the form you should use



qi wang

unread,
Apr 8, 2019, 9:30:20 AM4/8/19
to YALMIP
Thanks a lot. I see.

qi wang

unread,
Apr 9, 2019, 12:18:30 AM4/9/19
to YALMIP
Hi, Prof.Johan 
 Sorry for bothering you again.
 The problem is I use 2 norms in both the objective function in the mainproblem and sub-problem.
 But there is  no error in the main function. While there is an error in the sub function. 

wrongly using optimizer (line 193)
Failed exporting the model: Model creation failed (Only 1- and inf-norms can be used in a nonconvex fashion)

I do not know why this is so?

Johan Löfberg

unread,
Apr 9, 2019, 2:26:14 AM4/9/19
to YALMIP
you cannot use 2-norm operator in a nonconvex fashion. It is intended for convex cases only. Rewrite to quadratic model

qi wang

unread,
Apr 9, 2019, 3:30:10 AM4/9/19
to YALMIP
Cannot use 2-norm operator in a nonconvex fashion means the primitive function must be convex?
And my primitive function is convex.
I use 2-norm to replace the quadratic term of augmented Lagrange function in ADMM algorithm.

Johan Löfberg

unread,
Apr 9, 2019, 3:41:01 AM4/9/19
to YALMIP
that error message appears when you use the norm in non-convexity preserving way, such as morm()>= or norm ==, or maximizing norm, or multiplying it with some term, or using it inside another function not guaranteeing convexity

This for instance will fail, as YALMIP cannot know if this will be convex once the parameter is fixed
>> sdpvar x y(2,1);
>> P = optimizer([], x*norm(y-x), [],x,y)



qi wang

unread,
Apr 11, 2019, 10:05:48 AM4/11/19
to YALMIP
Hi! Prof. johan.lofberg:
Sorry for bothering you again.
There are another few questions.
1.Does yalmip('clear') clear all the variables in the code, including the matrix, variables, etc. defined in the front part of the code and sdpvar variables
2.Does the reason why yalmip solves more and more slowly is that the memory occupied by the code has not been released and the memory usage is getting bigger and bigger?
3.Can I free up memory by yalmip('clear')?

qi wang

unread,
Apr 11, 2019, 10:47:04 AM4/11/19
to YALMIP
4.After the main problem executes yalmip(‘clear’) , will the variables in the sub-problem be cleared too? Or sub-problem should also execute yalmip (‘clear’) to clear all the variables to release memory?

qi wang

unread,
Apr 11, 2019, 10:53:47 AM4/11/19
to YALMIP
5.And in the contrast, after the sub-problem executes yalmip(‘clear’) , will the variables in the main problem be cleared too?

Johan Löfberg

unread,
Apr 11, 2019, 3:04:36 PM4/11/19
to YALMIP
yalmip('clear') deletes all sdpvar objects, but most importantly resets the internal database, which can grow huge, in particular when you have nonlinear terms

murtaza

unread,
Apr 11, 2019, 8:24:33 PM4/11/19
to YALMIP
Hi Professor Johan,

Is there a method for yalmip which just clear a particular variable. I mean yalmip('clear') clear everything(similar to clear in matlab) but do yalmip have method like clearvar in matlab which just clear a particular variable being used repetitively.

Johan Löfberg

unread,
Apr 12, 2019, 1:45:14 AM4/12/19
to YALMIP
you just clear it like you would any other variable in MATLAB (but the internals will not be cleaned up, and that is typically the main problem)

However, if you for some reason feel you have to do some advanced stuff around this, it is a sign that the logic in your code is flawed, typically. Define once, use many times

Eric Zhang

unread,
Jan 4, 2025, 12:12:56 AMJan 4
to YALMIP
Dear Prof Johan,

I am Eric Zhang. Inspired by the aforementioned discussion, I have a new question. Will more and more constraints also make the solving time longer and longer?

According to your suggestions, we should define variables only once before loops and use the variables many times. I followed your suggestion. However, in my problem, the constraints must be changed for each iteration during the loop. That means we cannot define the constraints only once before loops but we have to define the new constraints for each iteration, solve the problem, go to the next iteration, and repeat defining the new constraints again. Therefore, there will be more and more constraints. Although I used 'constraints = [];' to reset the constraints, I do not know if the internals are cleaned up or not.

Finally, I found the 'yalmiptime' reached 50 seconds while the 'solvertime' was below 1 second.

Hence, will more and more constraints also make the solving time longer and longer? If so, what should I do to speed my code up? Thank you so much.

Johan Löfberg

unread,
Jan 7, 2025, 5:53:59 AMJan 7
to YALMIP
More constraints can make solution time both faster and slower, depending on the problem.

yalmip('clear') is related to variables, i.e. don't redefine more and more variables. Either declare them outside any loop, or if using a loop with variables defined inside then clear internals

It could be that you are creating constraints that take a lot of symbolic processing that you should improve by doing things manually, such as defining SOCPs instead of quadratics etc. Impossible to say without code



Reply all
Reply to author
Forward
0 new messages