-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslime_mold.py
51 lines (43 loc) · 1.68 KB
/
slime_mold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import scipy.sparse as sparse
import networkx as nx
from queue import PriorityQueue
# Initialize the network
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0)])
# Initialize the parameters
N = 10 # number of plasmodium agents
evaporation_rate = 0.1 # rate of slime evaporation
# concentration of slime at each node
slime_concentration = sparse.csr_matrix((1, len(G.nodes)))
# Initialize the plasmodium agent
plasmodium = np.zeros((len(G.nodes)))
# Generate start and end nodes
start_node = np.random.choice(list(G.nodes))
end_node = np.random.choice(list(G.nodes))
# Propagate the plasmodium agent along the network using a priority queue
queue = PriorityQueue()
queue.put((-plasmodium[start_node], start_node))
while not queue.empty():
current_node = queue.get()[1]
if current_node == end_node:
break
neighbors = list(G.neighbors(current_node))
if len(neighbors) == 0:
break
for neighbor in neighbors:
next_concentration = plasmodium[current_node] / len(neighbors)
if next_concentration > slime_concentration[0, neighbor]:
slime_concentration[0, neighbor] = next_concentration
queue.put((-next_concentration, neighbor))
plasmodium[neighbor] += plasmodium[current_node] / len(neighbors)
# Update the slime concentration at each node
slime_concentration *= (1 - evaporation_rate)
# Find the shortest path between two nodes based on the slime concentration
path = [start_node]
while path[-1] != end_node:
neighbors = list(G.neighbors(path[-1]))
next_node = neighbors[np.argmax(slime_concentration[0, neighbors])]
path.append(next_node)
# Print the shortest path
print(path)