Learn nonlinear programming and quadratic programming

nonlinear programming

Nonlinear programming model (NP)

If the objective function or constraints contain nonlinear functions, the planning problem is called a nonlinear programming problem. Generally speaking, solving nonlinear programming problems is much more difficult than solving linear programming problems. Moreover, unlike linear programming, which has a general method called the simplex method, nonlinear programming currently does not have a general algorithm suitable for various problems, and each method has its own specific scope of application.
Insert image description here
Insert image description here
Insert image description here

Mathematical Model of Nonlinear Programming

Insert image description here
Insert image description here
Insert image description here

List

Find the following nonlinear programming
Insert image description here
code

%将以下代码分别复制到MATLAB® 路径上名为fun1.m和fun3.m 的文件中。
    % fun1.m内容
    % function f=fun1(x)
    % f=sum(x.^2)+8;
            % fun3.m内容
            % function [g,h]=fun3(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];

            
[x,y]=fmincon('fun1',rand(3,1),[],[],[],[],zeros(3,1),[],'fun3')%主代码仅仅只有这一行

Running results:
Insert image description here
The code given by the teacher did not run successfully at first because all the functions were placed in the same script, causing function definition and reference to fail. The author edited a piece of code himself when he could
not run the above code.

fun=@(x)x(1)^2+x(2)^2+x(3)^2+8;
A=[];
b=[];
Aeq=[];
beq=[];
x0=rand(3,1);
nonlcon=@fun2;  %调用了路径为fun.2的函数
                %函数fun.2内容
                % function [c,ceq]=fun2(x)
                % c=[-x(1)^2+x(2)-x(3)^2
                %     x(1)+x(2)^2+x(3)^3-20];
                % ceq=[-x(1)-x(2)^2+2
                %     x(2)+2*x(3)^2-3];
x=fmincon(fun,x0,A,b,Aeq,beq,zeros(3,1),[],nonlcon)

Running results:
Insert image description here
Although the code written by the author can also find the optimal solution for x, it does not directly find the solution for y. But when the author wrote this code, the function fun2 was successfully created and referenced. This inspired the author to think about why the first piece of code failed to run, and then the first piece of code was corrected and ran successfully.

quadratic planning

Insert image description here

The command to solve quadratic programming in Matlab
is [x, fval] = quadprog (H, f, A, b, Aeq, beq, lb, ub, x0, options). The
return value x is the value of the decision vector x, and the return value fval is the value of the objective function at x (for details, please refer to the help after running help quadprog in the Matlab command window)

example

Insert image description here

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))

operation result
Insert image description here

Application examples: supply and site selection

Insert image description here

Modeling

Insert image description here

Situation of using temporary stock yard

Insert image description here

Solution to the first question
code

a=[1.25 8.75 0.5 5.75 3 7.25];
b=[1.25 0.75 4.75 5 6.5 7.75];
d=[3 5 4 7 6 11];
x=[5 2];
y=[1 7];
e=[20 20];
for i=1:6
    for j=1:2
        aa(i,j)=sqrt((x(j)-a(i))^2+(y(j)-b(i))^2);
    end
end  %计算每个料场到每个工地的距离
CC=[aa(:,1);aa(:,2)]';%将两个料场到每个工地的距离以12维列向量的形式存放
A=[1 1 1 1 1 1 0 0 0 0 0 0;0 0 0 0 0 0 1 1 1 1 1 1];
B=[20;20];%每个料场提供的水泥小于20吨
Aeq=[1 0 0 0 0 0 1 0 0 0 0 0;
    0 1 0 0 0 0 0 1 0 0 0 0;
    0 0 1 0 0 0 0 0 1 0 0 0;
    0 0 0 1 0 0 0 0 0 1 0 0;
    0 0 0 0 1 0 0 0 0 0 1 0;
    0 0 0 0 0 1 0 0 0 0 0 1];
beq=[d(1);d(2);d(3);d(4);d(5);d(6)];%两个料场向同一工厂提供的水泥等于该工厂日用量
VLB=[0 0 0 0 0 0 0 0 0 0 0 0];%下限
VUB=[];%上限
x0=[1 2 3 0 1 0 0 1 0 1 0 1];%初始值
[xx,fval]=linprog(CC,A,B,Aeq,beq,VLB,VUB,x0)%返回目标函数在解 xx处的值:fval = f'*x

Operation results
Insert image description here
and solution to the second question

The situation of rebuilding two new material yards

Insert image description here
Insert image description here
There are still two steps required here.
First create the m file liaochang.m to define the objective function.
Take the initial value x0 and edit the main program
. Different initial value selections will lead to different final convergence results. Generally, the initial value is randomly generated, and then the result is generated by running, and the result is used as the initial value to recompile and run the code. If better results are obtained, repeat the above steps. If no better result is obtained, the default result is the optimal result
Code

            %将以下代码分别复制到MATLAB® 路径上名为liaochang.m 的文件中
            % function f=liaochang(x)
            % a=[1.25 8.75 0.5 5.75 3 7.25];
            % b=[1.25 0.75 4.75 5 6.5 7.75];
            % d=[3 5 4 7 6 11];
            % e=[20 20];
            % f1=0;
            % for i=1:6
            %     s(i)=sqrt((x(13)-a(i))^2+(x(14)-b(i))^2);
            %     f1=s(i)*x(i)+f1;
            % end
            % f2=0;
            % for i=7:12
            %     s(i)=sqrt((x(15)-a(i-6))^2+(x(16)-b(i-6))^2);
            %     f2=s(i)*x(i)+f2;
            % end
            % f=f1+f2;
%主代码
x0=[3 5 0 7 0 1 0 0 4 0 6 10 5 1 2 7]';
A=[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0];
B=[20;20];
Aeq=[1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0];
beq=[3;5;4;7;6;11];
vlb=[zeros(12,1);-inf;-inf;-inf;-inf];
vub=[];
[x,fval,exitflag]=fmincon('liaochang',x0,A,B,Aeq,beq,vlb,vub)

Solving nonlinear problems still requires creating and referencing the objective function.
Running result
Insert image description here
[x, fval, exitflag, output] = fmincon(___) also returns the value exitflag that describes the exit condition of fmincon, and the structure output that provides information about the optimization process.
Using additional output checks, to easily check the quality of the solution, request the exitflag and output outputs.
An exitflag value of 1 indicates that the solution is a local minimum.
The output structure reports several statistics about the solution process. Specifically, it gives the number of iterations in output.iterations, the number of function evaluations in output.funcCount, and the feasibility in output.constrviolation.

Guess you like

Origin blog.csdn.net/Luohuasheng_/article/details/128513495