OptiRouteX is a program designed to optimize and calculate the shortest possible route between specified locations using genetic algorithms. The program uses latitude and longitude coordinates for various waypoints, an origin, and a destination to determine the optimal path.
- Dynamic Route Optimization: Calculates the shortest possible route between the origin, destination, and waypoints using genetic algorithms.
- Real-Time Map Rendering: Visualizes the optimized route on an interactive map interface.
- Real-Time Traffic Integration: Incorporates real-time traffic data into the route optimization process, dynamically adjusting the route to avoid congested areas.
- Waypoint Management: Easily customize and manage waypoints through a JSON configuration file.
app.py
: This file acts as the main API server for the application, handling requests related to the route optimization process.index.html
: The main HTML file that renders the frontend interface where users can interact with the map and see the optimized route.rollup.config.js
: Configuration file for Rollup.js, which is used to bundle the JavaScript modules into a single file for use in the application.
config.js
: Contains configuration settings, such as API keys and other constants, required by the JavaScript components of the application.distanceMeasure.js
: Handles the logic for measuring distances between waypoints, origin, and destination. This is a core component that interacts with the genetic algorithm.index.js
: The main JavaScript entry point for the frontend, responsible for initializing the map and setting up interactions with other components.mapSingleton.js
: Implements a singleton pattern for managing the map instance, ensuring that only one instance of the map exists at any time.
bestRoute/
: Contains the best-optimized route file, saved after the algorithm completes its execution.generationResults/
: Stores detailed results of each generation during the route optimization process, providing insights into the evolution of the algorithm.evaluation.ipynb
: A Jupyter notebook for evaluating and visualizing the performance of the genetic algorithm, useful for analysis and debugging.
bundle.js
: The bundled JavaScript file generated by Rollup.js, containing all the necessary JavaScript code for the frontend in a single, optimized file.
locations.json
: The configuration file where all location data (origin, destination, and waypoints) is stored. This file is used by the program to perform route calculations.algorithm.py
: The core Python script that implements the genetic algorithm for route optimization, interacting with other components likedistanceMeasure.js
to dynamically calculate the best route.
- Obtain your HERE API credentials by signing up at HERE Developer Portal.
- Create a
.env
file in the root directory of the project. - Add the following lines to your
.env
file:MAPS_API_KEY=your-here-apikey CODE=your-here-code ID=your-here-id
To ensure that all dependencies are managed properly, it is recommended to use a virtual environment. Follow these steps to set up and activate your virtual environment:
In the root directory of your project, run the following command to create a virtual environment:
python -m venv venv
This will create a new directory called venv
containing the Python interpreter and all the packages installed for this environment.
Before running your program, you need to activate the virtual environment.
-
On macOS/Linux:
source venv/bin/activate
-
On Windows:
venv\Scripts\activate
Your command prompt will change to indicate that the virtual environment is active.
Once the virtual environment is activated, install the necessary dependencies from the requirements.txt
file:
pip install -r requirements.txt
This ensures that all required packages are installed in your virtual environment.
Ensure that the locations are properly set up in the JSON file located at ./models/locations.json
. The JSON file should follow this structure:
{
"origin": {
"location": "Suntec City",
"lat": 1.292982,
"lng": 103.857003
},
"destination": {
"location": "Bugis Junction",
"lat": 1.300639,
"lng": 103.854837
},
"waypoints": [
{
"location": "Merlion",
"lat": 1.28668,
"lng": 103.853607
},
{
"location": "313 Somerset",
"lat": 1.301114,
"lng": 103.838872
},
{
"location": "Marina Bay Sands",
"lat": 1.28437,
"lng": 103.8599
}
]
}
-
Do Not Change the "origin" and "destination" Keys: The keys
"origin"
and"destination"
are required by the program. Only change thelat
,lng
, and//
values under these keys to update the coordinates and description. -
Waypoints Formatting: For the waypoints, you can customize the name of each waypoint from
location
(e.g.,"Merlion"
,"313 Somerset"
) and provide the correspondinglat
andlng
values. Ensure that each waypoint follows this format.
You can obtain the latitude and longitude coordinates from findlatitudeandlongitude for coordinates.
Here is where you can retrieve the latitude and longitude
After setting up the virtual environment and locations, you can run the program by following these steps:
In the first terminal (with the virtual environment activated), start the Flask server:
python app.py
In a second terminal (also with the virtual environment activated), run the following command to bundle your JavaScript files:
npm run bundle
Subsequently, start a simple HTTP server to serve the frontend:
python -m http.server 8000
In a third terminal (with the virtual environment activated), run the genetic algorithm script:
python models/algorithm.py
Ensure all three terminals are running simultaneously, as they are interdependent.
When you are done, you can deactivate the virtual environment by running:
deactivate
After running the program, you can find the following results stored in the data
folder:
- Best Route: The best optimized route calculated by the algorithm.
- Generation Results: Detailed results for each generation during the route optimization process.
- Ensure all configurations and settings are correctly set before running the program.
- Regularly update the
locations.json
file to reflect any changes in your waypoints or geographical data.
Codes that form the functionalities of the map were all taken from HERE Maps API for JavaScript Developer Guide