为了数模,整理下MATLAB中常见的全局优化求解器。
算法 | Matlab求解器 | 作用 |
---|---|---|
全局搜索 | GlobalSearch | 寻找全局最小值 |
多起点搜索 | MultiStart | 寻找多个局部最小值(也可以得到全局最优) |
模式搜索 | patternsearch | 用模式搜索方式寻找函数的最小值 |
遗传算法 | ga | 用遗传算法寻找函数的最小值 |
粒子群算法 | particleswarm | 用粒子群算法寻找函数的最小值 |
模拟退火 | simulannealbnd | 用模拟退火算法寻找函数的最小值 |
官方文档中的demo
% 创建优化相关参数
opts = optimoptions(@fmincon,'Algorithm','interior-point');
% 创建优化问题
problem = createOptimProblem('fmincon','objective',...
@(x) x.^2 + 4*sin(5*x),'x0',3,'lb',-5,'ub',5,'options',opts);
% 指定优化求解器
gs = GlobalSearch;
% 求解
[x,f] = run(gs,problem)
运行结果为
GlobalSearch stopped because it analyzed all the trial points.
All 26 local solver runs converged with a positive local solver exit flag.
x =
-0.3080
f =
-3.9032
注:
参数名 | 作用 |
---|---|
‘MaxIterations’ | 指定最大迭代次数 |
‘ConstraintTolerance’ | 约束容忍度,默认为 1 0 − 6 10^{-6} 10−6 |
‘MaxFunctionEvalutions’ | 函数评估的最大次数 |
与GlobalSearch的用法基本相同,需要注意的是在使用MultiStart时,run函数需要有三个参数:求解器,优化问题,求解器个数n。如此才能够从多个起点同时进行运算。
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
@(x) x.^2 + 4*sin(5*x),'x0',3,'lb',-5,'ub',5,'options',opts);
ms = MultiStart;
[x,f] = run(ms,problem,20)
运行结果为
MultiStart completed the runs from all start points.
All 20 local solver runs converged with a positive local solver exit flag.
x =
-0.3080
f =
-3.9032
与GlobalSearch的结果一致。
模式搜索pattersearch,全是小写字母,说明其是一个函数。而上面的GlobalSearch,MultiStart为类。因此用法上自然也有所不同。其可求解如下形式的问题:
min F ( x ) s.t. A ∗ X < = B , A e q ∗ X = B e q ( 线 性 约 束 ) C ( X ) < = 0 , C e q ( X ) = 0 ( 非 线 性 约 束 ) L B < = X < = U B \begin{array}{rl} \textrm{min}\quad &F(x)\\ \textrm{s.t.}\quad& A*X <= B,Aeq*X = Beq(线性约束) \\ &C(X) <= 0, Ceq(X) = 0 (非线性约束)\\ & LB <= X <= UB \end{array} mins.t.F(x)A∗X<=B,Aeq∗X=Beq(线性约束)C(X)<=0,Ceq(X)=0(非线性约束)LB<=X<=UB
函数调用方式如下(更多形式见官方文档)
[x,fval] = patternsearch(fun,x0,A,b,Aeq,beq,lb,up,nonlcon,options)
下面还是给几个MATLAB官方文档中的demo。
%% 无约束优化
% 定义优化函数
fun = @psoobj;
% 指定初始值
x0 = [0,0];
% 求解
x = patternsearch(fun,x0)
% end
function y = psoobj(x)
y = exp(-x(1)^2-x(2)^2)*(1+5*x(1)+6*x(2)+12*x(1)*cos(x(2)));
end
运行结果为
Optimization terminated: mesh size less than options.MeshTolerance.
x =
-0.7037 -0.1860
%% 带线性不等式约束的优化问题
% 定义优化函数
fun = @psoobj;
% 设置约束Ax <= b
A = [-3,-2;
-4,-7];
b = [-1;-8];
x0 = [0.5,-0.5];
x = patternsearch(fun,x0,A,b)
function y = psoobj(x)
y = exp(-x(1)^2-x(2)^2)*(1+5*x(1)+6*x(2)+12*x(1)*cos(x(2)));
end
运行结果为
Optimization terminated: mesh size less than options.MeshTolerance.
x =
5.2824 -1.8758
%% 带box约束的优化问题
% 定义优化函数
fun = @psoobj;
% 设置约束Ax <= b
lb = [0,-Inf];
ub = [Inf,-3];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1,-5];
x = patternsearch(fun,x0,A,b,Aeq,beq,lb,ub)
function y = psoobj(x)
y = exp(-x(1)^2-x(2)^2)*(1+5*x(1)+6*x(2)+12*x(1)*cos(x(2)));
end
Optimization terminated: mesh size less than options.MeshTolerance.
x =
0.1880 -3.0000
可求解如下形式的问题:
min F ( x ) s.t. A ∗ X < = B , A e q ∗ X = B e q ( 线 性 约 束 ) C ( X ) < = 0 , C e q ( X ) = 0 ( 非 线 性 约 束 ) L B < = X < = U B X ( i ) integer vector INTCON \begin{array}{rl} \textrm{min}\quad &F(x)\\ \textrm{s.t.}\quad& A*X <= B,Aeq*X = Beq(线性约束) \\ &C(X) <= 0, Ceq(X) = 0 (非线性约束)\\ & LB <= X <= UB\\ & X(i)\quad \textrm{integer}\\ & \textrm{vector\; INTCON} \end{array} mins.t.F(x)A∗X<=B,Aeq∗X=Beq(线性约束)C(X)<=0,Ceq(X)=0(非线性约束)LB<=X<=UBX(i)integervectorINTCON
[x,fval,exitflag,output,population,scores] = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon,options)
注:
nvar: Number of design variables
nonlcon: Nonlinear constraint function
intcon : Index vector for integer variables
若 X ( i ) X(i) X(i)为整数,则 intcon = i \textrm{intcon} = i intcon=i
若nonlcon非空,则A,b为空
Demo
A = [1 1;-1 2;2 1];
b = [2;2;3];
lb = zeros(2,1);
[x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb)
运行结果为:
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
0.6670 1.3340
fval =
-8.2258
exitflag =
1
可求解如下形式的问题:
min F ( x ) L B < = X < = U B \begin{array}{rl} \textrm{min}\quad &F(x)\\ & LB <= X <= UB \end{array} minF(x)LB<=X<=UB
调用形式
[X,FVAL,EXITFLAG] = particleswarm(FUN,NVARS,LB,UB,OPTIONS)
Demo
fun = @(x) x(1)*exp(-norm(x)^2);
lb = [-10,-15];
ub = [15,20];
x = particleswarm(fun,2,lb,ub)
运行结果如下
Optimization ended: relative change in the objective value
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
x =
-0.7071 -0.0000
fval =
-0.4289
existflag =
1
可求解如下形式的问题:
min F ( x ) L B < = X < = U B \begin{array}{rl} \textrm{min}\quad &F(x)\\ & LB <= X <= UB \end{array} minF(x)LB<=X<=UB
调用形式
[X,FVAL,EXITFLAG] = simulannealbnd(FUN,X0,LB,UB,options)
Demo
fun = @dejong5fcn;
x0 = [0 0];
lb = [-64,-64];
ub = [64, 64];
[x,fval,existflag] = simulannealbnd(fun,x0,lb,ub)
运行结果为
Optimization terminated: change in best function value less than options.FunctionTolerance.
x =
-0.0062 -31.9643
fval =
2.9821
existflag =
1