摘要:本文介绍了 GlobalSearch 函数的使用句式(一)、三个运行案例(二)、以及 GlobalSearch 函数的参数设置(三、四)。详细介绍如下:
一、函数句法
Syntax
gs = GlobalSearch
gs = GlobalSearch(Name,Value)
gs = GlobalSearch(oldGS,Name,Value)
gs = GlobalSearch(ms)
二、实战案例
(1)Example1:Run GlobalSearch on Multidimensional Problem
- gs = GlobalSearch creates gs, a GlobalSearch solver with its properties set to the defaults.
代码:
rng default % For reproducibility
gs = GlobalSearch;
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
+ x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
problem = createOptimProblem('fmincon','x0',[-1,2],...
'objective',sixmin,'lb',[-3,-3],'ub',[3,3]);
[x,fval] = run(gs,problem)
运行结果:
注意: 在MATLAB中,rng default 的作用是将随机数生成器的种子(seed)设置为默认值。种子是一个起始值,用于生成伪随机数序列。通过将种子设置为默认值,你可以确保在每次运行程序时,生成的伪随机数序列都是相同的,从而实现结果的可复现性。具体而言,rng default将随机数生成器的种子设置为 MATLAB 的默认值,这样每次运行代码时,生成的随机数序列都将相同。这对于需要随机性的算法,但又需要可重复的结果的情况非常有用,例如在进行随机实验或优化算法中。
(2)Example2:Run GlobalSearch on 1-D Problem
- Consider a function with several local minima.
fun = @(x) x.^2 + 4*sin(5*x);
fplot(fun,[-5,5])
图示如下:
- To search for the global minimum, run GlobalSearch using the fmincon ‘sqp’ algorithm.
代码:
rng default % For reproducibility
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
fun,'x0',3,'lb',-5,'ub',5,'options',opts);
gs = GlobalSearch;
[x,f] = run(gs,problem)
运行结果:
(3)Example3:调用fmincon时,含非线性约束的 GlobalSearch 函数用法
主函数代码如下:
clear all
clc
% 目标函数
fun = @(x) sin(x(1)) + 0.1 * x(2)^2;
% 非线性约束
nonlcon = @(x) constraintFunction(x);
% 定义优化问题
problem = createOptimProblem('fmincon', 'objective', fun, 'nonlcon', nonlcon, 'x0', [0, 0], 'lb', [-5, -5], 'ub', [5, 5]);
% 创建 GlobalSearch 对象
gs = GlobalSearch;
% 运行全局搜索
[x, fval, exitflag, output] = run(gs, problem);
% 显示结果,包括函数计算次数
disp('全局最优解:');
disp(['x = ' num2str(x)]);
disp(['目标函数值 = ' num2str(fval)]);
disp(['退出标志 = ' num2str(exitflag)]);
disp(['函数计算次数:' num2str(output.funcCount)]);
子函数代码如下:
% 定义约束函数
function [c, ceq] = constraintFunction(x)
c = x(1)^2 + x(2)^2 - 1; % 非线性不等式约束
ceq = []; % 非线性等式约束为空
end
运行结果:
-
需要注意,nonlcon 的非线性约束的写法!
-
另外,如果需要向非线性约束中传递参数,直接加参数即可,主函数中改为:
% 非线性约束
a=1;
nonlcon = @(x) constraintFunction(x,a);
- 子函数中改为下式即可:
% 定义约束函数
function [c, ceq] = constraintFunction(x,a)
c = a*x(1)^2 + x(2)^2 - 1; % 非线性不等式约束
ceq = []; % 非线性等式约束为空
end
三、基于 MultiStart 设置 GlobalSearch 参数
- Create a nondefault MultiStart object.
ms = MultiStart('FunctionTolerance',2e-4,'UseParallel',true)
- Create a GlobalSearch object that uses the available properties from ms.
gs = GlobalSearch(ms)
- gs has the same nondefault value of FunctionTolerance as ms. But gs does not use the UseParallel property.
四、更新 GlobalSearch 参数
- Create a GlobalSearch object with a FunctionTolerance of 1e-4.
gs = GlobalSearch('FunctionTolerance',1e-4)
- Update the XTolerance property to 1e-3 and the StartPointsToRun property to ‘bounds’.
gs = GlobalSearch(gs,'XTolerance',1e-3,'StartPointsToRun','bounds')
- You can also update properties one at a time by using dot notation.
gs.MaxTime = 1800
五、Algorithms 原理
For a detailed description of the algorithm, see GlobalSearch Algorithm. Ugray et al. [1] describe both the algorithm and the scatter-search method of generating trial points.
六、网址链接:
[1] GlobalSearch
[2] How GlobalSearch and MultiStart Work
参考文献:
[1] Ugray, Zsolt, Leon Lasdon, John Plummer, Fred Glover, James Kelly, and Rafael Martí. Scatter Search and Local NLP Solvers: A Multistart Framework for Global Optimization. INFORMS Journal on Computing, Vol. 19, No. 3, 2007, pp. 328–340.