%% 淘个代码 %%
% 微信公众号搜索:淘个代码,获取更多代码
% 季节优化算法(SOA)
clear all;
clc;
close all
%% Problem Statement
func_name = 'F8';
ProblemParams.CostFuncName = func_name;
[lowerbound, upperbound, dimension,fobj]=fun_info(ProblemParams.CostFuncName);
ProblemParams.CostFuncName=fobj;
ProblemParams.lb=lowerbound;
ProblemParams.ub=upperbound;
ProblemParams.NPar = dimension;
ProblemParams.VarMin =ProblemParams.lb;
ProblemParams.VarMax = ProblemParams.ub;
if numel(ProblemParams.VarMin)==1
ProblemParams.VarMin=repmat(ProblemParams.VarMin,1,ProblemParams.NPar);
ProblemParams.VarMax=repmat(ProblemParams.VarMax,1,ProblemParams.NPar);
end
ProblemParams.SearchSpaceSize = ProblemParams.VarMax - ProblemParams.VarMin;
AlgorithmParams.NumOfTrees = 8;
AlgorithmParams.NumOfYears = 100;
AlgorithmParams.Pmin = 0.1;
AlgorithmParams.Pmax = 0.8;
%% Main Loop
for year= 1:AlgorithmParams.NumOfYears
p=AlgorithmParams.Pmax-(year/AlgorithmParams.NumOfYears)*(AlgorithmParams.Pmax-AlgorithmParams.Pmin); %pr, ps and pw are in the range [0.4, 0.6]
AlgorithmParams.RenewRate=p;
AlgorithmParams.SeedingRate=p;
AlgorithmParams.ColdThreshold=p;
AlgorithmParams.CompetitionRate = p;
%% Spring Season
if (year==1)
InitialTrees = CreateForest(AlgorithmParams, ProblemParams);
Forest=InitialTrees;
InitialCost = feval(ProblemParams.CostFuncName,InitialTrees');
Forest(:,end+1) = InitialCost';
else
Forest = Renew(Forest, Seeds, AlgorithmParams, ProblemParams);
end
%% Summer Season (Growth & Competition)
[Forest] = Competition (Forest, AlgorithmParams, ProblemParams, year);
%% Autumn Season
Seeds = Seeding(Forest,AlgorithmParams, ProblemParams);
s=size(Seeds,1);
AlgorithmParams.s=s;
%% Winter Season
Forest = Resistance(Forest,AlgorithmParams, ProblemParams);
Costs = Forest(:,end);
MinimumCost(year) = min(Costs);
fprintf('Minimum Cost in Iteration %d is %3.16f \n', year,MinimumCost(year));
end
figure('Position',[454 445 694 297]);
subplot(1,2,1);
func_plot(func_name); % Function plot
title('Parameter space')
xlabel('x_1');
ylabel('x_2');
zlabel([func_name,'( x_1 , x_2 )'])
subplot(1,2,2); % Convergence plot
CNT=20;
k=round(linspace(1,AlgorithmParams.NumOfYears,CNT)); %随机选CNT个点
% 注意:如果收敛曲线画出来的点很少,随机点很稀疏,说明点取少了,这时应增加取点的数量,100、200、300等,逐渐增加
% 相反,如果收敛曲线上的随机点非常密集,说明点取多了,此时要减少取点数量
iter=1:1:AlgorithmParams.NumOfYears;
if ~strcmp(func_name,'F16')&&~strcmp(func_name,'F9')&&~strcmp(func_name,'F11') %这里是因为这几个函数收敛太快,不适用于semilogy,直接plot
semilogy(iter(k),MinimumCost(k),'k-o','linewidth',1);
else
plot(iter(k),MinimumCost(k),'k-o','linewidth',1);
end
xlabel('Iteration#');
ylabel('Best fitness so far');
legend('SO');
这段MATLAB代码实现了季节优化算法(Seasonal Optimization Algorithm, SOA)来求解优化问题。下面详细介绍代码的功能:
clear all;
clc;
close all
清除工作区中的所有变量,清空命令窗口,并关闭所有打开的图形窗口,为新的计算做准备。
func_name = 'F8';
ProblemParams.CostFuncName = func_name;
[lowerbound, upperbound, dimension,fobj]=fun_info(ProblemParams.CostFuncName);
ProblemParams.CostFuncName=fobj;
ProblemParams.lb=lowerbound;
ProblemParams.ub=upperbound;
ProblemParams.NPar = dimension;
ProblemParams.VarMin =ProblemParams.lb;
ProblemParams.VarMax = ProblemParams.ub;
if numel(ProblemParams.VarMin)==1
ProblemParams.VarMin=repmat(ProblemParams.VarMin,1,ProblemParams.NPar);
ProblemParams.VarMax=repmat(ProblemParams.VarMax,1,ProblemParams.NPar);
end
ProblemParams.SearchSpaceSize = ProblemParams.VarMax - ProblemParams.VarMin;
F8
。fun_info
函数获取目标函数的上下界、维度和函数句柄。AlgorithmParams.NumOfTrees = 8;
AlgorithmParams.NumOfYears = 100;
AlgorithmParams.Pmin = 0.1;
AlgorithmParams.Pmax = 0.8;
设置算法的相关参数,如树木数量、迭代年数、最小概率和最大概率。
for year= 1:AlgorithmParams.NumOfYears
p=AlgorithmParams.Pmax-(year/AlgorithmParams.NumOfYears)*(AlgorithmParams.Pmax-AlgorithmParams.Pmin);
AlgorithmParams.RenewRate=p;
AlgorithmParams.SeedingRate=p;
AlgorithmParams.ColdThreshold=p;
AlgorithmParams.CompetitionRate = p;
% 春季
if (year==1)
InitialTrees = CreateForest(AlgorithmParams, ProblemParams);
Forest=InitialTrees;
InitialCost = feval(ProblemParams.CostFuncName,InitialTrees');
Forest(:,end+1) = InitialCost';
else
Forest = Renew(Forest, Seeds, AlgorithmParams, ProblemParams);
end
% 夏季
[Forest] = Competition (Forest, AlgorithmParams, ProblemParams, year);
% 秋季
Seeds = Seeding(Forest,AlgorithmParams, ProblemParams);
s=size(Seeds,1);
AlgorithmParams.s=s;
% 冬季
Forest = Resistance(Forest,AlgorithmParams, ProblemParams);
Costs = Forest(:,end);
MinimumCost(year) = min(Costs);
fprintf('Minimum Cost in Iteration %d is %3.16f \n', year,MinimumCost(year));
end
p
,并更新算法的更新率、播种率、寒冷阈值和竞争率。figure('Position',[454 445 694 297]);
subplot(1,2,1);
func_plot(func_name);
title('Parameter space')
xlabel('x_1');
ylabel('x_2');
zlabel([func_name,'( x_1 , x_2 )'])
subplot(1,2,2);
CNT=20;
k=round(linspace(1,AlgorithmParams.NumOfYears,CNT));
iter=1:1:AlgorithmParams.NumOfYears;
if ~strcmp(func_name,'F16')&&~strcmp(func_name,'F9')&&~strcmp(func_name,'F11')
semilogy(iter(k),MinimumCost(k),'k-o','linewidth',1);
else
plot(iter(k),MinimumCost(k),'k-o','linewidth',1);
end
xlabel('Iteration#');
ylabel('Best fitness so far');
legend('SO');
semilogy
或 plot
函数。综上所述,这段代码通过季节优化算法对指定的目标函数进行优化,并将优化过程的收敛情况和目标函数的参数空间进行可视化展示。