shukdevdatta123 commited on
Commit
459eb5b
·
verified ·
1 Parent(s): 0cb1c8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -76,42 +76,46 @@ def display_tsp():
76
  num_nodes = st.slider("Number of nodes:", min_value=3, max_value=30, value=20)
77
  radius = st.slider("Edge radius:", min_value=0.1, max_value=1.0, value=0.4)
78
 
79
- # Create random geometric graph based on user input
80
- G_custom = nx.random_geometric_graph(num_nodes, radius, seed=3)
81
- pos = nx.get_node_attributes(G_custom, "pos")
82
 
83
- # Depot should be at (0.5, 0.5)
84
- pos[0] = (0.5, 0.5)
 
 
85
 
86
- H = G_custom.copy()
 
87
 
88
- # Calculating the distances between the nodes as edge's weight.
89
- for i in range(len(pos)):
90
- for j in range(i + 1, len(pos)):
91
- dist = math.hypot(pos[i][0] - pos[j][0], pos[i][1] - pos[j][1])
92
- dist = dist
93
- G_custom.add_edge(i, j, weight=dist)
94
 
95
- # Find TSP cycle using Christofides' approximation
96
- cycle = christofides(G_custom, weight="weight")
97
- edge_list = list(nx.utils.pairwise(cycle))
 
 
 
98
 
99
- # Draw closest edges on each node only
100
- nx.draw_networkx_edges(H, pos, edge_color="blue", width=0.5)
 
101
 
102
- # Draw the TSP route
103
- nx.draw_networkx(
104
- G_custom,
105
- pos,
106
- with_labels=True,
107
- edgelist=edge_list,
108
- edge_color="red",
109
- node_size=200,
110
- width=3,
111
- )
112
 
113
- st.pyplot(plt)
114
- st.write("The route of the traveler is:", cycle)
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  # Display Drawing: Traveling Salesman Problem if selected
117
  if sidebar_option == "Drawing: Traveling Salesman Problem":
 
76
  num_nodes = st.slider("Number of nodes:", min_value=3, max_value=30, value=20)
77
  radius = st.slider("Edge radius:", min_value=0.1, max_value=1.0, value=0.4)
78
 
79
+ # Add a button to generate a new graph
80
+ generate_button = st.button("Generate Graph")
 
81
 
82
+ if generate_button:
83
+ # Create random geometric graph based on user input
84
+ G_custom = nx.random_geometric_graph(num_nodes, radius, seed=3)
85
+ pos = nx.get_node_attributes(G_custom, "pos")
86
 
87
+ # Depot should be at (0.5, 0.5)
88
+ pos[0] = (0.5, 0.5)
89
 
90
+ H = G_custom.copy()
 
 
 
 
 
91
 
92
+ # Calculating the distances between the nodes as edge's weight.
93
+ for i in range(len(pos)):
94
+ for j in range(i + 1, len(pos)):
95
+ dist = math.hypot(pos[i][0] - pos[j][0], pos[i][1] - pos[j][1])
96
+ dist = dist
97
+ G_custom.add_edge(i, j, weight=dist)
98
 
99
+ # Find TSP cycle using Christofides' approximation
100
+ cycle = christofides(G_custom, weight="weight")
101
+ edge_list = list(nx.utils.pairwise(cycle))
102
 
103
+ # Draw closest edges on each node only
104
+ nx.draw_networkx_edges(H, pos, edge_color="blue", width=0.5)
 
 
 
 
 
 
 
 
105
 
106
+ # Draw the TSP route
107
+ nx.draw_networkx(
108
+ G_custom,
109
+ pos,
110
+ with_labels=True,
111
+ edgelist=edge_list,
112
+ edge_color="red",
113
+ node_size=200,
114
+ width=3,
115
+ )
116
+
117
+ st.pyplot(plt)
118
+ st.write("The route of the traveler is:", cycle)
119
 
120
  # Display Drawing: Traveling Salesman Problem if selected
121
  if sidebar_option == "Drawing: Traveling Salesman Problem":