Disciplined convex programming error help

(Saqib Iqbal) #1

When constraint dv ==J\dP; is executed the following error occur.

Cannot perform the operation {mixed constant/affine}*{concave}
z = mtimes( x, y, ‘ldivide’ );

(Mark L. Stone) #2

Is this the same problem as your post at 1/x^2 (convex using DCP) ? if so, pick one, and show the full problem details. if not, show the full problem details here.

If you don’t see it listed in the CVX User’s Guide, it is not allowed. Multiplication of two CVX variables or expressions, as you have apparently done, is not allowed in CVX, except in Geometric Programming mode, which has its own set of rules.

(Saqib Iqbal) #3

clear all;
close all;

%% Pre defined Data
N=2; %Number of houses, PVs and Batteries

load p_ava.mat; %PV Available
p_ava=p_ava(1:N,:);
t=24; %time slots

T=t+1;
b_d_r=200ones(N,t); %Battery charging rate
b_c_r=200
ones(N,t); %Battery Discahrging rate
l= randi(N,N,t); %Load Demand

%% Distance Calculation
distpl=zeros(N,N); %Distances from PV to Load
for ii = 1:N
for jj = 1:N
distpl(ii,jj) = abs(ii - jj);
end
end
distbl=distpl; %Distances from Battery to Load
distpb=distpl; %Distances from PV to Battery

%% Optimization Code
cvx_begin
variables pl(N,N,t) bl(N,N,t) pb(N,N,t) B(N,T) p(N,24) psize LLLL

minimize(LLLL+sum(sum(squeeze(sum(bl,3)).*distbl))+sum(sum(squeeze(sum(pl,3)).*distpl))+sum(sum(squeeze(sum(pb,3)).distpb)) + 5sum(max(B’)))

subject to

LLLL==loss(pl);
pl>=0; bl>=0; pb>=0; B>=0; p>=0; psize>=0; %non-negitivety constraints
squeeze(sum(pb,2)) <=b_c_r ; %battery charing rate
squeeze(sum(bl,1)) <=b_d_r; %battery discharging rate

squeeze(sum(pl,1))+ squeeze(sum(pb,1)) == p; %pv supply
squeeze(sum(pl,2)) + squeeze(sum(bl,2)) == l; %load demand
p-psize*p_ava==zeros(N,t); %pv size & available irradiance
for i=1:1:t; %Battery SOC
pbm=sum(pb,2);
blm=sum(bl,1);
blmx=blm(:,:,i);
blmx=blmx’;
pbmx=pbm(:,:,i);
B(:,i+1) - B(:,i) - pbmx + blmx == zeros(N,1);
end
B(:,1)==B(:,25); %Battery initial and final SOC

cvx_end
%%

function [LLLL]=loss(pl)
N=2;
n=N/2;

y=zeros(702,3);
POW=[5 10 20 50 100 150 200 250 300 350 400 450 500];

V=[24 48 120 230 325 380];

res=2.*[0.004066 0.005127 0.008152 0.01296 0.02061 0.03277 .05211 .08286 0.1317];

LL=0;
LLLL=0;
for x=1:1:2;
for y=1:1:2;
for z=14:1:14;

for w=4:1:4;

POWER=POW(w+1);

for e=1:1:1;

volt =V(e+1);
volt;
AM=zeros(N+1,N+1);
for r=0:1:0

%% G matrix for C

for i=1:n+1
for j=1:n+1
if abs(i-j)==1
AM(i,j)= -1/res(r+1);
elseif i==j
AM(i,j)=2/res(r+1);
end
end
end

for i=n+2:N+1
for j=n+2:N+1
if abs(i-j)==1
AM(i,j)= -1/res(r+1);
elseif i==j
AM(i,j)=2/res(r+1);
end
end
end

AM(1,N+1)= -1/res(r+1);
AM(N+1,1)= -1/res(r+1);
AM(n+1,n+1)=0.5AM(n+1,n+1);
AM(n+2,n+2)=0.5
AM(n+2,n+2);

%% Newton Raphson
AAA=10ones(2,2,14);
P_sch=[pl(x,y,z); -pl(x,y,z)];
vslack=volt;
vsup=volt
ones(N,1);
v=[vslack; vsup];
P=zeros(N+1,1);
dP=ones(N+1,1);
dv=ones(N+1,1);
count=0;
J1=0;

