Borrar filtros
Borrar filtros

How can I export to a file xopt and fval parameters from a pattern search function at each iteration?

12 visualizaciones (últimos 30 días)
the function is: [xopt,fval] = patternsearch(@(x)bi_dop(x,rx,tx,const,dop,phi,t), x0, [],[],[],[],lb,ub,options)
Is there a way to iterate inside the pattern search so that the output does not give me only the final value of xopt.

Respuestas (1)

Kristen Amaddio
Kristen Amaddio el 27 de Jul. de 2017
You can define an 'OutputFcn' in your 'optimoptions' in order to store this information. When defined, the optimization algorithm will automatically input a parameter called 'optimValues' to that 'OutputFcn'. This parameter contains the 'x' and 'fval' data at each iteration, and you can access it in order to build up a history of those values over all the iterations.
For more information about using output functions during optimization, see the following documentation pages:
Here is an example of how you could code this:
function history = runpattern
% Objective function
function y = psobj(x)
y = exp(-x(1)^2-x(2)^2)*(1+5*x(1) + 6*x(2) + 12*x(1)*cos(x(2)));
end
% Initialize history fields to store iterative data
history.x = [];
history.fval = [];
% Simple patternsearch example
x0 = [0 0];
options = optimoptions('patternsearch','Display','iter','OutputFcn',@outfun);
[xopt, fval] = patternsearch(@psobj,x0,[],[],[],[],[],[],[],options)
% OutputFcn for patternsearch
% IMPORTANT: optimValues contains data from the current iteration.
% We will use this parameter in order to access 'x' and 'fval' information
function [stop,options,optchanged] = outfun(optimValues,options,flag)
stop = false; % Let algorithm continue to next iteration
optchanged = false; % We are not making any changes to 'options'
switch flag % Current state in which the output is called
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; optimValues.x];
otherwise
end
end
end
Now call this function:
>> history = runpattern
You should see that the output struct, history, contains the data for all iterations. Now that you have this data, you can write 'history.x' and 'history.fval' to a file.

Categorías

Más información sobre Direct Search en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by