Hi, I'm new to CVX Programming. I'm implementing a very basic program but it seems like i'm not doing it wrong. Can someone tell me what mistake i'm doing?


(Mobeen Mahmood) #1

image
I’m trying to implement this basic CVX program. I’m confused how to minimize w here.

 b = 30.46;
 cvx_begin
 variable w((2*N+1),M+1) complex
 expressions z1 z2
 M = 7;
 N = 8;
 minimize norm(w)
 z1 = w'*dot_beampattern_sv(RT,thetaT);
 z2 = (M+1)*(2*N+1);
 subject to
 for r = 1:length(R)
 for a=1:length(theta)
 abs(w'*dot_beampattern_sv(R(r),theta(a))) <= b ;
 z1 = z2 ;
 end
 end
 cvx_end

(Mark L. Stone) #2

There are a lot of confusing things here. Your graphic does not clearly convey what the problem to be solved is. And your attempted solution is so mixed up that it only serves to further confuse what the problem is which you want to solve.

Is b a constant (30.46) or is it an optimization variable to be minimized? If the latter, it needs to be declared as a (CVX) variable. And of course, b is a very different objective function than norm(w), so you need to figure out what the objective actually is.

The values of M and N must be set before they are used in a variable declaration. I believe your variable w is supposed to be a complex column vector (it appears so based on everything else), in which case the 2nd dimension must be one (not M+1). I leave you to check what the 1st dimension should be.

I believe your inequality should use square_abs, not abs. Presumably, this constraint is supposed to hold (be specified) for each of several values of s, and if so, I will have to defer to you as to whether your double for loop correctly indexes over all occurrences.

You don’t need to declare z1 and z2 as expressions. Equality constraints need to use == not = . So your equality constraint should be something like
w'*dot_beampattern_sv(RT,thetaT) == (M+1)*(2*N+1)
If, as appears to be the case, there is only one instance of this constraint, i.e., one value of t for which the constraint must hold, then it should not be inside the for loops.

This should be enough to get you started on fixing up your program. There may be more to fix than what I have indicated. You may also want to re-read the CVX Users’ Guide http://cvxr.com/cvx/doc/ .


(Mobeen Mahmood) #3

Hi Mark, Thanks for your reply.

1)I want to find optimized value of w which is a complex matrix of size (17,8). and i want my objective function to minimize w. Here b is just a threshold of value 30.46.

2_)Also, i want optimized values of w should be complex but i couldn’t found out how to write such a objective function. Lastly,
w’dot_beampattern_sv(RT,thetaT) == (M+1)(2N+1) is actually an assignment expression. I want L.H.S to be assigned value of (M+1)(2*N+1). So should i still have to use equality constraints?


(Mark L. Stone) #4

If w is a matrix, then some of your expressions with it result in vectors not scalars. For instance, your equality constraint will be constraining all elements of a vector (the left-hand side) to the value of the right=-hand side. Your inequality also would seem rather strange.

You need to figure out what your objective function is supposed to be, because I don’t know what it is supposed to be.

Use an equality constraint. That forces the left-hand-side to have the value in the right-hand side.

In summary, you need to formulate an optimization problem which makes sense. You need to clearly define what the optimization variables are, what their dimensions are, what the objective function is, and what the constraints are. You don’t seem to have done that. Then figure out how to formulate that problem n CVX.


(Mobeen Mahmood) #5

Thank you mark, I’ll try to look into the details and will see where i’m lagging. If my optimization variable is w of size say (17*8), how will i write it in CVX?


(Mark L. Stone) #6

If you mean for w
to be a 17 by 8 complex matrix variable
variable w(17,8) complex

If you mean for w to be a 17*8 by 1 complex vector variable
variable w(17*8) complex
or
variable w(17*8,1) complex


(Mobeen Mahmood) #7

So my optimization variable is w which is a matrix of dimension 17 by 8. I want optimized value of w to be complex(real + imaginary parts). How can i set this in my objective function?


(Mark L. Stone) #8

Your objective function must be (evaluate to) a real scalar. Perhaps you really do want it to be norm(w) ? And for b to just be input data (contrary to your graphic)? Then you must fix up your constraints so that they make sense for w being a matrix. But everything looks suspicious.

