Spaces:
Sleeping
Sleeping
souvik0306
commited on
Commit
·
a9de22e
1
Parent(s):
173f404
Refactor flight route UI and main script
Browse files- Old/flight_route_ui.py +0 -67
- Old/main.py +0 -36
- Old/optimizer.py +0 -103
- README.md +4 -6
- app.py +19 -77
- doc.md +0 -38
- map_generator.py +2 -2
- optimize.py +35 -51
- requirements.txt +1 -0
Old/flight_route_ui.py
DELETED
@@ -1,67 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
from Old.optimizer import *
|
4 |
-
from weather import *
|
5 |
-
from flight_distance import * # Assuming this function is defined in your aircraft capabilities module
|
6 |
-
|
7 |
-
# Load airport data and aircraft data from CSV files
|
8 |
-
airport_df = pd.read_csv(r'airport.csv') # Adjust the path to your CSV file
|
9 |
-
aircraft_df = pd.read_csv(r'aircraft.csv') # Adjust the path to your CSV file
|
10 |
-
|
11 |
-
# Create a combined option list with both IATA codes and airport names
|
12 |
-
airport_options = [f"{row['IATA']} - {row['Airport_Name']}" for _, row in airport_df.iterrows()]
|
13 |
-
|
14 |
-
# Ensure the correct column is used for aircraft types
|
15 |
-
aircraft_type_column = 'Aircraft' # Adjust if your column name is different
|
16 |
-
if aircraft_type_column not in aircraft_df.columns:
|
17 |
-
raise ValueError(f"Column '{aircraft_type_column}' not found in aircraft_types.csv. Available columns: {aircraft_df.columns}")
|
18 |
-
|
19 |
-
aircraft_options = aircraft_df[aircraft_type_column].tolist()
|
20 |
-
|
21 |
-
# Function to determine if a route can be flown
|
22 |
-
def check_route(airport_selections, aircraft_type):
|
23 |
-
# Extract IATA codes from the selected options
|
24 |
-
airports = [selection.split(" - ")[0] for selection in airport_selections]
|
25 |
-
|
26 |
-
# Get coordinates for selected airports as a dictionary {IATA: (latitude, longitude)}
|
27 |
-
lat_long_dict = get_airport_lat_long(airports) # Ensure this function returns a dictionary in the expected format
|
28 |
-
|
29 |
-
# Pass only the required details in the expected format for the weather function
|
30 |
-
raw_weather = fetch_weather_for_all_routes(airports, lat_long_dict) # This should receive the correct lat_long_dict
|
31 |
-
|
32 |
-
# Extract route factors (e.g., conditions impacting the route)
|
33 |
-
route_factors = extract_route_factors(raw_weather)
|
34 |
-
|
35 |
-
# Calculate distances between selected airports
|
36 |
-
trip_distance = calculate_distances(airports)
|
37 |
-
|
38 |
-
# Ensure the graph is bidirectional
|
39 |
-
for (a, b), dist in list(trip_distance.items()):
|
40 |
-
trip_distance[(b, a)] = dist
|
41 |
-
|
42 |
-
# Find the optimal route
|
43 |
-
optimal_route, optimal_distance = find_optimal_route(airports, trip_distance, route_factors)
|
44 |
-
|
45 |
-
# Check if the aircraft can fly the route without refueling
|
46 |
-
result = can_fly_route(aircraft_type, airports)
|
47 |
-
|
48 |
-
# Convert all dictionary keys to strings for JSON compatibility
|
49 |
-
return {
|
50 |
-
"Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
|
51 |
-
"Total Round Trip Distance": str(optimal_distance), # Convert to string if necessary
|
52 |
-
"Can Fly Route": str(result) # Convert to string if necessary
|
53 |
-
}
|
54 |
-
|
55 |
-
# Gradio Interface
|
56 |
-
with gr.Blocks() as demo:
|
57 |
-
gr.Markdown("## Airport Route Feasibility Checker")
|
58 |
-
airport_selector = gr.Dropdown(airport_options, multiselect=True, label="Select Airports (IATA - Name)")
|
59 |
-
aircraft_selector = gr.Dropdown(aircraft_options, label="Select Aircraft Type")
|
60 |
-
check_button = gr.Button("Check Route Feasibility")
|
61 |
-
|
62 |
-
result_output = gr.JSON(label="Result")
|
63 |
-
|
64 |
-
check_button.click(fn=check_route, inputs=[airport_selector, aircraft_selector], outputs=result_output)
|
65 |
-
|
66 |
-
# Launch the Gradio app
|
67 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Old/main.py
DELETED
@@ -1,36 +0,0 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
from flight_distance import *
|
3 |
-
from Old.optimizer import *
|
4 |
-
from weather import *
|
5 |
-
|
6 |
-
airport_identifiers = ['CCU', 'CDG', 'SIN'] # Replace with actual identifiers
|
7 |
-
|
8 |
-
#Get Airport Coordinates
|
9 |
-
lat_long_dict = get_airport_lat_long(airport_identifiers)
|
10 |
-
# print("Coordinates: \n",lat_long_dict)
|
11 |
-
|
12 |
-
#Get Distance between each node (airports)
|
13 |
-
trip_distance = calculate_distances(airport_identifiers)
|
14 |
-
# print("Distance b/w Airports: \n",trip_distance)
|
15 |
-
|
16 |
-
#Get onroute weather
|
17 |
-
raw_weather = fetch_weather_for_all_routes(airport_identifiers, lat_long_dict)
|
18 |
-
route_factors = extract_route_factors(raw_weather)
|
19 |
-
# print("On Route weather: \n", raw_weather)
|
20 |
-
|
21 |
-
# # Ensure the graph is bidirectional (undirected)
|
22 |
-
for (a, b), dist in list(trip_distance.items()):
|
23 |
-
trip_distance[(b, a)] = dist
|
24 |
-
|
25 |
-
# Find the optimal route with the new cost metric
|
26 |
-
optimal_route, optimal_distance = find_optimal_route(airport_identifiers, trip_distance, route_factors)
|
27 |
-
|
28 |
-
# Display the optimal route and the total adjusted distance/cost
|
29 |
-
print("Optimal Route:", " -> ".join(optimal_route) + f" -> {optimal_route[0]}")
|
30 |
-
print("Total Round Trip Distance:", optimal_distance)
|
31 |
-
|
32 |
-
aircraft_type = "Airbus A350-900"
|
33 |
-
|
34 |
-
# Check if the aircraft can fly the route without refuel
|
35 |
-
result = can_fly_route(aircraft_type, airport_identifiers)
|
36 |
-
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Old/optimizer.py
DELETED
@@ -1,103 +0,0 @@
|
|
1 |
-
import itertools
|
2 |
-
|
3 |
-
def extract_route_factors(raw_weather):
|
4 |
-
"""
|
5 |
-
Extracts route factors from raw weather data by breaking down routes into individual segments.
|
6 |
-
|
7 |
-
Parameters:
|
8 |
-
- raw_weather (dict): The raw weather data with routes and corresponding weather details.
|
9 |
-
|
10 |
-
Returns:
|
11 |
-
- dict: A dictionary with segments as keys (in tuple format) and a list of weather and temperature data.
|
12 |
-
"""
|
13 |
-
route_factors = {}
|
14 |
-
for route, segments in raw_weather.items():
|
15 |
-
for segment in segments:
|
16 |
-
segment_key = tuple(segment['segment'].split(' -> '))
|
17 |
-
if segment_key not in route_factors:
|
18 |
-
route_factors[segment_key] = []
|
19 |
-
route_factors[segment_key].append({
|
20 |
-
'weather': segment['weather'],
|
21 |
-
'temperature': segment['temperature']
|
22 |
-
})
|
23 |
-
return route_factors
|
24 |
-
|
25 |
-
def weather_risk(weather):
|
26 |
-
risk_factors = {
|
27 |
-
"clear sky": 0.1,
|
28 |
-
"few clouds": 0.2,
|
29 |
-
"scattered clouds": 0.3,
|
30 |
-
"broken clouds": 0.4,
|
31 |
-
"overcast clouds": 0.5,
|
32 |
-
"light rain": 0.6,
|
33 |
-
"rain": 0.7,
|
34 |
-
"storm": 0.9
|
35 |
-
}
|
36 |
-
return risk_factors.get(weather, 0.5) # Default risk factor if not listed
|
37 |
-
|
38 |
-
# Function to normalize temperature impact
|
39 |
-
def temperature_impact(temperature):
|
40 |
-
# Assuming ideal temperature for fuel efficiency is around 20-25°C
|
41 |
-
if temperature < 20 or temperature > 25:
|
42 |
-
return abs(temperature - 22.5) / 30 # Normalize to a value between 0 and 1
|
43 |
-
return 0.1 # Low impact in the ideal range
|
44 |
-
|
45 |
-
# Calculate the adjusted cost for each route segment
|
46 |
-
def calculate_adjusted_cost(segment, base_distance, route_factors):
|
47 |
-
# Handle both directions of the segment
|
48 |
-
if segment in route_factors:
|
49 |
-
factors = route_factors[segment]
|
50 |
-
elif (segment[1], segment[0]) in route_factors:
|
51 |
-
factors = route_factors[(segment[1], segment[0])]
|
52 |
-
else:
|
53 |
-
raise ValueError(f"Segment {segment} not found in route factors.")
|
54 |
-
|
55 |
-
# Aggregate weather and temperature data if there are multiple entries for the segment
|
56 |
-
weather_descriptions = [factor["weather"] for factor in factors]
|
57 |
-
temperatures = [factor["temperature"] for factor in factors]
|
58 |
-
|
59 |
-
most_common_weather = max(set(weather_descriptions), key=weather_descriptions.count)
|
60 |
-
avg_temperature = sum(temperatures) / len(temperatures)
|
61 |
-
|
62 |
-
weather_cost = weather_risk(most_common_weather) * 100 # Weight for weather impact
|
63 |
-
temperature_cost = temperature_impact(avg_temperature) * 50 # Weight for temperature impact
|
64 |
-
|
65 |
-
total_cost = base_distance + weather_cost + temperature_cost
|
66 |
-
return total_cost
|
67 |
-
|
68 |
-
# Update the distance function to include additional factors
|
69 |
-
def calculate_route_distance(route, distances, route_factors):
|
70 |
-
"""Calculate the total cost for a given route, including additional factors."""
|
71 |
-
total_distance = 0
|
72 |
-
for i in range(len(route) - 1):
|
73 |
-
segment = (route[i], route[i + 1])
|
74 |
-
if segment not in distances:
|
75 |
-
segment = (route[i + 1], route[i])
|
76 |
-
base_distance = distances[segment]
|
77 |
-
total_distance += calculate_adjusted_cost(segment, base_distance, route_factors)
|
78 |
-
|
79 |
-
# Add distance to return to the starting point
|
80 |
-
last_segment = (route[-1], route[0])
|
81 |
-
if last_segment not in distances:
|
82 |
-
last_segment = (route[0], route[-1])
|
83 |
-
base_distance = distances[last_segment]
|
84 |
-
total_distance += calculate_adjusted_cost(last_segment, base_distance, route_factors)
|
85 |
-
|
86 |
-
return total_distance
|
87 |
-
|
88 |
-
def find_optimal_route(airports, distances, route_factors):
|
89 |
-
"""Find the optimal route that covers all airports."""
|
90 |
-
best_route = None
|
91 |
-
min_distance = float('inf')
|
92 |
-
|
93 |
-
# Generate all possible permutations of the route
|
94 |
-
for route in itertools.permutations(airports):
|
95 |
-
try:
|
96 |
-
current_distance = calculate_route_distance(route, distances, route_factors)
|
97 |
-
if current_distance < min_distance:
|
98 |
-
min_distance = round(current_distance,2)
|
99 |
-
best_route = route
|
100 |
-
except ValueError as e:
|
101 |
-
print(e) # Log the error to debug missing segments
|
102 |
-
|
103 |
-
return best_route, min_distance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
@@ -12,6 +12,8 @@ license: mit
|
|
12 |
|
13 |
# Flight Route Optimization
|
14 |
|
|
|
|
|
15 |
## Overview
|
16 |
|
17 |
This project focuses on optimizing flight routes to minimize travel time and costs using advanced algorithms and data analysis techniques.
|
@@ -20,8 +22,6 @@ This project focuses on optimizing flight routes to minimize travel time and cos
|
|
20 |
|
21 |
- Efficient route calculation
|
22 |
- Cost optimization
|
23 |
-
- Real-time data integration
|
24 |
-
- User-friendly interface
|
25 |
|
26 |
## Installation
|
27 |
|
@@ -42,9 +42,7 @@ This project focuses on optimizing flight routes to minimize travel time and cos
|
|
42 |
|
43 |
1. Run the main script:
|
44 |
```bash
|
45 |
-
python
|
46 |
```
|
47 |
-
2.
|
48 |
-
|
49 |
-
|
50 |
---
|
|
|
12 |
|
13 |
# Flight Route Optimization
|
14 |
|
15 |
+
## Check out the demo here - [HuggingFace Space](https://huggingface.co/spaces/souvik0306/Flight_Route_Planner)
|
16 |
+
|
17 |
## Overview
|
18 |
|
19 |
This project focuses on optimizing flight routes to minimize travel time and costs using advanced algorithms and data analysis techniques.
|
|
|
22 |
|
23 |
- Efficient route calculation
|
24 |
- Cost optimization
|
|
|
|
|
25 |
|
26 |
## Installation
|
27 |
|
|
|
42 |
|
43 |
1. Run the main script:
|
44 |
```bash
|
45 |
+
python app.py
|
46 |
```
|
47 |
+
2. Check out the [live demo](https://huggingface.co/spaces/souvik0306/Flight_Route_Planner) to see the project in action.
|
|
|
|
|
48 |
---
|
app.py
CHANGED
@@ -16,101 +16,39 @@ aircraft_type_column = 'Aircraft'
|
|
16 |
aircraft_options = aircraft_df[aircraft_type_column].tolist()
|
17 |
|
18 |
def check_route(airport_selections, aircraft_type):
|
19 |
-
# Extract IATA codes from the selected options
|
20 |
airports = [selection.split(" - ")[0] for selection in airport_selections]
|
21 |
-
|
22 |
-
# Step 1: Get Airport Coordinates
|
23 |
lat_long_dict = get_airport_lat_long(airports)
|
24 |
-
|
25 |
-
# Step 2: Calculate Distances between each node (airports)
|
26 |
trip_distance = calculate_distances(airports)
|
27 |
-
|
28 |
-
# Step 3: Get on-route weather
|
29 |
raw_weather = fetch_weather_for_all_routes(airports, lat_long_dict)
|
30 |
route_factors = extract_route_factors(raw_weather)
|
31 |
|
32 |
-
# Step 4: Ensure the graph is bidirectional (undirected)
|
33 |
for (a, b), dist in list(trip_distance.items()):
|
34 |
trip_distance[(b, a)] = dist
|
35 |
|
36 |
-
# Step 5: Find the optimal route based on weather, temperature, and distance
|
37 |
optimal_route, optimal_distance = find_optimal_route(airports, trip_distance, route_factors)
|
38 |
-
|
39 |
-
# Step 6: Fetch Aircraft Details
|
40 |
aircraft_specs = get_aircraft_details(aircraft_type)
|
41 |
|
42 |
-
# Check if aircraft details were retrieved successfully
|
43 |
if isinstance(aircraft_specs, str):
|
44 |
-
return {"Error": aircraft_specs}, ""
|
45 |
-
|
46 |
-
# Step 7: Check if the aircraft can fly the route
|
47 |
-
route_feasibility = check_route_feasibility(optimal_route, trip_distance, aircraft_specs)
|
48 |
-
|
49 |
-
# Collect sectors needing refuel
|
50 |
-
refuel_sectors = set() # Track sectors that require refueling
|
51 |
-
sector_details = []
|
52 |
-
refuel_required = False # Flag to track if refueling is required
|
53 |
-
|
54 |
-
for i in range(len(optimal_route) - 1):
|
55 |
-
segment = (optimal_route[i], optimal_route[i + 1])
|
56 |
-
segment_distance = trip_distance.get(segment) or trip_distance.get((segment[1], segment[0]))
|
57 |
-
|
58 |
-
# Calculate fuel and time for this sector
|
59 |
-
fuel, time = calculate_fuel_and_time_for_segment(segment_distance, aircraft_specs)
|
60 |
-
sector_info = {
|
61 |
-
"Sector": f"{optimal_route[i]} -> {optimal_route[i+1]}",
|
62 |
-
"Fuel Required (kg)": round(fuel, 2),
|
63 |
-
"Flight Time (hrs)": round(time, 2)
|
64 |
-
}
|
65 |
-
|
66 |
-
# Check if refueling is required for this sector
|
67 |
-
if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
|
68 |
-
sector_info["Refuel Required"] = "Yes"
|
69 |
-
refuel_sectors.add((optimal_route[i], optimal_route[i + 1])) # Add to refuel sectors
|
70 |
-
refuel_required = True
|
71 |
-
else:
|
72 |
-
sector_info["Refuel Required"] = "No"
|
73 |
-
|
74 |
-
sector_details.append(sector_info)
|
75 |
-
|
76 |
-
# Check the final leg (return to the starting point)
|
77 |
-
last_segment = (optimal_route[-1], optimal_route[0])
|
78 |
-
last_segment_distance = trip_distance.get(last_segment) or trip_distance.get((last_segment[1], last_segment[0]))
|
79 |
-
fuel, time = calculate_fuel_and_time_for_segment(last_segment_distance, aircraft_specs)
|
80 |
-
|
81 |
-
# Add final leg details
|
82 |
-
final_leg_info = {
|
83 |
-
"Sector": f"{optimal_route[-1]} -> {optimal_route[0]}",
|
84 |
-
"Fuel Required (kg)": round(fuel, 2),
|
85 |
-
"Flight Time (hrs)": round(time, 2)
|
86 |
-
}
|
87 |
-
|
88 |
-
if fuel > aircraft_specs['Max_Fuel_Capacity_kg']:
|
89 |
-
final_leg_info["Refuel Required"] = "Yes"
|
90 |
-
refuel_sectors.add((optimal_route[-1], optimal_route[0])) # Add final leg to refuel sectors
|
91 |
-
refuel_required = True
|
92 |
-
else:
|
93 |
-
final_leg_info["Refuel Required"] = "No"
|
94 |
-
|
95 |
-
sector_details.append(final_leg_info)
|
96 |
|
97 |
-
|
98 |
-
map_html = create_route_map(airports_dict, lat_long_dict, optimal_route,
|
99 |
|
100 |
-
|
101 |
-
if refuel_required:
|
102 |
result = {
|
103 |
"Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
|
104 |
-
"Total Round Trip Distance":
|
105 |
-
"
|
106 |
-
"
|
|
|
|
|
107 |
}
|
108 |
else:
|
109 |
result = {
|
110 |
"Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
|
111 |
-
"Total Round Trip Distance":
|
112 |
-
"Can Fly
|
113 |
-
"Sector Details":
|
114 |
}
|
115 |
|
116 |
return result, map_html
|
@@ -125,14 +63,18 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
|
|
125 |
airport_selector = gr.Dropdown(airport_options, multiselect=True, label="Select Airports (IATA - Name)")
|
126 |
aircraft_selector = gr.Dropdown(aircraft_options, label="Select Aircraft Type")
|
127 |
check_button = gr.Button("Check Route Feasibility")
|
128 |
-
result_output = gr.JSON(label="Result")
|
129 |
|
130 |
with gr.Column():
|
131 |
gr.Markdown("## Route Map")
|
132 |
-
map_output = gr.HTML(label="Route Map")
|
133 |
|
134 |
# Connect the button click to the check_route function
|
135 |
-
check_button.click(
|
|
|
|
|
|
|
|
|
136 |
|
137 |
# Launch the Gradio app
|
138 |
demo.launch()
|
|
|
16 |
aircraft_options = aircraft_df[aircraft_type_column].tolist()
|
17 |
|
18 |
def check_route(airport_selections, aircraft_type):
|
|
|
19 |
airports = [selection.split(" - ")[0] for selection in airport_selections]
|
|
|
|
|
20 |
lat_long_dict = get_airport_lat_long(airports)
|
|
|
|
|
21 |
trip_distance = calculate_distances(airports)
|
|
|
|
|
22 |
raw_weather = fetch_weather_for_all_routes(airports, lat_long_dict)
|
23 |
route_factors = extract_route_factors(raw_weather)
|
24 |
|
|
|
25 |
for (a, b), dist in list(trip_distance.items()):
|
26 |
trip_distance[(b, a)] = dist
|
27 |
|
|
|
28 |
optimal_route, optimal_distance = find_optimal_route(airports, trip_distance, route_factors)
|
|
|
|
|
29 |
aircraft_specs = get_aircraft_details(aircraft_type)
|
30 |
|
|
|
31 |
if isinstance(aircraft_specs, str):
|
32 |
+
return {"Error": aircraft_specs}, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
feasibility_result = check_route_feasibility(optimal_route, trip_distance, aircraft_specs)
|
35 |
+
map_html = create_route_map(airports_dict, lat_long_dict, optimal_route, feasibility_result["Refuel Sectors"])
|
36 |
|
37 |
+
if feasibility_result["Can Fly Entire Route"]:
|
|
|
38 |
result = {
|
39 |
"Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
|
40 |
+
"Total Round Trip Distance": f"{optimal_distance} km",
|
41 |
+
"Total Fuel Required": feasibility_result["Total Fuel Required (kg)"],
|
42 |
+
"Total Flight Time": feasibility_result["Total Flight Time (hrs)"],
|
43 |
+
"Can Fly Entire Route": "Yes",
|
44 |
+
"Sector Details": feasibility_result["Sector Details"]
|
45 |
}
|
46 |
else:
|
47 |
result = {
|
48 |
"Optimal Route": " -> ".join(optimal_route) + f" -> {optimal_route[0]}",
|
49 |
+
"Total Round Trip Distance": f"{optimal_distance} km",
|
50 |
+
"Can Fly Entire Route": "No, refueling required in one or more sectors.",
|
51 |
+
"Sector Details": feasibility_result["Sector Details"]
|
52 |
}
|
53 |
|
54 |
return result, map_html
|
|
|
63 |
airport_selector = gr.Dropdown(airport_options, multiselect=True, label="Select Airports (IATA - Name)")
|
64 |
aircraft_selector = gr.Dropdown(aircraft_options, label="Select Aircraft Type")
|
65 |
check_button = gr.Button("Check Route Feasibility")
|
66 |
+
result_output = gr.JSON(label="Feasibility Result (Route, Fuel, Refueling Info)")
|
67 |
|
68 |
with gr.Column():
|
69 |
gr.Markdown("## Route Map")
|
70 |
+
map_output = gr.HTML(label="Interactive Route Map with Refueling Sectors")
|
71 |
|
72 |
# Connect the button click to the check_route function
|
73 |
+
check_button.click(
|
74 |
+
fn=check_route,
|
75 |
+
inputs=[airport_selector, aircraft_selector],
|
76 |
+
outputs=[result_output, map_output]
|
77 |
+
)
|
78 |
|
79 |
# Launch the Gradio app
|
80 |
demo.launch()
|
doc.md
DELETED
@@ -1,38 +0,0 @@
|
|
1 |
-
# Flight Route Optimization
|
2 |
-
|
3 |
-
## Overview
|
4 |
-
|
5 |
-
This project focuses on optimizing flight routes to minimize travel time and costs using advanced algorithms and data analysis techniques.
|
6 |
-
|
7 |
-
## Features
|
8 |
-
|
9 |
-
- Efficient route calculation
|
10 |
-
- Cost optimization
|
11 |
-
- Real-time data integration
|
12 |
-
- User-friendly interface
|
13 |
-
|
14 |
-
## Installation
|
15 |
-
|
16 |
-
1. Clone the repository:
|
17 |
-
```bash
|
18 |
-
git clone https://github.com/yourusername/Flight_Route_Optimization.git
|
19 |
-
```
|
20 |
-
2. Navigate to the project directory:
|
21 |
-
```bash
|
22 |
-
cd Flight_Route_Optimization
|
23 |
-
```
|
24 |
-
3. Install the required dependencies:
|
25 |
-
```bash
|
26 |
-
pip install -r requirements.txt
|
27 |
-
```
|
28 |
-
|
29 |
-
## Usage
|
30 |
-
|
31 |
-
1. Run the main script:
|
32 |
-
```bash
|
33 |
-
python main.py
|
34 |
-
```
|
35 |
-
2. Follow the on-screen instructions to input your flight details.
|
36 |
-
|
37 |
-
|
38 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map_generator.py
CHANGED
@@ -57,8 +57,8 @@ def create_route_map(airports, lat_long_dict, optimal_route, refuel_sectors):
|
|
57 |
bottom: 50px; left: 50px; width: 250px; height: 90px;
|
58 |
background-color: white; border:2px solid grey; z-index:9999; font-size:14px;">
|
59 |
<strong>Legend</strong><br>
|
60 |
-
<i class="fa fa-
|
61 |
-
<i class="fa fa-
|
62 |
</div>
|
63 |
'''
|
64 |
route_map.get_root().html.add_child(folium.Element(legend_html))
|
|
|
57 |
bottom: 50px; left: 50px; width: 250px; height: 90px;
|
58 |
background-color: white; border:2px solid grey; z-index:9999; font-size:14px;">
|
59 |
<strong>Legend</strong><br>
|
60 |
+
<i class="fa fa-minus" style="color:red"></i> Solid line: No refuel required<br>
|
61 |
+
<i class="fa fa-ellipsis-h" style="color:red"></i> Dotted line: Refuel required
|
62 |
</div>
|
63 |
'''
|
64 |
route_map.get_root().html.add_child(folium.Element(legend_html))
|
optimize.py
CHANGED
@@ -2,9 +2,6 @@ import itertools
|
|
2 |
from flight_distance import *
|
3 |
|
4 |
def extract_route_factors(raw_weather):
|
5 |
-
"""
|
6 |
-
Extract weather and temperature factors for each route segment.
|
7 |
-
"""
|
8 |
route_factors = {}
|
9 |
for route, segments in raw_weather.items():
|
10 |
for segment in segments:
|
@@ -17,32 +14,20 @@ def extract_route_factors(raw_weather):
|
|
17 |
})
|
18 |
return route_factors
|
19 |
|
20 |
-
|
21 |
def weather_risk(weather):
|
22 |
-
"""
|
23 |
-
Return the risk factor associated with weather conditions.
|
24 |
-
"""
|
25 |
risk_factors = {
|
26 |
"clear sky": 0.1, "few clouds": 0.2, "scattered clouds": 0.3,
|
27 |
"broken clouds": 0.4, "overcast clouds": 0.5, "light rain": 0.6,
|
28 |
"rain": 0.7, "storm": 0.9
|
29 |
}
|
30 |
-
return risk_factors.get(weather, 0.5)
|
31 |
-
|
32 |
|
33 |
def temperature_impact(temperature):
|
34 |
-
"""
|
35 |
-
Calculate the impact of temperature on fuel efficiency.
|
36 |
-
"""
|
37 |
if temperature < 20 or temperature > 25:
|
38 |
-
return abs(temperature - 22.5) / 30
|
39 |
-
return 0.1
|
40 |
-
|
41 |
|
42 |
def calculate_adjusted_cost(segment, base_distance, route_factors):
|
43 |
-
"""
|
44 |
-
Calculate the adjusted cost of a segment considering weather and temperature.
|
45 |
-
"""
|
46 |
if segment in route_factors:
|
47 |
factors = route_factors[segment]
|
48 |
elif (segment[1], segment[0]) in route_factors:
|
@@ -52,21 +37,13 @@ def calculate_adjusted_cost(segment, base_distance, route_factors):
|
|
52 |
|
53 |
weather_descriptions = [factor["weather"] for factor in factors]
|
54 |
temperatures = [factor["temperature"] for factor in factors]
|
55 |
-
|
56 |
most_common_weather = max(set(weather_descriptions), key=weather_descriptions.count)
|
57 |
avg_temperature = sum(temperatures) / len(temperatures)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
total_cost = base_distance + weather_cost + temperature_cost
|
63 |
-
return total_cost
|
64 |
-
|
65 |
|
66 |
def find_optimal_route(airports, distances, route_factors):
|
67 |
-
"""
|
68 |
-
Find the optimal route between airports considering distance and weather factors.
|
69 |
-
"""
|
70 |
best_route, min_distance = None, float('inf')
|
71 |
for route in itertools.permutations(airports):
|
72 |
total_distance = 0
|
@@ -74,45 +51,38 @@ def find_optimal_route(airports, distances, route_factors):
|
|
74 |
segment = (route[i], route[i + 1])
|
75 |
base_distance = distances.get(segment) or distances.get((segment[1], segment[0]))
|
76 |
total_distance += calculate_adjusted_cost(segment, base_distance, route_factors)
|
77 |
-
|
78 |
last_segment = (route[-1], route[0])
|
79 |
base_distance = distances.get(last_segment) or distances.get((last_segment[1], last_segment[0]))
|
80 |
total_distance += calculate_adjusted_cost(last_segment, base_distance, route_factors)
|
81 |
-
|
82 |
if total_distance < min_distance:
|
83 |
min_distance, best_route = round(total_distance, 2), route
|
84 |
-
|
85 |
return best_route, min_distance
|
86 |
|
87 |
-
|
88 |
-
### Modular Route Feasibility Checking Functions ###
|
89 |
-
|
90 |
def check_segment_feasibility(segment, trip_distance, aircraft_specs):
|
91 |
-
"""
|
92 |
-
Check if the aircraft can fly a single segment without refueling.
|
93 |
-
"""
|
94 |
segment_distance = trip_distance.get(segment) or trip_distance.get((segment[1], segment[0]))
|
95 |
fuel_required, flight_time = calculate_fuel_and_time_for_segment(segment_distance, aircraft_specs)
|
96 |
-
|
97 |
-
|
98 |
-
return False, fuel_required, flight_time
|
99 |
-
return True, fuel_required, flight_time
|
100 |
-
|
101 |
|
102 |
def check_route_feasibility(optimal_route, trip_distance, aircraft_specs):
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
total_fuel = 0
|
107 |
-
total_time = 0
|
108 |
-
can_fly_entire_route = True # Flag to check if entire route is feasible
|
109 |
|
110 |
for i in range(len(optimal_route) - 1):
|
111 |
segment = (optimal_route[i], optimal_route[i + 1])
|
112 |
can_fly, fuel, time = check_segment_feasibility(segment, trip_distance, aircraft_specs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
if not can_fly:
|
114 |
print(f"Cannot fly the sector {optimal_route[i]} -> {optimal_route[i+1]} without refueling.")
|
115 |
print(f"Fuel required: {round(fuel, 2)} kg, capacity: {aircraft_specs['Max_Fuel_Capacity_kg']} kg")
|
|
|
116 |
can_fly_entire_route = False
|
117 |
else:
|
118 |
print(f"Fuel required for {optimal_route[i]} -> {optimal_route[i+1]}: {round(fuel, 2)} kg")
|
@@ -122,9 +92,17 @@ def check_route_feasibility(optimal_route, trip_distance, aircraft_specs):
|
|
122 |
|
123 |
last_segment = (optimal_route[-1], optimal_route[0])
|
124 |
can_fly, fuel, time = check_segment_feasibility(last_segment, trip_distance, aircraft_specs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
if not can_fly:
|
126 |
print(f"Cannot fly the sector {optimal_route[-1]} -> {optimal_route[0]} without refueling.")
|
127 |
print(f"Fuel required: {round(fuel, 2)} kg, capacity: {aircraft_specs['Max_Fuel_Capacity_kg']} kg")
|
|
|
128 |
can_fly_entire_route = False
|
129 |
else:
|
130 |
print(f"Fuel required for {optimal_route[-1]} -> {optimal_route[0]}: {round(fuel, 2)} kg")
|
@@ -136,7 +114,13 @@ def check_route_feasibility(optimal_route, trip_distance, aircraft_specs):
|
|
136 |
return {
|
137 |
"Total Fuel Required (kg)": round(total_fuel, 2),
|
138 |
"Total Flight Time (hrs)": round(total_time, 2),
|
139 |
-
"Can Fly Entire Route": True
|
|
|
|
|
140 |
}
|
141 |
else:
|
142 |
-
return {
|
|
|
|
|
|
|
|
|
|
2 |
from flight_distance import *
|
3 |
|
4 |
def extract_route_factors(raw_weather):
|
|
|
|
|
|
|
5 |
route_factors = {}
|
6 |
for route, segments in raw_weather.items():
|
7 |
for segment in segments:
|
|
|
14 |
})
|
15 |
return route_factors
|
16 |
|
|
|
17 |
def weather_risk(weather):
|
|
|
|
|
|
|
18 |
risk_factors = {
|
19 |
"clear sky": 0.1, "few clouds": 0.2, "scattered clouds": 0.3,
|
20 |
"broken clouds": 0.4, "overcast clouds": 0.5, "light rain": 0.6,
|
21 |
"rain": 0.7, "storm": 0.9
|
22 |
}
|
23 |
+
return risk_factors.get(weather, 0.5)
|
|
|
24 |
|
25 |
def temperature_impact(temperature):
|
|
|
|
|
|
|
26 |
if temperature < 20 or temperature > 25:
|
27 |
+
return abs(temperature - 22.5) / 30
|
28 |
+
return 0.1
|
|
|
29 |
|
30 |
def calculate_adjusted_cost(segment, base_distance, route_factors):
|
|
|
|
|
|
|
31 |
if segment in route_factors:
|
32 |
factors = route_factors[segment]
|
33 |
elif (segment[1], segment[0]) in route_factors:
|
|
|
37 |
|
38 |
weather_descriptions = [factor["weather"] for factor in factors]
|
39 |
temperatures = [factor["temperature"] for factor in factors]
|
|
|
40 |
most_common_weather = max(set(weather_descriptions), key=weather_descriptions.count)
|
41 |
avg_temperature = sum(temperatures) / len(temperatures)
|
42 |
+
weather_cost = weather_risk(most_common_weather) * 100
|
43 |
+
temperature_cost = temperature_impact(avg_temperature) * 50
|
44 |
+
return base_distance + weather_cost + temperature_cost
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def find_optimal_route(airports, distances, route_factors):
|
|
|
|
|
|
|
47 |
best_route, min_distance = None, float('inf')
|
48 |
for route in itertools.permutations(airports):
|
49 |
total_distance = 0
|
|
|
51 |
segment = (route[i], route[i + 1])
|
52 |
base_distance = distances.get(segment) or distances.get((segment[1], segment[0]))
|
53 |
total_distance += calculate_adjusted_cost(segment, base_distance, route_factors)
|
|
|
54 |
last_segment = (route[-1], route[0])
|
55 |
base_distance = distances.get(last_segment) or distances.get((last_segment[1], last_segment[0]))
|
56 |
total_distance += calculate_adjusted_cost(last_segment, base_distance, route_factors)
|
|
|
57 |
if total_distance < min_distance:
|
58 |
min_distance, best_route = round(total_distance, 2), route
|
|
|
59 |
return best_route, min_distance
|
60 |
|
|
|
|
|
|
|
61 |
def check_segment_feasibility(segment, trip_distance, aircraft_specs):
|
|
|
|
|
|
|
62 |
segment_distance = trip_distance.get(segment) or trip_distance.get((segment[1], segment[0]))
|
63 |
fuel_required, flight_time = calculate_fuel_and_time_for_segment(segment_distance, aircraft_specs)
|
64 |
+
can_fly = fuel_required <= aircraft_specs['Max_Fuel_Capacity_kg']
|
65 |
+
return can_fly, fuel_required, flight_time
|
|
|
|
|
|
|
66 |
|
67 |
def check_route_feasibility(optimal_route, trip_distance, aircraft_specs):
|
68 |
+
total_fuel, total_time = 0, 0
|
69 |
+
refuel_sectors, sector_details = [], []
|
70 |
+
can_fly_entire_route = True
|
|
|
|
|
|
|
71 |
|
72 |
for i in range(len(optimal_route) - 1):
|
73 |
segment = (optimal_route[i], optimal_route[i + 1])
|
74 |
can_fly, fuel, time = check_segment_feasibility(segment, trip_distance, aircraft_specs)
|
75 |
+
sector_info = {
|
76 |
+
"Sector": f"{optimal_route[i]} -> {optimal_route[i+1]}",
|
77 |
+
"Fuel Required (kg)": round(fuel, 2),
|
78 |
+
"Flight Time (hrs)": round(time, 2),
|
79 |
+
"Refuel Required": "Yes" if not can_fly else "No"
|
80 |
+
}
|
81 |
+
sector_details.append(sector_info)
|
82 |
if not can_fly:
|
83 |
print(f"Cannot fly the sector {optimal_route[i]} -> {optimal_route[i+1]} without refueling.")
|
84 |
print(f"Fuel required: {round(fuel, 2)} kg, capacity: {aircraft_specs['Max_Fuel_Capacity_kg']} kg")
|
85 |
+
refuel_sectors.append((optimal_route[i], optimal_route[i+1]))
|
86 |
can_fly_entire_route = False
|
87 |
else:
|
88 |
print(f"Fuel required for {optimal_route[i]} -> {optimal_route[i+1]}: {round(fuel, 2)} kg")
|
|
|
92 |
|
93 |
last_segment = (optimal_route[-1], optimal_route[0])
|
94 |
can_fly, fuel, time = check_segment_feasibility(last_segment, trip_distance, aircraft_specs)
|
95 |
+
final_leg_info = {
|
96 |
+
"Sector": f"{optimal_route[-1]} -> {optimal_route[0]}",
|
97 |
+
"Fuel Required (kg)": round(fuel, 2),
|
98 |
+
"Flight Time (hrs)": round(time, 2),
|
99 |
+
"Refuel Required": "Yes" if not can_fly else "No"
|
100 |
+
}
|
101 |
+
sector_details.append(final_leg_info)
|
102 |
if not can_fly:
|
103 |
print(f"Cannot fly the sector {optimal_route[-1]} -> {optimal_route[0]} without refueling.")
|
104 |
print(f"Fuel required: {round(fuel, 2)} kg, capacity: {aircraft_specs['Max_Fuel_Capacity_kg']} kg")
|
105 |
+
refuel_sectors.append((optimal_route[-1], optimal_route[0]))
|
106 |
can_fly_entire_route = False
|
107 |
else:
|
108 |
print(f"Fuel required for {optimal_route[-1]} -> {optimal_route[0]}: {round(fuel, 2)} kg")
|
|
|
114 |
return {
|
115 |
"Total Fuel Required (kg)": round(total_fuel, 2),
|
116 |
"Total Flight Time (hrs)": round(total_time, 2),
|
117 |
+
"Can Fly Entire Route": True,
|
118 |
+
"Sector Details": sector_details,
|
119 |
+
"Refuel Sectors": refuel_sectors
|
120 |
}
|
121 |
else:
|
122 |
+
return {
|
123 |
+
"Can Fly Entire Route": False,
|
124 |
+
"Sector Details": sector_details,
|
125 |
+
"Refuel Sectors": refuel_sectors
|
126 |
+
}
|
requirements.txt
CHANGED
@@ -3,3 +3,4 @@ pandas
|
|
3 |
geopy
|
4 |
folium
|
5 |
requests
|
|
|
|
3 |
geopy
|
4 |
folium
|
5 |
requests
|
6 |
+
gradio
|