主要内容

simulannealbnd

使用模拟退火算法求函数的最小值

说明

x = simulannealbnd(fun,x0) 求计算目标函数值的函数句柄 fun 的局部最小值 xx0 是模拟退火算法的初始点(实数向量)。

注意

传递额外参数说明如何将额外的参数传递给目标函数(如有必要)。

示例

x = simulannealbnd(fun,x0,lb,ub)x 中的设计变量定义一组下界和上界,使解始终在 lb x ub 范围内。如果 x(i) 无下界,请设置 lb(i) = -Inf,如果 x(i) 无上界,请设置 ub(i) = Inf

示例

x = simulannealbnd(fun,x0,lb,ub,options) 使用 options 所指定的优化选项执行最小化。使用 optimoptions 创建 options。如果不存在边界,请设置 lb = [] 和/或 ub = []

示例

x = simulannealbnd(problem)problem 的最小值,它是 problem 中所述的一个结构体。

[x,fval] = simulannealbnd(___) 对上述任何语法,返回目标函数 fun 在解 x 处的值。

[x,fval,exitflag,output] = simulannealbnd(___) 还返回描述 simulannealbnd 的退出条件的值 exitflag,以及提供优化过程信息的结构体 output

示例

示例

全部折叠

最小化德容的第五个函数(一个具有许多局部最小值的二维函数)。运行此示例时此函数可用。

绘制德容的第五个函数。

dejong5fcn

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

使用 simulannealbnd 从点 [0,0] 开始最小化德容的第五个函数。

fun = @dejong5fcn;
x0 = [0 0];
x = simulannealbnd(fun,x0)
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x = 1×2

  -32.0285   -0.1280

simulannealbnd 算法使用 MATLAB® 随机数流,因此您获得的结果可能会有所不同。

在有界区域内最小化德容的第五个函数。运行此示例时此函数可用。

绘制德容的第五个函数。

dejong5fcn

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

从点 [0,0] 开始启动 simulannealbnd,对每个分量设置下界为 -64、上界为 64。

fun = @dejong5fcn;
x0 = [0 0];
lb = [-64 -64];
ub = [64 64];
x = simulannealbnd(fun,x0,lb,ub)
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x = 1×2

  -15.9790  -31.9593

simulannealbnd 算法使用 MATLAB® 随机数流,因此您获得的结果可能会有所不同。

通过设置选项以使用一些绘图函数,观察 simulannealbnd 的进度。

设置模拟退火选项以使用几个绘图函数。

options = optimoptions('simulannealbnd','PlotFcns',...
          {@saplotbestx,@saplotbestf,@saplotx,@saplotf});

从点 [0,0] 开始启动 simulannealbnd,对每个分量设置下界为 -64、上界为 64。最小化 dejong5fcn,该函数在您运行此示例时可用。

rng default % For reproducibility
fun = @dejong5fcn;
x0 = [0,0];
lb = [-64,-64];
ub = [64,64];
x = simulannealbnd(fun,x0,lb,ub,options)

Figure Simulated Annealing contains 4 axes objects. Axes object 1 with title Best Point, xlabel Variable number, ylabel Best point contains an object of type bar. Axes object 2 with title Best Function Value: 1.99203, xlabel Iteration, ylabel Function value contains an object of type scatter. Axes object 3 with title Current Point, xlabel Variable number, ylabel Current point contains an object of type bar. Axes object 4 with title Current Function Value: 1.99203, xlabel Iteration, ylabel Function value contains an object of type scatter.

simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x = 1×2

  -15.9790  -31.9593

获得模拟退火最小化的所有输出。

绘制德容的第五个函数,该函数在运行此示例时可用。

dejong5fcn

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

从点 [0,0] 开始启动 simulannealbnd,对每个分量设置下界为 -64、上界为 64。

fun = @dejong5fcn;
x0 = [0,0];
lb = [-64,-64];
ub = [64,64];
[x,fval,exitflag,output] = simulannealbnd(fun,x0,lb,ub)
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x = 1×2

  -15.9790  -31.9593

fval = 
1.9920
exitflag = 
1
output = struct with fields:
     iterations: 1762
      funccount: 1779
        message: 'simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.'
       rngstate: [1×1 struct]
    problemtype: 'boundconstraints'
    temperature: [2×1 double]
      totaltime: 0.6676

simulannealbnd 算法使用 MATLAB® 随机数流,因此您获得的结果可能会有所不同。

输入参数

全部折叠

要最小化的函数,指定为函数句柄或函数名称。fun 函数接受向量 x,并返回实数标量 f,即在 x 处计算的目标函数值。

fun 可以指定为一个文件的函数句柄:

x = simulannealbnd(@myfun,x0)

其中 myfun 是一个 MATLAB® 函数,例如

function f = myfun(x)
f = ...            % Compute function value at x

fun 也可以是匿名函数的函数句柄:

x = simulannealbnd(@(x)norm(x)^2,x0,lb,ub);

示例: fun = @(x)sin(x(1))*cos(x(2))

数据类型: char | function_handle | string

初始点,指定为实数向量。simulannealbnd 使用 x0 中的元素数目来确定 fun 接受的变量的数目。

示例: x0 = [1,2,3,4]

数据类型: double

下界,指定为实数向量或实数数组。如果 x0 中的元素数目等于 lb 中的元素数目,则 lb 指定

x(i) >= lb(i)(对于全部 i)。

如果 numel(lb) < numel(x0),则 lb 指定

x(i) >= lb(i),其中 1 <= i <= numel(lb)

在这种情况下,求解器会发出警告。

