Skip to content

Commit c7ed012

Browse files
committed
Fireworks Algorithm
1 parent d3a45d8 commit c7ed012

File tree

1 file changed

+3
-27
lines changed

1 file changed

+3
-27
lines changed

FA.py

+3-27
Original file line numberDiff line numberDiff line change
@@ -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=
4040
self.X = self.X.tolist()
4141
self.Y = [self.func(self.X[i]) for i in range(len(self.X))] # y = f(x) for all particles
4242

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)]
4743
self.bestindex = self.Y.index(min(self.Y))
4844
self.gbest_x = self.X[self.bestindex]
4945
self.gbest_y = min(self.Y) # global best y for all particles
5046
self.gbest_y_hist = [self.gbest_y] # gbest_y of every iteration
5147
# self.update_gbest()
5248

5349

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-
7450
def CalculateSi(self):
7551
self.MaxFitness = max(self.Y)
7652
temp = 0.
@@ -177,12 +153,12 @@ def run(self):
177153
return self.gbest_x, self.gbest_y_hist[-1]
178154

179155
if __name__ == '__main__':
180-
n_dim = 30
156+
n_dim = 3
181157
lb = [-100 for i in range(n_dim)]
182158
ub = [100 for i in range(n_dim)]
183159
demo_func = test_function.fm2
184-
pop_size = 100
185-
max_iter = 200
160+
pop_size = 10
161+
max_iter = 20
186162
fa = FA(n_dim=n_dim, pop_size=pop_size, max_iter=max_iter, lb=lb, ub=ub, func=demo_func)
187163
best_x, bext_y = fa.run()
188164
print(f'{demo_func(fa.gbest_x)}\t{fa.gbest_x}')

0 commit comments

Comments
 (0)