PID controller optimization in simulink using parallel simulations in objective function

7 ビュー (過去 30 日間)
Irfan Khan
Irfan Khan 2022 年 2 月 26 日
コメント済み: Farshid R 2022 年 9 月 26 日
I am tunning a PID controller in simulink using optimization tool (Genetic Algorithim). It is working fine but iterative simulations are in serial and taking too much time. I saw an answer to run simulations in parallel by using Simulink.SimulationInput.
I tried this solution and my simulation is running fast but the optimization values are not correct ,also the Fitness value graph is abnormal. I guess the problem is in the new objetive function for parallel simulations " cost(i2,1)=Simout.ITAE(end)". I would appriciate if an expert like you could revise the objective fucntion as i am stucked on it for many days. I am attaching my objective function.
Please note that simulation output block name ITAE (to workspace) , higlighted in green, is the cost function
Matlab version: R2021b
Simulink Model and PID controller varriables
Live Editor to optimize the PID gains
% Set nondefault solver options
options2 = optimoptions("ga","UseVectorized",true,"CreationFcn",...
"gacreationuniform","SelectionFcn","selectiontournament","MutationFcn",...
"mutationadaptfeasible","Display","iter","PlotFcn","gaplotbestf");
% Solve
[solution2,objectiveValue] = ga(@tunning,nVariables,[],[],[],[],...
zeros(nVariables,1),repmat(5,nVariables,1),[],[],options2);
The serial objective function which is Working fine but slow is as follows:
function cost = tunning(k)
assignin('base','k',k);
sim('test.slx');
cost= ITAE(length(ITAE));
end
Parallel simulation Objective function providing Wrong output is as follows:

回答 (1 件)

Sam Chak
Sam Chak 2022 年 8 月 19 日
I'm unfamiliar with the Simulink approach, but in MATLAB, the script to find the PID gains subject to ITAE criterion looks like this.
Due to time limit to run the simulation, smaller population size, bounds and shorter simulation duration are used.
fitfun = @costfun; % call cost function
PopSize = 3; % Population size
MaxGen = 6; % Maximum generation
nvars = 3; % number of design variables: P,I,D
A = -eye(nvars); % linear constraints subject to A*k <= b
b = zeros(nvars, 1); % (to search in the positive direction)
Aeq = [];
beq = []; % linear constraints subject to Aeq*k = beq
lb = [30.1 51.3 13.7]; % lower bounds for lb < k < ub
ub = [30.3 51.5 13.9]; % upper bounds for lb < k < ub
nonlcon = []; % nonlinear constraints subject to C(k) <= 0 and Ceq(k) = 0
[k, fval, exitflag, output] = ga(fitfun, nvars, A, b, Aeq, beq, lb, ub, nonlcon)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
k = 1×3
30.1343 51.3026 13.7872
fval = 0.0353
exitflag = 1
output = struct with fields:
problemtype: 'linearconstraints' rngstate: [1×1 struct] generations: 51 funccount: 2450 message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.' maxconstraint: 0 hybridflag: []
% Objective function to be optimized
function J = costfun(k)
% Transfer functions
G1 = tf(1, [0.2 1]);
G2 = tf(1, [0.5 1]);
G3 = tf(1, [10 0.8]);
Gp = G1*G2*G3;
H = 20;
% Compensator
kp = k(1); % Proportional gain
ki = k(2); % Integral gain
kd = k(3); % Derivative gain
Tf = 1/165.277936355792; % 1st-order Derivative filter coefficient (1/N)
Gc = pid(kp, ki, kd, Tf);
% Closed-loop system (disturbance path)
Gd = minreal(feedback(G3, H*Gc*G1*G2));
% Cost function
dt = 0.01;
ty = 0:dt:8;
[y, t] = step(Gd, ty);
itae = t.*abs(0 - H*y(:));
J = trapz(t, itae);
end
  4 件のコメント
Sam Chak
Sam Chak 2022 年 9 月 26 日
Yes, you can. And you are encouraged to post a new question, if it is not closely or directly related the OP's question or problem.
Farshid R
Farshid R 2022 年 9 月 26 日
Thank you, I created a new question, I would be happy if you could help me. Here is the link:
https://www.mathworks.com/matlabcentral/answers/1812615-optimization-with-fmincon-command-in-simulink

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by