-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFireFly_Alogrithm.py
59 lines (49 loc) · 1.49 KB
/
FireFly_Alogrithm.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
'''
@uthor : Amit Nandi
Date : 10/11/2016
Firefly Alogrithm
'''
#Rosenbrock function
def f(x,a=1,b=100):
return (a-x[0])**2+b*(x[1]-x[0]**2)**2
#Function to compute Intensity
def intensity(trialSolution,f):
import numpy as n
I = n.empty(trialSolution.shape[0])
for i in range(trialSolution.shape[0]):
I[i] = f(trialSolution[i])
return I
#Function to compute Euclidean distance
def dist(x,y):
import numpy as n
tmp = 0
for i in range(x.shape[0]):
tmp = tmp + (x[i]-y[i])**2
return n.sqrt(tmp)
#Firefly algorithm function
def FA(f,minRange,maxRange,maxItr=1000,nFireflies=25,gamma=1,beta0=2,alpha=0.2,delta=0.1,m=2):
import numpy as n
trialSolution = n.empty([nFireflies,len(minRange)])
for i in range(nFireflies):
for j in range(len(minRange)):
trialSolution[i][j] = n.random.uniform(minRange[j],maxRange[j])
itr = 0
I = intensity(trialSolution,f)
while itr!=maxItr:
for i in range(nFireflies):
for j in range(i+1):
if I[j]<I[i]:
r = dist(trialSolution[i],trialSolution[j])
beta = beta0*n.exp(-gamma*r**m)
e = delta*n.random.uniform(-1,1,size=len(minRange))
tmp = beta*n.dot(n.random.uniform(-1,1,size=(len(minRange),len(minRange))),trialSolution[i]-trialSolution[j])
trialSolution[i] = trialSolution[i] + alpha*e + tmp
I = intensity(trialSolution,f)
itr = itr + 1
bstValue = min(I)
bstSol = trialSolution[n.where(I==min(I))]
return list((bstValue,bstSol))
#Main
minRange = [0.5,0.5]
maxRange = [1.5,1.5]
a = FA(f,minRange,maxRange)