Getting error in my OuputFcn for Genetic Algorithm

Hi
I'm trying to save the state information by using an OutputFcn, but I keep getting errors when I call state.Generation. The errors only say
"Error in run_ga_search>myOutFun (line 63)
state.Generation"
Here's my OutputFcn code:
opts = gaoptimset('Display','off','OutputFcn',@myOutFun);
[x,fval,exitFlag,Output,population] = ga(@grating_cost_function_from_dc,nvars,[],[],[],[],LB,UB,[],opts);
function [state, options, optchanged] = myOutFun(options, state, flag)
%gen = state.Generation;
state.Generation
if strcmp(flag,'iter')
fileID = fopen([folder '/stoppingCriteria.txt'],'w');
fprintf(fileID, 'Stopping Criteria\n');
fprintf(fileID, 'Generation: %6.6f\n', state.Generation/options.MaxGenerations);
fprintf(fileID, 'Time: %6.6f\n', toc(state.StartTime)/options.MaxTime);
fprintf(fileID, 'StallG: %6.6f\n', (gen-state.LastImprovement)/options.MaxStallGenerations);
fprintf(fileID, 'StallT: %6.6f\n', toc(state.LastImprovementTime)/options.MaxStallTime);
fclose(fileID);
save([state_folder '/state_' num2str(state.Generation) '.mat'],'state');
end
end
Thanks

5 comentarios

For debugging purposes, before your reference to state.Generation, insert
class(state)
disp(state)
and see what shows up.
I get "Error in run_ga_search>myOutFun (line 63)
class(state)"
It sounds as if myOutFun has been invoked without any parameters, somehow. I suggest at the command line you invoke
dbstop if caught error
and run again. It should stop at the actual error call. You can then use dbstatus and dbup to find the place the call was made.
What is showing up is the kind of thing I would expect if your line
opts = gaoptimset('Display','off','OutputFcn',@myOutFun);
somehow is instead
opts = gaoptimset('Display','off','OutputFcn',myOutFun);
Yea that's what I'm thinking because the full error code is
"Error in run_ga_search_dc>myOutFun (line 62) if strcmp(flag,'iter')
Error using feval Output argument "optchanged" (and maybe others) not assigned during call to "/home/rishipat/diskstation_allusers/ydahmani/20160507_SiLN_GC/multi_variable_ga/run_ga_search_dc.m>run_ga_search_dc/myOutFun".
Error in gaoutput (line 28) [state,optnew,changed] = feval(functions{i},options,state,flag,args{i}{:});
Error in galincon (line 31) [state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in ga (line 359) [x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in run_ga_search_dc (line 59) [x,fval,exitFlag,Output,population] = ga(@grating_cost_function_from_dc,nvars,[],[],[],[],LB,UB,[],opts);
Error in run_many_fmin_searches_with_random_starting_values (line 15) run_ga_search_dc(1)"
Yanni Dahmani
Yanni Dahmani el 12 de Jul. de 2016
Editada: Yanni Dahmani el 13 de Jul. de 2016
I also noticed some errors in my code and corrected them but they weren't causing the error. The updated code looks like this:
opts = gaoptimset('OutputFcns',@myOutFun);
[x,fval,exitFlag,Output,population] = ga(@grating_cost_function_from_dc,nvars,[],[],[],[],LB,UB,[],opts);
function [state, options, optchanged] = myOutFun(options, state, flag)
if strcmp(flag,'iter')
state.Generation
fileID = fopen([folder '/stoppingCriteria.txt'],'w');
fprintf(fileID, 'Stopping Criteria\n');
fprintf(fileID, 'Generation: %6.6f\n', state.Generation/options.MaxGenerations);
fprintf(fileID, 'Time: %6.6f\n', toc(state.StartTime)/options.MaxTime);
fprintf(fileID, 'StallG: %6.6f\n', (state.Generation-state.LastImprovement)/options.MaxStallGenerations);
fprintf(fileID, 'StallT: %6.6f\n', toc(state.LastImprovementTime)/options.MaxStallTime);
fclose(fileID);
save([state_folder '/state_' num2str(state.Generation) '.mat'],'state');
end
end
And after doing dbstop if error, it looks like my output function isn't getting passed any parameters but I could be wrong, so I really don't know what's going on. It finds the error at [state,optnew,changed] = feval(functions{i},options,state,flag,args{i}{:}); If it helps I'm running matlab 2014b

Iniciar sesión para comentar.

 Respuesta aceptada

Yanni Dahmani
Yanni Dahmani el 6 de Ag. de 2016

0 votos

I figured out the problem. I only had code in my OuputFcn for one instance, i.e when the flag was iter so the outputfcn didn't know what to do when the flag was set to any other flag.

Más respuestas (0)

Categorías

Preguntada:

el 11 de Jul. de 2016

Respondida:

el 6 de Ag. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by