Constraint violation

Hello everybody.
I tried to solve my CP by cvx, but I don’t know why constraints do not work. at the end the answer is NAN and constraints are violated!!!
please help me.

T_1_l=9;   T_1_u=23;  N_a=3;     E_1_max=1.5;  E_1_min=0.2;  eps_1=0.2; 
T_2_l=12;  T_2_u=22;  Taw_2=4;   E_2_min=0.2;  E_2_0=[1 1 1 1];  eps_2=0.2;   
T_3_l=1;   T_3_u=18;  Taw_3=3;   E_3_0=[0.8 1.6 1.1];  E_3_min=0;  eps_3=0.2;
T_4_l=8;   T_4_u=23;   E_4_max=4;    E_4_min=0;  a=0.9;  B=-0.0095;
W_comf=75;  W_des_min=70;  W_des_max=79;  Cc_1=0.1;   Cc_2=0.1;
T_5_l=1;   T_5_u=24;   E_5_max=2.3;  E_5_min=0; 
D_max=0.2;   R_5_l=-0.25;   R_5_u=0.25;   B_5_max=0.8;  Gamma_5=0.5;  B_5_ini= 0.4  % sahmi az charge battery k bayad akhar bemoone :)
lambda_1=1;  lambda_2=1;    lambda_3=1;    %?????? az maghale bayad dar biad
eta_5_1=5*(exp(-7));  eta_5_2=4*(exp(-7));  eta_5_3=1;  eta_5_4=0.2;
T_s_l=7;  T_s_u=18;

P=[3 2.75 2.35 2.5 2.30 2.5 2.75 3.25 3.30 3.35 3.45 3.5 3.4 3.3 3.25 3.15 3.15 3.5 4.25 4.5 4 6.75 3.5 3.35];
W_out=[65 62 62 61 61 62 70 80 82 83 85 86 88 90 92 95 97 100 95 88 83 80 78 76];

n1=T_1_u-T_1_l+1;
n2=T_2_u-T_2_l-Taw_2+2; %=M_2
n3=T_3_u-T_3_l-Taw_3+2; %=M_3
n4=T_4_u-T_4_l+1;
n5=T_5_u-T_5_l+1;

cvx_begin

variable y_1(n1,1);
variable Lambda_2(1,n2);
variable Lambda_3(1,n3);
variable ee_4(n4,1);
variable d_5(n5,1);
variable b_5(n5,1);
expression e_1(24,1);
expression e_2(24,1);
expression e_3(24,1);
expression e_4(24,1);
expression e_5(24,1);
expression c_1(24,1);
expression c_2(24,1);
expression c_3(24,1);
expression c_4(24,1);
expression c_5(24,1);
expression c_5_b(24,1);
expression c_5_d(24,1);
expression W_in(24,1);

for t=1:24
    if (T_1_l)<=t && t<=(T_1_u)
        e_1(t)=E_1_max*y_1(t-T_1_l+1)+E_1_min*(1-y_1(t-T_1_l+1));
        c_1(t)=(1+eps_1*t)*y_1(t-T_1_l+1);
        
    else
        e_1(t)=0;
        c_1(t)=0;
    end
    e_1=e_1';
    C_1=c_1';
    
    

     %SA-UL1:a2
    if (T_2_l)<=t && t<=(T_2_u)
        for m=1:(T_2_u-T_2_l-Taw_2+2)  
            if (T_2_l+m)<=t && t<=(T_2_l+m+Taw_2-1);
                E_2(m,t)=E_2_0(t-(T_2_l+m-1));
                w_2(m,t)=1+eps_2*(T_2_l+m-1);
            else
                E_2(m,t)=0;
                w_2(m,t)=0;
            end
        end
                e_2=Lambda_2*E_2;  
                c_2=Lambda_2*w_2;
    else 
        e_2(t)=0;
        c_2(t)=0;
    end
    e_2=e_2';
    C_2=c_2';
    

    if (T_3_l)<=t && t<=(T_3_u)
        for m=1:(T_3_u-T_3_l-Taw_3+2) 
            if (T_3_l+m)<=t && t<=(T_3_l+m+Taw_3-1);
                E_3(m,t)=E_3_0(t-(T_3_l+m-1));
                w_3(m,t)=1+eps_3*(T_3_l+m-1);
            else
                E_3(m,t)=0;
                w_3(m,t)=0;
            end
        end
                e_3=Lambda_3*E_3;
                c_3=Lambda_3*w_3;
    else 
        e_3(t)=0;
        c_3(t)=0;
    end
    e_3=e_3';
    C_3=c_3';

    if (T_4_l)<=t && t<=(T_4_u)
            if t==T_4_l
                W_in(t)=70+a*(W_out(t)-70)+(1000*B)*ee_4(t-T_4_l+1);
                e_4(t)=ee_4(t-T_4_l+1);
            else
                W_in(t)=W_in(t-1-T_4_l+1)+a*(W_out(t)-W_in(t-1-T_4_l+1))+(1000*B)*ee_4(t-T_4_l+1);
                e_4(t)=ee_4(t-T_4_l+1);
            end
            c_4(t)=Cc_1*(square(W_in(t)-W_comf))-Cc_2;
        else
            e_4(t)=0;
            c_4(t)=0;
    end
        e_4=e_4';
        C_4=c_4';

        if (T_s_l)<=t && t<=(T_s_u)
            v_t(t)=0.2;
        else
            v_t(t)=0;
        end
        v_t=v_t';
