Mathematical Modeling - Particle Swarm Algorithm Steps and Program Details


foreword

Optimization algorithms have always been a popular question type in mathematical modeling competitions. Many problems can be simplified into planning problems and answered using heuristic algorithms. Therefore, the priority and cost-effectiveness of learning optimization problems are still very high. Three heuristic algorithms can solve most problems. Heuristic algorithms include simulated annealing algorithm, genetic algorithm, particle swarm algorithm, ant colony algorithm, etc. Simulated annealing and genetic algorithm have been mentioned before, and here we will talk about particle swarm algorithm When it's over, the following heuristic algorithms will no longer appear.
Simulated Annealing:
Genetic Algorithms:


1. What is particle swarm?

In 1995, American scholars Kennedy and Eberhart jointly proposed the particle swarm algorithm, whose basic idea was inspired by the research results of modeling and simulating the behavior of bird groups. Its core idea is to use the information sharing of individuals in the group to make the movement of the whole group evolve from disorder to order in the problem solving space, so as to obtain a feasible solution to the problem.

An intuitive explanation is actually to simulate a flock of birds foraging, using the group behavior of birds foraging and personal habits to search for places closer to food.
Particle swarm algorithm has some assumptions.
Bird foraging is affected by group behavior and personal habits:
(1) Birds that are close to food will send messages to other birds of the same kind. My place is relatively close to food. Come here quickly. , then all birds will have a tendency to be attracted to it.
(2) Birds have their own experience, and they have searched for food before, so they have a tendency to take the next step based on experience.
(3) Birds have an inertia when flying, so when they take the next step, they will fly with inertia.
The above three are the key points of bird flight. Based on the above-mentioned foraging behavior of birds, a particle swarm optimization algorithm was developed.
Introduce basic concepts based on the above ideas:

symbol Particle swarm meaning actual problem meaning
x x x particle The solution to the optimization problem
f ( x ) f(x) f(x) adaptability Evaluate the quality of the solution, the objective function in programming problems
p d b e s t i d pdbest^d_i p d b e s tid The i-th best position in the d-th iteration The best position found so far for a single particle
g d b e s t d gdbest^d gdbestd The optimal position of the group in the d-th iteration The best position found so far by Particle Swarm
v i d v_i^d vid At the dth iteration, the velocity of the i-th particle distance to next solution
n n n Number of particles number of solutions
w w w Inertia weight of particles none
c 1 c_1 c1 Particle Individual Learning Factor none
c 2 c_2 c2 Particle's Social Learning Factor none
x x x Location The coordinates where the particle (solution) is located

Note: x can represent the particle and the position of the particle at the same time, the two are not contradictory

2. The core formula of particle swarm algorithm

1. speed

The speed of a bird (particle) at step d = self-cognition part + social cognition part + its own speed-inertia
v ( d ) = w ∗ vid - 1 + c 1 ∗ r 1 ∗ ( pbestid - xid ) + c 2 ∗ r 2 ∗ ( gbestd‐xid ) v(d) = w*v_i^{d‐1} + c_1*r_1*(pbest^d_i‐x^d_i) + c_2*r_2*(gbest^d‐ x^d_i)v(d)=wvid1+c1r1( p b e s tidxid)+c2r2(gbestdxid)

where r 1 r_1r1, r 2 r_2 r2It is a random number on [0,1]. It can be known from the formula expression that the speed of a bird can be composed of the vector sum of three parts, which is in line with the habits and cognition of birds.

2. Location

The position of the bird at the d+1 step = the position of the d-th step + the speed of the d-th step * the time of movement
xid + 1 + xid + vid x_i^{d+1}+x_i^d+v_i^dxid+1+xid+vid

3. Setting of core parameters

The setting of core parameters generally refers to the parameter settings of other papers. Generally, everyone has a default parameter setting, which is the result of continuous experiments.

inertia weight

The inertia weight is generally set at 0.9, and in other cases it is set at 0.9-1.2. There are no specific restrictions. Some scholars also propose to set the inertia weight as an adaptive inertia weight or a random inertia weight.

learning factor

The learning factor is divided into individual learning factor and social learning factor, and it is generally more appropriate to take 2. There is also room for improvement in the learning factor, but generally the improvement will not be too complicated. Novices only need to learn this, and follow-up improvements can be understood by themselves.

Automatically exit the iterative loop

