Study notes-Matlab algorithm articles-planning algorithm

01 linear programming

Matlab求解线性规划命令:[x,fval]=linprog(c,A,b,Aeq,beq,LB,UB,X0,OPTIONS)
这里 fval 返回目标函数的值,LB 和 UB 分别是变量 x 的下界和上界,x0是x的初始值,OPTIONS 是控制参数。

Example ( e01 ) to solve the following linear programming problem

                                                       

Matlab solves the linear programming problem, it is necessary to uniformly convert greater than or equal to less than or equal

c=[2;3;-5];
a=[-2 5 -1;1 3 1];
b=[-10;12];
aeq=[1 1 1];
beq=7;
x=linprog(-c,a,b,aeq,beq,zeros(3,1));
value=c'*x;
fprintf('x1=%.4f,x2=%.4f,x3=%.4f\nz:%.4f\n',x,value);

result: 

x1=6.4286,x2=0.5714,x3=0.0000
z:14.5714

02 integer programming

Solution

1. Branch and bound method - can solve pure integer or mixed integer linear programming

2. Plane cutting method - can solve pure integer or mixed integer linear programming

3. Implicit enumeration method solve " 0-1 " integer programming

4. Monte Carlo method - can solve various types of planning problems

02.1 Branch and Bound

       Properly systematically search for all feasible solution spaces of the optimization problem with constraints (the feasible solution is a finite number). This is the content of branching and delimiting. Usually, the entire feasible solution space is repeatedly divided into smaller and smaller subsets, called branches; and a target lower bound (for the minimum problem) is calculated for the solution set in each subset, which is called delimitation. After each branch, those subsets whose bounds exceed the target value of the known feasible solution set will not be branched further. In this way, many subsets can be ignored. This is called pruning. This is the main idea of ​​the branch and bound method.

Example ( e02 ) to solve the following integer programming problem 

                                                  

1. Regardless of the integer limit, the corresponding linear programming is solved, and the optimal solution is:

x1=4.8092,x2=1.8168,z=355.8779

The above solution does not meet the integer condition. At this time, z is the upper bound of the optimal objective function value of the problem. And x1=0, x2=0 takes to the lower bound, so the actual solution range is: 0<=z<=356

2. Branch. Take x1<=[4.8092]=4 ; x1>=[408092]+1=5

The problem is further broken down into:

           

At this time , the solution of B1 is: x1=4, x2=2.1, z1=349 ; the solution of B2 is: x1=5; x2=1.57, z2=341.4 .

It can be seen that the optimal solution range is 0<=z<=349

Solution by branch and bound method:

3. Solve the boundary of B1 again

B11x1=4x2=2z11=340

B12x1=1.43x2=3z12=327.14

4. The branch solution of B2 is obtained

B21x1=5.44x2=1z21=308

B22 no feasible solution

The B21 and B22 pruning, the optimal solution to the original problem:

x1=4x2=2z=340

02.2 Implicit enumeration

        Because the solution space of the exhaustive method is too large, it is often impossible to achieve exhaustion, so some methods are often designed to check only a part of the combination of variable values ​​to find the optimal solution to the problem. Such a method is called implicit enumeration.

Example (e04) : Solve the following 0-1 programming problem

                                                    

method:

1. First find a feasible solution tentatively, it is easy to see that (x1,x2,x3)=(1,0,0) satisfies the constraints, so it is a feasible solution, and z=3

2. Because it is a maximum value problem, when seeking an optimal solution, any solution with a target value of 3 <z can be deleted without checking whether it satisfies the constraint condition, because it is definitely not the optimal solution, so a constraint condition should be added ( Lower bound of target value )

3. Improve filter conditions

4. Since the target value is first calculated for each combination to verify the filter conditions, the combination with a larger target value z should be calculated first , so that the filter threshold can be raised in advance to reduce the amount of calculation

02.3 Monte Carlo

Because the solution space of the enumeration method is too large, the probability method can be used to randomly select some solutions for verification, so that when there are enough random solutions, the optimal solution can be obtained with a high probability. This is the idea of ​​Monte Carlo.

Example (e05) : Solve the following nonlinear programming problem

                                       

