急急急!!!求解粒子群算法matlab程序 5
1个回答
展开全部
%------------------------------------------------------------------
%------Initial Format--------------------------------------------------
clear all;
clc;
format long;
%------Given initial conditions----------------------------------------------
c1=1.4962; %learning factor1
c2=1.4962; %learning factor2
w=0.9872; %inertia weight
MaxDT=200; %Maximum number of iterations
D=2; %Search space dimensions (number of variables )
N=40; %Initialize the number of groups of individuals
eps=10^(-6); %Setting accuracy
%------Initialized individuals of population (can limit the position and velocity)------------
for i=1:N
for j=1:D
x(i,j)=randn; %position initialization using random
v(i,j)=randn; %velocity initialization using random
end
end
%------First calculate the fitness of each particle, and initialize Pi and Pg----------------------
for i=1:N
p(i)=fitness(x(i,:),D);
y(i,:)=x(i,:);
end
pg=x(1,:); %Pg is global extreme
for i=2:N
if fitness(x(i,:),D)<fitness(pg,D)
pg=x(i,:);
end
end
%------the main loop,iterate according to the formula followed , until meet the accuracy------------
for t=1:MaxDT
for i=1:N
v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(pg-x(i,:));
x(i,:)=x(i,:)+v(i,:);
if fitness(x(i,:),D)<p(i)
p(i)=fitness(x(i,:),D);
y(i,:)=x(i,:);
end
if p(i)<fitness(pg,D)
pg=y(i,:);
end
end
Pbest(t)=fitness(pg,D);
end
%------, the final results
disp('*************************************************************')
disp('Global best position:')
Solution=pg'
disp('Extreme optimization result:')
Result=fitness(pg,D)
disp('*************************************************************')
%------Initial Format--------------------------------------------------
clear all;
clc;
format long;
%------Given initial conditions----------------------------------------------
c1=1.4962; %learning factor1
c2=1.4962; %learning factor2
w=0.9872; %inertia weight
MaxDT=200; %Maximum number of iterations
D=2; %Search space dimensions (number of variables )
N=40; %Initialize the number of groups of individuals
eps=10^(-6); %Setting accuracy
%------Initialized individuals of population (can limit the position and velocity)------------
for i=1:N
for j=1:D
x(i,j)=randn; %position initialization using random
v(i,j)=randn; %velocity initialization using random
end
end
%------First calculate the fitness of each particle, and initialize Pi and Pg----------------------
for i=1:N
p(i)=fitness(x(i,:),D);
y(i,:)=x(i,:);
end
pg=x(1,:); %Pg is global extreme
for i=2:N
if fitness(x(i,:),D)<fitness(pg,D)
pg=x(i,:);
end
end
%------the main loop,iterate according to the formula followed , until meet the accuracy------------
for t=1:MaxDT
for i=1:N
v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(pg-x(i,:));
x(i,:)=x(i,:)+v(i,:);
if fitness(x(i,:),D)<p(i)
p(i)=fitness(x(i,:),D);
y(i,:)=x(i,:);
end
if p(i)<fitness(pg,D)
pg=y(i,:);
end
end
Pbest(t)=fitness(pg,D);
end
%------, the final results
disp('*************************************************************')
disp('Global best position:')
Solution=pg'
disp('Extreme optimization result:')
Result=fitness(pg,D)
disp('*************************************************************')
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询