Summary of MATLAB cvx errors, cvx error resolution, summary of cvx knowledge points, cvx programming skills

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Recently, in the process of using MATLAB cvx to solve the optimization problem, I encountered various problems. There are many bugs, and the expression that seems reasonable has repeatedly reported errors. Finally, after more than a month of debugging, I finally solved the optimization problem. .
During the debugging period, many methods were used, such as the transformation of problems and constraints, non-convex transformation into approximate convexity, consulting data, changing formula writing, etc. This article records the errors and bugs encountered in the programming process, hoping to help To those who have the same problem.

If you are a cvx beginner and want to master general cvx syntax and programming methods, you can read this article:
Basic knowledge of CVX toolbox in MATLAB to solve convex optimization problems - syntax, variable declaration, objective function, constraints, cvx programming errors and solution

There are many specific and proprietary functions in the CVX toolbox. These functions can not only simplify expressions, but also solve many error reporting problems through function replacement, such as division, reciprocal, logarithm, and so on. The following article summarizes the functions commonly used in cvx. Reading this article will definitely help you!
A summary of commonly used replacement functions in the MATLAB cvx toolbox to solve the problem of expression errors


Summary of cvx programming errors and solutions

Mistake 1 - cannot convert from cvx to double

Error message:
cannot convert from cvx to double
insert image description here

Cause of error:
If the variable declaration is outside the cvx framework (outside cvx_begin-cvx_end), then the process of saving data from cvx type to double (outside double) will occur during cvx operation. The intermediate variables must be defined in the cvx expression.
Specifically, the variable type in cvx is its unique data type, that is, your optimized variable is the cvx type. If you want your cvx optimized variable to perform mathematical operations with other defined double data, you must A variable of type expression is declared ahead of time to accept this operation.
Solution:
For example: a is a double type other than cvx, and b is a variable variable in cvx. If you want to implement a b in cvx , you must define a variable of type expression to receive the value of a b.
error code:

 b = rand(3,2);   
 cvx_begin
    variable a;
    variable c;  %c为variable变量
    c = a * b;   %因为c是variable变量,所以进行该操作会出现错误
    minimize f(x)
    subject to
        a <= 10;
cvx_end

Change the variable c; in the above code to expression c; to get the correct code.

Correct code:

 b = rand(3,2);   
 cvx_begin
    variable a;
    expression c;  %c为variable变量
    c = a * b;   %因为c是variable变量,所以进行该操作会出现错误
    minimize f(x)
    subject to
        a <= 10;
cvx_end

Summary of knowledge points: the difference between variable and expression in cvx
In the CVX software package, Variable (variable) and Expression (expression) are the two most basic types of CVX variable declarations.

Variable is a variable that can be optimized. It can be regarded as a container that stores variables to be solved during the optimization process. When declaring a Variable, you need to specify its shape, whether it is a real number, whether it is a non-negative number, etc., so that you can easily control the value range of the variable and the choice of optimization methods. Variable has the property to be solved in the optimization problem, and it is defined as the object to be optimized in CVX.

Expression is used to specify the constraint relationship between variables, and can also be understood as a mathematical expression that can be calculated. Expression can be obtained by combining variable and basic arithmetic operations, point multiplication, matrix multiplication, atomic functions and other operations, and can be a fixed value or an expression related to Variable.

Variable is generally an unknown quantity in the problem and needs to be solved, while Expression is usually known and determined. Since Expression does not need to be solved, CVX performs symbol-related operations and simplification on it, which helps to improve the efficiency of numerical calculations. That is, Expression is used as an intermediate variable, and cvx will not save the calculation result of Expression in the end.

Mistake 2 - the equal sign problem

报错提示:
This is often an indication that an equality constraint was written with one equals ‘=’ instead of two ‘==’. The model must be rewritten before cvx can proceed.

Cause of error:
The variable variable in cvx is not allowed to be assigned manually. It will be overwritten by the result only when the result is finally obtained. It is not allowed to use the equal sign to assign a value to a variable. Cvx will dynamically optimize the variable and artificial copying is not allowed. .

Solution:
Use expression to declare temporary variables, and expression variables can use "=".

// 声明临时变量
cvx_begin

    variable a;   %优化变量,不可以赋值修改,可以用双等号==来写约束条件
    expression b;  %临时变量,可以用等号=来赋值修改
    minimize(f(x));
    subject to
       a == 0;  %约束条件,a必须等于零
       b = 10;  %给b赋值为10,优化结束后自动清除
 			
cvx_end  

Error 3 - {real affine} .* {invalid}

Error message:
Disciplined convex programming error:
Cannot perform the operation: {real affine} .* {invalid} The
following operations cannot be performed: affine* Invalid
insert image description here
error reason:
There are invalid data in the variable, such as positive and negative infinity +Inf, -Inf, Null data NaN, or 0.

Solution:
Check that the data in the variable is invalid, modify the value of the variable, or adjust the initialization parameters, or check which step of the operation has invalid data, and modify the statement to eliminate the invalid data.

Mistake 4—Division Problems

Error message:
There is an error in point division operation, error./ (line 29)
insert image description here
Error reason:
The division in cvx is prone to problems. Sometimes you think that the division operation is normal, but it will make mistakes in cvx, so there is a special function in cvx Some functions for division operations, inv_pos().

Solution:
For example: x ./ y reports an error,
replace y with inv_pos(y), and modify the above formula to x * inv_pos(y)

 cvx_begin
    variable a;
    variable b;  %c为variable变量
    minimize (a * inv_pos(b))  %不要用a/b
    subject to
        a <= 10;
