In the previous note, we wrote a target function. You can see that the definition field of the target function (you don't know the definition field, that is, the value range of the independent variable) has no restrictions, in actual problems, there are often many restrictions on independent variables, such as the number of them or the number of them. Formal Jianghu terms are used to refer to these restrictions as "constraints ". This part focuses on the issue of "constraints. The constraints are divided into the following four types:
1. Boundary constraints. Maximum and minimum values of independent variables: x> = L and x <= u
2. Linear Inequality Constraints. Ax <= B, where A is the mxn matrix, X is the Nx1 matrix, and B is the 3x1 matrix. Describes the limitations of M-type inequalities on N variables.
3. Linear Equality Constraints. Basically the same 2.
4. Nonlinear Constraints.
Let's take a look at the example below. The example is vivid and easy to understand.
I. Boundary Constraints
A boundary constraint mainly includes the limitation on the upper and lower bounds of a variable. In an optimization function, if you know its boundary, You can include these boundary constraints in the description of the problem, then you will get the results you need quickly and accurately.
For a vector X, the boundary constraint must be the same as the dimension of the vector X. For example, if X = [x1, x2, X3, X4], where X1 is a scalar, the length of the boundary constraint must also be 4. For example, the constraint of the lower bound is L = [1, 2, 4, 3, 4], the upper bound is u = [5, 6, 7, 8], which is equivalent to the following formula:
1 <= X1 <= 5
2 <= X2 <= 6
3 <= X3 <= 7
4 <= X4 <= 8
If the constraint is
How do I represent it? In Matlab, INF indicates positive infinity, and-INF indicates negative infinity. The upper and lower bounds can be written as follows: L = [-INF,-INF, 8,-INF], upper bounds u = [INF, 3, INF, INF].
Ii. Linear Inequality Constraints
There are the following inequalities
X1 +X3 ≤ 4,
2X2-X3 ≥-2,
X1-X2 +X3-X4 ≥ 9.
Here there is a vector x composed of four scalar values. If it is written in the form of ax <= B, A is a 3x4 matrix, and B is a 3x1 matrix.
A = [1 0 1 0; B = [4;
0-2 1 0; 2;
-1 1-1 1]-9]
X = [x1, x2, X3, X4]
Ax <= B
Note that the numbers in the red font are the opposite to those in the original format.
3. Linear Equality Constraints
Basically, the linear inequality constraint is the same as above. The only difference is that A is converted into aeq, and B is converted into beq. I will not go into details here.
4. Nonlinear Constraints
The form of non-linear inequality constraints, such as C (x) <= 0, the form of non-linear equality constraints, such as CEQ (x) = 0; the expression of Non-linear constraints generally uses the function method, just like C (x) <= 0 and CEQ (x) = 0.
The following points are important: a non-linear constraint function must return two components: one is an inequality constraint and the other is an equality constraint. Although they may not exist at the same time, if one of them does not exist, then [] is returned.
Assume that the following inequality constraints exist.
The M file format for writing this constraint into the function is as follows:
Function [C, CEQ] = ellipseparabola (X)
% Inside the ellipse bounded by (-3 <x <3), (-2 <Y <2)
% Above the line y = x ^ 2-1
C (1) = (x (1) ^ 2)/9 + (X (2) ^ 2)/4-1; % inequality constraint 1
C (2) = x (1) ^ 2-X (2)-1; % inequality constraint 2
CEQ = []; % equality constraint, empty
End
Ellipseparabola returns a [] result as a nonlinear equality constraint. Both inequality constraints are in the form of ≤ 0.
If you provide the gradient information of C and CEQ, the algorithm may run faster and give you more trusted results. The following is an example of how to write a constrained function that contains gradient information.
Function [C, CEQ, gradc, gradceq] = ellipseparabola (X)
% Inside the ellipse bounded by (-3 <x <3), (-2 <Y <2)
% Above the line y = x ^ 2-1
C (1) = x (1) ^ 2/9 + X (2) ^ 2/4-1;
C (2) = x (1) ^ 2-X (2)-1;
CEQ = [];
If nargout> 2
Gradc = [2 * x (1)/9, 2 * x (1 );...
X (2)/2,-1];
Gradceq = [];
End