示例: 要指定所有控制项变量均为正值,lb = zeros(size(x0))

数据类型: double

上界,指定为实数向量或实数数组。如果 x0 中的元素数目等于 ub 中的元素数目,则 ub 指定

x(i) <= ub(i)(对于全部 i)。

如果 numel(ub) < numel(x0),则 ub 指定

x(i) <= ub(i),其中 1 <= i <= numel(ub)

在这种情况下,求解器会发出警告。

示例: 要指定所有控制项变量都小于 1,ub = ones(size(x0))

数据类型: double

优化选项,指定为 optimoptions 返回的对象或结构体。有关详细信息,请参阅模拟退火选项

optimoptions 隐藏以斜体列出的选项;请参阅optimoptions 隐藏的选项

{} 表示默认值。请参阅 模拟退火选项 中的选项详细信息。

选项描述

AcceptanceFcn

算法用于确定是否接受新点的函数。指定为 'acceptancesa' 或函数句柄。

函数句柄 | {'acceptancesa'}

AnnealingFcn

算法用于生成新点的函数。指定为内置退火函数的名称或函数句柄。

函数句柄 | 函数名称 | 'annealingboltz' | {'annealingfast'}

DataType

决策变量的类型

'custom' | {'double'}

Display

显示级别

'off' | 'iter' | 'diagnose' | {'final'}

DisplayInterval

迭代输出的间隔

正整数 | {10}

FunctionTolerance

函数值的终止容差

对于 options 结构体,使用 TolFun

非负标量 | {1e-6}

HybridFcn

在求解器迭代期间或迭代结束时自动运行 HybridFcn(另一个优化函数)。指定为名称或函数句柄。

请参阅 何时使用混合函数

'fminsearch' | 'patternsearch' | 'fminunc' | 'fmincon' | {[]}

或者

1×2 元胞数组 | {@solver, hybridoptions},其中 solver = fminsearchpatternsearchfminuncfmincon {[]}

HybridInterval

调用 HybridFcn 的间隔(如果不是 'end''never'

正整数 | 'never' | {'end'}

InitialTemperature

温度初始值

非负标量 | 正向量| {100}

MaxFunctionEvaluations

允许的目标函数计算的最大次数

对于 options 结构体,使用 MaxFunEvals

非负整数 | {3000*numberOfVariables}

MaxIterations

允许的最大迭代次数

对于 options 结构体,使用 MaxIter

非负整数 | {Inf}

MaxStallIterations

当前点处适应度函数值的平均变化小于 options.FunctionTolerance 的迭代次数

对于 options 结构体,使用 StallIterLimit

非负整数 | {500*numberOfVariables}

MaxTime

算法在运行 MaxTime 秒后停止

对于 options 结构体,使用 TimeLimit

非负标量 | {Inf}

ObjectiveLimit

所需的最小目标函数值

标量 | {-Inf}

OutputFcn

函数获取迭代数据,并可以在运行时更改选项

对于 options 结构体,使用 OutputFcns

函数句柄 | 函数句柄的元胞数组 | {[]}

PlotFcn

迭代期间调用的绘图函数

对于 options 结构体,使用 PlotFcns

函数句柄 | 内置绘图函数名称 | 函数句柄的元胞数组 | 内置绘图函数名称的元胞数组 | 'saplotbestf' | 'saplotbestx' | 'saplotf' | 'saplotstopping' | 'saplottemperature' | {[]}

PlotInterval

每隔一段时间调用一次绘图函数

正整数 | {1}

ReannealInterval

再退火间隔

非负整数 | {100}

TemperatureFcn

用于更新温度计划的函数

函数句柄 | 内置温度函数名称 | 'temperatureboltz' | 'temperaturefast' | {'temperatureexp'}

示例: options = optimoptions(@simulannealbnd,'MaxIterations',150)

数据类型: struct

问题结构体,指定为含有以下字段的结构体:

  • objective - 目标函数

  • x0 - 起点

  • lb - x 的下界

  • ub - x 的上界

  • solver'simulannealbnd'

  • options - 使用 optimoptions 或 options 结构体创建的选项

  • rngstate - 可选字段,用于重置随机数生成器的状态

注意

problem 必须具有上面指定的所有字段。

数据类型: struct

输出参量

全部折叠

解,以实数向量形式返回。x 的大小与 x0 的大小相同。通常情况下,当 exitflag 为正时,x 是该问题的局部解。

解处的目标函数值,以实数形式返回。通常,fval = fun(x)

simulannealbnd 停止的原因,以整数形式返回。

退出标志含义
1

options.MaxStallIterations 次迭代中,目标函数值的平均变化小于 options.FunctionTolerance

5

目标函数值小于 options.ObjectiveLimit

0

已达到函数计算或迭代的最大次数。

-1

优化被输出函数或绘图函数终止。

-2

找不到可行点。

-5

超过时间限制。

有关优化过程的信息,以包含下列字段的结构体形式返回:

  • problemtype - 问题类型:无约束或边界约束。

  • iterations - 计算的迭代次数。

  • funccount - 目标函数的计算次数。

  • message - 算法终止的原因。

  • temperature - 求解器终止时的温度。

  • totaltime - 求解器运行的总时间。

  • rngstate - 在算法即将开始之前 MATLAB 随机数生成器的状态。您可以使用 rngstate 中的值来重新生成 simulannealbnd 的输出。请参阅 重现结果

替代功能

App

优化实时编辑器任务为 simulannealbnd 提供了一个可视化界面。

版本历史记录

在 R2007a 中推出