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 件のコメント

Walter Roberson
Walter Roberson 2016 年 7 月 12 日
For debugging purposes, before your reference to state.Generation, insert
class(state)
disp(state)
and see what shows up.
Yanni Dahmani
Yanni Dahmani 2016 年 7 月 12 日
I get "Error in run_ga_search>myOutFun (line 63)
class(state)"
Walter Roberson
Walter Roberson 2016 年 7 月 12 日
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
Yanni Dahmani 2016 年 7 月 12 日
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 2016 年 7 月 12 日
編集済み: Yanni Dahmani 2016 年 7 月 13 日
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

サインインしてコメントする。

 採用された回答

Yanni Dahmani
Yanni Dahmani 2016 年 8 月 6 日

0 投票

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.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by