Artificial Bee Colony
Optimization(ABC)
Name – Adarsh Godia
UID- 22MSM40206
What is ABC?
• ABC is an optimization algorithm.
• Problem Solving method inspired by the behaviour of the Honeybees.
• In this algorithm, a colony of artificial bees work together to find the best solution to an
optimization problem.
• The bees communicate with each other to share information about the quality of potential
solutions and explore new ones.
Note:- Food
Bees live in a colony.
Bees leave the colony in search of food.
Why do we care ?
• Bees are solving an optimization problem.
• The optimization problem is finding the best food source.
• Turns out : Bees are pretty good at finding the optimum
solution/location of the food.
• Learning from their methods seems like a good idea for solving
our own optimization problem.
How Bees optimize food search?
value 3
value 3 value 3
value 3 value 3 value 3
ABC Algorithm
Pseudo Code
Python code for ABC
import random
import numpy as np
# Initialize the problem parameters
n = 50 # number of employed bees
m = 20 # number of onlooker bees
d = 30 # dimension of the problem
lb = -5.12 # lower bound of the search space
ub = 5.12 # upper bound of the search space
max_iterations = 10 # maximum number of iterations
# Initialize the population
population = np.random.uniform(lb, ub, (n, d))
# Evaluate the fitness of the population
fitness = np.zeros(n)
for i in range(n):
x = population[i]
fitness[i] = np.sum(x ** 2)
# Initialize the best solution
best_solution = population[np.argmin(fitness)]
best_fitness = np.min(fitness)
# Start the iterations
for t in range(max_iterations):
# Employed bees phase
for i in range(n):
# Choose a random dimension
j = random.randint(0, d - 1)
# Choose a random neighbor
k = random.choice([x for x in range(n) if x != i])
# Generate a new solution
new_solution = np.copy(population[i])
phi = np.random.uniform(-1, 1)
new_solution[j] = population[i][j] + phi * (population[i][j] - population[k]
[j])
# Evaluate the fitness of the new solution
new_fitness = np.sum(new_solution ** 2)
# Update the population if the new solution is better
if new_fitness < fitness[i]:
population[i] = new_solution
fitness[i] = new_fitness
# Update the best solution if the new solution is even better
if new_fitness < best_fitness:
best_solution = new_solution
best_fitness = new_fitness
# Onlooker bees phase
fitness_sum = np.sum(fitness)
for i in range(m):
# Choose a solution based on the fitness values
j = random.choices(range(n),
weights=fitness/fitness_sum)[0]
# Choose a random dimension
k = random.randint(0, d - 1)
# Choose a random neighbor
l = random.choice([x for x in range(n) if x != j])
# Generate a new solution
new_solution = np.copy(population[j])
phi = np.random.uniform(-1, 1)
new_solution[k] = population[j][k] + phi *
(population[j][k] - population[l][k])
# Update the population if the new solution is better
if new_fitness < fitness[j]:
population[j] = new_solution
fitness[j] = new_fitness
# Update the best solution if the new solution is even better
if new_fitness < best_fitness:
best_solution = new_solution
best_fitness = new_fitness
# Scout bees phase
for i in range(n):
# Replace a solution with a new random one if it has not been improved in a while
if fitness[i] >= best_fitness:
population[i] = np.random.uniform(lb, ub, d)
fitness[i] = np.sum(population[i] ** 2)
print("Best solution:", best_solution)
print("Best fitness:", best_fitness)
OUTPUT
THANK YOU