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

(John Wick) #1

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.

sparse(A) does not accept char inputs A
(Mark L. Stone) #2

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.

(John Wick) #3

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.

(Mark L. Stone) #4

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.

(azam ghamari) #5

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

(Mark L. Stone) #6

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

(azam ghamari) #7

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.

(Mark L. Stone) #8

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

(azam ghamari) #9

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

(Mark L. Stone) #10

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

(azam ghamari) #11

ahan this the trace? how can I find t?

(Mark L. Stone) #12

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

(azam ghamari) #13

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

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

(Mark L. Stone) #14

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.

(azam ghamari) #15

yes,thanks.it gives.