我正在尝试计算任何 Petrinet 的可达性图。发生计算的函数并没有让我触发启用的转换,因此 graphgh 只有带有初始标记的初始节点我不知道出了什么问题或我可以纠正什么
def calculate_reachability_tree(num_places, num_transitions, markings_vector, incidence_matrix):
# Initialize the reachability tree and markings list
reachability_tree = nx.DiGraph()
markings_list = [tuple(markings_vector.flatten())]
reachability_tree.add_node(0)
# Initialize the queue with the initial markings
queue = deque([(0, markings_vector)])
while queue:
parent_index, parent_markings = queue.popleft()
print(parent_markings)
for t in range(num_transitions):
print(num_transitions)
# Check if the transition is enabled
if np.all(parent_markings + incidence_matrix[:, t] >= 0):
print('in if')
# Fire the transition
new_markings = parent_markings + incidence_matrix[:, t]
# Check if the new markings are already in the markings list
try:
child_index = markings_list.index(tuple(new_markings.flatten()))
except ValueError:
# Add the new markings to the markings list and reachability tree
child_index = len(markings_list)
new_markings_clipped = new_markings[:num_places]
markings_list.append(tuple(new_markings_clipped.flatten()))
reachability_tree.add_node(child_index)
queue.append((child_index, new_markings_clipped))
# Add an edge between the parent and child markings
reachability_tree.add_edge(parent_index, child_index, transition=t)
return reachability_tree, markings_list