1 of 21

The Firefly Algorithm� Dharmik Trivedi�

2 of 21

The Firefly Algorithm:

​

In this algorithm behavior of fireflies seen in the nature is used to optimize a function in given domain. The data points of the function to be optimized is considered as a firefly in a given domain.

Certain Assumptions are made which are as following:

​

Assumptions:

​

  • Fireflies are unisex, one firefly will be attracted to other fireflies regardless of their sex. In other words one firefly can be attracted to any other firefly in given domain(space).

​

  • The attractiveness is proportional to the brightness of the firefly. They both decrease as their distance increases. Thus, for any two flashing fireflies, the less bright one will move towards the brighter one. If for a firefly there is no brighter one than that particular firefly, it will move randomly.

​

  • The brightness of a firefly is determined by the objective function.

​

3 of 21

 

4 of 21

 

5 of 21

Sample Code for Firefly Algorithm:

​

​

% taking x1 and x2 randomly within the domain

x1=lb+rand([n 1]).*(ub-lb);

x2=lb+rand([n 1]).*(ub-lb);

fun=((x1.^2)+x2-11).^2+(x1+(x2.^2)-7).^2; % Himmelblau Function

x=[x1 x2]; % n data points

for c=1:k %Number of iterations

for i=1:n

for j=1:n

if fun(i)>fun(j)

a=x(i,:);b=x(j,:);

r=sqrt((a(1)-b(1))^2+(a(2)-b(2))^2); % distance between i and j

A=a+B0*exp(-gamma*r).*(b-a)+[(a0*delta*(rand-0.5)*L),(a0*delta*(rand-0.5)*L)];

x(i,:)=A;

x1(i)=x(i,1);

x2(i)=x(i,2);

fun=((x1.^2)+x2-11).^2+(x1+(x2.^2)-7).^2;

end

end

end

end

​

6 of 21

Booth Function solved by Firefly Algorithm

 

x1

x2

fun

n=5 t=100

1.0233

2.9918

0.0015

1.0596

2.9635

0.007

0.9258

3.0918

0.0152

 

 

n=8 t=100

1.0631

2.9687

0.009

0.9416

3.0097

0.013

1.0261

2.9926

0.0021

 

 

n=15 t=100

1.0098

3.006

0.0011

1.0299

2.971

0.0017

0.0094

2.9942

1.72E-04

 

 

n=5 t=10000

0.9885

3.0106

2.49E-04

0.9895

3.0105

2.20E-04

0.9963

3

6.74E-05

0.9992

2.9999

3.91E-06

Analtic :

1

3

0.00E+00

Complexity of the algorithm:

​

​

​

 

Booth Function

7 of 21

The Hybrid Algorithm:

​

As it can be seen from the table that for even after 10000 iterations we can expect accuracy of order 1e-5. To increase the accuracy we can compile gradient methods with this algorithm. After a fair amount of iterations the Firefly algorithm will give the data pouts that are in the vicinity of the actual solution. Now this point can be used as the initial guess for the gradient method and it will give more accurate results.

​

​

Advantages:

​

  • The time required is reduced.

​

  • The accuracy of the solution is reduced.

​

​

A multimodal function may have global minima at multiple points but the algorithm will give only one solution in the end. This can be remedy by using the algorithm multiple times and comparing the solutions.

​

​

​

8 of 21

Solved Examples:

1. Ackley Function:

 

9 of 21

10 of 21

2. Bird Function:

 

11 of 21

12 of 21

3. Levi’s Function:

 

13 of 21

14 of 21

4. McCormik Function:

 

15 of 21

16 of 21

5.Himmelblau’s Function:

​

 

17 of 21

18 of 21

19 of 21

20 of 21

 

21 of 21

Why Firefly algorithm is efficient ?

​

​

  • Firefly algorithm is based on attraction and attractiveness. This leads to the fact that the whole population can automatically subdivide into subgroups, and each group can swarm around each mode or local optimum. Among all these modes, the best global solution can be found.

​

​

  • This subdivision allows the fireflies to be able to find all optima simultaneously, if the population size is sufficiently higher than the number of modes.

​

​

  • The automatic subdivision ability of FA is particularly suitable for highly nonlinear, multimodal optimization problems.