Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,6 @@ from geopy.geocoders import Nominatim
|
|
4 |
from geopy.distance import geodesic
|
5 |
from itertools import combinations
|
6 |
import numpy as np
|
7 |
-
import polyline
|
8 |
-
import requests
|
9 |
|
10 |
# Algoritma Held-Karp untuk TSP
|
11 |
def held_karp_tsp(dist_matrix):
|
@@ -70,11 +68,12 @@ def get_coordinates(city_name):
|
|
70 |
|
71 |
# Menghitung jarak antar semua pasangan kota
|
72 |
def create_distance_matrix(coordinates):
|
73 |
-
|
|
|
74 |
dist_matrix = np.zeros((n, n))
|
75 |
for i in range(n):
|
76 |
for j in range(i + 1, n):
|
77 |
-
dist_matrix[i][j] = geodesic(
|
78 |
dist_matrix[j][i] = dist_matrix[i][j]
|
79 |
return dist_matrix
|
80 |
|
@@ -96,27 +95,31 @@ def plot_route(map_obj, coordinates, route):
|
|
96 |
st.title("Traveling Salesman Problem Solver")
|
97 |
|
98 |
# Input kota
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
if
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
if
|
107 |
st.error("Ada kota yang tidak ditemukan. Pastikan nama kota benar.")
|
108 |
else:
|
109 |
# Buat distance matrix
|
110 |
-
dist_matrix = create_distance_matrix(
|
111 |
min_cost, optimal_route = held_karp_tsp(dist_matrix)
|
112 |
|
113 |
# Tampilkan hasil
|
114 |
st.write(f"Biaya total minimum: {min_cost:.2f} km")
|
115 |
-
st.write("Rute optimal:", " -> ".join([
|
116 |
|
117 |
# Buat peta
|
118 |
-
map_obj = folium.Map(location=
|
119 |
-
plot_route(map_obj,
|
120 |
|
121 |
# Render map
|
122 |
st_folium = st.components.v1.html(folium.Map._repr_html_(map_obj), width=700, height=500)
|
|
|
4 |
from geopy.distance import geodesic
|
5 |
from itertools import combinations
|
6 |
import numpy as np
|
|
|
|
|
7 |
|
8 |
# Algoritma Held-Karp untuk TSP
|
9 |
def held_karp_tsp(dist_matrix):
|
|
|
68 |
|
69 |
# Menghitung jarak antar semua pasangan kota
|
70 |
def create_distance_matrix(coordinates):
|
71 |
+
valid_coordinates = [c for c in coordinates if c is not None]
|
72 |
+
n = len(valid_coordinates)
|
73 |
dist_matrix = np.zeros((n, n))
|
74 |
for i in range(n):
|
75 |
for j in range(i + 1, n):
|
76 |
+
dist_matrix[i][j] = geodesic(valid_coordinates[i], valid_coordinates[j]).kilometers
|
77 |
dist_matrix[j][i] = dist_matrix[i][j]
|
78 |
return dist_matrix
|
79 |
|
|
|
95 |
st.title("Traveling Salesman Problem Solver")
|
96 |
|
97 |
# Input kota
|
98 |
+
st.subheader("Pilih Kota")
|
99 |
+
city_count = st.number_input("Jumlah kota", min_value=2, step=1)
|
100 |
+
city_names = []
|
101 |
+
for i in range(int(city_count)):
|
102 |
+
city_name = st.text_input(f"Kota {i+1}", key=f"city_{i}")
|
103 |
+
city_names.append(city_name)
|
104 |
|
105 |
+
if st.button("Optimasi Rute"):
|
106 |
+
coordinates = [get_coordinates(city) for city in city_names]
|
107 |
+
valid_coordinates = [c for c in coordinates if c is not None]
|
108 |
+
|
109 |
+
if len(valid_coordinates) < 2:
|
110 |
st.error("Ada kota yang tidak ditemukan. Pastikan nama kota benar.")
|
111 |
else:
|
112 |
# Buat distance matrix
|
113 |
+
dist_matrix = create_distance_matrix(valid_coordinates)
|
114 |
min_cost, optimal_route = held_karp_tsp(dist_matrix)
|
115 |
|
116 |
# Tampilkan hasil
|
117 |
st.write(f"Biaya total minimum: {min_cost:.2f} km")
|
118 |
+
st.write("Rute optimal:", " -> ".join([city_names[i] for i in optimal_route]))
|
119 |
|
120 |
# Buat peta
|
121 |
+
map_obj = folium.Map(location=valid_coordinates[0], zoom_start=5)
|
122 |
+
plot_route(map_obj, valid_coordinates, optimal_route)
|
123 |
|
124 |
# Render map
|
125 |
st_folium = st.components.v1.html(folium.Map._repr_html_(map_obj), width=700, height=500)
|