This article was published as a part of the Data Science Blogathon.
Introduction
Travel planning can be a daunting task, especially when it comes to finding the most cost-effective and efficient itinerary. It can be frustrating to spend hours comparing prices and distances, only to find that the options available don’t quite meet your needs. One of the main challenges of travel planning is finding the right balance between cost and distance. Some modes of transportation may be cheaper but may also take longer, adding to the overall travel time.
On the other hand, faster modes of transportation, such as planes, can be much more expensive. Many people have limited budgets for their trips, and the cost of transportation, accommodation, and activities can quickly add up. Finding ways to save money on travel can be a top priority for many people, but it can be difficult to find affordable options that still meet your needs.
In this article, we will help you in making a tool that can record real-time data and help you create a well planned travel itinerary. So, let’s begin!
Making Your Optimal Travel Itinerary
To address these challenges, it is essential to have a tool that can help you find the optimal itinerary for your trip. In this article, we will attempt to build a tool that creates optimal travel itineraries by minimizing both the cost and distance traveled, ensuring that your journey is as smooth and affordable as possible. Using the Google Maps API, this tool can fetch real-time distance data and combine it with a pre-defined list of transportation options and their corresponding costs. This allows you to select your starting city and builds an itinerary based on that allowing you to explore what combinations of travel sequence minimize your cost. In the following sections, we will detail the code and discuss how it can be used to make travel planning a breeze.
Building the Tool | Example and Approach
Suppose we want to travel across four cities – Delhi, Mumbai, Chennai, and Kolkata. We start by importing the necessary libraries, defining the list of cities we wish to travel to, and generating the distance between their different combinations using the Google Maps API.
# Enter your Google Maps API key here API_KEY = "Your API Key" # Create a Google Maps client gmaps = googlemaps.Client(key=API_KEY)
The code then creates an empty list called distances to store the distance between each combination of cities. It then uses a nested for loop to iterate through each combination of cities. If the two cities are different, it uses the Google Maps client to determine the distance between them. It appends this distance, along with the names of the two cities, to the list of distances. After the for loop has been completed, the code creates pandas dataframe from the list of distances, with columns From City, To City, and Distance.
# Create an empty list to store the distance between each combination of cities distances = [] # Loop through each combination of cities for i in range(len(cities)): for j in range(len(cities)): if i != j: # Use the Google Maps client to get the distance between the two cities distance = gmaps.distance_matrix(cities[i], cities[j]) # Append the distance to the list distances.append([cities[i], cities[j], distance['rows'][0]['elements'][0]['distance']['value']/1000]) # Create a dataframe from the list of distances df = pd.DataFrame(distances, columns=['From City', 'To City', 'Distance']) # Print the dataframe df
We get the following output
We have considered four modes that can be used for transportation across these cities – bus, plane, train and car. For demonstration, we
will assume random costs associated with the travel across these cities via each transportation mode. Sharing the markdown version of the table below.
|index|From City|To City|Distance|Transportation Mode|Cost|
|—|—|—|—|—|—|
|0|Delhi|Mumbai|1398|bus|1000|
|1|Delhi|Mumbai|1398|plane|4000|
|2|Delhi|Mumbai|1398|train|1200|
|3|Delhi|Mumbai|1398|car|2500|
|4|Mumbai|Delhi|1401|bus|1300|
|5|Mumbai|Delhi|1401|plane|3500|
|6|Mumbai|Delhi|1401|train|1200|
|7|Mumbai|Delhi|1401|car|2500|
|8|Delhi|Chennai|2187|bus|2200|
|9|Delhi|Chennai|2187|plane|8000|
|10|Delhi|Chennai|2187|train|1800|
|11|Delhi|Chennai|2187|car|4000|
|12|Chennai|Delhi|2187|bus|800|
|13|Chennai|Delhi|2187|plane|9000|
|14|Chennai|Delhi|2187|train|1800|
|15|Chennai|Delhi|2187|car|4000|
|16|Mumbai|Chennai|1337|bus|1500|
|17|Mumbai|Chennai|1337|plane|4000|
|18|Mumbai|Chennai|1337|train|1200|
|19|Mumbai|Chennai|1337|car|2800|
|20|Chennai|Mumbai|1340|bus|1800|
|21|Chennai|Mumbai|1340|plane|10000|
|22|Chennai|Mumbai|1340|train|1500|
|23|Chennai|Mumbai|1340|car|3000|
|24|Delhi|Kolkata|1541|bus|1800|
|25|Delhi|Kolkata|1541|plane|6782|
|26|Delhi|Kolkata|1541|train|1400|
|27|Delhi|Kolkata|1541|car|3000|
|28|Kolkata|Delhi|1541|bus|1700|
|29|Kolkata|Delhi|1541|plane|6500|
|30|Kolkata|Delhi|1541|train|1500|
|31|Kolkata|Delhi|1541|car|3200|
|32|Mumbai|Kolkata|1899|bus|1900|
|33|Mumbai|Kolkata|1899|plane|6222|
|34|Mumbai|Kolkata|1899|train|1600|
|35|Mumbai|Kolkata|1899|car|5000|
|36|Kolkata|Mumbai|1900|bus|1800|
|37|Kolkata|Mumbai|1900|plane|7047|
|38|Kolkata|Mumbai|1900|train|1800|
|39|Kolkata|Mumbai|1900|car|4800|
|40|Chennai|Kolkata|1663|bus|1900|
|41|Chennai|Kolkata|1663|plane|7428|
|42|Chennai|Kolkata|1663|train|1200|
|43|Chennai|Kolkata|1663|car|4000|
|44|Kolkata|Chennai|1663|bus|1750|
|45|Kolkata|Chennai|1663|plane|7100|
|46|Kolkata|Chennai|1663|train|1400|
|47|Kolkata|Chennai|1663|car|4500|
To build the itinerary, we ask the user to enter the starting city for their journey. After the user enters the starting city, the code filters the DataFrame to only include rows where the From City is the starting city. This allows us to begin building our travel itinerary from the user’s chosen starting location.
Next, the DataFrame is sorted by the Distance and Cost columns, ensuring that we prioritize finding the most cost-effective and efficient route for the first leg of our journey. We then select the first row of the resulting DataFrame, which represents the optimal combination of From City, To City, Distance, and Cost for the first leg of the journey.
The selected row is then appended to a new DataFrame called final_journey. This DataFrame will eventually hold the entire itinerary for our trip.
Next, we create a modified version of the original DataFrame called modified_df that excludes any cities that have already been included in the final_journey DataFrame. This is important because we don’t want to visit the same city twice.
A while loop is then used to iterate through the modified_df DataFrame until all cities have been included in the final_journey DataFrame. For each iteration, we filter the modified_df DataFrame only to include rows where the From City is the To City of the previous leg of the journey and the To City is not the From City of the previous leg. This ensures that we always find the next city to visit that is directly connected to the previous one.
Once the next city has been selected, it is sorted by the Distance and Cost columns and appended to the final_journey DataFrame. The modified_df DataFrame is then updated to exclude the cities that have been included in the final_journey DataFrame. This process is repeated until there are no more rows in the modified_df DataFrame, indicating that all cities have been visited.
final_journey = pd.DataFrame() # Ask the user to enter the starting city starting_city = input("Enter the starting city: ") # Filter the DataFrame to only include rows where the 'From City' is the starting city starting_journey = df[df['From City'] == starting_city] # Sort the data by the 'Distance' and 'Cost' columns starting_journey = starting_journey.sort_values(by=['Distance', 'Cost']) # Select the first row of the starting journey starting_journey = starting_journey.iloc[0] # Append the starting journey to the final journey final_journey = final_journey.append(starting_journey) # Create a modified DataFrame that excludes the cities in the final journey modified_df = df[~df['From City'].isin(final_journey['From City'].unique())] # Use a while loop to repeat the process until all cities have been visited while len(modified_df) > 0: # Select the next city to visit based on the 'From City' and 'To City' columns next_city = modified_df[(modified_df['From City'].isin([final_journey['To City'].unique()[-1]])) & (~modified_df['To City'].isin([final_journey['From City'].unique()[-1]]))] # Sort the data by the 'Distance' and 'Cost' columns next_city = next_city.sort_values(by=['Distance', 'Cost']) # Select the first row of the next city next_city = next_city.iloc[0] # Append the next city to the final journey final_journey = final_journey.append(next_city) # Update the modified DataFrame to exclude the cities in the final journey modified_df = df[~df['From City'].isin(final_journey['From City'].unique())] # Print the final journey print(final_journey) print('Total Cost ',final_journey['Cost'].sum()) print('Total Distance Travelled ',final_journey['Distance'].sum(),'Km')
Exploring the Capabilities of the Tool Created
Now that we have the tool ready let’s explore it to build some itineraries by selecting our desired starting destinations. Given the starting city, the output should give us the optimal sequence of cities that will minimize the distance traveled and the cost.
Let’s start with the starting destination as ‘Delhi.’
As we see in the output above, the code optimizes our travel plan and tells us that we should start by taking a bus from Delhi to Mumbai and from thereon, go to Chennai and then Kolkata, before returning to Delhi, all by train. Following this route and transportation means, we will be spending Rs. 4900 while covering 5939 km.
Now, let’s try entering ‘Chennai’ as the starting destination.
Our optimal travel sequence comes out to be
Chennai – Mumbai – Delhi – Kolkata – Chennai
Covering the entire journey by train, we will be able to travel 5945 km by spending Rs. 5500.
Use Cases of the Python’s Travel Itinerary
There are several use cases for a tool like this:
- Planning a multi-city vacation: If you’re planning a trip that involves visiting multiple cities, this tool can help you find the most cost-effective and efficient way to travel between them. You can select your starting city, and the tool will build an itinerary that minimizes both the cost and distance traveled.
- Business travel: If you frequently travel for work, this tool can help you find the most economical way to get from one city to another. You can easily compare the costs and distances of different transportation options and choose the one that best fits your needs and budget.
- Road trips: If you’re planning a road trip with friends or family, this tool can help you find the best route. You can enter your starting city and the cities you want to visit, and the tool will find the most cost-effective way to get there.
- Backpacking: If you’re planning a backpacking trip, this tool can help you find the most affordable way to get between different destinations. Whether you’re travelling by plane, train, bus, or car, this tool can help you save money.
- Travel budgeting: If you have a limited budget for your trip, this tool can help you find the most cost-effective way to travel between cities. You can easily compare the costs of different transportation options and choose the one that fits your budget.
Conclusion
This code provides a simple and effective solution for creating optimal travel itineraries by minimizing both cost and distance. By using real-time distance data from the Google Maps API and combining it with user-defined transportation options and costs, this tool allows users to easily plan their trips and find the most cost-effective and efficient routes. The following are some of the key takeaways from this article.
1. Travel planning can be daunting because finding the right balance between cost and distance is challenging.
2. A tool that uses the Google Maps API can be used to create optimal travel itineraries by minimizing both the cost and distance traveled.
3. The tool allows users to input their starting city and creates an itinerary based on real-time distance data and a pre-defined list of transportation options and costs.
4. The tool uses Python’s libraries like pandas to generate an itinerary and Google Maps API, which returns the distance between different combinations of cities.
5. The tool can be used to make travel planning more efficient and cost-effective. Additionally, this tool can be further enhanced to consider additional transportation options and variations in costs over different timings, adding an element of time optimization to the itinerary generation.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.