Artificial Bee Colony (ABC) for Solving the Traveling Salesman Problem (TSP)
This project implements an Artificial Bee Colony (ABC) algorithm for solving the Traveling Salesman Problem (TSP), where the goal is to find the shortest possible route that visits a set of cities exactly once and returns to the starting point.
Table of Contents
Author and contact
Author: Angel Sanz Gutierrez
Contact: sanzangel017@gmail.com
GitHub: AngelS017
License: MIT License
Version History
[v 2.0.2] - 2024-10-24
- Changes on how to pass the hyperparameters for the different employed and onlooker bee mutation strategies.
- Eliminated the search for unnecessary hyperparameter combinations from the ABC algorithm thanks to the new hyperparameter step in the mutation strategies.
- Updated documentation (README and comments on functions)
[v 2.0.0] - 2024-09-17
- Added new functionality in ArtificialBeeColonyOptimizer: grid_search_abc()
- Updated the minimum required Python version to 3.8
[v 1.0.3] - 2023-09-14
- Initial release of the Artificial Bee Colony for TSP,
Overview
The Artificial Bee Colony (ABC) algorithm is a bio-inspired optimization technique modeled after the intelligent foraging behavior of honeybees. This algorithm is highly effective for solving complex optimization problems like the Traveling Salesman Problem (TSP), where the objective is to find the shortest path that visits all cities exactly once and returns to the starting city.
In this implementation, bees are divided into three roles: employed bees, onlooker bees, and scout bees.
Each role contributes to exploring the search space and improving potential solutions.
-
Employed bees focus on exploitation new food sources (solutions).
-
Onlooker bees focus on selective exploitation, probabilistically select and improve upon the best-found solutions.
-
Scout bees introduce exploration by abandoning poor solutions.
This implementation provides a flexible ABC framework, allowing users to:
-
Customize the mutation strategies for exploring different paths.
-
Customize the porcentage of bees of each role (Employed and Onlooker).
-
Adjust the colony size, exploitation limits and the number of epochs to optimize the problem.
Algorithm Workflow
-
Initialization:
A population of bees (solutions) is randomly initialized. The population is divided into employed bees and onlooker bees.
Each employed bee is assigned a random TSP route, and the initial distance for each route is calculated.
-
Employed Bee Phase:
Employed bees explore new routes by applying mutation strategies.
If the new route is shorter, the employed bee updates its current route.
-
Onlooker Bee Phase:
Onlooker bees choose solutions based on the probability proportional to the solution’s quality (distance).
Onlookers explore new routes and improve upon the best ones found by employed bees.
-
Scout Bee Phase:
If an employed bee fails to find a better route for a predefined number of trials, it becomes a scout bee and is reinitialized with a new random route.
-
Convergence:
The algorithm is executed for a defined number of epochs, constantly trying to improve the routes and recording the best solution found.
Folder Structure
ArtificialBeeColony_TSP
├── abc_tsp
| ├── ArtificialBeeColony_TSP.py
| └── __init__.py
├── README.md
├── LICENSE
└── setup.py
Prerequisites
The following open source packages are used to develop this algorithm:
Additionally, the software requires the Python 3.8 or higher version, as this is the minimum version supported by the tqmd packages.
However, it is recommended that the most recent versions of Numpy and Python be installed in order to achieve optimal performance and the fastest results.
Installation
To install the ABC algorithm for the TSP, you just need to:
pip install abc-tsp
Classes and Functions
Usage
To use this implementation of the Artificial Bee Colony (ABC) algorithm for solving TSP, follow the basic structure provided below:
from abc_tsp import ArtificialBeeColonyOptimizer
distance_matrix = np.array([...])
abc_optimizer = ArtificialBeeColonyOptimizer(
ini_end_city=0,
population=15,
employed_percentage=0.5,
limit=2000,
epochs=60000,
distance_matrix=distance_matrix,
employed_mutation_strategy='k_opt',
onlooker_mutation_strategy='k_opt',
mutation_params = {'k_employed':5, 'k_onlooker':5},
seed=1234,
verbose=1
)
execution_time, paths_distances, final_best_path, final_best_path_distance = abc_optimizer.fit()
print(f"Best Path: {final_best_path}")
print(f"Best Path Distance: {final_best_path_distance}")
print(f"Execution Time: {execution_time} seconds")
Tuning Parameters
The ArtificialBeeColonyOptimizer class allows you to customize key parameters for tuning the algorithm:
- population
- employed_percentage
- limit
- epochs
- employed_mutation_strategy and onlooker_mutation_strategy
- mutation_params:
Depending on the data, you may need to search for the optimal values of these parameters in order to get the best result (probably taking into account the time and the path distance).
Contributing
If you wish to contribute, please fork the repository and create a pull request with a detailed description of the changes.
Contributions for adding new features, fixing bugs, or improving performance are welcome!
License
This project is licensed under the MIT License - see the LICENSE file for details.