Parallelisation for ga with Simulink Model
조회 수: 12 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Sebastian Castro
2017년 6월 19일
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
RANJORO Victor Nantenaina
2020년 9월 8일
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'.
추가 답변 (1개)
sahar jam
2020년 12월 19일
Hi dear magdalena
I have the same problem as you.
Thanks for sending me an answer you found.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!