% Basic particle swarm optimization )-----------
% Name: basic particle swarm optimization algorithm (PSO)
% Role: Solving Optimization Problems
% Description: it is a two-dimensional Variable. A small number of modifications are made in the program to accurately iterate the results.
% Author: Zhou Nan
% Time: 2009-12-18-
Clear all;
CLC;
% X-axis length percentage
X_rate = 0.02;
% Ordinate length percentage
Y_rate = 0.1;
% Font display position X-axis length percentage
Xforwarshow = 0.5;
X2_show = 0.5;
% Font display position X-axis length percentage
Yuncshow = 0.2;
Y2_show = 0.9;
The range of % X1 is [a1 B1];
A1 =-3.0;
B1 = 12.1;
The range of % X2 is [A2 B2];
% X2 [A2, B2]
A2 = 4.1;
B2 = 5.8;
C1 = 2; % learning factor 1
C2 = 2; % learning factor 2
% Inertia weight
W_0 = 0.9;
W_end = 0.4;
Maxdt = 30; % maximum number of iterations
D = 2; % search space dimension (number of unknowns)
N = 50; % initialize the number of group individuals
EPS = 10 ^ (-16); % set the precision (used when the minimum value is known), that is, the calculation precision
Rate = 0.2; % if the step size exceeds the boundary, it is shortened to the original rate.
% Initialize the individual (location and speed) of the population)
% Rand is a 0-1 even distribution.
RR = rand (2, N );
R1 = RR (1 ,:);
R2 = RR (2 ,:);
% Two-Dimensional initial position variable generation
Xx = rand (n, 2 );
X1 = XX (:, 1 );
X2 = XX (:, 2 );
X1 = (b1-a1) * X1 + A1;
X2 = (b2-a2) * X2 + A2;
X_x = [x1, x2];
% Speed
V1 = zeros (N, N );
V2 = V1;
% Initialize the optimal solution of an iteration
Pbest = zeros (maxdt, N );
UU = fx1tox2 (x_x (1 ,:));
Pbest (1, 1) = UU (1, 1 );
% Initialize the location of the optimal solution for an iteration
X_pbest = zeros (maxdt, N, d );
X_pbest (1,1, :) = x_x (1 ,:);
% Gbest is the initialization of the global optimal solution
Gbest = pbest (1, 1 );
Draw_gbest = pbest (); % draw_gbest is the initialization of the global optimal solution.
% Gbest is the location of the global optimal solution for initialization
X_gbest = zeros (1, D );
X_gbest (1, :) = x_x (1 ,:);
Xx_x = zeros (1, D); % local variable, used to store temporary variables
% Enters the main cycle and iterates in sequence according to the formula until the accuracy requirements are met.
For t = 1: maxdt
For I = 1: N
For j = 1: N
W (I) = (w_0-w_end) * (maxdt-I)/(maxdt) + w_end;
V1 (I, j) = W (I) * V1 (I, j) + C1 * r1 (j) * (x_pbest (T, J)-x_x (I, 1) + C2 * R2 (j) * (x_gbest (1)-x_x (I, 1 ));
V2 (I, j) = W (I) * V2 (I, j) + C1 * r1 (j) * (x_pbest (T, J)-x_x (I, 2) + C2 * R2 (j) * (x_gbest (2)-x_x (J, 2 ));
X_x (I, 1) = x_x (I, 1) + V1 (I, j );
X_x (I, 2) = x_x (I, 2) + V2 (I, j );
% Processing of variable out of bounds due to too long step size
While (x_x (I, 1)> B1) | (x_x (I, 2)> B2) | (x_x (I, 1 )) <A1) | (x_x (I, 2) <A2 ))
% Method 1: If the step size exceeds the boundary, the original rate is shortened.
% X_x (I, 1) = x_x (I, 1)-V1 (I, j );
% X_x (I, 2) = x_x (I, 2)-V2 (I, j );
% V1 (I, j) = V1 (I, j) * rate;
% V2 (I, j) = v2 (I, j) * rate;
% X_x (I, 1) = x_x (I, 1) + V1 (I, j );
% X_x (I, 2) = x_x (I, 2) + V2 (I, j );
% Method 2: If the step size exceeds the boundary, take a value near the boundary.
If (x_x (I, 1)> B1)
X_x (I, 1) = x_x (I, 1)-rate;
End
If (x_x (I, 2)> B2)
X_x (I, 2) = x_x (I, 2)-rate;
End
If (x_x (I, 1) <A1 ))
X_x (I, 1) = x_x (I, 1) + rate;
End
If (x_x (I, 2) <A2 ))
X_x (I, 2) = x_x (I, 2) + rate;
End
End
% Save D values and save the I-th local optimal solution of the nth Iteration
Xx_x (:, 1) = x_x (I, 1 );
Xx_x (:, 2) = x_x (J, 2 );
If (fx1tox2 (xx_x)> pbest (t, I ))
Pbest (t, I) = fx1tox2 (xx_x );
X_pbest (t, I, :) = xx_x;
End
End
% Save the global optimal solution
If (pbest (t, I)> gbest)
Gbest = pbest (t, I );
X_gbest = x_pbest (t, I ,:);
End
End
End
% Draw_gbest is used to plot the global optimal solution.
Draw_gbest = max (pbest ');
Mindle = draw_gbest (1 );
For t = 1: maxdt
If (draw_gbest (t)> Mindle)
Mindle = draw_gbest (t );
Else
Draw_gbest (t) = Mindle;
End
End
% Finally, the calculation result is given.
Disp ('the global optimal position of the function is :')
Solution = x_gbest
Disp ('the final optimization extreme value is :')
Result = gbest
Figure
X = (1: maxdt )';
Plot (x, pbest, 'B :');
Title ('particle swarm optimization algorithmic ')
Xlabel ('iterations ')
Ylabel ')
AA = min (pbest ));
BB = max (gbest );
String_cell {1} = '/fontsize {10}/fontname {Arial} green solid line: the best solution encountered so far ';
Text (maxdt + maxdt * x_rate) * x1_show, (AA + AA * y_rate) + (BB + BB * y_rate)-(AA + AA * y_rate )) * y1_show), string_cell, 'color', 'M ')
String_cell {1} = '/fontsize {10}/fontname {Arial} red dotted line: The best individual in the current generation ';
Text (maxdt + maxdt * x_rate) * x1_show, (AA + AA * y_rate) + (BB + BB * y_rate)-(AA + AA * y_rate )) * y1_show * y1_show), string_cell, 'color', 'M ')
Cell_string {4} = ['/fontsize {15}/RM global optimal position = 'num2str (x_gbest, 6)];
Cell_string {5} = '/fontsize {15 }';
Cell_string {6} = ['/fontsize {15}/RM global optimization extreme value = 'num2str (gbest, 6)];
Cell_string {7} = '/fontsize {15 }';
Cell_string {8} = ['/fontsize {15}/RM iterations = 'num2str (maxdt, 6)];
Text (maxdt + maxdt * x_rate) * x2_show, (AA + AA * y_rate) + (BB + BB * y_rate)-(AA + AA * y_rate )) * y2_show), cell_string, 'color', 'B', 'horizontalalignment ', 'center ')
Hold on
Plot (x, draw_gbest, 'r -')
Axis ([0, fix (maxdt + maxdt * x_rate), fix (AA + AA * y_rate), fix (BB + BB * y_rate)])
Hold off
Grid on