主要内容

基于问题的优化的输出功能

This example shows how to use an output function to plot and store the history of the iterations for a nonlinear problem. This history includes the evaluated points, the search directions that the solver uses to generate points, and the objective function values at the evaluated points.

对于基于求解器的方法,请参见优化工具箱的输出功能

Plot functions have the same syntax as output functions, so this example also applies to plot functions.

For both the solver-based approach and the problem-based approach, write the output function according to the solver-based approach. In the solver-based approach, you use a single vector variable, usually denotedx, instead of a collection of optimization variables of various sizes. So to write an output function for the problem-based approach, you must understand the correspondence between your optimization variables and the single solver-basedx。在优化变量和x, 利用Varindex。In this example, to avoid confusion with an optimization variable namedx, 利用"在”作为向量变量名称。

Problem Description

The problem is to minimize the following function of variablesxy:

f = exp ( x ) ( 4 x 2 + 2 y 2 + 4 x y + 2 y + 1 )

In addition, the problem has two nonlinear constraints:

x + y - x y 1 5 x y - 1 0

Problem-Based Setup

要在基于问题的方法中设置问题,请定义优化变量和优化问题对象。

x = optimvar('X');y = optimvar('y');prob = optimproblem;

将目标函数定义为优化变量中的表达式。

f = exp(x)*(4*x^2 + 2*y^2 + 4*x*y + 2*y + 1);

Include the objective function in概率

prob.Objective = f;

To include the nonlinear constraints, create optimization constraint expressions.

cons1 = x + y -x*y> = 1.5;CONS2 = x*y> = -10;prob.constraints.cons1 = cons1;prob.constraints.cons2 = cons2;

Because this is a nonlinear problem, you must include an initial point structurex0。利用x0.x = –1x0.y = 1

x0.x = -1; x0.y = 1;

Output Function

TheOUTFUN输出功能记录了由fminconduring its iterations. The output function also plots the points and keeps a separate history of the search directions for theSQP算法。搜索方向是从上一点到下一个点的向量fmincontries. During its final step, the output function saves the history in workspace variables, and saves a history of the objective function values at each iterative step.

For the required syntax of optimization output functions, seeOutput Function and Plot Function Syntax

An output function takes a single vector variable as an input. But the current problem has two variables. To find the mapping between the optimization variables and the input variable, useVarindex

idx = varindex(prob); idx.x
ans = 1
idx.y
ans = 2

映射显示x是可变1和y是可变2。因此,如果输入变量命名, thenx = in(1)y =(2)

类型OUTFUN
函数stop = offun(in OptimValues,state,idx)持久历史记录searchdir fhistory stop = false;切换状态案例“ init”持有历史记录= [];fhistory = [];SearchDir = [];案例“ iTer”%联合电流点和目标函数%与历史的值。必须是行矢量。fhistory = [fhistory;optimvalues.fval];历史= [历史记录;在(:)']; % Ensure in is a row vector % Concatenate current search direction with % searchdir. searchdir = [searchdir;... optimValues.searchdirection(:)']; plot(in(idx.x),in(idx.y),'o'); % Label points with iteration number and add title. % Add .15 to idx.x to separate label from plotted 'o' text(in(idx.x)+.15,in(idx.y),... num2str(optimValues.iteration)); title('Sequence of Points Computed by fmincon'); case 'done' hold off assignin('base','optimhistory',history); assignin('base','searchdirhistory',searchdir); assignin('base','functionhistory',fhistory); otherwise end end

Include the output function in the optimization by setting theoutputfcnoption. Also, set theAlgorithm使用的选项'SQP'算法而不是默认值“内点”算法。经过idxto the output function as an extra parameter in the last input. See传递额外的参数

outputfn = @(in OptimValues,state)OUTFUN(in OptimValues,state,idx);opts = optimoptions('fmincon','算法','SQP','OutputFcn',outputfn);

Run Optimization Using Output Function

Run the optimization, including the output function, by using the 'Options'name-value pair argument.

[sol,fval,eflag,output] = solve(prob,x0,'选项',opts)
使用Fmincon解决问题。

Figure contains an axes object. The axes object with title Sequence of Points Computed by fmincon contains 22 objects of type line, text.

当地最低限度发现满足约束。之所以完成优化,是因为目标函数在可行的方向,最优性公差值之内不折叠,并且在约束公差的值之内满足了约束。
sol =带有字段的结构:X:-9.5474 y:1.0474
FVAL = 0.0236
eflag = OptimalSolution
输出=带有字段的结构:iterations: 10 funcCount: 22 algorithm: 'sqp' message: 'Local minimum found that satisfies the constraints....' constrviolation: 0 stepsize: 1.4785e-07 lssteplength: 1 firstorderopt: 7.1930e-10 bestfeasible: [1x1 struct] objectivederivative: "reverse-AD" constraintderivative: "closed-form" solver: 'fmincon'

检查迭代历史。每一行optimhistory矩阵表示一个点。过去的几个点re very close, which explains why the plotted sequence shows overprinted numbers for points 8, 9, and 10.

disp(“位置”); disp(最佳历史)
Locations -1.0000 1.0000 -1.3679 1.2500 -1.6509 1.1813 -3.5870 2.0537 -4.4574 2.2895 -5.8015 1.5531 -7.6498 1.1225 -8.5223 1.0572 -9.5463 1.0464 -9.5474 1.0474 -9.5474 1.0474

检查SearchDirhistoryfunctionhistoryarrays.

disp('Search Directions');disp(SearchDirhistory)
搜索指示0 0 -0.3679 0.2500 -0.2831 -0.0687 -1.9360 0.8725 -0.8704 0.2358 -1.3441 -0.7364 -2.0877 -0.6493 -0.6493 -0.8725 -0.0.665-0.0.653 -1.0.653 -1.0.653 -1.0.653 -1.10.1.0.653 -1.103 -im.0.1.0.653 -1.1033 -10.653 -1.1033-153 -1.1.1.1.bist
disp('Function Values'); disp(功能史)
功能值1.8394 1.8513 1.7757 0.9839 0.6343 0.3250 0.0978 0.0517 0.0236 0.0236 0.0236

不受金宝app支持的功能需要FCN2OPTIMEXPR

If your objective function or nonlinear constraint functions are not composed of elementary functions, you must convert the functions to optimization expressions usingFCN2OPTIMEXPR。看将非线性函数转换为优化表达式。对于此示例,您将输入以下代码:

fun = @(x,y)exp(x)*(4*x^2 + 2*y^2 + 4*x*y + 2*y + 1); f = fcn2optimexpr(fun,x,y);

有关支持功能的列表,请参见金宝appSupported Operations for Optimization Variables and Expressions

看Also

相关话题