cvx_end

Summary of knowledge points: inv_pos function in cvx
In CVX, inv_pos(x) is an atomic function used to find the reciprocal of a positive number x, ie 1/x. However, when defining this function, you need to add a prefix "inv_pos" to indicate that the parameter x must be a positive number, otherwise undefined results will be generated.

In optimization problems, it is often necessary to solve the inverse of a matrix or vector. However, inverse operations cannot be performed when non-positive definite or singular matrices are involved. Therefore, in order to ensure that the matrix or vector is positive definite, you can use the inv_pos function in CVX, which will automatically determine the positive definiteness of the variable and solve its inverse. The inv_pos function returns inf (infinity) or NaN (not a number) when the variable is not positive definite.

For example, when solving the following convex optimization problem:

cvx_begin
    variable x
    minimize(inv_pos(x) - 1)
    subject to
        x >= 0.5;
cvx_end

The above problem requires solving a variable x such that 1/x is the smallest and x must be greater than or equal to 0.5. In this example, because we are using the inv_pos function, x must be positive or an error will occur. Finally, CVX will solve for a positive number x such that 1/x is the smallest.

Mistake 5—logarithmic log problem

Error message:
wrong use of cvx/log (line 64)
Disciplined convex programming error:
Illegal operation: log( {convex} ).
insert image description here
Error reason:
Convex function in log function is not allowed in cvx, that is, log (convex) is Illegal, there is a function specifically for log operations in cvx, namely rel_entr

Solution:
Use rel_entr to represent the log function
rel_entr(x,y) = xlog(x/y)
When you know the meaning of the above expression, you can transform your log expression, and then use multiple rel_entr
functions The combination to represent the expression you want to express.
For example: wlog(1+p/w), with the help of the mathematical formula log(1/x)=-log(x), the expression can be obtained:
wlog(1+p/w) = -rel_entr(w,w+p) , pay attention to the minus sign

%想要计算log(1+1/x)
    a = rel_entr(x+1,x)+rel_entr(x,x+1);

Summary of knowledge points: rel_entr function in cvx
In CVX, rel_entr(x, y) is an atomic function that represents a part of the KL divergence, ie x log(x/y). Among them, x and y have the same size and shape, and element-by-element calculations are performed.

The first parameter x of the rel_entr function can be any non-negative real number or real number vector (or matrix), and the second parameter y is also a non-negative real number or real number vector (or matrix), and there cannot be an item of 0 in y. The value of rel_entr(x, y) is 0 when x and y are equal. In CVX problems, rel_entr(x, y) is usually used to solve some special problems such as maximum entropy problems, information theory problems, and logarithmic probability regression.

Code example:

cvx_begin
    variable x(n);
    maximize(sum(rel_entr(x, p)));
    subject to
        sum(x) == 1;
        x >= 0;
cvx_end

Among them, x is an n-dimensional column vector, p is also a column vector of the same size and shape, and CVX will automatically convert the operations between all elements into the corresponding KL divergence formula. In this example, CVX divides each x(i) and p(i) item by item, and then calculates and adds a part of the KL divergence formula to solve the problem of maximizing x satisfying the constraints.

Error 6—Invalid Data

Error message:
Disciplined convex programming error:
Illegal operation: {invalid} + {convex}
insert image description here

Error reason:
Invalid data is added to a convex expression. The error reason and handling method are similar to error 3, and will not be repeated here.

Mistake 7—Non-convex problems

报错提示:
Disciplined convex programming error:
Cannot perform the operation: {positive constant} ./ {real affine}
insert image description here

Cause of error:
Divide a positive real number by an affine function. Obviously, the expression is non-convex. As a convex optimization toolbox, cvx cannot solve non-convex problems.

Solution:
Convert the expression to a convex expression, and re-transform the constraint or objective function. Instead of using the cvx toolbox, use other toolboxes for solving optimization problems, such as Gurobi, CPLEX, etc.

Summarize

The above summarizes several common mistakes in cvx programming. The ability is limited, and it may not be able to accurately solve the real problems you encounter. cvx programming looks simple, but in fact the water is very deep. Progress, I wish you an early success in the paper simulation!

If you are a cvx beginner and want to master general cvx syntax and programming methods, you can read this article:
Basic knowledge of CVX toolbox in MATLAB to solve convex optimization problems - syntax, variable declaration, objective function, constraints, cvx programming errors and solution

There are many specific and proprietary functions in the CVX toolbox. These functions can not only simplify expressions, but also solve many error reporting problems through function replacement, such as division, reciprocal, logarithm, and so on. The following article summarizes the functions commonly used in cvx. Reading this article will definitely help you!
A summary of commonly used replacement functions in the MATLAB cvx toolbox to solve the problem of expression errors

If you are not very familiar with the basic operations of MATLAB, especially matrix operations, logical operations, array generation, etc., you can refer to the following article: MATLAB basic operations, matrix multiplication, array matrix indexing, maximum and minimum operators,
zero Generation of matrix/random matrix/identity matrix, log function, meaning of Inf and NaN, if the statement is too long, use connectors to wrap lines, logical operators and differences

If the program debugging is successful and you need to draw the result graph, but if you are not sure about the basic knowledge and grammar of MATLAB drawing, you can refer to the following article:
MATLAB drawing must-see, drawing complete! MATLAB Drawing Basic Operation Encyclopedia - Line Chart, Scatter Chart, Color Style, Line Thickness Summary

Guess you like

Origin blog.csdn.net/qq_45296693/article/details/130860024