输出功能
什么是输出功能?
一个输出功能是优化函数在其算法的每次迭代中调用的函数。通常,您使用输出函数来生成图形输出,记录算法生成的数据的历史记录,或根据当前迭代中的数据停止算法。您可以将输出函数作为功能文件,本地函数或嵌套功能创建。
您可以使用OutputFcn
option with the following MATLAB®优化功能:
Creating and Using an Output Function
The following is a simple example of an output function that plots the points generated by an optimization function.
函数stop = offun(x,optimvalues,state)stop = false;坚持,稍等;图(x(1),x(2),'。');绘制
您可以使用此输出功能来绘制由fminsearch
in solving the optimization problem
To do so,
Create a file containing the preceding code and save it as
OUTFUN.M
在MATLAB路径上的文件夹中。设置值
Outputfcn
领域options
函数句柄的结构outfun
。options = optimset('outputfcn',@outfun);
Enter the following commands:
保持OBJFUN =@(x)EXP(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));[x fval] = fminsearch(objfun,[-1 1],选项)
These commands return the solution
x = 0.1290 -0.5323 fval = -0.5689
and display the following plot of the points generated by
fminsearch
:
Structure of the Output Function
输出函数的功能定义行具有以下形式:
stop = offun(x,optimvalues,state)
在哪里
停止
is a flag that istrue
orFalse
depending on whether the optimization routine halts or continues. See停止Flag。X
是当前迭代算法计算的点。optimValues
is a structure containing data from the current iteration.Fields in optimValuesdescribes the structure in detail.状态
是算法的当前状态。状态s of the Algorithmlists the possible values.
The optimization function passes the values of the input arguments tooutfun
在每次迭代中。
嵌套输出功能的示例
示例在Creating and Using an Output Functiondoes not require the output function to preserve data from one iteration to the next. When you do not need to save data between iterations, you can write the output function as a function file and call the optimization function directly from the command line. However, to have an output function to record data from one iteration to the next, write a single file that does the following:
Contains the output function as a nested function—seeNested Functions在MATLAB编程基础上,以获取更多信息。
调用优化功能。
在下面的示例中,该功能文件还包含目标函数作为本地函数。取而代之的是,将目标函数写成单独的文件或匿名函数。
嵌套功能可以访问周围文件中的变量。因此,此方法使输出函数能够将变量从一个迭代保存到下一个迭代。
The following example uses an output function to record thefminsearch
iterates in solving
输出函数将点的顺序返回,称为矩阵history
。
To run the example, do the following steps:
Open a new file in the MATLAB Editor.
Copy and paste the following code into the file.
Function [x fval history] = myproblem(x0) history = []; options = optimset('OutputFcn', @myoutput); [x fval] = fminsearch(@objfun, x0,options); function stop = myoutput(x,optimvalues,state); stop = false; if isequal(state,'iter') history = [history; x]; end end function z = objfun(x) z = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2)); end end
将文件保存为
myproblem.m
在MATLAB路径上的文件夹中。At the MATLAB prompt, enter
[x fval历史] = myproblem([ - 1 1]);
The functionfminsearch
返回X
,,,,the optimal point, andFVAL
,,,,the value of the objective function at x.
x,fval x = 0.1290 -0.5323 fval = -0.5689
In addition, the output functionMyOutput
返回矩阵history
,其中包含生成的分寒冷ithm at each iteration, to the MATLAB workspace. The first four rows ofhistory
是
历史(1:4,:) ANS = -1.0000 1.0000 -1.0000 1.0000 -1.0750 0.9000 -1.0125 0.8500
The final row of points inhistory
is the same as the optimal point,X
。
历史记录(end,:) ans = 0.1290 -0.5323 objfun(历史记录(end,:))ans = -0.5689
Fields in optimValues
The following table lists the fields of theoptimValues
structure that are provided by the optimization functionsfminbnd
,,,,fminsearch
,,,,andFzero
。
表的“命令行显示标题”列列出了设置时出现的标题展示
parameter ofoptions
至'iTer'
。
optimValues Field (optimValues.field) |
Description |
命令行显示标题 |
---|---|---|
|
Cumulative number of function evaluations |
|
|
Function value at current point |
|
|
迭代number — starts at |
|
|
程序messages |
|
状态s of the Algorithm
下表列出了可能的值状态
:
状态 |
Description |
---|---|
|
The algorithm is in the initial state before the first iteration. |
|
该算法正在执行迭代。在这种状态下,输出函数可以停止优化的当前迭代。您可能希望输出功能停止迭代以提高计算效率。当状态设置为 |
|
该算法在迭代的末尾。 |
|
The algorithm is in the final state after the last iteration. |
The following code illustrates how the output function uses the value of状态
至decide which tasks to perform at the current iteration.
切换状态案例“ INIT”设置的图或对话框案例“ ITER”%对图或对话框进行更新,根据需要的情况'intrupt'%检查条件对话框或最终情节结束
停止Flag
输出参数停止
is a flag that istrue
orFalse
。The flag tells the optimization function whether the optimization halts (true
)或继续(False
)。The following examples show typical ways to use the停止
Flag.
根据OptimValues中的数据停止优化
输出功能可以根据当前数据在任何迭代中停止优化optimValues
。例如,以下代码集停止
至true
如果目标函数值小于5
:
函数stop = myOutput(x,optimvalues,state)stop = false;%检查目标函数是否小于5。结尾
根据对话框输入停止优化
If you design a UI to perform optimizations, you can have the output function stop an optimization with, for example, a停止button. The following code shows how to do this callback. The code assumes that the停止按钮回调存储值true
in theOptimStop
Field of a手柄
structure called伙计们
存储在appdata
。
函数stop = myOutput(x,optimvalues,state)stop = false;%检查用户是否已要求停止优化。stop = getAppData(hobject,'optimstop');