-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssa.m
93 lines (71 loc) · 2.51 KB
/
ssa.m
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
function [bestSolution, bestFitness, iteration]=ssa(train, test, maxIteration, k)
settings;
[~, dimension] = size(train);
N=30;
Max_iter=ceil(maxIteration/N)+1;
dim=dimension - 1;
lb = zeros(1, dim);
ub = ones(1, dim);
%Initialize the positions of salps
SalpPositions=initialization(N,dim,ub,lb);
SalpFitness=zeros(1,N);
Sorted_salps=zeros(N,dim);
%calculate the fitness of initial salps
for i=1:size(SalpPositions,1)
SalpFitness(1,i)=testFunction(SalpPositions(i,:)', train, test, k);
end
[sorted_salps_fitness,sorted_indexes]=sort(SalpFitness);
for newindex=1:N
Sorted_salps(newindex,:)=SalpPositions(sorted_indexes(newindex),:);
end
FoodPosition=Sorted_salps(1,:);
FoodFitness=sorted_salps_fitness(1);
%Main loop
l=2; % start from the second iteration since the first iteration was dedicated to calculating the fitness of salps
while l<Max_iter+1
c1 = 2*exp(-(4*l/Max_iter)^2); % Eq. (3.2) in the paper
for i=1:size(SalpPositions,1)
SalpPositions= SalpPositions';
if i<=N/2
for j=1:1:dim
c2=rand();
c3=rand();
%%%%%%%%%%%%% % Eq. (3.1) in the paper %%%%%%%%%%%%%%
if c3<0.5
SalpPositions(j,i)=FoodPosition(j)+c1*((ub(j)-lb(j))*c2+lb(j));
else
SalpPositions(j,i)=FoodPosition(j)-c1*((ub(j)-lb(j))*c2+lb(j));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
elseif i>N/2 && i<N+1
point1=SalpPositions(:,i-1);
point2=SalpPositions(:,i);
SalpPositions(:,i)=(point2+point1)/2; % % Eq. (3.4) in the paper
end
SalpPositions= SalpPositions';
end
for i=1:size(SalpPositions,1)
Tp=SalpPositions(i,:)>ub;
Tm=SalpPositions(i,:)<lb;
SalpPositions(i,:)=(SalpPositions(i,:).*(~(Tp+Tm)))+ub.*Tp+lb.*Tm;
SalpFitness(1,i)=testFunction(SalpPositions(i,:)', train, test, k);
if SalpFitness(1,i)<FoodFitness
FoodPosition=SalpPositions(i,:);
FoodFitness=SalpFitness(1,i);
end
end
l = l + 1;
end
bestFitness=FoodFitness;
bestSolution=FoodPosition;
iteration=(l-2)*N;
end
function Positions=initialization(SearchAgents_no,dim,ub,lb)
Positions = zeros(SearchAgents_no, dim);
for i=1:dim
ub_i=ub(i);
lb_i=lb(i);
Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;
end
end