# Can this be solved in CVX?

(Dipak Narayanan) #1

Here, S is a given matrix, T is a known positive number
D is a known column vector

Lets say, K=32768 and T=4

``````cvx_begin
variable x(K,1) integer
minimize( power(2,norm(D.'-V,2)) )
subject to
V == (1/T)*sum(S.*x);
sum(x)== T;
cvx_end
``````

I am using MOSEK and Get an error as “Invalid variable specification: x(K,1)integer”

Can this optimization problem be modeled in CVX?
(Mark L. Stone) #2

If `K` really does equal 32768,
`variable x(K,1) integer`
should be accepted by CVX.

(Dipak Narayanan) #3

@Mark_L_Stone, Unfortunately, I am getting the same error!

(Mark L. Stone) #4

Perhaps K isn’t being set properly.

What does
`whos K`
show? What does `disp(K)` show?

Or maybe your MATLAB session got corrupted. Try starting from a new MATLAB session.

(Dipak Narayanan) #5

@Mark_L_Stone, disp(K) shows 32768. Anyway, below is the code I am using now,

``````cvx_solver Mosek

N=15;
K=2^N;
S=randi([5 10],K,N).*fliplr(de2bi((0:K-1)'));
OptS=[2^1 2^(N-2) 2^(N-1) 2^N];
N_TS=length(OptS);
D=((sum(S(OptS,:)))/N_TS)';

cvx_begin
variable x(K,1)integer
minimize(power(2,norm(D.'-V,2)))
subject to
V == (1/N_TS)*sum(S.*x);
sum(x)== N_TS;
cvx_end``````

(Mark L. Stone) #6

You need to fix this up and show a clean code, with no typos. Your code is missing the “n” at the end of dec2bin.

And most crucially, in your most recent post, there is no space between
`x(K,1)`
and
`integer`
As written, this produces the exact error message you received If a space is inserted before `integer`, that line is accepted by CVX. You are making it difficult for people to diagnose your problems, because in your first post, there is a space before `integer`, hence that runs without error.

(Dipak Narayanan) #7

@Mark_L_Stone, I think “de2bi” is correct.
Typing…help de2bi in matlab gives
de2bi Convert decimal numbers to binary numbers.
B = de2bi(D) converts a nonnegative integer decimal vector D to a
binary matrix B. Each row of the binary matrix B corresponds to one
element of D. The default orientation of the binary output is
Right-MSB; the first element in B represents the lowest bit.

Yes, unfortunately, I didnot put space between … and integer.

(Mark L. Stone) #8

Hopefully your problem is now resolved.

As for dec2bi , that produced an error message for me. I see now that it is in Communications System toolbox, which I don’t have, hence my error message…

(Dipak Narayanan) #9

@Mark_L_Stone, Unfortunately, no. Now, I get…

Undefined function or variable ‘V’.

Didn’t you get this error?

If I instead replace V with its equivalent term itself, it shows…

``````cvx_begin
variable x(K,1) integer
minimize( power(2,norm(Demand.' - (1/N_TS)*sum(S.*x), 2)))
subject to
sum(x)== N_TS;
cvx_end
``````

But if S is a matrix of size (32768 X 15) and x is a column vector of size (32768 X 1), sum(S.*x) should work fine.

Error using .* (line 46)
Matrix dimensions must agree.

(Mark L. Stone) #10

If you never declared or set `V,` then it will be undefined and produce an error.

Do you want `sum(S'*x)` ? That is dimensionally consistent `S.*x` is not dimensionally consistent in CVX (note that the new construct in MATLAB R2016b described in https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/ is not supported in CVX, i.e., for CVX expressions).

(Dipak Narayanan) #11

@Marc_L_Stone, Thank you very much.

Let,
S=[1 2 3 4;2 3 4 5; 3 4 5 6];
x=[1; 2; 3];

In my definition
S.*x=[1 2 3 4;4 6 8 10;9 12 15 18]
sum(S.*x)=[14 20 26 32]

That is I want to have
sum(S(i,:)*x(i)), i=1 to 32768 when S (32768 X 15) and x (32768 X 1)

That is the i’th row vector of S is multiplied by the i’th element in x. So, all the elements of the i’th vector is multiplied by the same number which is the i’th element in vector x.

(Mark L. Stone) #12

It looks like
`sum(S.*repmat(x,1,15))`
will do what I think (but am not sure) you want.

Or more simply,
`x'*S`

In your example, using 4 instead of 15, these both come out to [14 20 26 32] .

At this point, I think you are just dealing with matters of MATLAB syntax, not really CVX. So you may need to brush up on MATLAB.