季节优化算法

参考论文

Emami, Hojjat. “Seasons Optimization Algorithm.” Engineering with Computers, vol. 38, no. 2, Springer Science and Business Media LLC, Aug. 2020, pp. 1845–65, doi:10.1007/s00366-020-01133-5.

脚本解读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
clear all;
clc;

%% Problem Statement
ProblemParams.CostFuncName = 'F8';
[fobj, lowerbound, upperbound, globalCost, dimension]=GetBenchmarkFunction(ProblemParams.CostFuncName);
ProblemParams.CostFuncName=fobj;
ProblemParams.lb=lowerbound;
ProblemParams.ub=upperbound;
ProblemParams.NPar = dimension;
ProblemParams.gcost=globalCost;

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 = 50;
AlgorithmParams.Pmin = 0.4;
AlgorithmParams.Pmax = 0.6;


%% 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

这是一个MATLAB脚本,用于模拟森林生态系统演化过程,并寻找最优树种分布以最小化特定成本函数。

初始化部分

1
2
clear all;  % 清除工作空间中的所有变量
clc; % 清除命令窗口中的输出信息

问题参数设定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% 定义问题参数
ProblemParams.CostFuncName = 'F8'; % 设置成本函数为F8
[fobj, lowerbound, upperbound, globalCost, dimension] = GetBenchmarkFunction(ProblemParams.CostFuncName);
ProblemParams.CostFuncName = fobj; % 将成本函数句柄赋值给ProblemParams.CostFuncName
ProblemParams.lb = lowerbound; % 设置变量下界
ProblemParams.ub = upperbound; % 设置变量上界
ProblemParams.NPar = dimension; % 设置问题维度(即决策变量个数)
ProblemParams.gcost = globalCost; % 设置全局最小成本

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;
这部分代码定义了问题参数结构体ProblemParams,包括成本函数名、变量上下界、问题维度、全局最小成本、变量最小值、变量最大值和搜索空间大小。其中,GetBenchmarkFunction函数用于获取成本函数相关信息。

算法参数设定

1
2
3
4
5
% 定义算法参数
AlgorithmParams.NumOfTrees = 8; % 设置每棵树种的数量
AlgorithmParams.NumOfYears = 50; % 设置模拟年数
AlgorithmParams.Pmin = 0.4; % 设置参数P的最小值
AlgorithmParams.Pmax = 0.6; % 设置参数P的最大值
这部分代码定义了算法参数结构体AlgorithmParams,包括每棵树种数量、模拟年数和参数P的范围(P在森林生态模型中可能代表更新率、播种率、冷阈值、竞争率等)。

主循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
for year = 1:AlgorithmParams.NumOfYears
% 根据模拟年份计算参数P的值
p = AlgorithmParams.Pmax - (year / AlgorithmParams.NumOfYears) * (AlgorithmParams.Pmax - AlgorithmParams.Pmin);

% 更新参数P对应的森林生态模型参数
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
这部分代码是主循环,模拟森林生态系统演化过程。每年按照春季、夏季、秋季、冬季四个季节进行操作,包括创建或更新森林、生长与竞争、播种、抵抗寒冷等步骤,并在每个年份结束时记录最小成本。最后,输出当前年份的最小成本。整个循环共模拟AlgorithmParams.NumOfYears年。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
%% GetBenchmarkFunction.m
function [fobj, l, u, g, d]=GetBenchmarkFunction(number)
dim=2;
switch number

case 'F1'
% F1- SumSquares lower=[-10], upper=[10], gminimum=[0], dim=30
fobj = @F4;
l=[-10];
u=[10];
g=0;
d=dim;

case 'F2'
% F2- Schwefel 1.2 lower=[-100], upper=[100], gminimum=[0], dim=30
fobj = @F2;
l=[-100];
u=[100];
g=0;
d=dim;
case 'F3'
% F3- Rosenbrock lower=[-30], upper=[30], gminimum=[0], dim=30
fobj = @F1;
l=[-30];
u=[30];
g=0;
d=dim;

