今天为各位讲解Gaining‑sharing knowledge based algorithm(GSK)为什么讲解这个算法呢,因为自适应GSK算法(Adaptive Gaining-Sharing Knowledge Based Algorithm,AGSK)是CEC2020比赛的优胜算法。在讲解性能强悍的AGSK算法前,有必要先理解GSK的基本思想,而后再向深刻理解AGSK算法迈进。

目录

1.GSK算法基本思想

2.GSK算法数学描述

3.GSK算法基本步骤

4.GSK算法MATLAB代码

5.GSK算法实例验证


1.GSK算法基本思想

GSK算法的灵感来源于人在一生中获取和共享知识的过程,这个过程分为两个阶段:

1)初级获取和共享知识阶段,即人一生的前中期。在这一阶段,相比于通过大型网络(如工作、社交、朋友等)获取知识,人们更多地会通过小型网络(如家人、邻居、亲戚等)获取知识。虽然这一阶段的人们想法、观点尚未成熟,但是他们努力尝试分享自己的观点

2)高级获取和共享知识阶段,即人一生的中后期。这一阶段的人们通常会通过大型网络(如工作、社交、朋友等)获取知识,比如,这一阶段的人们通常喜欢成功学,相信成功者的观点,以使他们避免失败。这一阶段的人们思想十分成熟,他们会积极向他人分享自己的观点,期望帮助他人能从自己的分享中受益。


2.GSK算法数学描述

在了解GSK算法基本思想后,接下来对GSK算法进行数学描述:

假设第个体的适应度值为,其中为问题维数,为种群数目。下图可以清晰地展示两个阶段个体中初级部分高级部分的变化情况。

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码

从上述分析过程和上图中可以看出,初级要素数目和高级要素数目是变化的,则个体中初级要素数目的计算公式如下:


个体中高级要素数目的计算公式如下:


其中,表示初级要素数目,表示高级要素数目,表示问题维数,表示总迭代次数,表示当前迭代次数,表示知识学习率。


3.GSK算法基本步骤

如GSK基本思想所阐述的一样,GSK算法也分为初级和高级获取与共享知识两个阶段。

01 | 初级获取和共享知识阶段

假设求解函数最小值问题,在这一阶段,更新的方法如下:

(1)将种群中的个体按照适应度值从小到大的顺序进行排序,排序结果如下:

(2)的更新公式如下:


其中为更新后的个体,为随机选择的个体,为知识因素参数。 初级获取和共享知识阶段算法伪代码如下,其中为知识比率:

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_sed_02

02 | 高级获取和共享知识阶段

假设求解函数最小值问题,在这一阶段,更新的方法如下:

(1)将种群中的个体按照适应度值从小到大的顺序进行排序,然后将排序后的个体分成3类,即最佳个体、中等个体、最差个体,其中最佳个体占比,最差个体占比,中等个体占比,通常取。

(2)的更新公式如下:


其中为更新后的个体,为最佳个体中随机选择的个体,为最差个体中随机选择的个体,为中等个体中随机选择的个体,为知识因素参数。 高级获取和共享知识阶段算法伪代码如下,其中为知识比率:

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_最小值_03

03 | GSK算法流程图

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_sed_04

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_05


4.GSK算法MATLAB代码

GSK算法MATLAB代码链接为:https://www.mathworks.com/matlabcentral/fileexchange/73730-gaining-sharing-knowledge-based-algorithm,各位也可在公号后台回复【GSK】即可提取代码(不包括【】)。

求解CEC2017为例,GSK算法文件夹共包含如下文件:

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_06

主函数GSK.m代码如下所示:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Gaining-Sharing Knowledge Based Algorithm for Solving Optimization
%%Problems: A Novel Nature-Inspired Algorithm
%% Authors: Ali Wagdy Mohamed, Anas A. Hadi , Ali Khater Mohamed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;
clear all;

format long;
Alg_Name='GSK';
n_problems=30;
ConvDisp=1;
Run_No=51;

for problem_size = [10 30 50 100]