Analysis: calculated using enumeration method, then the number of solutions for the space 100 5 100 ^ 5 , a huge amount of calculation, if the random calculated using the Monte Carlo method 10 . 6 10 ^ 6 th points, you can use probabilistic methods to get the most proven The probability of merit is: 0.999954602 . Write MATLAB program to solve

function [f,g]=mengte(x)
f=x(1)^2+x(2)^2+3*x(3)^2+4*x(4)^2+2*x(5)-8*x(1)-2*x(2)-3*x(3)-...
x(4)-2*x(5);
g=[sum(x)-400
x(1)+2*x(2)+2*x(3)+x(4)+6*x(5)-800
2*x(1)+x(2)+6*x(3)-200
x(3)+x(4)+5*x(5)-200];
rng(sum(clock));
p0=0;
tic
for i=1:10^6
x=99*rand(5,1);
x1=floor(x);x2=ceil(x);
[f,g]=mengte(x1);
if sum(g<=0)==4
if p0<=f
x0=x1;p0=f;
end
end
[f,g]=mengte(x2);
if sum(g<=0)==4
if p0<=f
x0=x2;p0=f;
end
end
end
fprintf('x1=%.0f\nx2=%.0f\nx3=%.0f\nx4=%.0f\nx5=%.0f\nz=%.0f\n',x0,p0);
toc

result: 

>> e05
x1=4
x2=99
x3=14
x4=99
x5=6
z=49238
时间已过 1.861732 秒。

03 Nonlinear programming

Definition: If the objective function or constraint condition contains a nonlinear function, this kind of programming problem is called a nonlinear programming problem.

Standard form of nonlinear programming in Matlab :

                                                      

Where f(x) is a scalar function, A, B, and Beq Aeq are matrices and vectors of corresponding dimensions, and C(x) and Ceq (x) are nonlinear vector functions.

The solving command in Matlab is: X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)

                                              

function f=fun1(x)
    f=sum(x.^2)+8;
end
​function [g,h]=fun2(x)
    g=[-x(1)^2+x(2)-x(3)^2
    x(1)+x(2)^2+x(3)^3-20];
    h=[-x(1)-x(2)^2+2
    x(2)+2*x(3)^2-3];
end
options=optimset('largescale','off');
[x,y]=fmincon('fun1',rand(3,1),[],[],[],[],zeros(3,1),[],'fun2', options);
fprintf('x1=%.4f\nx2=%.4f\nx3=%.4f\ny=%.4f\n',x,y);

result:

>> e06

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.

<stopping criteria details>

x1=0.5522
x2=1.2033
x3=0.9478
y=10.6511

04 Secondary Planning

Definition: Quadratic programming is a type of non-linear programming. If the objective function of a certain non-linear programming is the quadratic function of the independent variable x and the constraints are all linear, this kind of programming is called quadratic programming.

The standard form of quadratic programming in Matlab is expressed as:

                                              

Here H is a real symmetric matrix, f and b are column vectors, and A is a matrix of corresponding dimensions.

Matlab solution: quadprog ()

H, f are the real symmetric matrix and column vector obtained after transforming the objective function into a standard form. Obviously H should be the Hessian matrix of the original function. Its return value is the vector x , X0 is the initial value of x ; A, B, Aeq, and Beq define linear constraints.

Example ( e07 ) to solve the following quadratic programming problem

                             

Before solving, the objective quadratic function needs to be transformed into a standard form:

                                                              

h=[4,-4;-4,8];
f=[-6;-3];
a=[1,1;4,1];
b=[3;9];
[x,value]=quadprog(h,f,a,b,[],[],zeros(2,1));
fprintf('x1=%.4f\nx2=%.4f\nz=%.4f\n',x,value);
x1=0:0.01:3;x2=x1;
[x1,x2]=meshgrid(x1,x2);
f=2*x1.^2-4*x1.*x2+4*x2.^2-6*x1-3*x2;
f(x1+x2<=3 & 4*x1+x2<=9)=-15;
mesh(x1,x2,f);

 result:

>> e07

Minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.

<stopping criteria details>

x1=1.9500
x2=1.0500
z=-11.0250

 

 

Guess you like

Origin blog.csdn.net/seek97/article/details/108306927