case 'F4'
% F4- Sphere lower=[-100], upper=[100], gminimum=[0], dim=30
fobj = @F3;
l=[-100];
u=[100];
g=0;
d=dim;


case 'F5'
% F5- Zakharov lower=[-5], upper=[10], gminimum=[0], dim=10
fobj = @F5;
l=[-5];
u=[10];
g=0;
d=dim;

case 'F6'
% F7- Griewank lower=[-600], upper=[600] gminimum=[0], dim=30
fobj = @F6;
l=[-600];
u=[600];
g=[0];
d=dim;

case 'F7'
% F7- Rastrigin lower=[-5.12], upper=[5.12] gminimum=[0], dim=30
fobj = @F7;
l=[-5.12];
u=[5.12];
g=0;
d=dim;

case 'F8'
% F8- Egg Crate Function lower=[0], upper=[10] gminimum=[0], dim=2
fobj = @F8;
l=[0];
u=[10];
g=-18.5547;
d=dim;
end
end

function z = F1(x)
% F1- SumSquares lower=[-10], upper=[10], gminimum=[0], dim=30
[m, n] = size(x);
x2 = x .^2;
I = repmat(1:n, m, 1);
z = sum( I .* x2, 2);
end


function z = F2(x)
% F2- Schwefel 1.2 lower=[-100], upper=[100], gminimum=[0], dim=30
z = 0;
v1 = 0;
dim=size(x,2);
for i = 1:dim
v1=0;
for j = 1: i
v1 = v1 + x(:,j);
end
z = z + v1.^2;
end
end

function z = F3(x)
% F3- Rosenbrock lower=[-30], upper=[30], gminimum=[0], dim=30
z = 0;
n = size(x, 2);
assert(n >= 1, 'Given input X cannot be empty');
a = 1;
b = 100;
for i = 1 : (n-1)
z = z + (b * ((x(:, i+1) - (x(:, i).^2)) .^ 2)) + ((a - x(:, i)) .^ 2);
end
end

