MATLAB: Parallelisation for ga with Simulink Model

gaGlobal Optimization Toolboxparallel computingsimulink

Hello, I want to optimize some parameters in my simulink model using a genetic algorithm. At the moment I am using the ga function from the global optimisation toolbox. I have an objective function, in which my simulink model is called.
[x,fval,exitflag,output,population,score] = ga(@ rast_simulink,2,[],[],[],[],[],[],[],[],options);
my objective function is
function scores = rast_simulink(pop)
options = simset('DstWorkspace','current','SrcWorkspace','current');
set_param('rast/x1','Value',num2str(pop(:,1)));
set_param('rast/x2','Value',num2str(pop(:,2)));
simOut = sim( 'rast','SaveOutput','on');
simout = simOut.get('simout');
scores = max (simout.signals.values);
My simulink model runs in acellarator mode, default parameter behavour is set to inlined This works, but since it is slow, I would like to make use of Matlab's parallelization features. How should I do that? When I set the ga option 'UseParallel' to 'always', I get the following error :
Error using rast_simulink (line 9)
Invalid Simulink object name: rast/x1
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in fcnvectorizer (line 16)
parfor (i = 1:popSize)
Error in makeState (line 58)
Score =
fcnvectorizer(state.Population(initScoreProvided+2:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 371)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars,
...
Error in tutorial_ga_simulink (line 9)
[x,fval,exitflag,output,population,score] = ga(@
rast_simulink,2,[],[],[],[],[],[],[],[],options);
Caused by:
Error using rast_simulink (line 9)
No block diagram 'rast' is loaded.
Failure in user-supplied fitness function evaluation. GA cannot continue.
end
(x1 is a block in my model).
I guess, that is because "set param" gets some problems, when multiple models are run in parallel. What can I do about that? Is the ga function from the global optimization toolbox made for running simulink models in parallel or do I have to write my own Genetic Algorithm in order to use something like parsim? What actually happens, when I switch on the "parallel" option (I guess, some for -loops turn into parfor, but how does it deal wih sim? ) I am also happy about other suggestions on how to make my optimisation run faster (does the rapid acellerator mode in Simulink make sense for this application, what else might slow it down?
Thanks a lot in advance! Magdalena

Best Answer

This is happening because each worker needs to have the model loaded in memory, as well as all its dependencies added to path (as needed in regular operation).
To do this, you want to use the parfevalOnAll and addAttachedFiles functions.
For example, you can do:
myPool = parpool;
addAttachedFiles(myPool,0,{'rast.slx','MyFolder');
parfevalOnAll(@load_system,0,'rast');
parfevalOnAll(@addpath,0,'MyFolder');
Hope this helps you get started.
- Sebastian