Running genetic algorithm and Simulink in parallel

9 views (last 30 days)
I am trying to control my system using the mpc controller, and I want to optimize its weights with a genetic algorithm. Part of my code is in Matlab, and the other part is in Simulink, and I want to run ga in parallel. So far, My code looks like the one below. It works without using the ga 'UseParallel' option, which is really slow. When I try to use the 'UseParallel' option, it gives me errors I don't know how to resolve. I would appreciate it if you could help me to resolve the issue.
code:
clear all
clc
x_eq = [0;0;0;0;0;0;0;0;0;deg2rad(3);deg2rad(17);deg2rad(15)];
u_eq = [0;0;0];
[A,B] = FlexibleSatStateJacobianFcn(x_eq,u_eq);
C = eye(12,12);
D = zeros(12,3);
plant = ss(A,B,C,D);
plant = setmpcsignals(plant,'MV',3);
Ts = 0.1; % Sampling time
p = 20; % Prediction horizon
m = 20; % Control horizon
mpcobj = mpc(plant,Ts,p,m);
mpcobj.Weights = struct('MVRate',1*[1 1 1], ...
'Output',[1*[1,1,1],1*[1,1,1],1*[1,1,1],1*[1,1,1]]); % Weights
x0 = [0;0;0;1;1;1;0;0;0;0;0;0];
no_var = 15;
lb = zeros(1,15);
ub = [10000*ones(1,12),10*ones(1,3)];
ga_opt = optimoptions('ga','Display','off','PlotFcn','gaplotdistance','PopulationSize',50,'MaxGenerations',50, ...
'UseParallel',true);
obj_fcn = @(gains) optimization_mpc_gains(gains);
[gains,best] = ga (obj_fcn,no_var,[],[],[],[],lb,ub,[],ga_opt)
function cost = optimization_mpc_gains(gains)
mdl = 'MPCSatellite';
PopulationSize = size(gains,1);
% create the SimluationInput object
for i1 = 1:PopulationSize
SimIn(i1) = Simulink.SimulationInput(mdl);
SimIn(i1) = SimIn(i1).setVariable('mpcobj.Weights.OutputVariables', gains(i1,1:12));
SimIn(i1) = SimIn(i1).setVariable('mpcobj.Weights.ManipulatedVariablesRate', gains(i1,13:end));
% do it for all of the variables
end
% run the simulations and get the SimulationOutput object
SimOut = parsim(SimIn);
% 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(i2).ITAE(end);
end
end
error:
Starting parallel pool (parpool) using the 'Processes' profile ...
Preserving jobs with IDs: 1 2 because they contain crash dump files.
You can use 'delete(myCluster.Jobs)' to remove all jobs created with profile Processes. To create 'myCluster' use 'myCluster = parcluster('Processes')'.
Connected to the parallel pool (number of workers: 8).
[05-May-2023 12:08:49] Checking for availability of parallel pool...
[05-May-2023 12:08:49] Starting Simulink on parallel workers...
[05-May-2023 12:09:10] Configuring simulation cache folder on parallel workers...
[05-May-2023 12:09:11] Loading model on parallel workers...
[05-May-2023 12:09:39] Running simulations...
[05-May-2023 12:10:03] Completed 1 of 1 simulation runs
[05-May-2023 12:10:03] Cleaning up parallel workers...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Checking for availability of parallel pool...
[05-May-2023 12:10:11] Running simulations...
-->Converting model to discrete time.
-->Assuming output disturbance added to measured output channel #4 is integrated white noise.
-->Assuming output disturbance added to measured output channel #7 is integrated white noise.
Assuming no disturbance added to measured output channel #10.
-->Assuming output disturbance added to measured output channel #8 is integrated white noise.
-->Assuming output disturbance added to measured output channel #1 is integrated white noise.
Assuming no disturbance added to measured output channel #11.
-->Assuming output disturbance added to measured output channel #9 is integrated white noise.
-->Assuming output disturbance added to measured output channel #6 is integrated white noise.
-->Assuming output disturbance added to measured output channel #2 is integrated white noise.
-->Assuming output disturbance added to measured output channel #5 is integrated white noise.
Assuming no disturbance added to measured output channel #12.
-->Assuming output disturbance added to measured output channel #3 is integrated white noise.
-->The "Model.Noise" property is empty. Assuming white noise on each measured output.
[05-May-2023 12:10:19] Completed 1 of 1 simulation runs
[05-May-2023 12:10:11] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
[05-May-2023 12:10:11] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
[05-May-2023 12:10:11] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
Warning: One or more simulations completed with errors. For more information, inspect the SimulationOutput objects at these indices:
[1]
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
[05-May-2023 12:10:12] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
[05-May-2023 12:10:11] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
Warning: One or more simulations completed with errors. For more information, inspect the SimulationOutput objects at these indices:
[1]
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
[05-May-2023 12:10:11] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
[05-May-2023 12:10:12] Running simulations...
[05-May-2023 12:10:24] Completed 1 of 1 simulation runs. Run 1 has errors.
Warning: One or more simulations completed with errors. For more information, inspect the SimulationOutput objects at these indices:
[1]
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
Warning: Unrecognized field name "ITAE".
> In Simulink/SimulationOutput/get
In indexing
In Simulink.internal.subsrefRecurser
In indexing
In optimization_mpc_gains (line 24)
In MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
In createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
In parallel_function>make_general_channel/channel_general (line 837)
In remoteParallelFunction (line 67)
Error using optimization_mpc_gains
Array indices must be positive integers or logical values.
Error in MPCSatGenetic>@(gains)optimization_mpc_gains(gains) (line 29)
obj_fcn = @(gains) optimization_mpc_gains(gains)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in fcnvectorizer (line 22)
parfor (i = 1:popSize)
Error in makeState (line 69)
Score = fcnvectorizer(state.Population(initScoreProvided+2:end,:),FitnessFcn,1,...
Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 416)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in MPCSatGenetic (line 31)
[gains,best] = ga (obj_fcn,no_var,[],[],[],[],lb,ub,[],ga_opt)
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
By the way the ITAE variable is the ga is a 2d array that I use To workspace block to save it in the work space.

Answers (1)

Alan Weiss
Alan Weiss on 8 May 2023
I am not at all sure that it is possible to run ga in parallel when the objective function is given by a Simulink model.
For a similar problem in parallel using surrogateopt, see Vectorized Surrogate Optimization for Custom Parallel Simulation. Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Find more on Simulation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!