Passing extra outputs from cost function

Dear Matlab Community,
I am using a GA to optimise a certain function, but I want to keep saving some additional data.
f=@(x)requestFitting(extractionInit,x); problemMultiga.fitnessfcn=f;
with
[f,extractionFit] = requestFitting(extractionInit,x)
The problem is, that through the function handle, information on the output of the cost function:extractionFit is lost.
Please help me, how can I save additional outputs of the cost function?
Thanks a lot!
[cost,]

 采纳的回答

There are several ways. Proper use of nested functions is one way. Using an output function is another.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

6 个评论

Thanks Alan, well I have tried to use those, but somehwo I dont get it right. How must the design of the nested functin look like.? Could you give me a more detailed hint how it should look like?
Thanks!
Take a look at this example, which uses a nested function to extract information and store it. Or take a look at this example of a nested output function.
Yes, these are complicated, subtle examples. For your case you will have to look at the overall idea and somehow adapt it.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Hi Alan,
I am wondering what the advantage of using nested functions approach over global variables? I understand functions would have their own local stacks is the only reason to keep it cleaner? Essentially they are 'global' variables for the function and nested functions. I am wondering if I could run an optimizer in parallel with this method. Thanks. I used the example you referenced and adapted for gamultiobj.
This seemed to work - a mashup of examples
function [x Fval rand_value_mat] = myproblem3(x0)
rand_value=[];
rand_value_mat=[];
states=[];
final_state=[];
FitnessFunction = @kurt_multiobjective; % Function handle to the fitness function
numberOfVariables = 3; % Number of decision variables
lb = [-5 -5 -5]; % Lower bound
ub = [5 5 5]; % Upper bound
A = []; % No linear inequality constraints
b = []; % No linear inequality constraints
Aeq = []; % No linear equality constraints
beq = []; % No linear equality constraints
options = optimoptions(@gamultiobj,'PlotFcn',@gaplotpareto,'OutputFcn', @myoutput,'UseParallel',true);
%%
% Run the |gamultiobj| solver and display the number of solutions found on
% the Pareto front and the number of generations.
[x,Fval,exitFlag,Output] = gamultiobj(FitnessFunction,numberOfVariables,A, ...
b,Aeq,beq,lb,ub,options);
for i=1:18
index(i)=find(final_state.Score==Fval(i,:),1)
end
Fval(:,3)=final_state.speed(index);
fprintf('The number of points on the Pareto front was: %d\n', size(x,1));
fprintf('The number of generations was : %d\n', Output.generations);
function[state,options,optchanged] = myoutput(options,state,flag)
optchanged = false;
switch flag
case 'init'
case {'iter','interrupt'}
rand_value_mat=[rand_value_mat,rand_value];
states=[states,state];
case 'done'
state.speed=9.83*ones(length(state.Population),1);
final_state=[state];
end
end
function y = kurt_multiobjective(x)
% using kur_multiobjective
% Initialize for two objectives
y = zeros(2,1);
rand_value=10;
% Compute first objective
for i = 1:2
y(1) = y(1) - 10*exp(-0.2*sqrt(x(i)^2 + x(i+1)^2));
end
% Compute second objective
for i = 1:3
y(2) = y(2) + abs(x(i))^0.8 + 5*sin(x(i)^3);
end
end
save('workspace.mat')
end
I tried parallel and it didnt seem to be a problem, I would have to know more about resource sharing that Matlabs pools do
Actually parallel did mess up

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by