end      
       uu=(e_1+e_2+e_3+e_4)-v_t;
       u=max(0,uu);
        C=(C_1+C_2'+C_3'+C_4)';
minimize (P*u+sum(C(:)))


subject to

        0<= y_1 <=1;
        0<= Lambda_2 <=1
        0<= Lambda_3 <=1
        E_4_min<= e_4 <=E_4_max
        W_des_min<= W_in <=W_des_max
        0<= d_5 <=D_max
        R_5_l<= b_5 <=R_5_u 
        for t=1:24
        0<= B_5_ini+sum(b_5(1:t)) <=B_5_max
        end
cvx_end

I don’t know what is “preformatted text”, could you pleas explain for me?
for example, y_1 should be bounded by (0,1); but after running the code, it is out of bound!!!
also, this objective function has 4 parts, when I comment out 3 of them, the constrain for remain is ok, but for more than one, constraints are out of bound.

thank you so much

Try editing your post and use the “preformatted text” , i.e. code, option for your entire MATLAB code. Then we can see what you really entered. As it appears now, %% VARIABLES variable y_1(n1,1); variable … appears to be commented out, but I can’t tell if that is true of your actual code.

You said the result was NaN; are you saying the solver failed?

See below for output from SeDuMi.

Here is the output from SDPT3:

Calling SDPT3 4.0: 381 variables, 141 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 141
 dim. of sdp    var  = 32,   num. of sdp  blk  = 16
 dim. of linear var  = 333
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|3.4e+01|1.6e+01|8.9e+06| 6.725677e+04  0.000000e+00| 0:0:01| chol  1  1 
 1|0.953|0.622|1.6e+00|6.2e+00|3.6e+06| 4.662503e+04 -6.821024e+03| 0:0:03| chol  1  1 
 2|0.504|0.065|7.9e-01|5.8e+00|4.6e+06|-3.463685e+06 -1.088162e+04| 0:0:03| chol  1  1 
 3|0.170|0.007|6.5e-01|5.8e+00|1.4e+07|-1.163356e+10 -1.190917e+04| 0:0:03| chol  1  1 
 4|0.061|0.001|6.1e-01|5.8e+00|9.7e+10|-1.344011e+15 -1.224312e+04| 0:0:03| chol  1  1 
 5|0.000|0.000|6.1e-01|5.8e+00|1.6e+13|-2.149955e+18 -2.734609e+05| 0:0:03| chol  1  1 
 6|0.000|0.000|6.1e-01|5.8e+00|1.9e+17|-3.609032e+22 -1.511796e+06| 0:0:03|
  sqlp stop: primal or dual is diverging, 1.4e+17
-------------------------------------------------------------------
 number of iterations   =  6
 Total CPU time (secs)  = 3.35  
 CPU time per iteration = 0.56  
 termination code       =  3
 DIMACS: 1.9e+00  0.0e+00  1.0e+01  0.0e+00  -1.0e+00  5.3e-06
-------------------------------------------------------------------
------------------------------------------------------------
Status: Failed
Optimal value (cvx_optval): NaN

Here is the output from SeDuMi:

Calling SeDuMi 1.34cvx: 381 variables, 141 equality constraints
   For improved efficiency, SeDuMi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.34cvx by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 141, order n = 366, dim = 382, blocks = 17
