Getting error in my OuputFcn for Genetic Algorithm
Show older comments
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 Comments
Walter Roberson
on 12 Jul 2016
For debugging purposes, before your reference to state.Generation, insert
class(state)
disp(state)
and see what shows up.
Yanni Dahmani
on 12 Jul 2016
Walter Roberson
on 12 Jul 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
on 12 Jul 2016
Yanni Dahmani
on 12 Jul 2016
Edited: Yanni Dahmani
on 13 Jul 2016
Accepted Answer
More Answers (0)
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!