problem with output of ga data in fitness function

조회 수: 4 (최근 30일)
Andreas Rhein
Andreas Rhein 2020년 12월 14일
댓글: Matt J 2020년 12월 14일
I do have a problem with the output of my ga. I formulated a ga problem in any form. My fitness and output function are shown below. The output function is pritty much a modified copy of the Matlab Docu example.
I want my ga to give me other values, then the actual score as an output. I highlighted it down in the code:
problem.fitnessfcn = @(x) myfitnessfun(x,otherParameter);
problem.options = optimoptions('ga','UseParallel', true,'PopulationSize',10,'OutputFcn',{@gaoutfun},'Display','iter');
[x,fval,exitflag,output,population,scores] = ga(problem);
%% This is my fitness function
function K = myfitnessfun(x,otherParameter)
% Definition of persistent variables for the history
persistent n k my_output_history
% the struct variable my_output is calculated with the function my_special_function as a function of x and otherParameter
[my_output] = my_special_function(x,otherParamter)
% The scalar fitness is calculted with a operator function make_scalar_value
K = make_scalar_value(my_output);
%% Here I use the persistent variables to store and save the values of my_output in the workspace.
% This is only working in serial run of the ga. It is not working for the parallel run
% I save all the data of all runs of the finess function in the cell array my_out_history
if isempty(n)
n=1;
k=1;
my_output_history = cell(1e4,2);
end
if rem(n,1) == 0
my_output_history{k,1} = my_output;
my_output_history{k,2} = x; % For the later assignement of the values i also store the vector of x in the cell array
assignin('base','my_output_history', my_output_history);
k = k+1;
end
n = n+1;
%% I want to access and save all the different values of my_output into the workspace.
% Here i want something like:
my_output_history(state.Individual) = my_output;
assignin('base','my_output',my_output_history);
end
%% This is my output function
% I cannot hand over the gaoutfun other values than options, state and flag
function [state,options,optchanged] = gaoutfun(options,state,flag)
persistent pophistory scorehistory bestscorehistory
optchanged = false;
switch flag
case 'init'
pophistory(:,:,1) = state.Population;
scorehistory(:,1) = state.Score;
bestscorehistory(1) = min(scorehistory(:,1));
assignin('base','gapopulationhistory',pophistory);
assignin('base','gascorehistory',scorehistory);
assignin('base','gabestscorehistory',bestscorehistory);
case 'iter'
% Update the pophistory every 10 generations.
if rem(state.Generation,1) == 0
ss = size(pophistory,3);
pophistory(:,:,ss+1) = state.Population;
scorehistory(:,ss+1) = state.Score;
bestscorehistory(ss+1,1) = min(scorehistory(:,ss+1));
assignin('base','gapopulationhistory',pophistory);
assignin('base','gascorehistory',scorehistory);
assignin('base','gabestscorehistory',bestscorehistory);
ibest = state.Best(end);
ibest = find(state.Score == ibest,1,'last');
bestx = state.Population(ibest,:);
disp(['best x in population: ', num2str(bestx)]);
% pause(0.01)
end
case 'done'
% Include the final population in the pophistory.
ss = size(pophistory,3);
pophistory(:,:,ss+1) = state.Population;
scorehistory(:,ss+1) = state.Score;
bestscorehistory(ss+1,1) = min(scorehistory(:,ss+1));
assignin('base','gapopulationhistory',pophistory);
assignin('base','gascorehistory',scorehistory);
assignin('base','gabestscorehistory',bestscorehistory);
end
end
Is there a way, i can access the actual state of the ga in the fitness function like it is done in the output function? At the moment I`m using the version that can be found above, but this i not working for parallel computing.
Basically i want at the end: The history not only of the scores (scalar values) and the population information but also of my own variable (struct) my_output, that is calculated as a function of x in the fitness function.
Any good ideas how do do this?
Thanks a lot!
  댓글 수: 1
Matt J
Matt J 2020년 12월 14일
Since you are using UseParallel what does "history" even mean? In parallel computing, there is no well-defined sequence of anything.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by