import tkinter as tk
from tkinter import messagebox
import itertools
import numpy as np
def total_distance(route, dist_matrix):
return sum(dist_matrix[route[i]][route[i + 1]] for i in range(len(route) - 1)) +
dist_matrix[route[-1]][route[0]]
def total_time(route, time_matrix):
return sum(time_matrix[route[i]][route[i + 1]] for i in range(len(route) - 1)) +
time_matrix[route[-1]][route[0]]
def total_toll_cost(route, toll_matrix):
return sum(toll_matrix[route[i]][route[i + 1]] for i in range(len(route) - 1)) +
toll_matrix[route[-1]][route[0]]
def find_best_routes(start, end, cities, dist_matrix, time_matrix, toll_matrix):
if start not in cities or end not in cities:
messagebox.showerror("Invalid Input", "Please select valid start and end countries.")
return
start_idx, end_idx = cities.index(start), cities.index(end)
remaining = [i for i in range(len(cities)) if i != start_idx and i != end_idx]
routes = list(itertools.permutations(remaining)) # Generate all permutations of the remaining
cities
best_routes = {
"distance": [],
"time": [],
"toll": [],
"combined": []
}
for route in routes:
full_route = [start_idx] + list(route) + [end_idx]
dist = total_distance(full_route, dist_matrix)
time = total_time(full_route, time_matrix)
toll = total_toll_cost(full_route, toll_matrix)
best_routes["distance"].append((dist, full_route))
best_routes["time"].append((time, full_route))
best_routes["toll"].append((toll, full_route))
combined_score = time * 0.7 + toll * 0.3 # Weighted score (70% time, 30% toll cost)
best_routes["combined"].append((combined_score, full_route))
best_routes["distance"].sort(key=lambda x: x[0])
best_routes["time"].sort(key=lambda x: x[0])
best_routes["toll"].sort(key=lambda x: x[0])
best_routes["combined"].sort(key=lambda x: x[0])
result_text = "Best 3 Routes by Distance:\n"
for i in range(min(3, len(best_routes["distance"]))):
route = best_routes["distance"][i][1]
result_text += f"{i+1}. {' -> '.join([cities[i] for i in route])} - {best_routes['distance'][i][0]}
km\n"
result_text += "\nBest 3 Routes by Travel Time:\n"
for i in range(min(3, len(best_routes["time"]))):
route = best_routes["time"][i][1]
result_text += f"{i+1}. {' -> '.join([cities[i] for i in route])} - {best_routes['time'][i][0]}
hours\n"
result_text += "\nBest 3 Routes by Toll Cost:\n"
for i in range(min(3, len(best_routes["toll"]))):
route = best_routes["toll"][i][1]
result_text += f"{i+1}. {' -> '.join([cities[i] for i in route])} - ${best_routes['toll'][i][0]}\n"
result_text += "\nOverall Best Routes (Balanced):\n"
for i in range(min(3, len(best_routes["combined"]))):
route = best_routes["combined"][i][1]
result_text += f"{i+1}. {' -> '.join([cities[i] for i in route])} - Time: {total_time(route,
time_matrix)} hours, Toll: ${total_toll_cost(route, toll_matrix)}\n"
result_label.config(text=result_text)
def on_button_click():
start_country = start_var.get()
end_country = end_var.get()
find_best_routes(start_country, end_country, cities, dist_matrix, time_matrix, toll_matrix)
cities = ["USA", "Canada", "Brazil", "India", "Germany"]
dist_matrix = np.array([
[0, 800, 7700, 13000, 7300],
[800, 0, 8000, 12000, 6400],
[7700, 8000, 0, 13000, 10500],
[13000, 12000, 13000, 0, 8000],
[7300, 6400, 10500, 8000, 0]
])
time_matrix = np.array([
[0, 12, 100, 150, 70],
[12, 0, 110, 140, 60],
[100, 110, 0, 160, 90],
[150, 140, 160, 0, 110],
[70, 60, 90, 110, 0]
])
toll_matrix = np.array([
[0, 20, 150, 250, 100],
[20, 0, 160, 240, 90],
[150, 160, 0, 250, 180],
[250, 240, 250, 0, 120],
[100, 90, 180, 120, 0]
])
root = tk.Tk()
root.title("Best Travel Route Finder")
start_var = tk.StringVar()
end_var = tk.StringVar()
tk.Label(root, text="Select the Start Country:").pack(pady=10)
start_menu = tk.OptionMenu(root, start_var, *cities)
start_menu.pack(pady=5)
tk.Label(root, text="Select the End Country:").pack(pady=10)
end_menu = tk.OptionMenu(root, end_var, *cities)
end_menu.pack(pady=5)
calculate_button = tk.Button(root, text="Find Best Routes", command=on_button_click)
calculate_button.pack(pady=20)
result_label = tk.Label(root, text="", justify=tk.LEFT)
result_label.pack(pady=10)
root.mainloop()