# Help with CVX formulation without for loop

Hi All,

I would appreciate your help in writing this cvx problem without using a for loop.

\min_{\beta} \left\{\sum_{i \sim j}\left(\frac{|\beta_{i}|^{\gamma}}{w_{i}} + \frac{|\beta_{j}|^{\gamma}}{w_{j}}\right)^{1/\gamma} \right\} ~\mbox{subject to}~ \quad\|A\tilde{\beta} - \tilde{\alpha}\beta\|_{\infty} \leq \tau
Here, i \sim j means variable i and j are connected and \gamma > 1 (an integer greater than 1).

I proceed as follows in CVX:
E=edge matrix, a N \times 2 matrix

cvx_begin
variable beta(p)
beta2=(beta./w).^(1/gamma)
ch=[beta2(E(: , 1))  beta2(E(: , 2))]
sumE=sum( (ch(:,1).*ch(:,1) + ch(:,2).*ch(:,2)).^(0.5))
minimize (sumE)
subject to
norm(   $A\tilde{\beta} - \tilde{\alpha}\beta$, inf) $\leq \tau$
cvx_end


but I get an error “Disciplined convex programming error: Illegal operation {convex}.^(0.5)”.

I tried using pow_p but also got an error. I know it comes from sumE in my cvx formulation above, but I don’t know how to circumvent around the problem without using a for loop.

Seal.

Your CVX code doesn’t look like it renders the same thing that your LaTeX code does. I do not think you can avoid the FOR loop. But I can tell you how to handle these terms:
$$\left(\frac{|\beta_{i}|^{\gamma}}{w_{i}} + \frac{|\beta_{j}|^{\gamma}}{w_{j}}\right)^{1/\gamma}$$

Just define this intermediate quantity:

beta2 = beta ./ w.^(1/gamma)


Then each of these terms looks like this:

norm([beta2(i),beta2(j)],gamma)

Hi,
I have two questions about vectorizing the for loops in cvx. I would really appreciate any help.

q1) I vectorize the following for loop as bellow but in running I get the error “Operands to the || and && operators must be convertible to logical scalar values.” could any one help me?

for m_index=1 : U
c1_=0;
for b= 1 : S
for k= 1 : C
c1_= c1_ + ULA(b,m_index,k);
end
end
c1(m_index)=c1_;


end

vectorized constraint:

variables ULA(S,U,C)
expressions c1(1,U)
m_index=1 : U;
c1=sum(ULA(:,m_index,:),[1 3]); %debugger stop here!

q2) I would appreciate your help to vectorize this code. I have read the How to vectorize most constraint loops in CVX thread, but I don’t figure out how to do that for bellow code:

for b=1 : S
for n= 1 : D
for k= 1 : C
I_BU= 0;
for bb= 1 : S
if ( bb ~= b )
for nn= 1 : D
if (nn ~= n)
I_BU= I_BU + X(bb)* DL_p_t(bb,nn,k)* (G_bd(bb,n,k)/temp);
end
end
end
end
C(b,n,k)=I_BU;
end
end
end


Here, X, G_bd, DL_p_t are matrices with dimension of 1*S and S*D*C, S*D*C respectively. DL_p_t is the only cvx variable.

you have to read them carefully, it will work, with the combination of permute and repmat.

for the second question, I can rewrite part of the code as below:

     cond1=repmat((1:S)',1,S)~=(repmat((1:S)',1,S)) %(if bb ~=b) (cond1 dim is S*S)
cond2=repmat((1:D)',1,D)~=(repmat((1:D)',1,D)) %(if nn ~=n) (cond2 dim is D*D)
eq=repmat(X,1,D,C)*DL_p_t .*G_bd /temp %equation dim is S*D*C


my problem is in the phase of applying conditions to the equation. should I at first expand the cond2 and eq to the same dim, which the result has 4 dim! and then applying cond1. In this way, the final result has 5 dim!! while it seems that the result should be S*D*C (C(b,n,k)=I_BU). I’m confused!

Just expand them to higher dimensions. The following might do your job:

F=(repmat((1:S)',1,D,C,S,D)~=permute(repmat((1:S)',1,D,C,S,D),[4 2 3 1 5])).*(permute(repmat((1:D)',1,S,D,C,S),[2 3 4 5 1])~=permute(repmat((1:D)',1,S,C,S,D),[2 1 3 4 5]));
TEMP=F.*permute(repmat( repmat(X',1,C,D,D).* permute(repmat(DL_p_t,1,1,1,D),[1 3 4 2]).* permute(repmat(G_bd,1,1,D),[1 3 2 4])./temp,1,1,1,1,S),[5 3 2 1 4]);
C=sum(sum(TEMP,5),4);


Thanks a lot Jack for your help.