Skip to content

OptiRouteX is a program designed to optimize and calculate the shortest possible route between specified locations using genetic algorithms.

Notifications You must be signed in to change notification settings

nehciyy/OptiRouteX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OptiRouteX

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.

Tech Stack

Python JavaScript Node.js Rollup.js HTML DEAP HERE Maps API HTTP Server

Features

  • 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.

File Structure and Descriptions

Root Directory

  • 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.

src Directory

  • 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.

data Directory

  • 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.

dist Directory

  • bundle.js: The bundled JavaScript file generated by Rollup.js, containing all the necessary JavaScript code for the frontend in a single, optimized file.

models Directory

  • 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 like distanceMeasure.js to dynamically calculate the best route.

Getting Started

1. Setting Up HERE API Credentials

  1. Obtain your HERE API credentials by signing up at HERE Developer Portal.
  2. Create a .env file in the root directory of the project.
  3. Add the following lines to your .env file:
    MAPS_API_KEY=your-here-apikey
    CODE=your-here-code
    ID=your-here-id

2. Setting Up the Virtual Environment

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:

Step 1: Create a 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.

Step 2: Activate the Virtual 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.

Step 3: Install the Required Dependencies

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.

3. Setting Up the Locations

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
    }
  ]
}

Important Notes:

  • Do Not Change the "origin" and "destination" Keys: The keys "origin" and "destination" are required by the program. Only change the lat, 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 corresponding lat and lng 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 Find latitude and longitude

4. Running the Program

After setting up the virtual environment and locations, you can run the program by following these steps:

Step 1: Start the Backend API (app.py)

In the first terminal (with the virtual environment activated), start the Flask server:

python app.py

Step 2: Bundle JavaScript with Rollup

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

Step 3: Run the Genetic Algorithm (algorithm.py)

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.

5. Deactivating the Virtual Environment

When you are done, you can deactivate the virtual environment by running:

deactivate

6. Retrieving Results

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.

Notes

  • 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.

References

Codes that form the functionalities of the map were all taken from HERE Maps API for JavaScript Developer Guide