algorithm - 谁能解释这种混合 PSOGA 与普通 GA 有何不同?
问题描述
这段代码是否有变异、选择和交叉,就像原始的遗传算法一样。
既然这样,混合算法(即 PSO 与 GA)是使用原始 GA 的所有步骤还是跳过一些
他们。请告诉我。我对此很陌生,但仍在努力理解。谢谢你。
%%% 混合 GA 和 PSO 代码
function [gbest, gBestScore, all_scores] = QAP_PSO_GA(CreatePopFcn, FitnessFcn, UpdatePosition, ...
nCity, nPlant, nPopSize, nIters)
% Set algorithm parameters
constant = 0.95;
c1 = 1.5; %1.4944; %2;
c2 = 1.5; %1.4944; %2;
w = 0.792 * constant;
% Allocate memory and initialize
gBestScore = inf;
all_scores = inf * ones(nPopSize, nIters);
x = CreatePopFcn(nPopSize, nCity);
v = zeros(nPopSize, nCity);
pbest = x;
% update lbest
cost_p = inf * ones(1, nPopSize); %feval(FUN, pbest');
for i=1:nPopSize
cost_p(i) = FitnessFcn(pbest(i, 1:nPlant));
end
lbest = update_lbest(cost_p, pbest, nPopSize);
for iter = 1 : nIters
if mod(iter,1000) == 0
parents = randperm(nPopSize);
for i = 1:nPopSize
x(i,:) = (pbest(i,:) + pbest(parents(i),:))/2;
% v(i,:) = pbest(parents(i),:) - x(i,:);
% v(i,:) = (v(i,:) + v(parents(i),:))/2;
end
else
% Update velocity
v = w*v + c1*rand(nPopSize,nCity).*(pbest-x) + c2*rand(nPopSize,nCity).*(lbest-x);
% Update position
x = x + v;
x = UpdatePosition(x);
end
% Update pbest
cost_x = inf * ones(1, nPopSize);
for i=1:nPopSize
cost_x(i) = FitnessFcn(x(i, 1:nPlant));
end
s = cost_x<cost_p;
cost_p = (1-s).*cost_p + s.*cost_x;
s = repmat(s',1,nCity);
pbest = (1-s).*pbest + s.*x;
% update lbest
lbest = update_lbest(cost_p, pbest, nPopSize);
% update global best
all_scores(:, iter) = cost_x;
[cost,index] = min(cost_p);
if (cost < gBestScore)
gbest = pbest(index, :);
gBestScore = cost;
end
% draw current fitness
figure(1);
plot(iter,min(cost_x),'cp','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',8)
hold on
str=strcat('Best fitness: ', num2str(min(cost_x)));
disp(str);
end
end
% Function to update lbest
function lbest = update_lbest(cost_p, x, nPopSize)
sm(1, 1)= cost_p(1, nPopSize);
sm(1, 2:3)= cost_p(1, 1:2);
[cost, index] = min(sm);
if index==1
lbest(1, :) = x(nPopSize, :);
else
lbest(1, :) = x(index-1, :);
end
for i = 2:nPopSize-1
sm(1, 1:3)= cost_p(1, i-1:i+1);
[cost, index] = min(sm);
lbest(i, :) = x(i+index-2, :);
end
sm(1, 1:2)= cost_p(1, nPopSize-1:nPopSize);
sm(1, 3)= cost_p(1, 1);
[cost, index] = min(sm);
if index==3
lbest(nPopSize, :) = x(1, :);
else
lbest(nPopSize, :) = x(nPopSize-2+index, :);
end
end
解决方案
如果您是优化新手,我建议您先分别研究每种算法,然后您可以研究 GA 和 PSO 如何组合,尽管您必须具备基本的数学技能才能理解两种算法的运算符并进行测试这些算法的效率(这才是真正重要的)。
此代码块负责父选择和交叉:
parents = randperm(nPopSize);
for i = 1:nPopSize
x(i,:) = (pbest(i,:) + pbest(parents(i),:))/2;
% v(i,:) = pbest(parents(i),:) - x(i,:);
% v(i,:) = (v(i,:) + v(parents(i),:))/2;
end
如何进行选择并不是很明显randperm
(我对 Matlab 没有经验)。
这是负责更新每个粒子的速度和位置的代码:
% Update velocity
v = w*v + c1*rand(nPopSize,nCity).*(pbest-x) + c2*rand(nPopSize,nCity).*(lbest-x);
% Update position
x = x + v;
x = UpdatePosition(x);
这个版本的速度更新策略使用了所谓的 Interia-Weight W
,这基本上意味着我们保留了每个粒子的速度历史(而不是完全重新计算它)。
值得一提的是,速度更新比交叉(每 1000 次迭代)更频繁。
推荐阅读
- java - 具有两种参数类型的 Scala 向左折叠
- angular - Cascaded Angular Material Design 示例(带有反应形式)
- javascript - 根据 ReactJs 中的计时器立即更改反应表中的数据
- ios - 为扩展视图添加委托和数据源
- c++ - 可能由于范围限制而无法构建的简单数组程序的编译器问题
- c - #define 值的最大值/最小值
- c# - 使用 C# 中的本机结构而不重新定义它
- python - Pyinstaller 无法使用 PyQt5 正确构建
- javascript - jQuery Siblings Selector 在按钮上不起作用
- c++ - 打印 Vec2 x 值