Main Content

使用模拟退火算法最小化

此示例显示了如何使用模拟退火算法创建和最小化目标函数(simulannealbnd函数)在全局优化工具箱中。有关算法细节,请参阅模拟退火如何工作

Simple Objective Function

The objective function to minimize is a simple function of two variables:

min f(x)=(4-2.1*x1^2 + x1^4/3)*x1^2 + x1*x2 +(-4 + 4*x2^2)*x2^2;X

如L.C.W.中所述,此功能称为“ CAM”。Dixon和G.P.Szego [1]。

为了实现目标函数计算,MATLAB®文件simple_objective.m具有以下代码:

类型simple_objective
函数y = simple_objective(x)%simple_objective for patternsearch solver%版权所有2004 The MathWorks,Inc。x1 = x(1);x2 = x(2);y =(4-2.1。*x1。^2+x1。^4./3)。*x1。^2+x1。;

所有全局优化工具箱求解器都假定目标有一个输入X, 在哪里Xhas as many elements as the number of variables in the problem. The objective function computes the scalar value of the objective function and returns it in its single output argumenty

最小化使用simulannealbnd

To minimize the objective function usingsimulannealbnd,将函数手柄传递到目标函数和起点X0作为第二个论点。对于可重复性,请设置随机数流。

ObjectiveFunction = @simple_objective;x0 = [0.5 0.5];% 初始点rng默认% For reproducibility[X,FVAL,EXITFLAG,输出] = simulanealbnd(objectionFunction,x0)
优化终止:最佳功能值的更改小于options.functionTolerance。
x =1×2-0.0896 0.7130
FVAL = -1.0316
出口= 1
输出=struct with fields:迭代:2948 FUNCCOUNT:2971消息:“优化终止:最佳功能值的更改小于选项。FunctionTolerance。”rngstate:[1x1 struct] alsiontype:“无约束”温度:[2x1 double]总时间:2.8401

simulannealbndreturns four output arguments:

  • X- 找到的最佳点

  • FVAL- 最佳点功能值

  • 出口— Integer corresponding to the reason the function stopped

  • 输出- 有关优化步骤的信息

约束最小化

You can usesimulannealbndto solve problems with bound constraints. Pass lower and upper bounds as vectors. For each coordinate一世,求解器确保lb(i) <= x(i) <= ub(i)。Impose the bounds–64 <= x(i)<= 64

lb = [-64 -64];ub = [64 64];

Run the solver with the lower and upper bound arguments.

[x,fval,exitFlag,output] = simulannealbnd(ObjectiveFunction,x0,lb,ub);
优化终止:最佳功能值的更改小于options.functionTolerance。
fprintf('The number of iterations was : %d\n',,,,输出。一世terations);
迭代次数为:2428
fprintf('功能评估的数量为:%d \ n',,,,输出。funccount);
The number of function evaluations was : 2447
fprintf('找到的最佳功能值是:%g \ n',,,,FVAL);
找到的最佳功能值是:-1.03163

求解器找到与以前相同的解决方案。

最小化使用Additional Arguments

有时,您希望一个目标函数通过在优化过程中充当常数的额外参数进行参数化。例如,在先前的目标函数中,您可能需要用可以更改的参数替换常数4、2.1和4,以创建目标函数家族。有关更多信息,请参阅Passing Extra Parameters

Rewrite the objective function to take three additional parameters in a new minimization problem.

min f(x)=(a -b*x1^2 + x1^4/3)*x1^2 + x1*x2 +(-c + c*x2^2)*x2^2;X

一种,,,,b,,,,一种ndC是在优化过程中起作用的目标函数的参数(它们不是最小化的一部分)。为了实现目标函数计算,MATLAB文件参数化_objective.mContains the following code:

类型parameterized_objective
function y = parameterized_objective(x,p1,p2,p3) %PARAMETERIZED_OBJECTIVE Objective function for PATTERNSEARCH solver % Copyright 2004 The MathWorks, Inc. x1 = x(1); x2 = x(2); y = (p1-p2.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-p3+p3.*x2.^2).*x2.^2;

同样,您需要将函数句柄传递给目标函数以及第二个参数的起点。

simulannealbndC一种lls the objective function with just one argumentX,,,,but the objective function has four arguments:X,,,,一种,,,,b,,,,一种ndC。要指示哪个变量是参数,请使用匿名函数捕获附加参数的值(常数一种,,,,b,,,,一种ndC)。创建函数句柄ObjectiveFunction到一个匿名函数,该功能接受一个输入X,,,,but callsparameterized_objectiveX,,,,一种,,,,b一种ndC。When you create the function handleObjectiveFunction,变量一种,,,,b,,,,一种ndC值存储在匿名功能吗一世on.

一种= 4; b = 2.1; c = 4;% Define constant valuesObjectiveFunction = @(x) parameterized_objective(x,a,b,c); x0 = [0.5 0.5]; [x,fval] = simulannealbnd(ObjectiveFunction,x0)
优化终止:最佳功能值的更改小于options.functionTolerance。
x =1×20.0898 -0.7127
FVAL = -1.0316

求解器找到与以前相同的解决方案。

References

[1] Dixon,L。C. W.和G.P。Szego(编辑)。Towards Global Optimisation 2.North-Holland: Elsevier Science Ltd., Amsterdam, 1978.

也可以看看

Related Topics