forked from 7ossam81/EvoloPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSA.py
137 lines (79 loc) · 3.54 KB
/
SSA.py
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
import random
import numpy
import math
from solution import solution
import time
def SSA(objf,lb,ub,dim,N,Max_iteration):
#Max_iteration=1000
#lb=-100
#ub=100
#dim=30
N=50 # Number of search agents
if not isinstance(lb, list):
lb = [lb] * dim
if not isinstance(ub, list):
ub = [ub] * dim
Convergence_curve=numpy.zeros(Max_iteration)
#Initialize the positions of salps
SalpPositions = numpy.zeros((N, dim))
for i in range(dim):
SalpPositions[:, i] = numpy.random.uniform(0, 1, N) * (ub[i] - lb[i]) + lb[i]
SalpFitness=numpy.full(N,float("inf"))
FoodPosition=numpy.zeros(dim)
FoodFitness=float("inf")
#Moth_fitness=numpy.fell(float("inf"))
s=solution()
print("SSA is optimizing \""+objf.__name__+"\"")
timerStart=time.time()
s.startTime=time.strftime("%Y-%m-%d-%H-%M-%S")
for i in range(0,N):
# evaluate moths
SalpFitness[i]=objf(SalpPositions[i,:])
sorted_salps_fitness=numpy.sort(SalpFitness)
I=numpy.argsort(SalpFitness)
Sorted_salps=numpy.copy(SalpPositions[I,:])
FoodPosition=numpy.copy(Sorted_salps[0,:])
FoodFitness=sorted_salps_fitness[0]
Iteration=1;
# Main loop
while (Iteration<Max_iteration):
# Number of flames Eq. (3.14) in the paper
#Flame_no=round(N-Iteration*((N-1)/Max_iteration));
c1 = 2*math.exp(-(4*Iteration/Max_iteration)**2); # Eq. (3.2) in the paper
for i in range(0,N):
SalpPositions= numpy.transpose(SalpPositions);
if i<N/2:
for j in range(0,dim):
c2=random.random()
c3=random.random()
#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]);
####################
elif i>=N/2 and i<N+1:
point1=SalpPositions[:,i-1];
point2=SalpPositions[:,i];
SalpPositions[:,i]=(point2+point1)/2; # Eq. (3.4) in the paper
SalpPositions= numpy.transpose(SalpPositions);
for i in range(0,N):
# Check if salps go out of the search spaceand bring it back
for j in range(dim):
SalpPositions[i,j]=numpy.clip(SalpPositions[i,j], lb[j], ub[j])
SalpFitness[i]=objf(SalpPositions[i,:]);
if SalpFitness[i]<FoodFitness:
FoodPosition=numpy.copy(SalpPositions[i,:])
FoodFitness=SalpFitness[i]
#Display best fitness along the iteration
if (Iteration%1==0):
print(['At iteration '+ str(Iteration)+ ' the best fitness is '+ str(FoodFitness)]);
Convergence_curve[Iteration]=FoodFitness
Iteration=Iteration+1;
timerEnd=time.time()
s.endTime=time.strftime("%Y-%m-%d-%H-%M-%S")
s.executionTime=timerEnd-timerStart
s.convergence=Convergence_curve
s.optimizer="SSA"
s.objfname=objf.__name__
return s