function z = F4(x)
% F4- Sphere lower=[-100], upper=[100], gminimum=[0], dim=30
z=sum(x'.^2)';
end
function z = F5(x)
% F5- Zakharov lower=[-5], upper=[10], gminimum=[0], dim=10
n = size(x, 2);
comp1 = 0;
comp2 = 0;
for i = 1:n
comp1 = comp1 + (x(:, i) .^ 2);
comp2 = comp2 + (0.5 * i * x(:, i));
end
z = comp1 + (comp2 .^ 2) + (comp2 .^ 4);
end

function z = F6(x)
% F6- Griewank lower=[-600], upper=[600] gminimum=[0], dim=30
n = size(x, 2);
sumcomp = 0;
prodcomp = 1;
for i = 1:n
sumcomp = sumcomp + (x(:, i) .^ 2);
prodcomp = prodcomp .* (cos(x(:, i) / sqrt(i)));
end
z = (sumcomp / 4000) - prodcomp + 1;
end

function z = F7(x)
% F7- Rastrigin lower=[-5.12], upper=[5.12] gminimum=[0], dim=30
n = size(x, 2);
A = 10;
z = (A * n) + (sum(x .^2 - A * cos(2 * pi .* x), 2));
end

function z = F8(x)
X = x(:, 1);
Y = x(:, 2);
%z = X.^2 + Y.^2 + (25 * (sin(X).^2 + sin(Y).^2));
z=X.*sin(4.*X)+ 1.1.*Y.*sin(2.*Y);
end

两个变量

输入参数:

number:字符串,表示要选择的基准测试函数编号(如"F1"、"F2"等)。

输出参数:

fobj:函数句柄,指向对应的基准测试函数。

l:变量下界向量,表示优化问题中每个变量的最小允许值。

u:变量上界向量,表示优化问题中每个变量的最大允许值。

g:全局最小值,表示所选基准测试函数的已知全局最小解对应的函数值。

d:问题维度,表示优化问题中变量的个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Minimum Cost in Iteration 1 is -4.6682556618584661 
Minimum Cost in Iteration 2 is -8.1975662567254535
Minimum Cost in Iteration 3 is -8.1975662567254535
Minimum Cost in Iteration 4 is -14.6935300252384948
Minimum Cost in Iteration 5 is -14.6935300252384948
Minimum Cost in Iteration 6 is -14.6935300252384948
Minimum Cost in Iteration 7 is -14.6935300252384948
Minimum Cost in Iteration 8 is -14.6935300252384948
Minimum Cost in Iteration 9 is -14.6935300252384948
Minimum Cost in Iteration 10 is -14.6935300252384948
Minimum Cost in Iteration 11 is -17.6789369064143571
Minimum Cost in Iteration 12 is -17.6789369064143571
Minimum Cost in Iteration 13 is -17.6789369064143571
Minimum Cost in Iteration 14 is -17.6789369064143571
Minimum Cost in Iteration 15 is -17.8433233416086168
Minimum Cost in Iteration 16 is -17.8433233416086168
Minimum Cost in Iteration 17 is -17.8433233416086168
Minimum Cost in Iteration 18 is -17.8433233416086168
Minimum Cost in Iteration 19 is -17.8433233416086168
Minimum Cost in Iteration 20 is -17.8433233416086168
Minimum Cost in Iteration 21 is -17.8433233416086168
Minimum Cost in Iteration 22 is -17.8433233416086168
Minimum Cost in Iteration 23 is -17.8433233416086168
Minimum Cost in Iteration 24 is -18.4564913551477119
Minimum Cost in Iteration 25 is -18.4564913551477119
Minimum Cost in Iteration 26 is -18.5092203075349104
Minimum Cost in Iteration 27 is -18.5092203075349104
Minimum Cost in Iteration 28 is -18.5092203075349104
Minimum Cost in Iteration 29 is -18.5092203075349104
Minimum Cost in Iteration 30 is -18.5365046804113760
Minimum Cost in Iteration 31 is -18.5365046804113760
Minimum Cost in Iteration 32 is -18.5365046804113760
Minimum Cost in Iteration 33 is -18.5365046804113760
Minimum Cost in Iteration 34 is -18.5365046804113760
Minimum Cost in Iteration 35 is -18.5365046804113760
Minimum Cost in Iteration 36 is -18.5365046804113760
Minimum Cost in Iteration 37 is -18.5365046804113760
Minimum Cost in Iteration 38 is -18.5365046804113760
Minimum Cost in Iteration 39 is -18.5365046804113760
Minimum Cost in Iteration 40 is -18.5365046804113760
Minimum Cost in Iteration 41 is -18.5365046804113760
Minimum Cost in Iteration 42 is -18.5365046804113760
Minimum Cost in Iteration 43 is -18.5365046804113760
Minimum Cost in Iteration 44 is -18.5365046804113760
Minimum Cost in Iteration 45 is -18.5365046804113760
Minimum Cost in Iteration 46 is -18.5365046804113760
Minimum Cost in Iteration 47 is -18.5365046804113760
Minimum Cost in Iteration 48 is -18.5365046804113760
Minimum Cost in Iteration 49 is -18.5365046804113760
Minimum Cost in Iteration 50 is -18.5365046804113760
1
2
3
4
5
6
7
8
9
xx=0:0.1:10;
yy=0:0.1:10;
[x,y]=meshgrid(xx,yy);
z=x.*sin(4.*x)+ 1.1.*y.*sin(2.*y);
subplot(1,2,1)
mesh(x,y,z)
subplot(1,2,2)
contour(x,y,z)
colorbar

F8 : z=x.sin(4.x)+ 1.1.y.sin(2.*y)

alt text

季节优化算法
http://jrhu0048.github.io/2024/04/26/ji-jie-you-hua-suan-fa/
作者
JR.HU
发布于
2024年4月26日
更新于
2024年4月26日
许可协议