一、获取代码方式
获取代码方式1: 通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
获取代码方式2: 通过紫极神光博客主页开通CSDN会员,凭支付凭证,私信博主,可获得此代码。
获取代码方式3: 完整代码已上传我的资源:【优化算法】社会群体优化算法(SGO)【含Matlab源码 1449期】
备注:开通CSDN会员,仅只能免费获得1份代码(有效期为开通日起,三天内有效); 订阅紫极神光博客付费专栏,可免费获得2份代码(有效期为订阅日起,三天内有效);
二、部分源代码
%--------------------------------------------------------------------------
% % Developed in MATLAB R2016a
clc;
clear all;
close all;
'MSGO'
pn=30; %population size
gn=250; %maximum number of iteration
% dim=30; %dimension of the fuction
% lb=-30; %lower bound
% ub=30; %upper bound
func_no=21;
[lb,ub,dim]=Test_Functions_Range(func_no);
[fitsgo,objvalue]=msgofunction(pn,dim,gn,lb,ub,func_no);
objvalue
fitsgo;
if objvalue>0
semilogy(fitsgo,'r','LineWidth',2);
else
plot(fitsgo,'r','LineWidth',2);
end
xlabel('Iterations');
ylabel('Fitness');
title('Convergence characteristics')
% for i=1:5
% point(i)=i;
% end
%plot(point,fitsgo(point),'-hb');
% %%
% title('Convergence characteristics')
% %%
% semilogy(fitsgo)
% %plot(fitsgo)
% xlabel('Number of Iterations');
% ylabel('Fitness value');
% %axis tight
grid on
% legend('MSGO','SouthEast');
%
%--------------------------------------------------------------------------
% % Developed in MATLAB R2016a
% The code is based on the following papers.
% A. Naik, A. Abraham and S.C. Satapathy(2020), Modified Social Group Optimization-a meta-heuristic
% algorithm to solve Short-term Hydrothermal Scheduling, Applied softcomputing,doi:10.1016/j.asoc.2020.106524
%--------------------------------------------------------------------------
function[fit,objvalue]=msgofunction(pn,dim,gn,lb,ub,func_no)
obj=@Test_Functions;
%Test_Functions(pop(i,:),func_no,dim);
pop=lb+rand(pn,dim)*(ub-lb);
c=0.2;
for i=1:pn
f(i)=obj(pop(i,:),func_no,dim);
end
[value ibest]=min(f);
% from here iteration start
fit=zeros(gn,1);
for g=1:gn
% improvement due to best person of the group
[value ibest]=min(f);
value;
guru=pop(ibest,:);
for i=1:pn
for j=1:dim
newpop(i,j)=c*pop(i,j)+rand*(guru(j)-pop(i,j));
if((newpop(i,j)<lb))
newpop(i,j)=lb;
end
if((newpop(i,j)>ub))
newpop(i,j)=ub;
end
end
f1=obj(newpop(i,:),func_no,dim);
if(f1<f(i))
pop(i,:)=newpop(i,:);
f(i)=f1;
end
end
[a b]=min(f);
gpop=pop(b,:);
SAP=0.7;
for i=1:pn
r1=floor(1+rand*pn);
while(r1==i)
r1=floor(1+rand*pn);
end
if(f(i)<f(r1))
if rand>SAP
for j=1:dim
popnew(i,j)=pop(i,j)+rand*(pop(i,j)-pop(r1,j))+rand*(gpop(j)-pop(i,j));
if(popnew(i,j)<lb)
popnew(i,j)=lb;
end
if(popnew(i,j)>ub)
popnew(i,j)=ub;
end
end
else
for j=1:dim
popnew(i,:)=lb+rand*(ub-lb);
end
end
else
for j=1:dim
popnew(i,j)=pop(i,j)+rand*(pop(r1,j)-pop(i,j))+rand*(gpop(j)-pop(i,j));
if(popnew(i,j)<lb)
popnew(i,j)=lb;
end
if(popnew(i,j)>ub)
popnew(i,j)=ub;
end
end
end
end
for i=1:pn
f2(i)=obj(popnew(i,:),func_no,dim);
if(f2(i)<f(i))
pop(i,:)=popnew(i,:);
f(i)=f2(i);
end
end
%end of learner phase
[minvalue position]=min(f);
fit(g)=minvalue;
objvalue=fit(g)
end
end
三、运行结果
四、matlab版本及参考文献
1 matlab版本 2014a
2 参考文献 [1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016. [2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.