Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import folium
|
|
| 4 |
import numpy as np
|
| 5 |
from scipy.optimize import linear_sum_assignment
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
|
| 8 |
def calculate_distances(coordinates):
|
| 9 |
"""Calculate distances between all points using Haversine formula"""
|
|
@@ -13,7 +14,6 @@ def calculate_distances(coordinates):
|
|
| 13 |
for i in range(n):
|
| 14 |
for j in range(n):
|
| 15 |
if i != j:
|
| 16 |
-
# Haversine formula
|
| 17 |
lat1, lon1 = coordinates[i]
|
| 18 |
lat2, lon2 = coordinates[j]
|
| 19 |
|
|
@@ -44,6 +44,17 @@ def get_place_coordinates(place_name):
|
|
| 44 |
except:
|
| 45 |
return None
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def main():
|
| 48 |
st.title("AI Travel Route Optimizer")
|
| 49 |
|
|
@@ -76,17 +87,20 @@ def main():
|
|
| 76 |
|
| 77 |
m = folium.Map(location=[center_lat, center_lon], zoom_start=4)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
# Add markers and route lines
|
| 80 |
for i in range(len(optimized_indices)):
|
| 81 |
current_idx = optimized_indices[i]
|
| 82 |
next_idx = optimized_indices[(i + 1) % len(optimized_indices)]
|
| 83 |
|
| 84 |
-
# Add marker
|
| 85 |
place_name, (lat, lon) = places[current_idx]
|
| 86 |
folium.Marker(
|
| 87 |
[lat, lon],
|
| 88 |
popup=f"Stop {i+1}: {place_name}",
|
| 89 |
-
icon=
|
| 90 |
).add_to(m)
|
| 91 |
|
| 92 |
# Add line to next point
|
|
@@ -101,10 +115,10 @@ def main():
|
|
| 101 |
# Display map
|
| 102 |
st.components.v1.html(m._repr_html_(), height=600)
|
| 103 |
|
| 104 |
-
# Display itinerary
|
| 105 |
st.header("Optimized Itinerary")
|
| 106 |
-
for
|
| 107 |
-
st.write(f"{
|
| 108 |
|
| 109 |
# Calculate total distance
|
| 110 |
total_distance = 0
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from scipy.optimize import linear_sum_assignment
|
| 6 |
import pandas as pd
|
| 7 |
+
from folium import plugins
|
| 8 |
|
| 9 |
def calculate_distances(coordinates):
|
| 10 |
"""Calculate distances between all points using Haversine formula"""
|
|
|
|
| 14 |
for i in range(n):
|
| 15 |
for j in range(n):
|
| 16 |
if i != j:
|
|
|
|
| 17 |
lat1, lon1 = coordinates[i]
|
| 18 |
lat2, lon2 = coordinates[j]
|
| 19 |
|
|
|
|
| 44 |
except:
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
def create_numbered_marker(number):
|
| 48 |
+
"""Create a custom marker with a number"""
|
| 49 |
+
return plugins.BeautifyIcon(
|
| 50 |
+
number=number,
|
| 51 |
+
border_color='#b4b4b4',
|
| 52 |
+
border_width=1,
|
| 53 |
+
text_color='black',
|
| 54 |
+
background_color='white',
|
| 55 |
+
inner_icon_style='margin-top:0;'
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
def main():
|
| 59 |
st.title("AI Travel Route Optimizer")
|
| 60 |
|
|
|
|
| 87 |
|
| 88 |
m = folium.Map(location=[center_lat, center_lon], zoom_start=4)
|
| 89 |
|
| 90 |
+
# Create optimized itinerary list first
|
| 91 |
+
optimized_places = [(i+1, places[idx][0]) for i, idx in enumerate(optimized_indices)]
|
| 92 |
+
|
| 93 |
# Add markers and route lines
|
| 94 |
for i in range(len(optimized_indices)):
|
| 95 |
current_idx = optimized_indices[i]
|
| 96 |
next_idx = optimized_indices[(i + 1) % len(optimized_indices)]
|
| 97 |
|
| 98 |
+
# Add numbered marker
|
| 99 |
place_name, (lat, lon) = places[current_idx]
|
| 100 |
folium.Marker(
|
| 101 |
[lat, lon],
|
| 102 |
popup=f"Stop {i+1}: {place_name}",
|
| 103 |
+
icon=create_numbered_marker(i+1)
|
| 104 |
).add_to(m)
|
| 105 |
|
| 106 |
# Add line to next point
|
|
|
|
| 115 |
# Display map
|
| 116 |
st.components.v1.html(m._repr_html_(), height=600)
|
| 117 |
|
| 118 |
+
# Display optimized itinerary
|
| 119 |
st.header("Optimized Itinerary")
|
| 120 |
+
for stop_num, place_name in optimized_places:
|
| 121 |
+
st.write(f"{stop_num}. {place_name}")
|
| 122 |
|
| 123 |
# Calculate total distance
|
| 124 |
total_distance = 0
|