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 presenting the Python Code for the Firefly algorithm. The objective function in the given code can be modified and updated according to your requirements.
import numpy as np
# Firefly Algorithm
# Objective function (replace with your own function)
def objective_function(x):
return np.sum(x**2)
# Problem dimension
dimension = 10
# Search space bounds
lower_bound = -5
upper_bound = 5
# Population size
population_size = 50
# Maximum number of iterations
max_iterations = 100
# Initialization
population = lower_bound + (upper_bound - lower_bound) * np.random.rand(population_size, dimension)
fitness = np.apply_along_axis(objective_function, 1, population)
# Main loop
for iteration in range(max_iterations):
# Move fireflies towards brighter ones
alpha = 0.2 # Attraction coefficient
beta = 1 # Absorption coefficient
gamma = 1 # Randomization parameter
for i in range(population_size):
for j in range(population_size):
if fitness[j] < fitness[i]:
distance = np.linalg.norm(population[i] - population[j])
attractiveness = np.exp(-gamma * distance**2)
population[i] += alpha * attractiveness * (population[j] - population[i]) + beta * (np.random.rand(dimension) - 0.5)
# Limit the updated positions within the search space
population[i] = np.maximum(lower_bound, population[i])
population[i] = np.minimum(upper_bound, population[i])
# Evaluate fitness of the updated population
fitness = np.apply_along_axis(objective_function, 1, population)
# Update the best solution and fitness
best_index = np.argmin(fitness)
best_solution = population[best_index]
best_fitness = fitness[best_index]
# Display the best fitness value at each iteration
print(f"Iteration {iteration+1}, Best Fitness = {best_fitness}")
# Display the final best solution and fitness
print("-------------------")
print("Optimization Results")
print("-------------------")
print("Best Solution:", best_solution)
print("Best Fitness:", best_fitness)
In the above Python Code for the Firefly Algorithm, one can modify it according to their optimization requirements. In case of any doubts or suggestions, leave your comment below in the comment box. Enjoyed Reading. Consider sharing it with your friends and loved ones to support us! Happy Learning!
If you’re interested in reading the research paper related to the applications of Firefly algorithm in various domains, check out the following link.