文档

输出功能

什么是输出功能?

一个输出功能是优化函数在其算法的每次迭代中调用的函数。通常,您使用输出函数来生成图形输出,记录算法生成的数据的历史记录,或根据当前迭代中的数据停止算法。您可以将输出函数作为功能文件,本地函数或嵌套功能创建。

您可以使用OutputFcnoption 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),'。');绘制

您可以使用此输出功能来绘制由fminsearchin solving the optimization problem

最小 X F (( X = 最小 X e X 1 (( 4 X 1 2 + 2 X 2 2 + X 1 X 2 + 2 X 2

To do so,

  1. Create a file containing the preceding code and save it asOUTFUN.M在MATLAB路径上的文件夹中。

  2. 设置值Outputfcn领域options函数句柄的结构outfun

    options = optimset('outputfcn',@outfun);
  3. 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 byfminsearch

Structure of the Output Function

输出函数的功能定义行具有以下形式:

stop = offun(x,optimvalues,state)

在哪里

  • 停止is a flag that istrueorFalsedepending on whether the optimization routine halts or continues. See停止Flag

  • X是当前迭代算法计算的点。

  • optimValuesis 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 thefminsearchiterates in solving

最小 X F (( X = 最小 X e X 1 (( 4 X 1 2 + 2 X 2 2 + X 1 X 2 + 2 X 2

输出函数将点的顺序返回,称为矩阵history

To run the example, do the following steps:

  1. Open a new file in the MATLAB Editor.

  2. 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
  3. 将文件保存为myproblem.m在MATLAB路径上的文件夹中。

  4. 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 inhistoryis 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 theoptimValuesstructure that are provided by the optimization functionsfminbnd,,,,fminsearch,,,,andFzero

表的“命令行显示标题”列列出了设置时出现的标题展示parameter ofoptions'iTer'

optimValues Field (optimValues.field)

Description

命令行显示标题

Funccount

Cumulative number of function evaluations

Func-count

FVAL

Function value at current point

最小F((X)

iteration

迭代number — starts at0

迭代

程序

程序messages

程序

状态s of the Algorithm

下表列出了可能的值状态

状态

Description

'init'

The algorithm is in the initial state before the first iteration.

'interrupt'

该算法正在执行迭代。在这种状态下,输出函数可以停止优化的当前迭代。您可能希望输出功能停止迭代以提高计算效率。当状态设置为'interrupt',,,,the values ofXandoptimValues是the same as at the last call to the output function, in which状态is set to'iTer'

'iTer'

该算法在迭代的末尾。

'done'

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 istrueorFalse。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停止按钮回调存储值truein theOptimStopField of a手柄structure called伙计们存储在appdata

函数stop = myOutput(x,optimvalues,state)stop = false;%检查用户是否已要求停止优化。stop = getAppData(hobject,'optimstop');

Related Topics