vincentiusyoshuac commited on
Commit
468bc02
·
verified ·
1 Parent(s): 72eae0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -45,15 +45,18 @@ def held_karp_tsp(dist_matrix):
45
  tour = [0]
46
  bits = (2 ** n - 1) - 1
47
  for _ in range(n - 1):
48
- tour.append(last_city)
49
- bits &= ~(1 << last_city)
50
- if bits == 0:
 
 
 
 
 
 
 
 
51
  break
52
- next_city = min(
53
- [(memo[(bits, k)] + dist_matrix[k][last_city], k) for k in range(n) if (bits, k) in memo],
54
- key=lambda x: x[0],
55
- )[1]
56
- last_city = next_city
57
 
58
  tour.append(0)
59
  return min_tour_cost, tour
@@ -97,6 +100,9 @@ def plot_route(map_obj, coordinates, route):
97
  # Streamlit UI
98
  st.title("Traveling Salesman Problem Solver")
99
 
 
 
 
100
  # Input kota
101
  st.subheader("Pilih Kota")
102
  city_count = st.number_input("Jumlah kota", min_value=2, step=1)
@@ -128,4 +134,5 @@ if st.button("Optimasi Rute"):
128
  plot_route(map_obj, valid_coordinates, optimal_route)
129
 
130
  # Render map
131
- st_folium = st.components.v1.html(folium.Map._repr_html_(map_obj), width=700, height=500)
 
 
45
  tour = [0]
46
  bits = (2 ** n - 1) - 1
47
  for _ in range(n - 1):
48
+ if last_city is not None:
49
+ tour.append(last_city)
50
+ bits &= ~(1 << last_city)
51
+ if bits == 0:
52
+ break
53
+ next_city = min(
54
+ [(memo[(bits, k)] + dist_matrix[k][last_city], k) for k in range(n) if (bits, k) in memo],
55
+ key=lambda x: x[0],
56
+ )[1]
57
+ last_city = next_city
58
+ else:
59
  break
 
 
 
 
 
60
 
61
  tour.append(0)
62
  return min_tour_cost, tour
 
100
  # Streamlit UI
101
  st.title("Traveling Salesman Problem Solver")
102
 
103
+ # Create map
104
+ map_obj = folium.Map(location=[0, 0], zoom_start=2)
105
+
106
  # Input kota
107
  st.subheader("Pilih Kota")
108
  city_count = st.number_input("Jumlah kota", min_value=2, step=1)
 
134
  plot_route(map_obj, valid_coordinates, optimal_route)
135
 
136
  # Render map
137
+ st.markdown("### Rute Optimal")
138
+ st_folium = st.components.v1.html(folium.Map._repr_html_(map_obj), width=800, height=600)