This repository contains a Python implementation of an Ant Colony Optimization (ACO) class and an example application solving the Traveling Salesman Problem (TSP).
ant_colony_optimization.py
: Contains theAntColonyOptimization
class, a flexible implementation of the ACO algorithm.aco_tsp_example.py
: Demonstrates how to use theAntColonyOptimization
class to solve a Traveling Salesman Problem.
- Python 3.6 or higher
- No additional libraries required
- Clone this repository:
git clone https://github.com/yourusername/aco-tsp-solver.git
cd aco-tsp-solver
- Run the TSP example:
python aco_tsp_example.py
This will execute the ACO algorithm to find an optimal solution for the TSP defined in the script.
To use the AntColonyOptimization
class for your own problems:
- Import the class:
from ant_colony_optimization import AntColonyOptimization
-
Define your distance function. It should take two city indices as input and return the distance between them.
-
Initialize the ACO:
aco = AntColonyOptimization(
num_ants=10,
num_cities=your_num_cities,
distance_func=your_distance_function,
alpha=1.0,
beta=2.0,
evaporation_rate=0.5,
q=100,
initial_pheromone=1.0
)
- Run the algorithm:
best_solution = aco.run(iterations=100)
- Interpret the result based on your problem domain.
- Modify the
alpha
,beta
,evaporation_rate
, andq
parameters to fine-tune the algorithm's performance. - Adjust the
num_ants
and number ofiterations
based on your problem's complexity and computational resources. - For problems other than TSP, you may need to modify the
construct_solution
andupdate_pheromone
methods in theAntColonyOptimization
class.
Contributions to improve the algorithm or add new features are welcome. Please feel free to submit pull requests or open issues for any bugs or enhancements.
This project is open-source and available under the MIT License.