main content

output function for problem-凯发k8网页登录

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.

for the solver-based approach to this example, see .

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 denoted x, 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-based x. to map between optimization variables and x, use . in this example, to avoid confusion with an optimization variable named x, use "in" as the vector variable name.

problem description

the problem is to minimize the following function of variables x and y:

f=exp(x)(4x2 2y2 4xy 2y 1).

in addition, the problem has two nonlinear constraints:

x y-xy1.5xy-10.

problem-based setup

to set up the problem in the problem-based approach, define optimization variables and an optimization problem object.

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

define the objective function as an expression in the optimization variables.

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

include the objective function in prob.

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 structure x0. use x0.x = –1 and x0.y = 1.

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

output function

the outfun output function records a history of the points generated by fmincon during its iterations. the output function also plots the points and keeps a separate history of the search directions for the sqp algorithm. the search direction is a vector from the previous point to the next point that fmincon tries. 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, see .

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, use varindex.

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

the mapping shows that x is variable 1 and y is variable 2. so, if the input variable is named in, then x = in(1) and y = in(2).

type outfun
function stop = outfun(in,optimvalues,state,idx)
     persistent history searchdir fhistory
     stop = false;
 
     switch state
         case 'init'
             hold on
             history = [];
             fhistory = [];
             searchdir = [];
         case 'iter'
         % concatenate current point and objective function
         % value with history. in must be a row vector.
           fhistory = [fhistory; optimvalues.fval];
           history = [history; in(:)']; % 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 the outputfcn option. also, set the algorithm option to use the 'sqp' algorithm instead of the default 'interior-point' algorithm. pass idx to the output function as an extra parameter in the last input. see .

outputfn = @(in,optimvalues,state)outfun(in,optimvalues,state,idx);
opts = optimoptions('fmincon','algorithm','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,'options',opts)
solving problem using fmincon.

figure contains an axes object. the axes object with title sequence of points computed by fmincon contains 22 objects of type line, text. one or more of the lines displays its values using only markers

local minimum found that satisfies the constraints.
optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
sol = struct with fields:
    x: -9.5474
    y: 1.0474
fval = 0.0236
eflag = 
    optimalsolution
output = struct with fields:
              iterations: 10
               funccount: 22
               algorithm: 'sqp'
                 message: 'local minimum found that satisfies the constraints....'
         constrviolation: 1.2434e-14
                stepsize: 1.4785e-07
            lssteplength: 1
           firstorderopt: 7.1930e-10
            bestfeasible: [1x1 struct]
     objectivederivative: "reverse-ad"
    constraintderivative: "closed-form"
                  solver: 'fmincon'

examine the iteration history. each row of the optimhistory matrix represents one point. the last few points are very close, which explains why the plotted sequence shows overprinted numbers for points 8, 9, and 10.

disp('locations');disp(optimhistory)
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

examine the searchdirhistory and functionhistory arrays.

disp('search directions');disp(searchdirhistory)
search directions
         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.8725   -0.0653
   -1.0241   -0.0108
   -0.0011    0.0010
    0.0000   -0.0000
disp('function values');disp(functionhistory)
function values
    1.8394
    1.8513
    1.7757
    0.9839
    0.6343
    0.3250
    0.0978
    0.0517
    0.0236
    0.0236
    0.0236

unsupported functions require fcn2optimexpr

if your objective function or nonlinear constraint functions are not composed of elementary functions, you must convert the functions to optimization expressions using . see . for this example, you would enter the following code:

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

for the list of supported functions, see .

see also

related topics

网站地图