Skip to content

Commit f25f15e

Browse files
authored
Update FireflyAlgorithm.py
1 parent 106ade6 commit f25f15e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

FireflyAlgorithm.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ def init_FA(self):
2323
for i in range(self.n):
2424
for j in range(self.D):
2525
self.populationArray[i][j] = rand.uniform(self.Lb, self.Ub)
26-
self.functionArray[i] = func(self.populationArray[i][0], self.populationArray[i][1])
26+
self.functionArray[i] = self.func(self.populationArray[i,:], self.D)
2727

2828
def update(self, i, j):
2929
scale = self.Ub - self.Lb
30-
r = (self.populationArray[i][0] - self.populationArray[j][0])**2 + (self.populationArray[i][1] - self.populationArray[j][1])**2
30+
r = 0
31+
for k in range(self.D):
32+
r += (self.populationArray[i][k] - self.populationArray[j][k])**2
3133
beta = self.beta0*math.exp(-self.gamma*r)
3234
for k in range(self.D):
3335
steps = (self.alpha*self.theta)*(rand.random() - 0.5)*scale
3436
self.tmpArray[k] = self.populationArray[i][k] + beta*(self.populationArray[j][k] - self.populationArray[i][k]) + steps
35-
if(func(self.tmpArray[0], self.tmpArray[1]) < self.functionArray[i]):
37+
if(self.func(self.tmpArray, self.D) < self.functionArray[i]):
3638
for k in range(self.D):
3739
self.populationArray[i][k] = self.tmpArray[k]
38-
self.functionArray[i] = func(self.tmpArray[0], self.tmpArray[1])
40+
self.functionArray[i] = self.func(self.tmpArray, self.D)
3941

4042
def doRun(self):
4143
self.init_FA()
@@ -45,5 +47,6 @@ def doRun(self):
4547
for j in range(self.n):
4648
if(self.functionArray[i] > self.functionArray[j] and i != j):
4749
self.update(i,j)
50+
print(self.populationArray)
4851
print(self.functionArray)
4952
return self.functionArray.min()

0 commit comments

Comments
 (0)