1
+ import random
2
+ import math
3
+
4
+
5
+ class FireflyAlgorithm ():
6
+
7
+ def __init__ (self , D , NP , nFES , alpha , betamin , gamma , LB , UB , function ):
8
+ self .D = D # dimension of the problem
9
+ self .NP = NP # population size
10
+ self .nFES = nFES # number of function evaluations
11
+ self .alpha = alpha # alpha parameter
12
+ self .betamin = betamin # beta parameter
13
+ self .gamma = gamma # gamma parameter
14
+ # sort of fireflies according to fitness value
15
+ self .Index = [0 ] * self .NP
16
+ self .Fireflies = [[0 for i in range (self .D )]
17
+ for j in range (self .NP )] # firefly agents
18
+ self .Fireflies_tmp = [[0 for i in range (self .D )] for j in range (
19
+ self .NP )] # intermediate pop
20
+ self .Fitness = [0.0 ] * self .NP # fitness values
21
+ self .I = [0.0 ] * self .NP # light intensity
22
+ self .nbest = [0.0 ] * self .NP # the best solution found so far
23
+ self .LB = LB # lower bound
24
+ self .UB = UB # upper bound
25
+ self .fbest = None # the best
26
+ self .evaluations = 0
27
+ self .Fun = function
28
+
29
+ def init_ffa (self ):
30
+ for i in range (self .NP ):
31
+ for j in range (self .D ):
32
+ self .Fireflies [i ][j ] = random .uniform (
33
+ 0 , 1 ) * (self .UB - self .LB ) + self .LB
34
+ self .Fitness [i ] = 1.0 # initialize attractiveness
35
+ self .I [i ] = self .Fitness [i ]
36
+
37
+ def alpha_new (self , a ):
38
+ delta = 1.0 - math .pow ((math .pow (10.0 , - 4.0 ) / 0.9 ), 1.0 / float (a ))
39
+ return (1 - delta ) * self .alpha
40
+
41
+ def sort_ffa (self ): # implementation of bubble sort
42
+ for i in range (self .NP ):
43
+ self .Index [i ] = i
44
+
45
+ for i in range (0 , (self .NP - 1 )):
46
+ j = i + 1
47
+ for j in range (j , self .NP ):
48
+ if (self .I [i ] > self .I [j ]):
49
+ z = self .I [i ] # exchange attractiveness
50
+ self .I [i ] = self .I [j ]
51
+ self .I [j ] = z
52
+ z = self .Fitness [i ] # exchange fitness
53
+ self .Fitness [i ] = self .Fitness [j ]
54
+ self .Fitness [j ] = z
55
+ z = self .Index [i ] # exchange indexes
56
+ self .Index [i ] = self .Index [j ]
57
+ self .Index [j ] = z
58
+
59
+ def replace_ffa (self ): # replace the old population according to the new Index values
60
+ # copy original population to a temporary area
61
+ for i in range (self .NP ):
62
+ for j in range (self .D ):
63
+ self .Fireflies_tmp [i ][j ] = self .Fireflies [i ][j ]
64
+
65
+ # generational selection in the sense of an EA
66
+ for i in range (self .NP ):
67
+ for j in range (self .D ):
68
+ self .Fireflies [i ][j ] = self .Fireflies_tmp [self .Index [i ]][j ]
69
+
70
+ def FindLimits (self , k ):
71
+ for i in range (self .D ):
72
+ if self .Fireflies [k ][i ] < self .LB :
73
+ self .Fireflies [k ][i ] = self .LB
74
+ if self .Fireflies [k ][i ] > self .UB :
75
+ self .Fireflies [k ][i ] = self .UB
76
+
77
+ def move_ffa (self ):
78
+ for i in range (self .NP ):
79
+ scale = abs (self .UB - self .LB )
80
+ for j in range (self .NP ):
81
+ r = 0.0
82
+ for k in range (self .D ):
83
+ r += (self .Fireflies [i ][k ] - self .Fireflies [j ][k ]) * \
84
+ (self .Fireflies [i ][k ] - self .Fireflies [j ][k ])
85
+ r = math .sqrt (r )
86
+ if self .I [i ] > self .I [j ]: # brighter and more attractive
87
+ beta0 = 1.0
88
+ beta = (beta0 - self .betamin ) * \
89
+ math .exp (- self .gamma * math .pow (r , 2.0 )) + self .betamin
90
+ for k in range (self .D ):
91
+ r = random .uniform (0 , 1 )
92
+ tmpf = self .alpha * (r - 0.5 ) * scale
93
+ self .Fireflies [i ][k ] = self .Fireflies [i ][
94
+ k ] * (1.0 - beta ) + self .Fireflies_tmp [j ][k ] * beta + tmpf
95
+ self .FindLimits (i )
96
+
97
+ def Run (self ):
98
+ self .init_ffa ()
99
+
100
+ while self .evaluations < self .nFES :
101
+
102
+ # optional reducing of alpha
103
+ self .alpha = self .alpha_new (self .nFES / self .NP )
104
+
105
+ # evaluate new solutions
106
+ for i in range (self .NP ):
107
+ self .Fitness [i ] = self .Fun (self .D , self .Fireflies [i ])
108
+ self .evaluations = self .evaluations + 1
109
+ self .I [i ] = self .Fitness [i ]
110
+
111
+ # ranking fireflies by their light intensity
112
+ self .sort_ffa ()
113
+ # replace old population
114
+ self .replace_ffa ()
115
+ # find the current best
116
+ self .fbest = self .I [0 ]
117
+ # move all fireflies to the better locations
118
+ self .move_ffa ()
119
+
120
+ return self .fbest
0 commit comments