Sephfox commited on
Commit
7e52935
·
verified ·
1 Parent(s): 49d654a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -34
app.py CHANGED
@@ -6,6 +6,7 @@ from scipy.spatial import distance
6
  from sklearn.cluster import KMeans
7
  import networkx as nx
8
  from collections import deque
 
9
 
10
  # Constants
11
  GRID_SIZE = 200
@@ -53,7 +54,8 @@ class Ant:
53
  if random.random() < self.genome['exploration_rate']:
54
  action = random.choice(possible_actions)
55
  else:
56
- action = np.argmax(self.q_table[self.position[0], self.position[1], possible_actions])
 
57
 
58
  reward = self.calculate_reward()
59
  self.update_q_table(action, reward)
@@ -126,7 +128,7 @@ def diffuse_pheromones(pheromone_grid):
126
  [0.1, 0.4, 0.1],
127
  [0.05, 0.1, 0.05]])
128
  for i in range(3): # For each pheromone type
129
- pheromone_grid[:,:,i] = np.convolve2d(pheromone_grid[:,:,i], kernel, mode='same', boundary='wrap')
130
 
131
  # Genetic Algorithm
132
  def crossover(parent1, parent2):
@@ -223,10 +225,12 @@ start_simulation = st.sidebar.button("Start Simulation")
223
  stop_simulation = st.sidebar.button("Stop Simulation")
224
  reset_simulation = st.sidebar.button("Reset Simulation")
225
 
 
 
 
 
226
  # Main simulation loop
227
  if start_simulation:
228
- total_food_collected = 0
229
- iterations = 0
230
  cluster_centers = np.array([[0, 0], [0, 0], [0, 0]])
231
 
232
  progress_bar = st.progress(0)
@@ -255,34 +259,37 @@ if reset_simulation:
255
  'learning_rate': learning_rate,
256
  'discount_factor': discount_factor})
257
  for _ in range(num_ants)]
 
 
258
 
259
- # Display final statistics
260
- st.write("## Final Statistics")
261
- st.write(f"Total Food Collected: {total_food_collected}")
262
- st.write(f"Average Food per Iteration: {total_food_collected / iterations if iterations > 0 else 0}")
263
-
264
- # Display heatmap of pheromone concentration
265
- st.write("## Pheromone Concentration Heatmap")
266
- fig, ax = plt.subplots(figsize=(10, 10))
267
- heatmap = ax.imshow(np.sum(pheromone_grid, axis=2), cmap='hot', interpolation='nearest')
268
- plt.colorbar(heatmap)
269
- st.pyplot(fig)
270
-
271
- # Display ant role distribution
272
- roles = [ant.role for ant in ants]
273
- role_counts = {role: roles.count(role) for role in set(roles)}
274
- st.write("## Ant Role Distribution")
275
- st.bar_chart(role_counts)
276
-
277
- # Display network graph of ant communication
278
- st.write("## Ant Communication Network")
279
- G = nx.Graph()
280
- for ant in ants:
281
- G.add_node(ant.position)
282
- for nearby_ant in ant.nearby_ants:
283
- G.add_edge(ant.position, nearby_ant.position)
284
-
285
- fig, ax = plt.subplots(figsize=(10, 10))
286
- pos = nx.spring_layout(G)
287
- nx.draw(G, pos, with_labels=False, node_size=30, node_color='skyblue', edge_color='gray', ax=ax)
288
- st.pyplot(fig)
 
 
6
  from sklearn.cluster import KMeans
7
  import networkx as nx
8
  from collections import deque
9
+ from scipy.signal import convolve2d
10
 
11
  # Constants
12
  GRID_SIZE = 200
 
54
  if random.random() < self.genome['exploration_rate']:
55
  action = random.choice(possible_actions)
56
  else:
57
+ q_values = [self.q_table[self.position[0], self.position[1], action] for action in possible_actions]
58
+ action = possible_actions[np.argmax(q_values)]
59
 
60
  reward = self.calculate_reward()
61
  self.update_q_table(action, reward)
 
128
  [0.1, 0.4, 0.1],
129
  [0.05, 0.1, 0.05]])
130
  for i in range(3): # For each pheromone type
131
+ pheromone_grid[:,:,i] = convolve2d(pheromone_grid[:,:,i], kernel, mode='same', boundary='wrap')
132
 
133
  # Genetic Algorithm
134
  def crossover(parent1, parent2):
 
225
  stop_simulation = st.sidebar.button("Stop Simulation")
226
  reset_simulation = st.sidebar.button("Reset Simulation")
227
 
228
+ # Initialize variables
229
+ total_food_collected = 0
230
+ iterations = 0
231
+
232
  # Main simulation loop
233
  if start_simulation:
 
 
234
  cluster_centers = np.array([[0, 0], [0, 0], [0, 0]])
235
 
236
  progress_bar = st.progress(0)
 
259
  'learning_rate': learning_rate,
260
  'discount_factor': discount_factor})
261
  for _ in range(num_ants)]
262
+ total_food_collected = 0
263
+ iterations = 0
264
 
265
+ # Display final statistics only if simulation has run
266
+ if iterations > 0:
267
+ st.write("## Final Statistics")
268
+ st.write(f"Total Food Collected: {total_food_collected}")
269
+ st.write(f"Average Food per Iteration: {total_food_collected / iterations}")
270
+
271
+ # Display heatmap of pheromone concentration
272
+ st.write("## Pheromone Concentration Heatmap")
273
+ fig, ax = plt.subplots(figsize=(10, 10))
274
+ heatmap = ax.imshow(np.sum(pheromone_grid, axis=2), cmap='hot', interpolation='nearest')
275
+ plt.colorbar(heatmap)
276
+ st.pyplot(fig)
277
+
278
+ # Display ant role distribution
279
+ roles = [ant.role for ant in ants]
280
+ role_counts = {role: roles.count(role) for role in set(roles)}
281
+ st.write("## Ant Role Distribution")
282
+ st.bar_chart(role_counts)
283
+
284
+ # Display network graph of ant communication
285
+ st.write("## Ant Communication Network")
286
+ G = nx.Graph()
287
+ for ant in ants:
288
+ G.add_node(ant.position)
289
+ for nearby_ant in ant.nearby_ants:
290
+ G.add_edge(ant.position, nearby_ant.position)
291
+
292
+ fig, ax = plt.subplots(figsize=(10, 10))
293
+ pos = nx.spring_layout(G)
294
+ nx.draw(G, pos, with_labels=False, node_size=30, node_color='skyblue', edge_color='gray', ax=ax)
295
+ st.pyplot(fig)