@@ -40,37 +40,13 @@ def __init__(self, pop_size=50, n_dim=2, m=50, a=0.04, b=0.8, A=40, lb=-1e5, ub=
40
40
self .X = self .X .tolist ()
41
41
self .Y = [self .func (self .X [i ]) for i in range (len (self .X ))] # y = f(x) for all particles
42
42
43
- # self.pbest_x = self.X.copy() # personal best location of every particle in history
44
- # self.pbest_y = [np.inf for i in range(self.pop)] # best image of every particle in history
45
- # self.fit = [1 / (1 + self.Y[i]) if self.Y[i] > 0 else 1 - self.Y[i] for i in range(self.pop)]
46
- # self.prob = [self.fit[i] / sum(self.fit) for i in range(self.pop)]
47
43
self .bestindex = self .Y .index (min (self .Y ))
48
44
self .gbest_x = self .X [self .bestindex ]
49
45
self .gbest_y = min (self .Y ) # global best y for all particles
50
46
self .gbest_y_hist = [self .gbest_y ] # gbest_y of every iteration
51
47
# self.update_gbest()
52
48
53
49
54
- # def update_pbest(self):
55
- # '''
56
- # personal best
57
- # :return:
58
- # '''
59
- # for i in range(len(self.Y)):
60
- # if self.pbest_y[i] > self.Y[i]:
61
- # self.pbest_x[i] = self.X[i]
62
- # self.pbest_y[i] = self.Y[i]
63
- #
64
- # def update_gbest(self):
65
- # '''
66
- # global best
67
- # :return:
68
- # '''
69
- # idx_min = self.pbest_y.index(min(self.pbest_y))
70
- # if self.gbest_y > self.pbest_y[idx_min]:
71
- # self.gbest_x = self.X[idx_min, :].copy()
72
- # self.gbest_y = self.pbest_y[idx_min]
73
-
74
50
def CalculateSi (self ):
75
51
self .MaxFitness = max (self .Y )
76
52
temp = 0.
@@ -177,12 +153,12 @@ def run(self):
177
153
return self .gbest_x , self .gbest_y_hist [- 1 ]
178
154
179
155
if __name__ == '__main__' :
180
- n_dim = 30
156
+ n_dim = 3
181
157
lb = [- 100 for i in range (n_dim )]
182
158
ub = [100 for i in range (n_dim )]
183
159
demo_func = test_function .fm2
184
- pop_size = 100
185
- max_iter = 200
160
+ pop_size = 10
161
+ max_iter = 20
186
162
fa = FA (n_dim = n_dim , pop_size = pop_size , max_iter = max_iter , lb = lb , ub = ub , func = demo_func )
187
163
best_x , bext_y = fa .run ()
188
164
print (f'{ demo_func (fa .gbest_x )} \t { fa .gbest_x } ' )
0 commit comments