# Detecting of returning error in CVX

(Jack Nguyen) #1

Hi, I have the problem of returning an error in CVX.
My algorithm runs with a loop around 100. For each loop, I random the input to run CVX. However, some random sets make CVX error “Disciplined convex programming error” then it stops the algorithm.
Is there any methods to detect the error in CVX. So that when the error occurs, the algorithm still can move to next loop to create the new random set and be continued.

A condition for avoiding CVX related errors
(Mark L. Stone) #2

You can do it using MATLAB’s try catch .https://www.mathworks.com/help/matlab/ref/try.html .

Here is a crude version which demonstrates it. After a CVX error, it will execute `cvx_clear` and go on to the next CVX problem.

The first time through the for loop, produces a DCP error in the minimize. The 2nd time through,CVX solves the problem.

``````for i = -1:2:1
try
cvx_begin
variable x
minimize(i*x^2)
x>=0
cvx_end
catch
disp('Whoops')
cvx_clear
end
end
``````

Output is:

``````Whoops

Calling SDPT3 4.0: 4 variables, 2 equality constraints
------------------------------------------------------------

num. of constraints =  2
dim. of sdp    var  =  2,   num. of sdp  blk  =  1
dim. of linear var  =  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|6.7e+00|8.4e+00|3.0e+02| 1.000000e+01  0.000000e+00| 0:0:00| chol  1  1
1|1.000|1.000|2.2e-06|8.7e-02|1.9e+01| 9.019569e+00 -9.098563e+00| 0:0:00| chol  1  1
2|1.000|1.000|5.6e-07|8.7e-03|2.8e+00| 1.410751e+00 -1.318898e+00| 0:0:00| chol  1  1
3|1.000|1.000|1.3e-07|8.7e-04|1.1e+00| 5.109862e-01 -5.983815e-01| 0:0:00| chol  1  1
4|0.919|0.919|2.2e-08|1.5e-04|1.1e-01| 5.314692e-02 -5.323401e-02| 0:0:00| chol  1  1
5|1.000|1.000|3.7e-09|8.7e-06|4.4e-02| 2.232379e-02 -2.166095e-02| 0:0:00| chol  1  1
6|0.919|0.918|7.9e-10|1.5e-06|4.2e-03| 2.079518e-03 -2.074893e-03| 0:0:00| chol  1  1
7|1.000|1.000|2.4e-09|8.7e-08|1.7e-03| 8.610391e-04 -8.672553e-04| 0:0:00| chol  1  1
8|0.919|0.919|4.7e-10|1.5e-08|1.6e-04| 8.128190e-05 -8.128799e-05| 0:0:00| chol  1  1
9|1.000|1.000|1.3e-09|9.6e-10|6.8e-05| 3.385876e-05 -3.380255e-05| 0:0:00| chol  1  1
10|0.919|0.919|1.1e-10|2.2e-10|6.4e-06| 3.181334e-06 -3.181032e-06| 0:0:00| chol  1  1
11|1.000|1.000|7.2e-16|2.1e-11|2.6e-06| 1.324601e-06 -1.324868e-06| 0:0:00| chol  1  1
12|1.000|1.000|2.7e-20|1.0e-12|3.8e-07| 1.893805e-07 -1.893516e-07| 0:0:00| chol  1  1
13|1.000|1.000|2.7e-20|1.0e-12|7.7e-08| 3.835028e-08 -3.835660e-08| 0:0:00| chol  1  1
14|1.000|1.000|1.1e-16|1.0e-12|1.4e-08| 7.157999e-09 -7.156403e-09| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   = 14
primal objective value =  7.15799934e-09
dual   objective value = -7.15640325e-09
gap := trace(XZ)       = 1.43e-08
relative gap           = 1.43e-08
actual relative gap    = 1.43e-08
rel. primal infeas (scaled problem)   = 1.11e-16
rel. dual     "        "       "      = 1.00e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 1.0e+00, 1.2e-04, 1.0e+00
norm(A), norm(b), norm(C) = 2.6e+00, 2.0e+00, 2.0e+00
Total CPU time (secs)  = 0.13
CPU time per iteration = 0.01
termination code       =  0
DIMACS: 1.1e-16  0.0e+00  1.0e-12  0.0e+00  1.4e-08  1.4e-08
-------------------------------------------------------------------

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +7.158e-09``````