本文主要是介绍优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天为各位讲解Gaining‑sharing knowledge based algorithm(GSK),为什么讲解这个算法呢,因为自适应GSK算法(Adaptive Gaining-Sharing Knowledge Based Algorithm,AGSK)是CEC2020比赛的优胜算法。在讲解性能强悍的AGSK算法前,有必要先理解GSK的基本思想,而后再向深刻理解AGSK算法迈进。
1.GSK算法基本思想
GSK算法的灵感来源于人在一生中获取和共享知识的过程,这个过程分为两个阶段:
1)初级获取和共享知识阶段,即人一生的前中期。在这一阶段,相比于通过大型网络(如工作、社交、朋友等)获取知识,人们更多地会通过小型网络(如家人、邻居、亲戚等)获取知识。虽然这一阶段的人们想法、观点尚未成熟,但是他们努力尝试分享自己的观点。
2)高级获取和共享知识阶段,即人一生的中后期。这一阶段的人们通常会通过大型网络(如工作、社交、朋友等)获取知识,比如,这一阶段的人们通常喜欢成功学,相信成功者的观点,以使他们避免失败。这一阶段的人们思想十分成熟,他们会积极向他人分享自己的观点,期望帮助他人能从自己的分享中受益。
2.GSK算法数学描述
在了解GSK算法基本思想后,接下来对GSK算法进行数学描述:
假设第 i i i个体 x i = ( x i 1 , x i 2 , . . . , x i D ) , i = 1 , 2 , . . . , N x_i=(x_{i1},x_{i2},...,x_{iD}),i=1,2,...,N xi=(xi1,xi2,...,xiD),i=1,2,...,N的适应度值为 f i , i = 1 , 2 , . . . , N f_i,i=1,2,...,N fi,i=1,2,...,N,其中 D D D为问题维数, N N N为种群数目。下图可以清晰地展示两个阶段个体中初级部分和高级部分的变化情况。
从上述分析过程和上图中可以看出,初级要素数目和高级要素数目是变化的,则个体中初级要素数目的计算公式如下:
D ( juniorphase ) = ( problemsize ) × ( 1 − G G E N ) k D(\text { juniorphase })=(\text { problemsize }) \times\left(1-\frac{G}{G E N}\right)^k D( juniorphase )=( problemsize )×(1−GENG)k
个体中高级要素数目的计算公式如下:
D ( seniorphase ) = problemsize − D ( juniorphase ) D(\text { seniorphase })=\text { problemsize }-D(\text { juniorphase }) D( seniorphase )= problemsize −D( juniorphase )
其中, D ( juniorphase ) D(\text { juniorphase }) D( juniorphase )表示初级要素数目, D ( seniorphase ) D(\text { seniorphase}) D( seniorphase)表示高级要素数目, p r o b l e m s i z e problemsize problemsize表示问题维数, G E N GEN GEN表示总迭代次数, G G G表示当前迭代次数, k ( k > 0 ) k(k>0) k(k>0)表示知识学习率。
3.GSK算法基本步骤
如GSK基本思想所阐述的一样,GSK算法也分为初级和高级获取与共享知识两个阶段。
01 | 初级获取和共享知识阶段
假设求解函数最小值问题,在这一阶段,更新 x i , i = 1 , 2 , . . . , N x_i,i=1,2,...,N xi,i=1,2,...,N的方法如下:
(1)将种群中的个体按照适应度值从小到大的顺序进行排序,排序结果如下: x best , … … , x i − 1 , x i , x i + 1 , … … x worst x_{\text {best }}, \ldots \ldots, x_{i-1}, x_i, x_{i+1}, \ldots \ldots x_{\text {worst }} xbest ,……,xi−1,xi,xi+1,……xworst
(2) x i , i = 1 , 2 , . . . , N x_i,i=1,2,...,N xi,i=1,2,...,N的更新公式如下:
x i n e w = { x i + k f × [ ( x i − 1 − x i + 1 ) + ( x r − x i ) ] f ( x i ) > f ( x r ) x i + k f × [ ( x i − 1 − x i + 1 ) + ( x i − x r ) ] f ( x i ) ≤ f ( x r ) x_i^{n e w}= \begin{cases}x_i+k_f \times\left[\left(x_{i-1}-x_{i+1}\right)+\left(x_r-x_i\right)\right] & f\left(x_i\right)>f\left(x_r\right) \\ x_i+k_f \times\left[\left(x_{i-1}-x_{i+1}\right)+\left(x_i-x_r\right)\right] & f\left(x_i\right) \leq f\left(x_r\right)\end{cases} xinew={xi+kf×[(xi−1−xi+1)+(xr−xi)]xi+kf×[(xi−1−xi+1)+(xi−xr)]f(xi)>f(xr)f(xi)≤f(xr)
其中 x i n e w x_i^{n e w} xinew为更新后的个体, x r x_r xr为随机选择的个体, k f k_f kf为知识因素参数。
初级获取和共享知识阶段算法伪代码如下,其中 k r k_r kr为知识比率:
02 | 高级获取和共享知识阶段
假设求解函数最小值问题,在这一阶段,更新 x i , i = 1 , 2 , . . . , N x_i,i=1,2,...,N xi,i=1,2,...,N的方法如下:
(1)将种群中的个体按照适应度值从小到大的顺序进行排序,然后将排序后的个体分成3类,即最佳个体、中等个体、最差个体,其中最佳个体占比 p p p,最差个体占比 p p p,中等个体占比 1 − 2 p 1-2p 1−2p,通常取 p = 0.1 p=0.1 p=0.1。
(2) x i , i = 1 , 2 , . . . , N x_i,i=1,2,...,N xi,i=1,2,...,N的更新公式如下:
x i n e w = { x i + k f × [ ( x p b e s t − x p w o r s t ) + ( x m − x i ) ] f ( x i ) > f ( x m ) x i + k f × [ ( x p b e s t − x p w o r s t ) + ( x i − x m ) ] f ( x i ) ≤ f ( x m ) x_i^{n e w}= \begin{cases}x_i+k_f \times\left[\left(x_{pbest}-x_{pworst}\right)+\left(x_m-x_i\right)\right] & f\left(x_i\right)>f\left(x_m\right) \\ x_i+k_f \times\left[\left(x_{pbest}-x_{pworst}\right)+\left(x_i-x_m\right)\right] & f\left(x_i\right) \leq f\left(x_m\right)\end{cases} xinew={xi+kf×[(xpbest−xpworst)+(xm−xi)]xi+kf×[(xpbest−xpworst)+(xi−xm)]f(xi)>f(xm)f(xi)≤f(xm)
其中 x i n e w x_i^{n e w} xinew为更新后的个体, x p b e s t x_{pbest} xpbest为最佳个体中随机选择的个体, x p w o r s t x_{pworst} xpworst为最差个体中随机选择的个体, x m x_{m} xm为中等个体中随机选择的个体, k f k_f kf为知识因素参数。
高级获取和共享知识阶段算法伪代码如下,其中 k r k_r kr为知识比率:
03 | GSK算法伪代码及流程图
4.GSK算法MATLAB代码
GSK算法MATLAB代码链接为:https://www.mathworks.com/matlabcentral/fileexchange/73730-gaining-sharing-knowledge-based-algorithm,各位也可在公号后台回复【GSK】即可提取代码(不包括【】)。
以求解CEC2017为例,GSK算法文件夹共包含如下文件:
主函数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_problemsoptimum = func * 100.0;%% Record the best resultsoutcome = [];fprintf('\n-------------------------------------------------------\n')fprintf('Function = %d, Dimension size = %d\n', func, problem_size)dim1=[];dim2=[];for run_id = 1 : Run_Nobsf_error_val=[];run_funcvals = [];pop_size = 100;G_Max=fix(max_nfes/pop_size);%% Initialize the main populationpopold = 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 populationfitness = feval(fhd,pop',func);fitness = fitness';nfes = 0;bsf_fit_var = 1e+300;%%%%%%%%%%%%%%%%%%%%%%%% for outfor i = 1 : pop_sizenfes = nfes + 1;%% if nfes > max_nfes; exit(1); endif nfes > max_nfes; break; endif fitness(i) < bsf_fit_varbsf_fit_var = fitness(i);endrun_funcvals = [run_funcvals;bsf_fit_var];end%%%%%%%%%%%%%%%%%%%%%%%% Parameter settings%%%%%%%%%%KF=0.5;% Knowledge FactorKR=0.9;%Knowledge RatioK=10*ones(pop_size,1);%Knowledge Rateg=0;%% main loopwhile nfes < max_nfesg=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,:)) ;endind1=~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), :)) ;endR0=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), :)) ;endind=~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), :)) ;endGained_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_sizenfes = nfes + 1;if nfes > max_nfes; break; endif children_fitness(i) < bsf_fit_varbsf_fit_var = children_fitness(i);bsf_solution = ui(i, :);endrun_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 loopbsf_error_val = bsf_fit_var - optimum;if bsf_error_val < val_2_reachbsf_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 figuresif (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 runfile_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:
F 6 ( x ) = f 20 ( M ( 0.5 ( x − o 6 ) 100 ) ) + F 6 ∗ F_6(\boldsymbol{x})=f_{20}\left(\mathbf{M}\left(\frac{0.5\left(\boldsymbol{x}-\boldsymbol{o}_6\right)}{100}\right)\right)+F_6 * F6(x)=f20(M(1000.5(x−o6)))+F6∗
该函数具有4个特性:多模态、不可分离、不对称、局部最优点数量巨大,该函数图像如下:
当求解问题维数为10维时,求解结果如下,求解最优值为600,已经达到全局最优值:
参考文献
[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,今天就到这里啦,各位可点击下方图片留言,下方图书为作者撰写书籍,助力各位快速入门智能优化算法。
咱们下期再见
近期你可能错过了的好文章
新书上架 | 《MATLAB智能优化算法:从写代码到算法思想》
优化算法 | 灰狼优化算法(文末有福利)
优化算法 | 鲸鱼优化算法
遗传算法(GA)求解带时间窗的车辆路径(VRPTW)问题MATLAB代码
粒子群优化算法(PSO)求解带时间窗的车辆路径问题(VRPTW)MATLAB代码
知乎 | bilibili | CSDN:随心390
公号 | 优化算法交流地
这篇关于优化算法 | Gaining Sharing Knowledge based Algorithm(附MATLAB代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!