How can I export to a file xopt and fval parameters from a pattern search function at each iteration?
6 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Kristen Amaddio
2017 年 7 月 27 日
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:
http://www.mathworks.com/help/optim/ug/output-functions.html (General optimization)
https://www.mathworks.com/help/gads/pattern-search-options.html (Focus on patternsearch)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Direct Search についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!