You need to tell us what your optimization problem is. We can’t tell you what it is. Once you have provided us a well-defined optimization problem, we may be able to offer help in applying CVX to it.


(Mobeen Mahmood) #9

Yes, my objective function wasn’t scalar that’s why it wasn’t making any sense. So now i have come up to something like this but it’s taking too long to find optimal value. I need your help.

b = 30.46;
for r=1:length(R)
    for a=1:length(theta)
cvx_begin 
   variable w((2*N+1)*(M+1),1) complex
    minimize real(w'*dot_beampattern_sv(R(r),theta(a)));
    subject to
    abs(w'*dot_beampattern_sv(R(r),theta(a))) <= b ;
      w'*dot_beampattern_sv(RT,thetaT) == (M+1)*(2*N+1) ;
       cvx_end 
    end
end

Here R and theta are having ranges like R=1:1000km, theta = -90:0.5:90. I want to minimize my objective function for each value of R and theta while keeping maximum at only one specified value of R and theta which i’m representing by RT, thetaT. b is a optimization threshold and dot_beampattern_sv is a function which will give my matrix of size w. I think this makes sense now but i’m confused why the optimization is taking way too long. Any idea?


(Mark L. Stone) #10

You are solving a total of 361000 optimization problems. If each problem takes CVX 1 second to formulate and solve, it will take about 100 hours to do all of them.

Moreover, oyu are not saving the results of your optimization problems after each problem (after cvx_end). So when your entire program ends, you will only have the optimal variable values (argmin) available for the last executed problem.


(Mobeen Mahmood) #11

So do i need to run my for loop within cvx begin —cvx end? Because i want my matrix to be optimized over all values of for loop. what do you say?

And how can i access optimized values of w outside cvx?


(Mark L. Stone) #12

If you really need to solve all these optimization problems, then keep cvx_begin … cvx_end inside the for loops. After cvx_end (i.e., before CVX is invoked again), add one or more MATLAB statements to save the results you want into arrays, or whatever… For instance cvx_optval, w, cvx_status.

You could consider making the increments between input data larger (at least for a n initial effort). For instance, making the increment between values of R and theta 10 times larger will reduce the number of problems to be solved by about a factor of 100. For testing purposes, I suggest you run it with length of R and theta each about 2 or 3, and verify it is running properly and you are storing results correctly. At that point, you can consider using cvx_begin quiet to reduce output and perhaps speed things up somewhat. You can then estimate how long large-scale runs will take and plan accordingly. If the run time is too long for you, CVX may not be the best tool, as you are incurring CVX processing overhead for each CVX problem.


(Mobeen Mahmood) #13

Alright. My final result has to contain optimized value of w which is a matrix of (136,1) so that means i don’t want to store optimized value for each for loop of R and theta. I want best optimized W for all possible R and theta. Should i run for loop inside cvx begin----cvx end?


(Mark L. Stone) #14

Given that you want to solve a separate optimization problem for each combination of input data values, you need the cvx_begin … cvx_end within the innermost for loop.

If you store the optimal values of w (16 bytes per element, due to being complex) in a 10000 by 361 by 136 array, that’s only 749.15 MB. So I suspect run time will be much more of an issue than memory.

if you don’t want to store all the optimal w values, you could store the best optimal w, call it, w_best, so far,. After each cvx_end, if the most recent cvx_optval is better than all the previous values, set w_best = w;

Given how long things will take to run, and how little memory is required, you may regret throwing out data which might have been useful, depending on what you see when analyzing results.


(Mobeen Mahmood) #15

Thank you Mark for your detailed answer and comprehensive explanation. I think i’ll need to store best possible w as you said. I was thinking that CVX tool itself will give me the best possible optimized matrix for all possible combinations of R and theta but instead it’ll give me best possible w for each single combination of R and theta so my w will keep on changing with each combination.


(Mobeen Mahmood) #16

Suppose i get optimal array of size (136,1). How can i excess this array outside cvx ?


(Mark L. Stone) #17

Set a MATLAB variable equal to it. After cvx_end, for instance, use the command w_save = w; . Or make w_save a 3 dimensional array by declaring before the for loops, w_save = NaN(136, length(R), length(theta));, then after each cvx_end, use
w_save(:,r,a) = w;