extra paramters to ga output function

I want to write an output function to the ga algorithm that will use and also change variables in my workspace in each iteration (plots, changes to a connected device etc). I read about it in the documentation but I couldn't quite figure it out. I would like to avoid using global variables, so is there a syntax example on how to add these extra input and output parameters? For example (which I know is incorrect but explains my intentions):
function [state,options,optchanged,a,b] = gaIterationOutput(options,state,flag,a,b,c,d)
% Do stuff with state,options,optchanged
% Do stuff with a,b,c,d
end
Here a,b,c,d are extra input parameters, and a,b are also sent as output parameters to the workspace. I understand it somehow needs to be done using anonymous functions, but I didn't understand exactly how.
Also, is it necessary to explicitly write
optchanged = false;
as can be seen in examples? (That is indeed the case with my code)
Thanks.

 Accepted Answer

Ok, I've managed to solve all my porblems. I still don't know how to use the suggested nested function, but instead I used handles to send extra parameters to the function, as in my above comment to Walter Roberson. In order to change variables in the base workspace I used the commands
evalin
assignin
So now I have full control over any variable through the ga's output function.
A nice thread I found, if anyone wants to review these commands: Use of evalin and assignin in ga output function

More Answers (1)

Yes, it is necessary to optchanged = false;
It is not possible to have extra outputs.
In your situation I would suggest not having the extra variables as inputs or outputs. Instead I would suggest that you use nested functions with shared variables. See http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html#f0-50984

5 Comments

Thanks for your reply. I still don't understand how to use them though. Where am I supposed to define the nested function? It can't be defined inside the outputfunc since its scope doesn't contain the needed variables; it can't be defined inside main; so my only idea is to define it inside the ga script or something, but it too doesn't have access to all of main's workspace. Thanks again.
Ok I partly figured this out. Instead of passing an output function
gaoutputfunc(options,state,flag)
I used handles to pass
@(options,state,flag)gaoutputfunc(options,state,flag,[extra parameters])
I can now work with these params inside the function. The only thing left is to be able to send information back to the main workspace.
Nest the definition of the output function within the function that defines the variables you want to be able to alter, which might be the main function.
Use this to have it pass through the state
state.extra_param=extra_param
Then you can save things in the output function
case {'iter','interrupt'}
disp('Iterating ...')
fname=[pwd,'\','analysis',date_var,'.mat'];
gen_num=state.Generation;
%gen_str=['gen',num2str(gen_num)]
gen_data(gen_num).state=state;
save(fname,'gen_data')
Hope this helps any in the future!
I am not sure how you got your solution to work, but I am working on it

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!