Getting error in my OuputFcn for Genetic Algorithm
Mostrar comentarios más antiguos
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
Walter Roberson
el 12 de Jul. de 2016
For debugging purposes, before your reference to state.Generation, insert
class(state)
disp(state)
and see what shows up.
Yanni Dahmani
el 12 de Jul. de 2016
Walter Roberson
el 12 de Jul. de 2016
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);
Yanni Dahmani
el 12 de Jul. de 2016
Yanni Dahmani
el 12 de Jul. de 2016
Editada: Yanni Dahmani
el 13 de Jul. de 2016
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Operating on Diagonal Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!