优化算法 | 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 )×(1GENG)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 ,……,xi1,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×[(xi1xi+1)+(xrxi)]xi+kf×[(xi1xi+1)+(xixr)]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 12p,通常取 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×[(xpbestxpworst)+(xmxi)]xi+kf×[(xpbestxpworst)+(xixm)]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_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:
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(xo6)))+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
公号 | 优化算法交流地

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K-Medoids(Partitioning Around Medoids,PAM)是一种基于质心的聚类算法,与K-Means类似,但K-Medoids使用实际数据点作为聚类中心(即medoids),而不是计算出的均值。它在许多应用中比K-Means更有效,特别是在离群值存在的情况下。以下是一个使用Python实现K-Medoids文本聚类算法的例子: 首先,我们需要安装必要的库,包括numpy和nltk。你可以通过以下命令安装它们: ```python !pip install numpy !pip install nltk ``` 然后导入必要的库: ```python import numpy as np import nltk from nltk.stem import SnowballStemmer from sklearn.feature_extraction.text import TfidfVectorizer ``` 接下来,我们定义一个函数来计算文本之间的相似度。我们使用余弦相似度来计算两个文本之间的距离: ```python def cosine_sim(text1, text2): stemmer = SnowballStemmer("english") tfidf = TfidfVectorizer(stop_words="english", tokenizer=nltk.word_tokenize, use_idf=True, norm="l2") stems1 = [stemmer.stem(word) for word in text1.split()] stems2 = [stemmer.stem(word) for word in text2.split()] stems = stems1 + stems2 tfidf.fit_transform(stems) sim = tfidf.transform([text1, text2]).toarray() return sim[0][1] ``` 现在我们实现K-Medoids算法。我们首先初始化medoids并计算每个点到medoids的距离。然后在每个迭代中选择一个非medoid点,并将其替换为与该点距离最小的medoid。我们重复此过程,直到聚类稳定。 ```python def kmedoids(cluster_num, data): n = data.shape[0] medoids = np.zeros((cluster_num), dtype=int) for i in range(cluster_num): medoids[i] = np.random.randint(n) old_medoids = np.copy(medoids) clusters = np.zeros((n), dtype=int) while True: # 计算每个点到medoids的距离 distances = np.zeros((n, cluster_num)) for i in range(n): for j in range(cluster_num): distances[i,j] = cosine_sim(data[i], data[medoids[j]]) # 分配到最近的medoid的簇 clusters = np.argmin(distances, axis=1) # 更新medoids for i in range(cluster_num): indices = np.where(clusters == i)[0] if len(indices) > 0: subset = distances[indices, :] scores = np.sum(subset, axis=0) j = np.argmin(scores) medoids[i] = indices[j] # 如果没有变化,停止 if np.array_equal(old_medoids, medoids): break old_medoids = np.copy(medoids) return clusters, medoids ``` 现在我们可以使用上述函数对文本进行聚类。假设我们有以下文本: ```python docs = [ "machine learning is a subset of artificial intelligence", "python is an excellent programming language", "chatbots are gaining popularity in recent times", "data science is the future", "nlp is a field of study focused on the interaction between human language and computers" ] ``` 我们可以将这些文本向量化并应用K-Medoids算法: ```python tfidf = TfidfVectorizer(stop_words="english") data = tfidf.fit_transform(docs).toarray() clusters, medoids = kmedoids(2, data) ``` 在上面的示例中,我们将文本聚类到两个簇中。现在我们可以打印每个簇的文本: ```python for i in range(2): indices = np.where(clusters == i)[0] print("Cluster", i+1, ":", [docs[j] for j in indices]) ``` 输出结果应该类似于以下内容: ``` Cluster 1 : ['machine learning is a subset of artificial intelligence', 'data science is the future', 'nlp is a field of study focused on the interaction between human language and computers'] Cluster 2 : ['python is an excellent programming language', 'chatbots are gaining popularity in recent times'] ``` 这就是K-Medoids文本聚类算法的Python实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值