Error using sparse. sparse(A) does not accept char inputs A

I am trying to solve the optimization problem given below:

\max log_2det(\delta * H * H')
subject to
trace(\delta) < 3

Here, H is a 3$\times$3 matrix and \delta (the optimization variable) is a diagonal matrix of the same size. Here is the code I have written:

H = [1 2 3; 4 5 6; 7 8 9];
cvx begin

variables double(v(1,3))

% Objective Function
maximize (log2 (det (diag((v)) * H * H')))

% Constraints
subject to
1 <= trace(diag(v)) < 3;

cvx end

However, I am getting an error that says, “Error using sparse sparse(A) does not accept char inputs A. Use sparse(double(A)) instead.” What am I doing wrong here? Thanks in advance for the help.

You have several syntax errors as well as optimization formulation problems.

Use cvx_begin and cvx_end. Use log_det (you can divide by log(2) if you want, but I won’t bother). Don’t use double.

The argument of log_det, namely diag((v)) * H * H' is not symmetric, so I think. is not a convex optimization problem. I will now guess that you intended H*diag(v)*H' to be that argument. Making all these changes, a problem which can be accepted by CVX can be entered (I also changed < 3 to <= 3, which is how it will be interpreted by CVX).

H = [1 2 3; 4 5 6; 7 8 9];
cvx_begin
variables v(1,3)
maximize (log_det (H*diag((v))* H'))
% Constraints
subject to
1 <= trace(diag(v)) <= 3;
cvx_end

However, this problem is unbounded, as can be seen by making v(1) large and v(2) = -v(1), which will still meet the constraint 1 <= trace(diag(v)) <= 3.

If you add a constraint such as v >= 0, the problem will be bounded and have a finite solution. However, with sedumi, sdpt3, and scs, in CVX3.0beta build 1177, this problem incorrectly is reported as being unbounded (i.e., with constraint v >= 0 added) which appears to be a bug in CVX 3.0 beta build 1177, because CVX 's successive approximation method is not being used when scs is used.

Wow! Thanks a lot for your reply. I have two more questions.

When I solve this system, the result is not integer. I tried to add some if/else statement to round the variable but CVx does not allow it. Is there anyway to get the optimized variable in binary form?

Also, if its not possible to get the binary result, how should I rewrite the problem to get the integer form solution for the diagonal matrix? Thanks a lot.

I think you’re out of luck for solving a mixed integer version of this problem with CVX and current versions of solvers. If you have CVX Professional and MOSEK, there is an integer capability for MILP/MIQP/MISOCP, but not for MISDP. Note that although CVX will call MOSEK for mixed integer problems in which CVX"s successive approximation method is used, CVX’s successive approximation will not work correctly, and will return unreliable, quite possibly erroneous results.

Although I suggested using logdet, for your problem, you could maximize det_rootn instead. The argmax will be the same. This will not cause CVX"s successive approximation method to be invoked. The bad news is that I believe that would require making use of a mixed integer SDP capability, which MOSEK does not currently have (for problems which can not be expressed as mixed integer SOCP)…

I recommend using det_rootn instead of log_det fo rthe continuous variable version of the problem… Actually, you could solve the problem by brute force enumeration of all possible integer vectors v satisfying the constraints, and evaluating the objective function. Largest objective function wins.

Hello
I run my program and it gives me the same error as above.
my program is
"clear all;clc;

A=0.8;
C=1.2;
Q=1;
R=1;
Pvar=0.64;
Ro=C*Pvar*C'+R;
% syms t
% K=[Ro t;t' inv(Ro)];
sigma=inv(C*Pvar*C'+R);
Pvar=0.64;
cvx begin
variable t
minimize trace(C*Pvar*Pvar*C'*sigma*t)
subject to
      
       t >=0;
       [Ro t;t' inv(Ro)]>=0;

cvx_end

Is anybody knows what’s the problem?
Thanks

You got this error message for the same reason the OP did, because you have
cvx begin
when you should have
cvx_begin

Thanks. I make it correct. Now my program is:
clear all;clc;

A=0.8;
C=1.2;
Q=1;
R=1;
Pvar=0.64;
Ro=C*Pvar*C'+R;
% syms t
% K=[Ro t;t' inv(Ro)];
sigma=inv(C*Pvar*C'+R);
Pvar=0.64;
cvx_begin
variable t
minimize trace(C*Pvar*Pvar*C'*sigma*t)
subject to
      
        t>=0;
       [Ro t;t' inv(Ro)]>=0;
%        [Ro t;t' inv(Ro)]==semidefinite(n+1);
% det(K)>=0;
cvx_end

Now it gives me the result “Optimal value (cvx_optval): +1.61192e-09” , nonthelese the result shall be t=-1.
may you please help me?

I believe you want [Ro t;t' inv(Ro)]>=0; to be an LMI, in which case you either need to use
cvx_begin sdp , or use [Ro t;t' inv(Ro)]==semidefinite(2), which both accomplish the same thing. Please read http://cvxr.com/cvx/doc/sdp.html .

Furthermore, if you want to obtain t = -1, then you need to get rid of the constraint t >= 0.

If you do these, you will get optimal t = -1 ((within tolerance of) as you thought it should be.

So either do

cvx_begin sdp
variable t
minimize trace(C*Pvar*Pvar*C'*sigma*t)
subject to  
[Ro t;t' inv(Ro)]>=0;
cvx_end

or do

cvx_begin
variable t
minimize trace(C*Pvar*Pvar*C'*sigma*t)
subject to       
[Ro t;t' inv(Ro)]==semidefinite(2);
cvx_end

thanks for your guidness. But in both cases, it gives me -0.306944!

That is the optimal objective value, achieved at t = -1.

ahan this the trace? how can I find t?

I don’t understand your question (maybe you made a typo?)

The objective function trace(C*Pvar*Pvar*C'*sigma*t) evaluated at t = -1 produces cvx_optval = -0.3069

It doesn’t give me the amount of t.
It gives me this:
alling SDPT3 4.0: 3 variables, 1 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 1
dim. of sdp var = 2, num. of sdp blk = 1


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|2.3e-01|4.2e+00|2.0e+02| 2.442000e+01 0.000000e+00| 0:0:00| chol 1 1
1|1.000|1.000|0.0e+00|4.7e-02|1.8e+01| 1.684805e+01 1.244577e-02| 0:0:00| chol 1 1
2|0.981|1.000|0.0e+00|4.7e-03|4.5e-01| 4.724357e-01 2.267079e-02| 0:0:00| chol 1 1
3|1.000|0.955|4.2e-17|6.6e-04|8.6e-02| 3.804759e-01 2.949241e-01| 0:0:00| chol 1 1
4|0.986|0.998|0.0e+00|4.9e-05|1.1e-03| 3.078818e-01 3.068230e-01| 0:0:00| chol 1 1
5|0.989|0.989|0.0e+00|5.2e-06|1.2e-05| 3.069546e-01 3.069466e-01| 0:0:00| chol 1 1
6|0.989|0.989|4.2e-17|5.8e-08|1.3e-07| 3.069443e-01 3.069442e-01| 0:0:00| chol 1 1
7|0.996|1.000|4.2e-17|1.0e-12|2.2e-09| 3.069442e-01 3.069442e-01| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 7
primal objective value = 3.06944214e-01
dual objective value = 3.06944212e-01
gap := trace(XZ) = 2.17e-09
relative gap = 1.34e-09
actual relative gap = 1.34e-09
rel. primal infeas (scaled problem) = 4.25e-17
rel. dual " " " = 1.00e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 3.7e-01, 1.0e+00, 2.4e+00
norm(A), norm(b), norm© = 2.4e+00, 1.3e+00, 3.0e+00
Total CPU time (secs) = 0.38
CPU time per iteration = 0.05
termination code = 0
DIMACS: 4.2e-17 0.0e+00 1.0e-12 0.0e+00 1.3e-09 1.3e-09


Status: Solved
Optimal value (cvx_optval): -0.306944

After cvx_end, t contains its optimal value of approximately -1.

This is also covered in the CVX Users’ Guide http://cvxr.com/cvx/doc/ , which I recommend you read or re-read as the case may be.

yes,thanks.it gives.