Get parallel simulations in objective function of optimization Tool (GA,PSO)

조회 수: 19(최근 30일)
Hey Guys
i have an optimization problem using the constrainted PSO:
The syntax and general working of the constrainted PSO is the same as GA.
In my objective function, i have to generate a simOut with data from a simulink simulation. One calculation needs about 7 hours of time, since the simulation needs about 3 seconds.To increase the algorithm speed, i want the simulations to run parallel. But it doesnt seem to work if i use "parpool('local')". So far ich changed the settings to "UseVectorized" so have the whole population in one objective function. My code looks like this:
options = psooptimset('Display','iter','OutputFcns',@myoutputFunc,'UseParallel',true,...
[xopt,fval,exitflag,output,population,scores] = pso(@objective,9,[],[],[],[],lb,ub,@constraint,options);
function obj = objective(x)
% WANT TO: check the number of workers , in my case 6
% WANT TO: set parallel simulations equal to work number
for p=1:length(x(:,1)) %WANT TO: set parallel working parfor loop to execute the parallel simulations
SimuGeometrics = calculateGeometrics(x(p,:)); %SimuGeometrics is a structure of lengths and angle of different body that are used for simulink
%start simulation
simOut = sim('Mechanism.slx','CaptureErrors','on','SrcWorkspace','current');
pos1 = [,,];
obj(p,1) = calculateDeviation(pos1);
disp(['x: ',num2str(x(p,:)) ,' obj: ',num2str(obj(p,1))]);
I know that there are some functions like parfor or parsim but i dont know how to include them into my Code.
Of cause, my WANTO TO's are only ideas how i think it can work, if for example setting a parfor loop is nonsense, you can suggest another solution
it seems like you described a possible solution for my problem. I will try out what you described today and will share my solution if i succeed, but it would help me alot if u could share your expertise on this.
I hope somebody can help me with this problem.

채택된 답변

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2021년 2월 12일
Hi Marcus.
I haven't used that File Exchange submission for PSO, but the general workflow in such situations is to:
  1. Pass the whole population to the objective function. This is done by setting the UseVectorized option to true.
  2. When the whole population is received by the objective function, it must create a SimulationInput object. You can learn about creating it here. Your SimulationInput object is an array, where each element represents a chromosome in the population.
  3. Then you pass the single SimulationInput object to parsim().
  4. Next, simulations are run and the SimulationOutput object is returned.
  5. Finally, you calculate the objective function values from the SimulationOutput object and return them to the solver.
The objective funciton's code might look something like this:
function f = ObjFun(x)
% get population size
PopulationSize = size(x, 1);
% create the SimluationInput object
for i1 = 1:PopulationSize
SimIn(i1) = Simulink.SimulationInput('MyModel');
SimIn(i1) = SimIn(i1).setVariable('MyVar1', x(i1,1))
SimIn(i1) = SimIn(i1).setVariable('MyVar2', x(i1,2))
% do it for all of the variables
% run the simulations and get the SimulationOutput object
SimOut = parsim(SimIn);
% initialize the arrays containing the objective function values
f = zeros (PopulationSize, 1);
% fill f with objective function values
for i1 = 1:PopulationSize
f(i1) = SimOut(i1).MyOutput;
Read about setVariable function here.
  댓글 수: 8
Irfan Khan
Irfan Khan 2022년 2월 26일
@Abdolkarim Mohammadi I used the objective function code and simulation is very fast now. But since i am new matlab user i made some mistakes. I fitness value graphs is abnormal. and the values in varriable solution2 are not giving stabilized output.
The background is i am tuning a PID controller in simulink using the genetic algorithim optimization tool. My cost function is "to workspace" (ITAE). In serial simulation the old objective function was working fine but in parallel simulation objective function, the output is not correct becuase i am not sure what to right in this line
cost(i2,1) = SimOut.ITAE(end);
Parallel Simulation objective function which is running but not tunning
function cost= tunning(k)
% get population size
PopulationSize = size(k, 1);
% create the SimluationInput object
for i1 = 1:PopulationSize
SimIn(i1) = Simulink.SimulationInput('test');
SimIn(i1) = SimIn(i1).setVariable('k(1)', k(i1,1));
SimIn(i1) = SimIn(i1).setVariable('k(2)', k(i1,2));
SimIn(i1) = SimIn(i1).setVariable('k(3)', k(i1,3));
% do it for all of the variables
% run the simulations and get the SimulationOutput object
SimOut = sim("test.slx",'CaptureErrors','on','SrcWorkspace','current');
%cost= ITAE;
% initialize the arrays containing the objective function values
cost= zeros (PopulationSize, 1);
% fill cost with objective function values
for i2 = 1:PopulationSize
cost(i2,1) = SimOut.ITAE(end);
Serial Objective Function which is slow but give right result
function cost = tunning_serial(k)
cost= ITAE(length(ITAE));
My Simulink Model using PID controller
Live Editor Optimization using Genetic Algorithim
% Set nondefault solver options
options2 = optimoptions("ga","UseVectorized",true,"CreationFcn",...
% Solve
[solution2,objectiveValue] = ga(@tunning,nVariables,[],[],[],[],...
Abnormal Fitnessvalue graphs
I guess this line Simout.ITAE(end) is the problem.
I have attached my objective function files

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by