MATLAB代码实现了季节优化算法(Seasonal Optimization Algorithm, SOA)来求解优化问题

%% 淘个代码 %%
% 微信公众号搜索:淘个代码,获取更多代码
% 季节优化算法(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)来求解优化问题。下面详细介绍代码的功能:

1. 初始化部分

clear all;
clc;
close all

清除工作区中的所有变量,清空命令窗口,并关闭所有打开的图形窗口,为新的计算做准备。

2. 问题定义

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 函数获取目标函数的上下界、维度和函数句柄。
  • 对问题参数进行设置,包括变量的最小值、最大值和搜索空间大小。

3. 算法参数设置

AlgorithmParams.NumOfTrees = 8;
AlgorithmParams.NumOfYears = 100;
AlgorithmParams.Pmin = 0.1;
AlgorithmParams.Pmax = 0.8;

设置算法的相关参数,如树木数量、迭代年数、最小概率和最大概率。

4. 主循环

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,并更新算法的更新率、播种率、寒冷阈值和竞争率。
  • 春季:第一年创建初始森林,并计算初始成本;后续年份对森林进行更新。
  • 夏季:对森林进行竞争操作,模拟树木的生长和竞争过程。
  • 秋季:森林进行播种操作,产生新的种子。
  • 冬季:森林抵抗寒冷,对森林进行筛选。
  • 记录每年的最小成本,并输出到命令窗口。

5. 结果可视化

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');
  • 创建一个包含两个子图的图形窗口。
  • 左子图绘制目标函数的参数空间。
  • 右子图绘制算法的收敛曲线,根据目标函数的不同选择使用 semilogyplot 函数。

综上所述,这段代码通过季节优化算法对指定的目标函数进行优化,并将优化过程的收敛情况和目标函数的参数空间进行可视化展示。

你可能感兴趣的:(matlab,算法,深度学习,matlab,算法,开发语言)