当前位置:问百问>百科知识>求粒子群算法MATLAB完整代码

求粒子群算法MATLAB完整代码

2025-01-27 22:14:16 编辑:zane 浏览量:527

求粒子群算法MATLAB完整代码

的有关信息介绍如下:

求粒子群算法MATLAB完整代码

%% 清空环境clearclctic%% 参数初始化% 粒子群算法中的两个参数c1 = 1.49445;c2 = 1.49445;maxgen = 200; % 进化次数sizepop = 20; % 种群规模Vmax = 1;Vmin = -1;popmax = 5;popmin = -5;%% 产生初始粒子和速度for i = 1:sizepop % 随机产生一个种群 pop(i,:) = 5 * rands(1,2); % 初始种群 V(i,:) = rands(1,2); % 初始化速度 % 计算适应度 fitness(i) = fun(pop(i,:)); % 染色体的适应度end% 找最好的染色体[bestfitness bestindex] = min(fitness);zbest = pop(bestindex,:); % 全局最佳gbest = pop; % 个体最佳fitnessgbest = fitness; % 个体最佳适应度值fitnesszbest = bestfitness; % 全局最佳适应度值%% 迭代寻优for i = 1:maxgen for j = 1:sizepop % 速度更新 V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:)); V(j,find(V(j,:)>Vmax)) = Vmax; V(j,find(V(j,:)popmax)) = popmax; pop(j,find(pop(j,:) 0.8 k = ceil(2*rand); pop(j,k) = rand; end % 适应度值 fitness(j) = fun(pop(j,:)); end % 个体最优更新 if fitness(j) < fitnessgbest(j) gbest(j,:) = pop(j,:); fitnessgbest(j) = fitness(j); end % 群体最优更新 if fitness(j) < fitnesszbest zbest = pop(j,:); fitnesszbest = fitness(j); end yy(i) = fitnesszbest; endtoc%% 结果分析plot(yy);title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);xlabel('进化代数');ylabel('适应度');fun函数如下function y = fun(x)y = -20*exp(-0.2*sqrt((x(1)^2+x(2)^2)/2)) - exp((cos(2*pi*x(1))+ cos(2*pi*x(2)))/2) + 20 + 2.71289;

版权声明:文章由 问百问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.wenbwen.com/article/242740.html
热门文章