PID control algorithm based on particle swarm algorithm to optimize BP neural network (Matlab code implementation)

1 Overview

     Traditional proportional-integral-derivative (Proportion Integral Derivative, PID) controllers have problems such as difficult parameter tuning, incapable of online real-time adjustment, and poor application effect in the face of complex nonlinear systems. , PSO) optimized back propagation (Back Propagation, BP) neural network PID control method. Combining the BP neural network with the PID controller, using the adaptive learning ability of the BP neural network to adjust the PID control parameters online in real time to improve the stability of the system, and aiming at the problem that the BP-PID self-learning process is easy to fall into the local minimum value, using the improved The PSO algorithm is optimized to ensure that the BP-PID system converges to the global optimal solution. Experiments are carried out based on simulation data, and the results show that the proposed method can effectively improve the control accuracy and control stability of the system. 

2 Running results

Part of the code:

function [wi_init, wo_init]=SPO_InitW()
%clear;
tic %This function indicates the start of timing
%Neural network parameters
IN=4; H=5; Out=3;
%------Given initialization conditions--- -------------------------------------------
c1=2; %1.4962; % acceleration constant
c2=2; %1.4962; % acceleration constant
%w=0.7298; % inertia weight
Wmax=0.9; Wmin=0.4; % ready to use linear weight decay method
MaxDT=50; % maximum number of iterations
D=(H*IN )+(Out*H); % Search space dimension (the number of unknowns in the test function sphere)
N=20; % Initialize the number of individuals in the group (generally 20 particles are enough)
Vmax=5;
Vmin=-5;
Pmax= 5;
Pmin=-5;

%------initialize the individual of the population (the range of position and speed can be limited here)------------
for i=1:N
        x(i,:)=0.15*rands (1,D); 
        v(i,:)=0.15*rands(1,D); 
end

%------First calculate the fitness of each particle, and initialize the individual optimal position y and the global optimal position Pg-------
for i=1:N
    p(i)=BPNN_Fitness(x (i,:)) ; % Calculate the fitness of each particle y(i,:)=x(i,:); % Initialize the individual optimal position y as the particle position end Pg=x
    at time step t=0 (1,:); %Pg is the global optimal position here is the initialization for i=2:N     if BPNN_Fitness(x(i,:))<BPNN_Fitness(Pg)         Pg=x(i,:); %Update the global optimal position The optimal position is initialized      end end






%------Enter the main loop and iterate according to the formula until the accuracy requirement is met ------------
for t=1:MaxDT
    fprintf('%d iteration---- -\n',t);
    %fprintf('Fitness=%f\n',Pbest(t));
    for i=1:N
        w=Wmax-(t-1)*(Wmax-Wmin)/( MaxDT-1);
        v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(Pg-x( i,:));
        v(i,find(v(i,:)>Vmax))=Vmax; % cannot exceed the maximum speed
        v(i,find(v(i,:)<Vmin))=Vmin; % Not lower than the minimum speed
        
        x(i,:)=x(i,:)+v(i,:); % updated the position of each particle
        x(i,find(x(i,:)>Pmax)) =Pmax;
        x(i,find(x(i,:)<Pmin))= Pmin;
        if BPNN_Fitness(x(i,:))<p(i)
            p(i)=BPNN_Fitness(x(i,:) ); % update fitness
            y(i,:)=x(i,:); % update individual best position
        end
        if p(i)<BPNN_Fitness(Pg)
            Pg=y(i,:); %Update the group best position after each iteration
        end
    end
    Pbest(t)=BPNN_Fitness(Pg); %Save the best group position of each generation Adaptation value
end
toc % This function indicates the end of timing

% Obtain the initial value of the neural network weights optimized by the particle swarm optimization algorithm
for t=1:H
       wi_init(t,:)=x(1,(t-1)*IN+1:t*IN);
end
for r= 1:Out
       wo_init(r,:)=x(1, ( (IN*H+1)+(r-1)*H ): ( (IN*H+1)+r*H-1 ) );
end

%------Finally give the calculation result --------
disp('************************** **********************************')    
disp('The optimal position of the optimal fitness function is:')
for i=1:D
    fprintf('x(%d)=%s\n',i,Pg(i));
end
fprintf('The final optimized extremum value is: %s\n',BPNN_Fitness(Pg ));
disp('****************************************** *****************')
disp('Iterative Analysis Results')
fprintf('Number of iterations: %d\n',MaxDT);

figure(1);
plot(Pbest,'Linewidth',2);
title( ['Fitness curve' ]);
grid on
xlabel('Number of iterations');ylabel('Fitness');

Complete code: Reply keywords in comment area

3 References

[1] Zeng Xiongfei. PID control algorithm based on particle swarm optimization of BP neural network [J]. Electronic Design Engineering, 2022, 30(11): 69-73+78. DOI: 10.14022/j.issn1674-6236.2022.11.015.

4 Matlab code implementation

Guess you like

Origin blog.csdn.net/weixin_61181717/article/details/127968043#comments_27834878