for count=1:1:100;
        
        P = zeros(N*3,1);
        P_cal= zeros(N+1,1);
        
        for i=1:N+1
            for j=1:N+1
                P(i)=P(i)+v(i)*v(j)*AM(i,j);
                 
            end
        end
        Pcal=P(2:N+1); %storing all the elements of P except first one in pcal
        
        dP = P_sch - Pcal;
        
        
        %% calculating Jacobian
        
        for i=1:N+1 % Pi
           for j=1:N+1 % differentiate every Pi with all the Vj
                if i==j
                   J1=0;
                   for k=1:N+1
                       J1 = J1 + v(k)*AM(j,k);
                   end
                   T_J(i,j) = J1 + v(i)*AM(i,j); % main assignment
               else
                   T_J(i,j) = v(i)*AM(i,j); % main assignment
               end
           end
        end
        for i=1:N
           for j=1:N
               J(i,j)= T_J(i+1,j+1);
           end
       end
        
    %dv =inv_pos(J)*[0.001;0.001];
 
      dv =J\dP;
       vsup = vsup+dv ;
       v = [vslack; vsup];
        count = count+1;
end

V_drop = 100*(volt-min(v))/volt;

%% Efficiency
for i=1:N+1
for j=1:N+1
LL = LL + ((v(i)-v(j))^2)*abs(AM(i,j));
end
end

V_drop;
Eff = 100*(POWERN)/(POWERN+LL);
%y((r+1),:)=[LL V_drop Eff];
end

end

end
LLLL=LLLL+LL;
LL=0;
end
end
end

end

(Mark L. Stone) #4

Do you want to help us out by telling us what problem you got where in your code? You have not provided a reproducible problem, because the input data has not been provided. You have a relatively short CVX program followed by lengthy MATLAB code. The code J\dP (but now as a MATLAB assignment v = J\dP rather than a CVX constraint v == J\dP) which you mentioned in your first post as causing a CVX error, is after cvx_end, so is a MATLAB calculation that at that point has nothing to do with CVX, and can not possibly produce a CVX error.

(Saqib Iqbal) #5

when i am calling loss(pl) function in cvx the following error occur “Conversion from cvx to double is not possible”…

(Mark L. Stone) #7

Given that your function loss is called with a CVX variable argument, it must follow all CVX’s rules, which obviously does not

I my previous post, I hadn’t looked carefully enough to see that the code after cvx_end is a function you are calling from CVX, so that code is relevant to CVX.

(Saqib Iqbal) #8

Error using .* (line 188)
Disciplined convex programming error:
This expression produced one or more invalid numeric values (NaNs).

Error in * (line 36)
z = feval( oper, x, y );

Error in loss (line 77)
**P(i)=P(i)+v(i)v(j)AM(i,j);

Error in conop_14_6 (line 34)
LLLL==loss(pl);

(Mark L. Stone) #9

Have you looked at the values of the items on the right-hand side just before the error? Which ones are CVX expressions? Of the ones which are not, do they have valid (double precision) values which are not NaN? CVX will produce an error if it operates on any inputs which have value NaN.

(Saqib Iqbal) #10

Yes, v(i) and AM(i,j) have valid values… and P(i) have cvx zero scalar as assigned earlier…

(Mark L. Stone) #11

What values of i and j is the error occurring at? You’ll just have to step though things using standard debugging techniques to see where something is first going wrong.

If you look at whos just before the error, that might be informative.

(Saqib Iqbal) #12

i=1 and j=1… look at whos???

(Mark L. Stone) #13

Look at whos just before the error occurs. Make sure that all items are as you expect. Correct dimensions, correct type (cvx expression vs. double precision, etc.)? You better not be multiplying cvx expressions.

Also keep in mind that sometimes CVX’s error message are not very indicative of what the real error is, but CVX is good at printing some error when there is an error, it might just have a misleading description.

(Saqib Iqbal) #14

Thanks for your response. I have debugged the code at each iteration …
At count=1, v(i) and v(j) are double variable and have valid values, where P(i) has cvx zero.

While at count=2, v(i) and v(j) both now are cvx expressions. …Multiplication of cvx is the issue, i am facing… *ANY ALTERNATE FOR THIS

(Mark L. Stone) #15

Based on that, I will assume your problem is non-convex, unless you prove otherwise. It looks like a big complicated mess, for which I have no idea what you are trying to do, other than the clue in your comment “Newton Raphson”:, which sounds like a rather suspicious thing to be inside a CVX program (calculations).

If you really have a non-convex problem (and I don’t know for sure), you should use a different tool, such as maybe YALMIP, which can handle non-convex nonlinearities if a suitable solver is called. Of course, YALMIP developer and forum answerer, Johan, might tell you that your model (if translated into YALMIP code) makes no sense.