#MATLAB# 全局优化求解器

为了数模,整理下MATLAB中常见的全局优化求解器。

算法 Matlab求解器 作用
全局搜索 GlobalSearch 寻找全局最小值
多起点搜索 MultiStart 寻找多个局部最小值(也可以得到全局最优)
模式搜索 patternsearch 用模式搜索方式寻找函数的最小值
遗传算法 ga 用遗传算法寻找函数的最小值
粒子群算法 particleswarm 用粒子群算法寻找函数的最小值
模拟退火 simulannealbnd 用模拟退火算法寻找函数的最小值

1. 全局搜索

官方文档中的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

注:

  1. optimoptions的第一参数指定求解器,如果只输入求解器名字,其他参数将按默认的配置。这里采用的是fmincon,其为求解非线性多元函数最小值的matlab函数。算法采用内点法,其他可选的算法参数有’active-set’,‘sqp’,‘sqp-legacy’,‘trust-region-reflective’。除此之外,常见的配置属性有(其余详见MATLAB官方文档):
参数名 作用
‘MaxIterations’ 指定最大迭代次数
‘ConstraintTolerance’ 约束容忍度,默认为 1 0 − 6 10^{-6} 106
‘MaxFunctionEvalutions’ 函数评估的最大次数

2. 多起点搜索

与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的结果一致。

3. 模式搜索

模式搜索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)AX<=B,AeqX=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。

  1. 求解无约束优化问题
%% 无约束优化
% 定义优化函数
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
  1. 求解带线性不等式约束的优化问题
%% 带线性不等式约束的优化问题
% 定义优化函数
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
  1. 求解带box约束的优化问题
%% 带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

4. 遗传算法

可求解如下形式的问题:

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)AX<=B,AeqX=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

5. 粒子群算法

可求解如下形式的问题:

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

6. 模拟退火

可求解如下形式的问题:

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

你可能感兴趣的:(优化,Matlab,工具箱)