max_nfes = 10000 * problem_size;
rand('seed', sum(100 * clock));
val_2_reach = 10^(-8);
max_region = 100.0;
min_region = -100.0;
lu = [-100 * ones(1, problem_size); 100 * ones(1, problem_size)];
fhd=@cec17_func;
analysis= zeros(30,6);
for func = 1 : n_problems
optimum = func * 100.0;
%% Record the best results
outcome = [];
fprintf('\n-------------------------------------------------------\n')
fprintf('Function = %d, Dimension size = %d\n', func, problem_size)
dim1=[];
dim2=[];
for run_id = 1 : Run_No
bsf_error_val=[];
run_funcvals = [];
pop_size = 100;
G_Max=fix(max_nfes/pop_size);

%% Initialize the main population
popold = repmat(lu(1, :), pop_size, 1) + rand(pop_size, problem_size) .* (repmat(lu(2, :) - lu(1, :), pop_size, 1));
pop = popold; % the old population becomes the current population

fitness = feval(fhd,pop',func);
fitness = fitness';

nfes = 0;
bsf_fit_var = 1e+300;

%%%%%%%%%%%%%%%%%%%%%%%% for out
for i = 1 : pop_size
nfes = nfes + 1;
%% if nfes > max_nfes; exit(1); end
if nfes > max_nfes; break; end
if fitness(i) < bsf_fit_var
bsf_fit_var = fitness(i);
end
run_funcvals = [run_funcvals;bsf_fit_var];
end

%%%%%%%%%%%%%%%%%%%%%%%% Parameter settings%%%%%%%%%%
KF=0.5;% Knowledge Factor
KR=0.9;%Knowledge Ratio
K=10*ones(pop_size,1);%Knowledge Rate

g=0;
%% main loop
while nfes < max_nfes
g=g+1;
D_Gained_Shared_Junior=ceil((problem_size)*(1-g/G_Max).^K);
D_Gained_Shared_Senior=problem_size-D_Gained_Shared_Junior;
pop = popold; % the old population becomes the current population

[valBest, indBest] = sort(fitness, 'ascend');
[Rg1, Rg2, Rg3] = Gained_Shared_Junior_R1R2R3(indBest);

[R1, R2, R3] = Gained_Shared_Senior_R1R2R3(indBest);
R01=1:pop_size;
Gained_Shared_Junior=zeros(pop_size, problem_size);
ind1=fitness(R01)>fitness(Rg3);

if(sum(ind1)>0)
Gained_Shared_Junior (ind1,:)= pop(ind1,:) + KF*ones(sum(ind1), problem_size) .* (pop(Rg1(ind1),:) - pop(Rg2(ind1),:)+pop(Rg3(ind1), :)-pop(ind1,:)) ;
end
ind1=~ind1;
if(sum(ind1)>0)
Gained_Shared_Junior(ind1,:) = pop(ind1,:) + KF*ones(sum(ind1), problem_size) .* (pop(Rg1(ind1),:) - pop(Rg2(ind1),:)+pop(ind1,:)-pop(Rg3(ind1), :)) ;
end
R0=1:pop_size;
Gained_Shared_Senior=zeros(pop_size, problem_size);
ind=fitness(R0)>fitness(R2);
if(sum(ind)>0)
Gained_Shared_Senior(ind,:) = pop(ind,:) + KF*ones(sum(ind), problem_size) .* (pop(R1(ind),:) - pop(ind,:) + pop(R2(ind),:) - pop(R3(ind), :)) ;
end
ind=~ind;
if(sum(ind)>0)
Gained_Shared_Senior(ind,:) = pop(ind,:) + KF*ones(sum(ind), problem_size) .* (pop(R1(ind),:) - pop(R2(ind),:) + pop(ind,:) - pop(R3(ind), :)) ;
end
Gained_Shared_Junior = boundConstraint(Gained_Shared_Junior, pop, lu);
Gained_Shared_Senior = boundConstraint(Gained_Shared_Senior, pop, lu);


D_Gained_Shared_Junior_mask=rand(pop_size, problem_size)<=(D_Gained_Shared_Junior(:, ones(1, problem_size))./problem_size);
D_Gained_Shared_Senior_mask=~D_Gained_Shared_Junior_mask;

D_Gained_Shared_Junior_rand_mask=rand(pop_size, problem_size)<=KR*ones(pop_size, problem_size);
D_Gained_Shared_Junior_mask=and(D_Gained_Shared_Junior_mask,D_Gained_Shared_Junior_rand_mask);

D_Gained_Shared_Senior_rand_mask=rand(pop_size, problem_size)<=KR*ones(pop_size, problem_size);
D_Gained_Shared_Senior_mask=and(D_Gained_Shared_Senior_mask,D_Gained_Shared_Senior_rand_mask);
ui=pop;

ui(D_Gained_Shared_Junior_mask) = Gained_Shared_Junior(D_Gained_Shared_Junior_mask);
ui(D_Gained_Shared_Senior_mask) = Gained_Shared_Senior(D_Gained_Shared_Senior_mask);

children_fitness = feval(fhd, ui', func);
children_fitness = children_fitness';

for i = 1 : pop_size
nfes = nfes + 1;
if nfes > max_nfes; break; end
if children_fitness(i) < bsf_fit_var
bsf_fit_var = children_fitness(i);
bsf_solution = ui(i, :);
end
run_funcvals = [run_funcvals;bsf_fit_var];

end

[fitness, Child_is_better_index] = min([fitness, children_fitness], [], 2);

popold = pop;
popold(Child_is_better_index == 2, :) = ui(Child_is_better_index == 2, :);

% fprintf('NFES:%d, bsf_fit:%1.6e,pop_Size:%d,D_Gained_Shared_Junior:%2.2e,D_Gained_Shared_Senior:%2.2e\n', nfes,bsf_fit_var,pop_size,problem_size*sum(sum(D_Gained_Shared_Junior))/(pop_size*problem_size),problem_size*sum(sum(D_Gained_Shared_Senior))/(pop_size*problem_size))

end % end while loop

bsf_error_val = bsf_fit_var - optimum;
if bsf_error_val < val_2_reach
bsf_error_val = 0;
end

fprintf('%d th run, best-so-far error value = %1.8e\n', run_id , bsf_error_val)
outcome = [outcome bsf_error_val];

%% plot convergence figures
if (ConvDisp)
run_funcvals=run_funcvals-optimum;
run_funcvals=run_funcvals';
dim1(run_id,:)=1:length(run_funcvals);
dim2(run_id,:)=log10(run_funcvals);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
end %% end 1 run

%% save ststiatical output in analysis file%%%%
analysis(func,1)=min(outcome);
analysis(func,2)=median(outcome);
analysis(func,3)=max(outcome);
analysis(func,4)=mean(outcome);
analysis(func,5)=std(outcome);
median_figure=find(outcome== median(outcome));
analysis(func,6)=median_figure(1);

file_name=sprintf('Results\\%s_CEC2017_Problem#%s_problem_size#%s',Alg_Name,int2str(func),int2str(problem_size));
save(file_name,'outcome');
%% print statistical output and save convergence figures%%%
fprintf('%e\n',min(outcome));
fprintf('%e\n',median(outcome));
fprintf('%e\n',mean(outcome));
fprintf('%e\n',max(outcome));
fprintf('%e\n',std(outcome));
dim11=dim1(median_figure,:);
dim22=dim2(median_figure,:);
file_name=sprintf('Figures\\Figure_Problem#%s_Run#%s',int2str(func),int2str(median_figure));
save(file_name,'dim1','dim2');
end %% end 1 function run

file_name=sprintf('Results\\analysis_%s_CEC2017_problem_size#%s',Alg_Name,int2str(problem_size));
save(file_name,'analysis');
end %% end all function runs in all dimensions

5.GSK算法实例验证

以求解CEC2017第6个测试函数为例,该函数为Shifted and Rotated Schaffer’s F7 Function:


该函数具有4个特性:多模态、不可分离、不对称、局部最优点数量巨大,该函数图像如下:

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_07

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_08

当求解问题维数为10维时,求解结果如下,求解最优值为600,已经达到全局最优值:

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_09

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_matlab代码_10

参考文献

[1]Mohamed A W, Hadi A A, Mohamed A K. Gaining-sharing knowledge based algorithm for solving optimization problems: a novel nature-inspired algorithm[J]. International Journal of Machine Learning and Cybernetics, 2020, 11(7): 1501-1529.

[2]https://www.mathworks.com/matlabcentral/fileexchange/73730-gaining-sharing-knowledge-based-algorithm


OK,今天就到这里啦,各位可点击下方图片留言,下方图书为作者撰写书籍,助力各位快速入门智能优化算法

优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)_最小值_11