nnz(A) = 1063 + 0, nnz(ADA) = 1397, nnz(L) = 855
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.38E+02 0.000
  1 :  -4.26E+02 1.53E+02 0.000 0.6400 0.9000 0.9000   2.99  1  1  6.1E+00
  2 :  -4.05E+04 3.79E+01 0.000 0.2485 0.9000 0.9000  -0.71  1  1  1.3E+01
  3 :  -2.02E+06 1.31E+00 0.000 0.0347 0.9900 0.9900  -0.62  1  1  2.0E+01
  4 :  -7.17E+09 2.98E-04 0.000 0.0002 0.9999 0.9999  -0.99  1  1  1.6E+01
  5 :  -1.04E+12 2.04E-06 0.000 0.0069 0.9990 0.9990  -1.00  4  6  1.6E+01
  6 :  -9.77E+14 2.17E-09 0.000 0.0011 0.9999 0.9999  -1.00 11 16  1.7E+01

Dual infeasible, primal improving direction found.
iter seconds  |Ax|    [Ay]_+     |x|       |y|
  6      0.9   8.9e-13   2.6e-10   8.8e-02   4.2e+02

Detailed timing (sec)
   Pre          IPM          Post
2.463E+00    3.481E+00    6.000E-02    
Max-norms: ||b||=3.790000e+01, ||c|| = 79,
Cholesky |add|=9, |skip| = 0, ||L.L|| = 3467.55.
------------------------------------------------------------
Status: Infeasible
Optimal value (cvx_optval): +Inf

Edit: Thanks to Erling for pointing out that, unlike SeDuMi and MOSEK, SDPT3 can not reliably detect infeasibility by default. See my comment under Erling’s answer inquiring about non-default settings.

I am repeating here my comment in response to the OP’s comment, so that the answer will be seen to have been revised, in order to catch the attention of the OP, since comments do not trigger any change on http://ask.cvxr.com/questions/ .

By PSO, do you mean Particle Swarm Optimization? If so, which software program did you use, and does it claim to enforce constraints? Insert the “answers” from PSO into the constraints, and directly verify their feasibility or not. If the “answers” are not feasible, then they’re not solutions. If any of the “answers” are feasible, request Erling to run the problem in MOSEK for you and tell you what happens.

From Marks output the problem seems to be infeasible. Now SDPT3 in contrast to SeDuMi and MOSEK cannot detect infeasibility reliably by default. Therefore, you may get NANs and other funny things.

sorry,I don’t know what is “preformatted text”, could you pleas explain for me? and also I think the problem definition is correct, and I got answers by PSO.

We fixed the preformatted text issue for you.

By PSO, do you mean Particle Swarm Optimization? If so, which software program did you use, and does it claim to enforce constraints? Insert the “answers” from PSO into the constraints, and directly verify their feasibility or not. If the “answers” are not feasible, then they’re not solutions.

Erling, are there non-default settings which make SDPT3 reliably detect infeasibility?

The SDPT3 package has two solvers: a homogeneous self-dual variant and a so-called infeasible method. The latter is the default; I don’t think the HSD solver was ever more than an experiment. And that’s too bad, because HSD solvers are better with infeasible problems.

Thank you so much dear Mark and dear mcg for your kindly help. I checked my answers as you said and the problem was infeasible! Now I fixed this problem, but an error is remained: " Invalid quadratic form(s): not a square". could you pleas help me?

Thank you so much dear Mark and dear mcg for your kindly helps. I checked my answers and the problem was infeasible. Now I fixed that, but an error is remained: “Invalid quadratic form(s): not a square”. Could you pleas help me?

That is a sign that somewhere in your new model you have multiplied to CVX variables together in a way that violates the DCP ruleset. The only valid quadratic forms are things like x'*x or x'*P*x that produce scalar convex quadratics.

all terms I multiplied, are scalar : c_5_b(t)=eta_5_1square(b_5(t-T_5_l+1))-eta_5_2(b_5(t-T_5_l+1)b_5(t+1-T_5_l+1)) +eta_5_3(min((b_5(t-T_5_l+1)-eta_5_4*B_5_max),0))^2; where b_5 is my variable

You’ll have to track down the exact expression where the error occurred. I’m not saying CVX is bug-free—it most certainly is not!—but I cannot remember the last time it was wrong about a DCP ruleset violation. (The error messages could stand to be improved sometimes though.)

Here is final edition dear Mark.

clc
clear all
close all
cvx_begin
  
T_1_l=9;   T_1_u=23;  N_1=3;     E_1_max=1.5;  E_1_min=0.2;  eps_1=0.2; 

T_2_l=12;  T_2_u=22;  Taw_2=4;   E_2_min=0.2;  E_2_0=[1 1 1 1];  eps_2=0.2;   

