Matlab implementation of particle swarm algorithm (including source code)

thought

For individual birds, the first choice for the fastest access to food is usually: look at where the other birds are. They believe that the clustering effect exists, and they will go where most of the birds go first. If not, they will go. Look elsewhere

The core idea
uses the sharing of information by individuals in the group so that the movement of the entire group produces an evolutionary process from disorder to order in the problem solving space, so as to obtain the optimal solution of the problem.

The particle swarm optimization algorithm is derived from the predation behavior of birds and fish schools, and then abstracted into an algorithm. Then, I will introduce a particle how birds prey to explain the particle swarm optimization algorithm.

For example, it is
known that there is a group of birds in an area, and there is some food in one or more places in this area. Birds are birds. They don't know the specific location of the food or where the food is. Most, but when they encounter food, they know that it is food -_-, and they can distinguish the amount of food. All living things in nature are constantly looking for food, which is why they can survive.

Solve-find the most food place

Solution: This process: Know the conditions, solve it.

So, how do birds find food? How to find the place with the most food in this area?

1. Share information between bird flocks and fly towards the place where the group knows the most food
2. Based on your own experience, fly towards the place where you know the most food

Through these two behaviors, the flocks of birds share information with each other, and as they continue to search, they can find the most food location.

Through the above analysis, birds have two behaviors: 1. Social behavior; 2. Individual behavior;
Social behavior: Birds will learn from the best birds in the flock, that is, move toward the place where the most food is currently known.
Individual behavior: Move to the place where you know the most food according to your search.
Then, based on the bird swarm's method of finding food, each bird is abstracted into a particle, and each particle has the above two behaviors, resulting in particle swarm optimization (PSO).

official

Solve the next speed: the
individual is in all the travel paths-the difference between the optimal value and the current position (the relationship between the individual extreme value and the current position), and the
group is in all the travel paths-the difference between the optimal value and the current position Value (relationship between group extreme value and current position)

Solve the next position:
two adjacent intervals, the default time is 1, so the speed and position can be added directly
Insert picture description here

program

fun.m

function y = fun(x)
% 函数用于计算粒子适应度值,也就是公式
%x           input           输入粒子 
%y           output          粒子适应度值 
y = sin(10 * pi * x) / x;

main.m

 %% I. 清空环境
clc
clear all

%% II. 绘制目标函数曲线图
x = 1:0.01:2;  %1-2 
y = sin(10*pi*x) ./ x;%实际要优化的函数  矩阵的对应相除——点除  sin(10*π*x)
figure%单独执行可以出现表格,和前几行一起执行也是表格,必须和第四行一起执行才可以出现表格
plot(x, y)
hold on%添加新绘图时,保存当前figure

%% III.初始化参数
c1 = 1.49445;
c2 = 1.49445;

maxgen = 50;   % 进化次数  
sizepop = 10;   %种群规模

Vmax = 0.5;
Vmin = -0.5;
popmax = 2; %个体变化范围,与pop范围对应相同
popmin = 1;

%加入权重(1)
ws = 0.9;%start
we = 0.4;%end

%% IV. 产生初始粒子(种群) 速度 适应度函数
for i = 1:sizepop%10
    % 随机产生一个种群population=10,同时定义pop()
    %pop(i,:)——10*1double,不是1*10
    %因为右侧没有i,只是左侧的pop(i,:)
    %初始种群 rands(1)函数是随机产生[-1,1],经过运算后,变化范围是{
    
    12},[1,2]中随机10%pop(i,:) = rand + 1;%pop(i,:) = rand(1) + 1;两者都可以
    pop(i,:) = (rands(1) + 1) / 2 + 1;   
    V(i,:) = 0.5 * rands(1);  %初始化速度,[-0.5,0.5]
    % 适应度 把初始化的,10*1的种群,代入到函数中,得到1*10的适应度函数
    %fitness函数只能显示1*n的数据,这是设置的
    fitness(i) = fun(pop(i,:));   %针对每一个个体去调用适应度函数
end
%% V. 个体极值和群体极值 个体最佳和群体最佳
%选择最大或者最小都可以,但是一定要统一,毕竟是随机
%[最大值 对应的位置]
[bestfitness bestindex] = max(fitness);%1-10中最大的适应度函数值就是,fitness不加括号
%g_best = max(pop);输出的群体最佳和群体极值是一样(结果虽然是一样的,是偶然,),    0.9528    1.0490

%这三个里面,只有,p_best与fitness函数无关
%位置——在群体中,适应度函数值最大的对应的位置
g_best = pop(bestindex,:);      %全局最佳,并不是十个数里面最大的gbest = max(pop);,而是群体极值中最大的,对应的位置
p_best = pop;                   %个体最佳,十个个体,此时没有进入循环,在循环中,个体最佳是所有循环中个体最佳的

%值
p_best_fitness = fitness;       %个体最佳适应度值——个体极值
g_best_fitness = bestfitness;   %全局最佳适应度值——群体极值

%% VI. 迭代寻优
for i = 1:maxgen%50
        %加入权重(2)
    w = ws - (ws - we)*(maxgen - i)/maxgen;
    for j = 1:sizepop%10
        % 速度更新           个体最佳*10 每一次都选取最佳               全局最佳(每次循环中的最佳)
        V(j,:) = w*V(j,:) + c1*rand*(p_best(j,:) - pop(j,:)) + c2*rand*(g_best - pop(j,:));
        
        V(j,find(V(j,:)>Vmax)) = Vmax;%边界约束
        V(j,find(V(j,:)<Vmin)) = Vmin;
        
        % 种群更新
        pop(j,:) = pop(j,:) + V(j,:);
        pop(j,find(pop(j,:)>popmax)) = popmax;
        pop(j,find(pop(j,:)<popmin)) = popmin;
        
        % 适应度值更新
        fitness(j) = fun(pop(j,:)); 
    end
    
    for j = 1:sizepop    
        % 个体最优更新
        if fitness(j) > p_best_fitness(j)%当前的适应度函数值大于个体极值,就把当前个体,以及适应度函数值赋给best
            p_best(j,:) = pop(j,:);
            p_best_fitness(j) = fitness(j);
        end
        
        % 群体最优更新
        if fitness(j) > g_best_fitness%当前的适应度函数值大于群体极值,就把当前个体,以及适应度函数值赋给best
            g_best = pop(j,:);
            g_best_fitness = fitness(j);
        end
    end 
    yy(i) = g_best_fitness;          
end

%% VII. 输出结果并绘图
[g_best_fitness g_best]%寻找到的极值点——[值,位置]
plot(g_best, g_best_fitness,'r*')%极值点对应的变量

figure
plot(yy)
title('最优个体适应度','fontsize',12);
xlabel('进化代数','fontsize',12);ylabel('适应度','fontsize',12);


result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43641765/article/details/111414915