Parallelisation for ga with Simulink Model

18 views (last 30 days)
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

Accepted Answer

Sebastian Castro
Sebastian Castro on 19 Jun 2017
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
  2 Comments
magdalena
magdalena on 20 Jun 2017
Thanks a lot! (I thought, that happens automatically :S) Actually, just loading the model on the workers with
spmd
currDir = pwd;
addpath(currDir);
tmpDir = tempname;
mkdir(tmpDir);
cd(tmpDir);
load_system('rast');
end
before I run ga did the trick (so I guess, it needs the different tmp files as well,since that is the only difference I see between your lines of code and what I have now)
RANJORO Victor Nantenaina
as for me the variable in simuink can found in workspace, then that error appear:
Error using objectif (line 157)
The expression: -(p*u(1)*(Ls+Lch)*u(3))+(p*u(1)*Fi_eff)
in 'MSAP2/GENERATRICE /Fcn2'
has a syntax error
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 17)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 359)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in principal (line 7)
[params,Fxmin]=ga(ObjectiveFunction,6,[],[],[],[],pmin,pmax,[],[]);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> Ls
Ls =
NaN
>> Rs
Rs =
0
>> params
Undefined function or variable 'params'.

Sign in to comment.

More Answers (1)

sahar jam
sahar jam on 19 Dec 2020
Hi dear magdalena
I have the same problem as you.
Thanks for sending me an answer you found.

Community Treasure Hunt

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

Start Hunting!