A comprehensive route optimization system implementing Dijkstra's Algorithm, A* Algorithm, and Genetic Algorithm for finding optimal paths. Features a Flask-based backend and an interactive web dashboard for real-time visualization and algorithm comparison.
-
Three Powerful Algorithms:
- Dijkstra's Algorithm: Classic shortest path algorithm
- A* Algorithm: Heuristic-based pathfinding with improved efficiency
- Genetic Algorithm: Evolutionary approach for multi-waypoint optimization
-
Interactive Dashboard:
- Real-time route visualization on interactive maps
- Side-by-side algorithm comparison
- Performance metrics and statistics
- Support for waypoints and intermediate stops
- Distance and time optimization modes
-
Advanced Features:
- Realistic road network simulation
- Dynamic graph generation
- Multiple optimization metrics
- Responsive design for all devices
Before you begin, ensure you have the following installed:
- Python 3 or later - Download Python
- pip (Python package installer) - Usually comes with Python
- A modern web browser (Chrome, Firefox, Safari, or Edge)
Create a new folder for your project and organize the files:
route-optimization/
βββ app.py
βββ algorithms.py
βββ graph_generator.py
βββ requirements.txt
βββ README.md
βββ templates/
β βββ index.html
βββ static/
βββ style.css
βββ script.js
Windows:
- Download Python from python.org
- Run the installer
- IMPORTANT: Check "Add Python to PATH" during installation
- Click "Install Now"
Open your terminal/command prompt and run:
python --version
# or
python3 --versionIf you see the Python version number, you have successfully installed Python.
# Create project folder
mkdir route-optimization
cd route-optimization
# Create necessary subdirectories
mkdir templates
mkdir staticCopy all the provided files to their respective locations:
app.py,algorithms.py,graph_generator.py,requirements.txtβ Root directoryindex.htmlβ templates/ folderstyle.css,script.jsβ static/ folder
Open terminal in the project directory and run:
# Install required packages
pip install -r requirements.txt
# Or if you're using pip3:
pip3 install -r requirements.txtThis will install:
- Flask (web framework)
- Flask-CORS (cross-origin resource sharing)
- NumPy (numerical computations)
- Requests (HTTP library)
- Geopy (geographical calculations)
- NetworkX (graph analysis)
- OSMnx (OSM data processing)
# Start the Flask server
python app.py
# Or if you're using python3:
python3 app.pyYou should see output like:
Graph initialized with 30 nodes
Starting Flask server...
Visit http://localhost:5000 in your browser
* Running on http://0.0.0.0:5000
- Open your web browser
- Navigate to:
http://localhost:5000 - You should see the Route Optimization Dashboard!
- Select Algorithm: Choose from Dijkstra, A*, or Genetic Algorithm
- Choose Optimization Metric: Distance (km) or Time (minutes)
- Select Nodes: Pick start and end points from dropdowns
- Find Route: Click "Find Route" button
- View Results: See the optimal path highlighted on the map with statistics
- Select "Genetic Algorithm" from the algorithm dropdown
- Waypoint section will appear automatically
- Click "+ Add Waypoint" to add intermediate stops
- Select desired waypoints from dropdowns
- Click "Find Route" to optimize multi-stop route
- Configure start and end nodes
- Click "Compare All" button
- All algorithms will run simultaneously
- Routes displayed in different colors:
- Blue: Dijkstra's Algorithm
- Green: A* Algorithm
- Purple: Genetic Algorithm
- View comparison table with detailed metrics
Click "Random Nodes" button to automatically select random start, end, and waypoint nodes for testing.
Click "Clear Map" to remove all routes and reset the view.
- Best for: Finding guaranteed shortest path
- Pros: Always finds optimal solution, well-tested
- Cons: Slower for large graphs, explores many nodes
- Use case: When you need the absolute shortest path
- Best for: Fast pathfinding with heuristics
- Pros: Faster than Dijkstra, still finds optimal path
- Cons: Requires coordinate data, slightly complex
- Use case: Real-time navigation, interactive systems
- Best for: Multi-waypoint route optimization
- Pros: Handles complex constraints, good for TSP-like problems
- Cons: May not find absolute optimum, slower execution
- Use case: Delivery routes, tour planning with multiple stops
Edit app.py to change graph generation:
def initialize_graph():
graph_data, positions_data = GraphGenerator.generate_city_graph(
num_nodes=30, # Change number of nodes
density=0.3, # Connection density (0-1)
city_center=(28.6139, 77.2090) # City coordinates
)In app.py, modify genetic algorithm settings:
path, cost, stats = optimizer.genetic_algorithm(
start, end, waypoints, metric,
population_size=50, # Population size
generations=100, # Number of generations
mutation_rate=0.2 # Mutation probability
)- Total Distance/Time: Overall cost of the route
- Path Length: Number of nodes in the route
- Nodes Explored: Number of nodes algorithm visited
- Execution Time: Time taken to compute route (milliseconds)
When comparing algorithms, the table shows:
- Algorithm name with color-coded badge
- Total cost (distance or time)
- Number of nodes in path
- Nodes explored during computation
- Execution time
1. "ModuleNotFoundError: No module named 'flask'"
# Solution: Install dependencies
pip install -r requirements.txt2. "Address already in use" or "Port 5000 is busy"
# Solution: Change port in app.py
app.run(debug=True, host='0.0.0.0', port=5001)3. Map not displaying
- Check your internet connection (map tiles need to load)
- Clear browser cache and reload
- Check browser console for errors (F12)
4. "Python not recognized"
- Python not added to PATH
- Reinstall Python with "Add to PATH" checked
- Or use full path:
C:\Python39\python.exe app.py
5. Blank page or styling issues
- Ensure
templates/andstatic/folders exist - Check file names match exactly (case-sensitive)
- Look for errors in terminal
Flask runs in debug mode by default, showing detailed errors. If you encounter issues:
- Check the terminal where Flask is running
- Look for error messages
- Check browser console (F12 β Console tab)
This is a development server suitable for:
- β Local testing
- β Learning and experimentation
- β Algorithm development
NOT suitable for:
- β Production deployment
- β Public internet exposure
- β Handling sensitive data
For production, use a proper WSGI server like Gunicorn or uWSGI.
- Reduce Nodes: Lower
num_nodesfor faster computation - Adjust Density: Lower density means fewer connections
- GA Parameters: Reduce
generationsorpopulation_sizefor faster GA - Disable Debug: Set
debug=Falsein production
Edit static/style.css:
:root {
--primary-color: #2563eb; /* Change main color */
--success-color: #10b981; /* Change success color */
}Edit static/script.js to change map provider:
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// Change to other tile providers like:
// 'https://tiles.stadiamaps.com/tiles/alidade_smooth/{z}/{x}/{y}{r}.png'
});- Dijkstra's Algorithm: Wikipedia
- A* Algorithm: Red Blob Games Tutorial
- Genetic Algorithms: Introduction to GA
- Flask Documentation: flask.palletsprojects.com
If you encounter issues:
- Check this README thoroughly
- Review error messages carefully
- Ensure all files are in correct locations
- Verify Python and package versions
- Check terminal output for clues
route-optimization/
β
βββ app.py # Flask web server and API endpoints
βββ algorithms.py # Implementation of all three algorithms
βββ graph_generator.py # Graph generation and utilities
βββ requirements.txt # Python dependencies
βββ README.md # This file
β
βββ templates/
β βββ index.html # Main dashboard HTML structure
β
βββ static/
βββ style.css # All styling and animations
βββ script.js # Frontend logic and map interactions
This project demonstrates:
- Algorithm implementation and comparison
- Graph theory concepts
- Web application development
- API design
- Data visualization
- Real-world problem solving
Perfect for:
- Computer Science students
- Algorithm enthusiasts
- Web developers learning backend
- Anyone interested in optimization
After getting it running:
- Experiment with different algorithms
- Try various graph sizes and densities
- Compare performance on different routes
- Modify parameters to see their effects
- Add your own features!
This project is released under the MIT License.
Happy Optimizing! πΊοΈβ¨
For questions or improvements, feel free to open an issue!