In this lesson, we will study MATLAB Code for Firefly Algorithm. As we know, the Firefly Algorithm (FA) is a nature-inspired optimization algorithm that simulates the behavior of fireflies. Fireflies are bioluminescent insects that emit light to attract mates and communicate with each other. The FA uses this behavior to find optimal solutions to complex problems, as discussed in the previous lessons (see Introduction to Firefly Algorithm). In the FA, each firefly represents a potential solution to the problem. The fireflies are randomly initialized in the search space, and their light intensity is proportional to their fitness. The fireflies then move toward each other, with the brighter fireflies attracting the dimmer ones. This process continues until the fireflies converge on the best solution. Here we are giving detailed working for MATLAB code for Firefly algorithm. Code can be reused and modified for your specific purposes.
function result = sphere_func(x)
% x is a vector of input values
% result is the value of the sphere function at x
% Compute the sum of squares of elements in x
result = sum(x.^2);
end
% Firefly Algorithm
clc;
clear all;
% Firefly Algorithm Parameters
pop_size = 50;
num_vars = 30;
lb = -100;
ub = 100;
num_generations = 1000;
alpha = 0.2; % Randomness factor
beta0 = 1; % Attractiveness at r = 0
gamma = 0.5; % Light absorption coefficient
% Initialize population and light intensity
population = lb + (ub - lb) * rand(pop_size, num_vars);
intensity = zeros(pop_size, 1);
for i = 1:pop_size
intensity(i) = sphere_func(population(i, :));
end
% Main loop
for gen = 1:num_generations
for i = 1:pop_size
for j = 1:pop_size
if intensity(i) > intensity(j) % Firefly i is attracted to brighter (lower fitness) firefly j
% Calculate distance between fireflies i and j
r = norm(population(i, :) - population(j, :));
% Attractiveness
beta = beta0 * exp(-gamma * r^2);
% Update firefly i towards j
new_position = population(i, :) + ...
beta * (population(j, :) - population(i, :)) + ...
alpha * (rand(1, num_vars) - 0.5);
% Ensure boundaries
new_position = max(new_position, lb);
new_position = min(new_position, ub);
% Update intensity for the new position
new_intensity = sphere_func(new_position);
% Greedy selection
if new_intensity < intensity(i)
population(i, :) = new_position;
intensity(i) = new_intensity;
end
end
end
end
% Display current best intensity
best_intensity = min(intensity);
fprintf('Generation %d: Best Intensity = %f\n', gen, best_intensity);
end
% Final evaluation
best_solution = population(intensity == min(intensity), :);
fprintf('Final Best Intensity = %f\n', best_intensity);
disp('Best Solution:');
disp(best_solution);
If you’re looking for Python implementation of the Firefly Algorithm, click here. Interested in reading more about the implementation details of the Algorithm, see Implementation of Firefly Algorithm. Also, check out the book chapter on Firefly Algorithm here.
Leave your comments below in case of any further assistance. Share with your friends and loved ones to support us!