T_3_l=1;   T_3_u=18;  Taw_3=3;   E_3_0=[0.8 1.6 1.1];  E_3_min=0;  eps_3=0.2;

T_4_l=8;   T_4_u=23;   E_4_max=4;    E_4_min=0;  a=0.9;  B=-0.0095;
W_comf=75;  W_des_min=70;  W_des_max=79;  Cc_1=0.1;   Cc_2=0.1;

T_5_l=1;   T_5_u=24;   E_5_max=2.3;  E_5_min=0; 
D_max=0.2;   R_5_l=-0.25;   R_5_u=0.25;   B_5_max=0.8;  Gamma_5=0.5;  B_5_ini= 0.4  % sahmi az charge battery k bayad akhar bemoone :)
lambda_1=1;  lambda_2=1;    lambda_3=1;    %?????? az maghale bayad dar biad
eta_5_1=5*(exp(-7));  eta_5_2=4*(exp(-7));  eta_5_3=1;  eta_5_4=0.2;

T_s_l=7;  T_s_u=18;

P=[3 2.75 2.35 2.5 2.30 2.5 2.75 3.25 3.30 3.35 3.45 3.5 3.4 3.3 3.25 3.15 3.15 3.5 4.25 4.5 4 6.75 3.5 3.35];
W_out=[65 62 62 61 61 62 70 80 82 83 85 86 88 90 92 95 97 100 95 88 83 80 78 76];

n1=T_1_u-T_1_l+1;
n2=T_2_u-T_2_l-Taw_2+2; %=M_2
n3=T_3_u-T_3_l-Taw_3+2; %=M_3
n4=T_4_u-T_4_l+1;
n5=T_5_u-T_5_l+1;

variable y_1(n1,1);
variable Lambda_2(n2,1);
variable Lambda_3(n3,1);
variable ee_4(n4,1);
variable d_5(n5,1);
variable b_5(n5,1);
expression e_1(24,1);
expression c_1(24,1);
expression e_2(24,1);
expression c_2(24,1);
expression e_3(24,1);
expression c_3(24,1);
expression e_4(24,1);
expression c_4(24,1);
expression W_in(24,1);
expression e_5(24,1);
expression c_5(24,1);
expression c_5_b(24,1);
expression c_5_d(24,1);
expression minn(1,24);
for t=1:24
    
    if (T_1_l)<=t && t<=(T_1_u)
        e_1(t)=E_1_max*y_1(t-T_1_l+1)+E_1_min*(1-y_1(t-T_1_l+1));
        c_1(t)=(1+eps_1*t)*y_1(t-T_1_l+1);
    else
        e_1(t)=0;
        c_1(t)=0;
    end
    e_1=e_1';
    C_1=c_1';

    
    
if (T_2_l)<=t && t<=(T_2_u)
        for m=1:(T_2_u-T_2_l-Taw_2+2)         %m too formool ha az 0 ta M-1 boode, chon too matris mire vase shomaresh yeki ezafe kardam!!!
            if (T_2_l+m-1)<=t && t<=(T_2_l+m+Taw_2-2);
                E_2(t-T_2_l+1,m)=E_2_0(t-(T_2_l+m-2));
            else
                E_2(t-T_2_l+1,m)=E_2_min;
            end
            w_2(t-T_2_l+1,m)=1+eps_2*(T_2_l+m-1);
        end
         else 
        e_2(t)=0;
        c_2(t)=0;
end
if (T_3_l)<=t && t<=(T_3_u)
        for m=1:(T_3_u-T_3_l-Taw_3+2)         %m too formool ha az 0 ta M-1 boode, chon too matris mire vase shomaresh yeki ezafe kardam!!!
            if (T_3_l+m-1)<=t && t<=(T_3_l+m+Taw_3-2);
                E_3(t-T_3_l+1,m)=E_3_0(t-(T_3_l+m-2));
            else
                E_3(t-T_3_l+1,m)=E_3_min;
            end
            w_3(t-T_3_l+1,m)=1+eps_3*(T_3_l+m-1);
        end
         else 
        e_3(t)=0;
        c_3(t)=0;
