This is a Python-based visualization tool for the Ant Colony Optimization algorithm applied to the Traveling Salesman Problem (TSP). The application provides an interactive GUI to visualize how ants find the shortest path connecting all cities.
- Interactive visualization of ACO algorithm in real-time
- Support for both Euclidean and geographic distances
- Adjustable parameters (alpha, beta, decay rate, etc.)
- Manual city placement or random generation
- Loading cities from CSV or JSON files
- Saving city configurations
- Step-by-step or continuous animation
- Pheromone trail visualization
- Clone the repository or download the source code
- Install required packages:
pip install -r requirements.txtpython aco.pypython aco.py --cities 20 --ants 30 --load european_cities.csv --save output.csvParameters:
--cities: Number of random cities to generate (default: 20)--ants: Number of ants to use (default: 20)--load: Load cities from a file--save: Save the final city configuration to a file
The application supports loading cities from CSV or JSON files.
For geographic coordinates, the CSV should contain columns for:
- City name
- Latitude
- Longitude
- Country (optional)
Example with european_cities.csv:
city,lat,lng,country
London,51.5074,0.1278,UK
Paris,48.8566,2.3522,France
Berlin,52.5200,13.4050,Germany
...To load this file:
- Click the "Load Cities" button
- Select your CSV file
- The application will automatically detect geographic coordinates and calculate distances using the Haversine formula
The application supports different JSON formats:
- Geographic coordinates:
[
{"name": "London", "lat": 51.5074, "lng": 0.1278, "country": "UK"},
{"name": "Paris", "lat": 48.8566, "lng": 2.3522, "country": "France"}
]- Screen coordinates:
[
{"x": 100, "y": 200},
{"x": 300, "y": 400}
]- Start/Pause: Begin or pause the simulation
- Step: Run a single iteration of the ACO algorithm
- Load Cities: Load city data from a file
- Save Cities: Save current city configuration
- Reset: Reset the simulation with current parameters
- Add Cities Manually: Enter manual city placement mode
- Clear Cities: Remove all cities
- Run to Target: Run the simulation until reaching the target iteration
- Reset Iteration: Reset the iteration counter
- Apply Parameters: Apply any parameter changes
- Toggle Pheromones: Show or hide pheromone trails
- save image as png: Save image
- Animation Speed: Control the simulation speed (fps)
- Cities: Number of cities (when regenerating)
- Ants: Number of ants in the simulation
- Alpha: Pheromone weight in path selection
- Beta: Distance weight in path selection
- Decay: Pheromone evaporation rate
- Target Iteration: Set the target iteration for "Run to Target"
- Alpha (α): Controls the influence of pheromones on path selection. Higher values make ants more likely to follow paths with stronger pheromone trails.
- Beta (β): Controls the influence of distance on path selection. Higher values make ants prefer shorter paths.
- Decay Rate: Controls how quickly pheromones evaporate. Lower values make the algorithm explore more.
- Number of Ants: More ants can explore more paths but may slow down the simulation.
- Use "Add Cities Manually" to create a custom city configuration
- For geographic data, load a CSV with latitude and longitude information
- Adjust parameters to see how they affect the solution quality
- Use "Step" to see the algorithm progress iteration by iteration
- Watch the pheromone trails grow stronger on better paths
- The best path found so far is shown in black
- Download the
european_cities.csvfile with major European cities - Run the program:
python aco.py - Click "Load Cities" and select
european_cities.csv - The cities will appear on the map with geographic scaling
- Click "Apply Parameters" to initialize the ACO algorithm with these cities
- Click "Start" to run the algorithm and find the shortest route
The best path found by the ACO algorithm will be displayed on the screen and can be saved using keyword 'S' or using save image as png button . You can save the city configuration and coordinates by clicking "Save Cities".
You can modify the following constants in the code:
SCREEN_WIDTHandSCREEN_HEIGHT: Change the window size- Colors:
BG_COLOR,CITY_COLOR,ANT_COLOR, etc. FONT_SIZE: Change the text size
This project is provided for educational purposes.
