% Improved Grey Wolf Optimizer
function [Alpha_scores_FGWO,Alpha_pos_FGWO,Convergence_curve1]=FGWO(SearchAgents_no,Max_iter,lb,ub,dim,objfun)
global kkk
%display('FGWO is optimizing your problem');
% SearchAgents_no=30;
% Max_iter=500;
% dim=10;
% objfun=@(x)sum(x.^2);
% ub=1; %/*lower bounds of the parameters. */
% lb=-1;%/*upper bound of the parameters.*/
%Alpha_scores_FGWO = zeros(1,runtime);
%for r=1:runtime
% initialize alpha, beta, and delta_pos
Alpha_pos_FGWO=zeros(1,dim);
Alpha_score=inf; %change this to -inf for maximization problems
Beta_pos=zeros(1,dim);
Beta_score=inf; %change this to -inf for maximization problems
Delta_pos=zeros(1,dim);
Delta_score=inf; %change this to -inf for maximization problems
%% Initialize the positions of search agents
% ub=ones(1,dim)*1; %/*lower bounds of the parameters. */
% lb=ones(1,dim)*(-1);%/*upper bound of the parameters.*/
% Range = repmat((ub-lb),[SearchAgents_no 1]);
% Lower = repmat(lb, [SearchAgents_no 1]);
% % 在搜索空间初始化食物源在-1和1之间
% Positions = rand(SearchAgents_no,dim) .* Range + Lower;
%% cat映射
% %灰狼位置混沌初始化种群
% x=double(zeros(1,dim)); % 定义初始序列
% X0=[0.1,0.3284]; % 初值
% y=x;
% z1=rand(1,dim);
% z2=rand(1,dim);
% % 赋初值
% z1(1)=X0(1);
% z2(1)=X0(2);
%
% for i=1:SearchAgents_no
% for j=1:dim
% x(i,j)=mod(z1(j)+z2(j),1); % 取余
% y(i,j)=mod(z1(j)+2*z2(j),1); % 取余
% z1(j)= x(i,j);
% z2(j)= y(i,j);
% end
% end
%
% X=y; % 利用y序列
%
% for i=1:SearchAgents_no
% for j=1:dim
% Positions_1(i,j) = lb+X(i,j)*(ub-lb);
% %Positions(i,j) = lb+X(i,j)*(ub-lb);
% end
% end
%% Logistic映射
% X1=double(zeros(1,dim)); % 定义初始序列
% z0 = rand(1,dim);
% u=4; % 混沌系统初始条件
% for i=1:SearchAgents_no
% for j=1:dim
% X1(i,j) = u*z0(j)*(1-z0(j));
% z0(j) = X1(i,j);
% end
% end
%
% for i=1:SearchAgents_no
% for j=1:dim
% Positions_1(i,j) = lb+X1(i,j)*(ub-lb);
% end
% end
%% Fuch映射
X3=double(zeros(1,dim));
z2=rand(1,dim);
% 赋初值
z2(1)=0.4835;
for i=1:SearchAgents_no
for j=1:dim
X3(i,j) = cos(1/(z2(j)^2)); %产生混沌序列1
z2(j) = X3(i,j);
end
end
for i=1:SearchAgents_no
for j=1:dim
Positions_1(i,j) = lb+X3(i,j)*(ub-lb);
end
end
%% Tent映射
%X2=double(zeros(1,dim));
%z1=rand(1,dim);
% 赋初值
%z1(1)=0.4835;
%for i=1:SearchAgents_no
%for j=1:dim
%if z1(j)<=0.51 && z1(j)>=0
% X2(i,j)=1/0.51*z1(j);
% z1(j)= X2(i,j);
% elseif z1(j)<=1 && z1(j)>0.51
% X2(i,j)=1/0.51*(1-z1(j));
% z1(j)= X2(i,j);
% end
% end
%end
%for i=1:SearchAgents_no
% for j=1:dim
% Positions_1(i,j) = lb(j)+X2(i,j)*(ub(j)-lb(j));%求初始解x_i
% Positions_1(i,j) = lb+X2(i,j)*(ub-lb);
% end
%end
%%
% 反向学习策略
min_number = min(Positions_1(i,:));
max_number = max(Positions_1(i,:));
for i=1:size(Positions_1,1)
for j=1:size(Positions_1,2)
Positions_2(i,j) = rand()*(min_number+max_number) - Positions_1(i,j);%每个初始解x_i对应的反向解OP_i
% Positions_2(i,j) = lb+ub - Positions_1(i,j);
end
end
Positions = [Positions_1;Positions_2];%两种解合并组成新的种群
for z =1: size(Positions,1)
fitness1(z,: )=feval(objfun,Positions(z,:));%求适应度函数
end
[m,index] = sort(fitness1);%适应度按升序排列
Positions = Positions(index(1:SearchAgents_no),:);%取前no最优的初始解得到新的灰狼初始种群
%% 同随机初始化位置对比
% 在搜索空间初始化食物源在-1和1之间
% ub1=ones(1,dim)*1; %/*lower bounds of the parameters. */
% lb1=ones(1,dim)*(-1);%/*upper bound of the parameters.*/
% Range1 = repmat((ub1-lb1),[SearchAgents_no 1]);
% Lower1 = repmat(lb1, [SearchAgents_no 1]);
% Positions_old = rand(SearchAgents_no,dim) .* Range1 + Lower1;
% figure
% h1 = plot(1:SearchAgents_no,x,'+','MarkerSize',4);
% xlabel('迭代次数','FontSize',10,'FontName','Times New Roman')
% ylabel('Cat混沌序列值','FontSize',10,'FontName','Times New Roman')
% 行数为40,列数为30
%此处的初始种群可以加以改进,比如混沌初始化,反向学习策略产生初始种群,已经有佳点集理论应用到这上面了。
%% 引入PSO记忆算法部分
% 群体交流系数 b1 和个体记忆系数 b2,是[0,1]之间的常数
b1 = 0.5;
b2 = 0.5;
p = 0.2;% 优胜劣汰选择概率
% 加入个体记忆
pBestScore=zeros(SearchAgents_no);
pBest=zeros(SearchAgents_no,dim); % noP * dim
l=1;% Loop counter
for i=1:SearchAgents_no
pBestScore(i)=inf;
end %引入部分结束。
% Main loop
while l<=Max_iter
% 种群适应度和边界初始化
for i=1:size(Positions,1) % 其实是SearchAgents_no 种群个数 ,i 表示每一个灰狼个体
% Return back the search agents that go beyond the boundaries of the search space
Flag4ub=Positions(i,:)>ub;
Flag4lb=Positions(i,:)<lb;
Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
% Calculate objective function for each search agent
% 适应度函数
% fitness=fobj(Positions(i,:));
fitness = feval(objfun,Positions(i,:));
% 个体最优保存
if(pBestScore(i)>fitness)
pBestScore(i)=fitness;
pBest(i,:)=Positions(i,:);
end
% Update Alpha, Beta, and Delta
% 更新 Alpha, Beta, and Delta 位置
if fitness<Alpha_score
Alpha_score=fitness; % Update alpha
Alpha_pos_FGWO=Positions(i,:);
end
fitness_Alpha = feval(objfun,Alpha_pos_FGWO); %fitness_Alpha的值是为了进行后面计算权重系数
% 关键是这个优先顺序
if fitness>Alpha_score && fitness<Beta_score
Beta_score=fitness; % Update beta
Beta_pos=Positions(i,:);
end
fitness_Beta = feval(objfun,Beta_pos); %同fitness_Alpha
if fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score
Delta_score=fitness; % Update delta
Delta_pos=Positions(i,:);
end
fitness_Delta = feval(objfun,Delta_pos);%同fitness_Alpha
end
%% 利用Cat混沌局部搜索
min_X = min(Alpha_pos_FGWO);
max_X = max(Alpha_pos_FGWO);
for j=1:size(Alpha_pos_FGWO,2)
Z0(j) = (Alpha_pos_FGWO(j)-min_X)/(max_X-min_X);% 将最优位置映射到(0,1)之间
end
% 进行Tent映射迭代
M=double(zeros(1,dim)); % 定义初始序列
Z1=[0.1,0.4835]; % 初值
N=M;
z1=rand(1,dim);
z2=rand(1,dim);
% 赋初值
z1(1)=Z1(1);
z2(1)=Z1(2);
% 设置混沌迭代次数C_max
C_max=50;
for t=1:C_max
for j=1:size(Alpha_pos_FGWO,2)
M(t,j)=mod(z1(j)+z2(j),1); % 取余
N(t,j)=mod(z1(j)+2*z2(j),1); % 取余
z1(j)= M(t,j);
z2(j)= N(t,j);
end
end
V=N(t,:); % 得到第t次迭代混度搜索的序列(y序列)
% 下面开始进行载波到原解空间,产生最优解的邻域解
for j=1:size(Alpha_pos_FGWO,2)
%Alpha_pos_new(j) = Alpha_pos(j)+ (max_X-min_X)*(2*V(j)-1)/2;% 将最优位置映射到(0,1)之间
Alpha_pos_new(j) = Alpha_pos_FGWO(j)+ 3*(max_X-min_X)*(2*V(j)-1)/10;% 将最优位置映射到(0,1)之间
end
% 与原来的解相比较,保留最优解
if feval(objfun,Alpha_pos_new)>feval(objfun,Alpha_pos_FGWO)
Alpha_pos_FGWO=Alpha_pos_FGWO;
else
Alpha_pos_FGWO=Alpha_pos_new;
- 1
- 2
- 3
- 4
- 5
- 6
前往页