end

    if (T_4_l)<=t && t<=(T_4_u)
            if t==T_4_l
                W_in(t)=70+a*(W_out(t)-70)+(1000*B)*ee_4(t-T_4_l+1);
                e_4(t)=ee_4(t-T_4_l+1);
            else
                W_in(t)=W_in(t-1-T_4_l+1)+a*(W_out(t)-W_in(t-1-T_4_l+1))+(1000*B)*ee_4(t-T_4_l+1);
                e_4(t)=ee_4(t-T_4_l+1);
            end
            c_4(t)=Cc_1*(square(W_in(t)-W_comf))-Cc_2;
    else
        if t==1
            W_in(t)=W_des_min;
        else
            W_in(t)=W_in(t-1);
        end
            e_4(t)=0;
            c_4(t)=0;
        
    end
        e_4=e_4';
        C_4=c_4';
     
            e_5(t)=E_5_max*(1-d_5(t-T_5_l+1))+b_5(t-T_5_l+1); %pos
            if t==T_5_u
                eta1(t)=eta_5_1*square((b_5(t-T_5_l+1)));
                eta2(t)=eta_5_2*(b_5(t)*B_5_max*Gamma_5);
                eta3(t)=eta_5_3*square_pos(max((-b_5(t-T_5_l+1)+eta_5_4*B_5_max),0));
                c_5_b(t)=eta1(t)-eta2(t)+eta3(t);
                c_5_d(t)=pow_p(lambda_1*(D_max-d_5(t-T_5_l+1)+lambda_2),-lambda_3);
                c_5(t)=c_5_b(t)+c_5_d(t);
            
            elseif (T_5_l)<=t && t<(T_5_u)  
                eta1(t)=eta_5_1*square((b_5(t-T_5_l+1)));
                eta2(t)=eta_5_2*(b_5(t))*b_5(t+1);
                eta3(t)=eta_5_3*square_pos(max((b_5(t-T_5_l+1)-eta_5_4*B_5_max),0));
                c_5_b(t)=eta1(t)-eta_2(t)+eta3(t);
                c_5_d(t)=pow_p(lambda_1*(D_max-d_5(t-T_5_l+1)+lambda_2),-lambda_3);
                c_5(t)=c_5_b(t)+c_5_d(t);
            else 
                e_5(t)=0;
                c_5(t)=0;
        end
        e_5=e_5;
        C_5=c_5;

 if (T_s_l)<=t && t<=(T_s_u)
            v_t(t)=0.2;
        else
            v_t(t)=0;
        end
        v_t=v_t';
end
e_2(T_2_l:T_2_u)=E_2*Lambda_2;       % farz mikonam lambda yek matrise sotoonie m*1 ast va E_2 ham yek matrisesatrie 1*m bedast amade,be jaye sigma,zarbe matrisi neveshtam
c_2(T_2_l:T_2_u)=w_2*Lambda_2;
   
    e_2=e_2;
    C_2=c_2';
    
e_3(T_3_l:T_3_u)=E_3*Lambda_3;       % farz mikonam lambda yek matrise sotoonie m*1 ast va E_2 ham yek matrisesatrie 1*m bedast amade,be jaye sigma,zarbe matrisi neveshtam
c_2(T_3_l:T_3_u)=w_3*Lambda_3;
   
    e_3=e_3;
    C_3=c_3';

 uu=(e_1+e_2+e_3+e_4+e_5)-v_t;
 u=max(0,uu);
 C=(C_1+C_2+C_3+C_4+c_5');
 minimize (P*u+sum(C(:)));
 
 
 subject to
 
 0<= y_1 <=1;
 sum(y_1(:,1))==N_1;
 0<= Lambda_2 <=1;
 sum(Lambda_2(:,1))==1;
 0<= Lambda_3 <=1;
 sum(Lambda_3(:,1))==1;
 E_4_min<= e_4 <=E_4_max;
 W_des_min<= W_in <=W_des_max
 0<= d_5 <=D_max;
 R_5_l<= b_5 <=R_5_u;
         for t=1:24
        0<= B_5_ini+sum(b_5(1:t)) <=B_5_max;
        end
 
 cvx_end

and error is as below:

??? Error using ==> cvx.times at 262
Disciplined convex programming error:
    Invalid quadratic form(s): not a square.


Error in ==> cvx.mtimes at 36
    z = feval( oper, x, y );

Error in ==> new at 125
                eta2(t)=eta_5_2*(b_5(t))*b_5(t+1);

You have a couple of instances of squaring variables in violation of DCP rules. I’ll let you figure out whether your model can be reformulated consistent with DCP rules.

For example: Error using cvx/pow_cvx (line 142)
Disciplined convex programming error:
Illegal operation: {concave} .^ {2}
(Consider POW_P, POW_POS, or POW_ABS instead.)
Error in cvx/power (line 55)
z = pow_cvx( x, y, ‘power’ );

You would be more likely to quickly get helpful help if you provide more information. For instance, you could have told us the errors you were getting and where.

you are right, but I don’t know what should I do!!!