returning additional values from a fitness function for genetic optimisation

Is there a simple way to return additional values from a fitness function (in addition to the objective value) when using an optimisation algorithm, such as genetic algorithm or multi-objective ga in matlab?
For example, it is easy enough to specify additional parameters to be passed to a fitness function using a function handle:
f = @(x)parameterfun(x,a,b,c);
However, is possible to do something similar for the outputs of the fitness function?
Some ideas I had include saving the values in the fitness-function to a file, and then reading them in after the ga has finished in the parent function, or saving the values as global variables, but is there a simpler approach?

 Respuesta aceptada

Alan Weiss
Alan Weiss el 16 de Sept. de 2015
You can use nested functions to do what you want. The example of a nested output function might help.
Alan Weiss
MATLAB mathematical toolbox documentation

5 comentarios

Hi Alan, I've taken a look at the nested output function example but I don’t understand how it would work. I have the ga working currently but want to log all the outputs from the fitness function at the end of each generation for more analysis.
I have the following parameters passed to the fitness function, and the line for the GA :
f=@(x)framework0(x,y,z);
[DVbest, sold, exit, output, population, scores] = ga(f,14,[],[],[],[],DV_l,DV_u,[],options);
The fitness function, framework0, has 3 outputs I want to log, the function looks something like this:
function [output] = framework0(x,y,z)
[x1,y1,z1] = regression (x,y,z);
[x2,y2,z2] = framework1 (x,y,z,x1,y1,z1);
output = -x2(3); %Single value output to GA
end
I had a similar idea to save the output values (x2,y2,z2) in the fitness function but I’m assuming using nested functions would be quicker to operate.
Usually, people use an output function to record what they like.
Alan Weiss
MATLAB mathematical toolbox documentation
I currently have an output function which records the population, score and state - but i dont understand how to pass through the extra variables from the fitness function to the output function.
You pass extra variables to an output function in exactly the same way that you pass extra parameters anywhere else.
Alan Weiss
MATLAB mathematical toolbox documentation
Do you mind showing me what that would look like using the above sample code? I understand passing the extra parameters in the example provided in the link, which is similar to passing variables into the fitness function, but i cant seem to get it to work from the ga to the output function. From what I understood so far, I've added the following to the options line:
options = optimoptions('ga','CreationFcn',@gacreationframework,...
'OutputFcn',@(options,state,flag)gaoutputframework(options,state,flag,fout),...
'MaxGenerations',1,...
'PopulationSize',4,...
'UseVectorized',false,...
'UseParallel',true); %
and followed a similar line with the output function:
function [state,options,optchanged] = gaoutputframework(options,state,flag,fout)
where fout contain the extra parameters within the fitness function. But this doesnt work...

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 14 de Sept. de 2015

Comentada:

el 15 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by