-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOS.m
175 lines (142 loc) · 6.41 KB
/
SOS.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
% ----------------------------------------------------------------------- %
% Symbiotic Organisms Search(SOS) for unconstrained benchmark problems
% a simplified version, last revised: 2014.08.27
% ----------------------------------------------------------------------- %
% Files of the Matlab code used in the artile:
%
% Min-Yuan Cheng, Doddy Prayogo,
% Symbiotic Organisms Search: A new metaheuristic optimization algorithm,
% Computers & Structures 139 (2014), 98-112
% http://dx.doi.org/10.1016/j.compstruc.2014.03.007
%
% ----------------------------------------------------------------------- %
% Written by Doddy Prayogo at National Taiwan University of Science and
% Technology (NTUST)
% Email: doddyprayogo@ymail.com
% ----------------------------------------------------------------------- %
%% --- MAIN OPTIMIZER ---
function [bestOrganism bestFitness]=SOS(ecosize,funnum)
tic;
% Outputs: best organism/solution and best fitness
% Inputs: ecosystem/population size and # of benchmark problems
% Example: [A,B]=SOS (50,17), SOS will solve Sphere (F17) with 50 organisms
% (please see the "OBJECTIVE FUNCTIONS" and "SETUP" sub-functions)
%format compact
fprintf('-------------------------------------------------------------------------\n');
fprintf(' Symbiotic Organisms Search(SOS) for unconstrained benchmark problems\n');
fprintf('-------------------------------------------------------------------------\n\n');
% --- Counters, Parameters & Matrix Initialization
[globalMin lb ub n maxFE]=terminate(funnum);
fprintf(' Ecosystem Size: %d\t\tMaxFE: %d\t\tFunctionNumber: %d',ecosize,maxFE,funnum);
fprintf('\n\n');
fprintf('-------------------------------------------------------------------------\n\n');
FE=0; % Function of Evaluation Counter
eco=zeros(ecosize,n); % Ecosystem Matrix
fitness =zeros(ecosize,1); % Fitness Matrix
% --- Ecosystem Initialization
for i=1:ecosize
% Initialize the organisms randomly in the ecosystem
eco(i,:)=rand(1,n).*(ub-lb)+lb;
% Evaluate the fitness of the new solution
fitness(i,:)=fobj(eco(i,:),funnum);
% Increase the number of function evaluation counter
%FE=FE+1;
end
% --- Main Looping
while FE<maxFE
for i=1:ecosize % Organisms' Looping
% Update the best Organism
[bestFitness,idx]=min(fitness); bestOrganism=eco(idx,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mutualism Phase
% Choose organism j randomly other than organism i
j=i;
while i==j
seed=randperm(ecosize);
j=seed(1);
end
% Determine Mutual Vector & Beneficial Factor
mutualVector=mean([eco(i,:);eco(j,:)]);
BF1=round(1+rand); BF2=round(1+rand);
% Calculate new solution after Mutualism Phase
ecoNew1=eco(i,:)+rand(1,n).*(bestOrganism-BF1.*mutualVector);
ecoNew2=eco(j,:)+rand(1,n).*(bestOrganism-BF2.*mutualVector);
ecoNew1=bound(ecoNew1,ub,lb);
ecoNew2=bound(ecoNew2,ub,lb);
% Evaluate the fitness of the new solution
fitnessNew1=fobj(ecoNew1,funnum);
fitnessNew2=fobj(ecoNew2,funnum);
% Accept the new solution if the fitness is better
if fitnessNew1<fitness(i)
fitness(i)=fitnessNew1;
eco(i,:)=ecoNew1;
end
if fitnessNew2<fitness(j)
fitness(j)=fitnessNew2;
eco(j,:)=ecoNew2;
end
% Increase the number of function evaluation counter
FE=FE+2;
% End of Mutualism Phase
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Commensialism Phase
% Choose organism j randomly other than organism i
j=i;
while i==j
seed=randperm(ecosize);
j=seed(1);
end
% Calculate new solution after Commensalism Phase
ecoNew1=eco(i,:)+(rand(1,n)*2-1).*(bestOrganism-eco(j,:));
ecoNew1=bound(ecoNew1,ub,lb);
% Evaluate the fitness of the new solution
fitnessNew1=fobj(ecoNew1,funnum);
% Accept the new solution if the fitness is better
if fitnessNew1<fitness(i)
fitness(i)=fitnessNew1;
eco(i,:)=ecoNew1;
end
% Increase the number of function evaluation counter
FE=FE+1;
% End of Commensalism Phase
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parasitism Phase
% Choose organism j randomly other than organism i
j=i;
while i==j
seed=randperm(ecosize);
j=seed(1);
end
% Determine Parasite Vector & Calculate the fitness
parasiteVector=eco(i,:);
seed=randperm(n);
pick=seed(1:ceil(rand*n)); % select random dimension
parasiteVector(:,pick)=rand(1,length(pick)).*(ub(pick)-lb(pick))+lb(pick);
fitnessParasite=fobj(parasiteVector,funnum);
% Kill organism j and replace it with the parasite
% if the fitness is lower than the parasite
if fitnessParasite < fitness(j)
fitness(j)=fitnessParasite;
eco(j,:)=parasiteVector;
end
% Increase the number of function evaluation counter
FE=FE+1;
% End of Parasitism Phase
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end % End of Organisms' Looping
% Checking the termination criteria
if bestFitness<globalMin
break
end
end % End of Main Looping
% --- Update the best Organism
[bestFitness,idx]=min(fitness); bestOrganism=eco(idx,:);
% --- Display the result
%disp(['Funnum: ', num2str(funnum)])
disp(['Best Fitness: ', num2str(bestFitness)])
disp(['Best Organism: ', num2str(bestOrganism)])
toc;
%% --- Boundary Handling ---
function a=bound(a,ub,lb)
a(a>ub)=ub(a>ub); a(a<lb)=lb(a<lb);
end