After the birds have found food, continuing to search for food will only waste calculation time, so we need to set a specific condition so that the calculation can automatically exit the iteration after the condition is met.
(1) Exit the loop when the maximum number of iterations is reached
(2) Exit the loop when the optimal value reached by the flock remains unchanged for a long time.
Then there are two ways to exit the loop. The optimal value remains unchanged for a long time. The programming idea can be As follows
1. Initialize the counter, the optimal value iteration tolerance value.
2. Define the tolerance of fitness.
3. Compare the change value of fitness with the tolerance of fitness. If the former is smaller, add 1 to the counter; otherwise, clear the counter to 0.
4. Judging whether the counter has exceeded the "optimum value iteration tolerance value", if the value of the counter exceeds the optimal value iteration tolerance value, then we will jump
out of the iteration loop, the loop ends, and the birds stop foraging.

3. Detailed code explanation

The Matlab code is as follows (example):
For the title, we use
y = 11 sin x + 7 cos 5 xy=11sin\ x+7cos\ 5xy=11sin x+7 c o s 5 x 
is used as an example, and the interval is defined in [-10,10]

clc,clear
%% 粒子群算法中的预设参数(参数的设置不是固定的,可以适当修改)
D = 500;  % 迭代的次数
n = 10;     % 粒子数量
factor = 1;      % 变量个数
c1 = 2;    % 个体学习因子
c2 = 2;    % 社会学习因子
w = 0.9;   % 惯性权重
D = 50;   % 迭代的次数
vmax = 1.2; % 粒子的最大速度
x_lb = -10;    % x的下界
x_ub = 10;    % x的上界
x = zeros(n,factor);
for i = 1: factor    % 将粒子群随机在定义域内
    x(:,i) = x_lb(i) + (x_ub(i)-x_lb(i))*rand(n,1);
end
v = -vmax + 2*vmax .* rand(n,factor);  % 随机初始化粒子的速度(这里我们设置为[-vmax,vmax])
f = zeros(n,1);
for i = 1:n  % 循环整个粒子群,计算每一个粒子的适应度
    f(i) = fun1(x(i,:));   % fun1为计算适应度的函数,
end
pbest = x;   % 单个粒子迄今为止找到的最佳位置
gbest = pbest(find(f == min(f)));  % 定义所有粒子迄今为止找到的最佳位置
fmean = zeros(1,D); %粒子群迭代平均适应度
fbest = zeros(1,D); %粒子群迭代的最优适应度
js = 0;
r = 20;
g = fun1(gbest); %当前最优值
%% 开始迭代()
for i = 1:D
    x1 = x + v; %更新位置
    v1 = w.*v + c1*rand()*(pbest-x) + c1*rand()*(gbest-x);  %更新速度
    for j=1:n  %判断位置有没有超过取值区间,超过了取值区间就修改为区间最值
        if x1(j,1) < x_lb
            x(j,1) = x_lb;
        elseif x1(j,1) > x_ub
            x(j,1) = x_ub;
        else
            x(j,1) = x1(j,1);
        end
        %判断速度有没有超过最大值,超过了就修改为最大值
        if v1(j,1) > vmax
            v(j,1) = vmax;
        else
            v(j,1) = v1(j,1);
        end
        f(j,1) = fun1(x(j,1));
        if f(j,1) < fun1(pbest(j,1))
            pbest(j,1) = x(j,1);
            if f(j,1) < fun1(gbest)
                gbest = x(j,1);
            end
        end
    end
    f = fun1(x);
    fmean(1,i) = mean(fun1(x)); %记录每次迭代平均适应度
    fbest(1,i) = fun1(gbest); %记录每次迭代的最优适应度
    %自动退出迭代循环
    if fun1(gbest) - g < 0.0000001 
        js = js + 1;
    end
    g = fun1(gbest); %更新最优值的退出值
    if js > r
        break
    end
end
xd=1:i;
plot(xd,fmean(1,1:i))
hold on
plot(xd,fbest(1,1:i))
xlabel('迭代次数')
legend('粒子群平均适应度','粒子群最优适应度')
disp('最佳的位置是:');
disp(gbest)
disp('此时最优值是:');
disp(-fun1(gbest)) %加负号是因为有设定适应度的时候就是设定为越小越好

function y = fun1(x)
    y = 11*sin(x) + 7*cos(5*x);
    %    y = -(11*sin(x) + 7*cos(5*x));  % 如果调用fmincon函数,则需要添加负号改为求最小值
end

Summarize

The above is what I want to talk about today. This article only briefly introduces the use of particle swarms, and there are more methods for particle swarms, including finding the maximum value of multivariate functions. Each optimization algorithm can be applied. The difference It lies in the generation of solutions, which is also the difference between most of the optimization algorithms (the new solutions generated by some algorithms can be shared, and you will encounter them when you do too many questions in the future), the same thing about the optimization algorithms is that they are all based on inheritance The previous information, through multiple iterations to find the optimal value, is not much different in essence, just learn two or three.

Guess you like

Origin blog.csdn.net/weixin_52952969/article/details/125647157