我正在尝试编写一种更快的方法来评估在这里找到的Shekel函数:https://www.sfu.ca/~ssurjano/shekel.html
他们的代码与我一直在使用的代码非常相似,除了我将x值的矩阵传递给函数,并在每个元素计算中以三个循环结束。 Matlab应该能够做得更好。
这是我以前使用的代码:
function S = Shekel(X,m)
[R,d] = size(X);
% R is the population size, m the number of minima, and d the dimensions
% input control %
if d > 4
error('More than 4 dimensions !!')
end
if nargin==1
m=10;
elseif (m > 10) || (m < 2)
error('Wrong m')
end
% coefficients %
A = [4 4 4 4
1 1 1 1
8 8 8 8
6 6 6 6
3 7 3 7
2 9 2 9
5 5 3 3
8 1 8 1
6 2 6 2
7 3.6 7 3.6];
c = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
S = zeros(R,1);
for r = 1:R
s = 0;
for i = 1:m
denom = c(i);
for j = 1:d
denom = denom + (X(r,j) - A(i,j))^2;
end
s = s - 1/denom;
end
S(r) = s;
end
还有一个R实现,与我想做的类似:https://www.sfu.ca/~ssurjano/Code/shekelr.html
到目前为止,我已经明白了这一点,但是它和上一个一样慢,因为我正在做完全相同的事情:
S = zeros(R,1);
for r = 1:R
%S(r) = -sum(1./(sum((repmat(X(r,:),m,1)-A).^2,2)'+c),2);
S(r) = -sum(1./(sum((repmat(X(r,:),m,1)-A).^2,2).'+c),2);
end
可以用此函数调用,您应该得到-0.3007:
clear
p = 40; m = 10; d = 4;
X = repmat([1 2 3 4], p, 1);
OF = @Shekel;
OF(X, m)
有人可以建议一种更快的方法吗?
PS。该功能用于测试差异演化
编辑:实际操作(人口规模= 2,尺寸= 4,#minimums = 10)
X =
1.5381 0.7603 3.2619 7.7624
8.1874 1.9172 0.4234 5.0153
A =
4.0 4.0 4.0 4.0
1.0 1.0 1.0 1.0
8.0 8.0 8.0 8.0
6.0 6.0 6.0 6.0
3.0 7.0 3.0 7.0
2.0 9.0 2.0 9.0
5.0 5.0 3.0 3.0
8.0 1.0 8.0 1.0
6.0 2.0 6.0 2.0
7.0 3.6 7.0 3.6
repmat(X(1,:),m,1)-A
1.5381 0.7603 3.2619 7.7624 4.0 4.0 4.0 4.0
1.5381 0.7603 3.2619 7.7624 1.0 1.0 1.0 1.0
1.5381 0.7603 3.2619 7.7624 8.0 8.0 8.0 8.0
1.5381 0.7603 3.2619 7.7624 6.0 6.0 6.0 6.0
1.5381 0.7603 3.2619 7.7624 - 3.0 7.0 3.0 7.0
1.5381 0.7603 3.2619 7.7624 2.0 9.0 2.0 9.0
1.5381 0.7603 3.2619 7.7624 5.0 5.0 3.0 3.0
1.5381 0.7603 3.2619 7.7624 8.0 1.0 8.0 1.0
1.5381 0.7603 3.2619 7.7624 6.0 2.0 6.0 2.0
1.5381 0.7603 3.2619 7.7624 7.0 3.6 7.0 3.6
X-A =
-2.4619 -3.2397 -0.7381 3.7624
0.5381 -0.2397 2.2619 6.7624
-6.4619 -7.2397 -4.7381 -0.2376
-4.4619 -5.2397 -2.7381 1.7624
-1.4619 -6.2397 0.2619 0.7624
-0.4619 -8.2397 1.2619 -1.2376
-3.4619 -4.2397 0.2619 4.7624
-6.4619 -0.2397 -4.7381 6.7624
-4.4619 -1.2397 -2.7381 5.7624
-5.4619 -2.8397 -3.7381 4.1624
(repmat(X(1,:),m,1)-A)。^ 2
(X-A)^ 2
6.0612 10.4955 0.5448 14.1560
0.2895 0.0574 5.1162 45.7307
41.7568 52.4129 22.4495 0.0564
19.9090 27.4542 7.4971 3.1062
2.1373 38.9335 0.0686 0.5813
0.2134 67.8922 1.5924 1.5315
11.9851 17.9748 0.0686 22.6809
41.7568 0.0574 22.4495 45.7307
19.9090 1.5368 7.4971 33.2058
29.8329 8.0638 13.9733 17.3260
sum((repmat(X(1,:),m,1)-A)。^ 2,2)。'
X-A行的总和
31.2575 51.1939 116.6756 57.9665 41.7208 71.2296 52.7094 109.9944 62.1487 69.1959
c =
0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5
sum((repmat(X(1,:),m,1)-A)。^ 2,2)。'+ c
sum(X-A)+ c
31.3575 51.3939 116.8756 58.3665 42.1208 71.8296 53.0094 110.6944 62.6487 69.6959
1 ./(sum((repmat(X(r(:,),m,1)-A)。^ 2,2)。'+ c)
1 / sum(X-A)+ c
0.0319 0.0195 0.0086 0.0171 0.0237 0.0139 0.0189 0.0090 0.0160 0.0143
-sum(1 ./(sum((repmat(X(1,:),m,1)-A)。^ 2,2)。'+ c),2)
Shekel(X(1))= -sum(1 / sum(X-A)+ c)
-0.1729
..等等,对于X(2)