0

I am trying to calculate the reachability graph for any petrinet. the function where the calculation happens is not enabeling me to fire enabeled transitions and therefore the grapgh has only the initial node with initial markings i have no idea what wrong or what i can correct

        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
samm bodie
  • 11
  • 1
  • Petri nets are just one kind of graph. I do not know the complexity of your algorithm, but if it is worst than n^3 where n is (state_count+transition_count), then you can be better using the https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm – Mathieu CAROFF Apr 21 '23 at 09:14
  • Hi, you question mentions a reachability GRAPH, but the code attempts to produce a reachability TREE, those are generally two different things. I just want to make sure that you are aware of this. Additionaly you pass the incidence matrix as an argument to you function. If the Petri net contains self-/short-loops (an arc from T to P and an arc from P to T), then you cannot decide if a transition is enabled based on the incidence matrix. Finally, your algotihm does not terminate if the Petri net has an infinite number of reachable states (assuming that the markings are not limited to 0s and 1s) – Minop Apr 21 '23 at 11:17
  • @MathieuCAROFF based on my understanding of the topic, the linked algorithm is not helpful here. Reachability for Petri nets is not defined on the Vertices of the Petri net, but on the possible markings of the graph. The algorithms, I know, are constructive, so the one in the question goes in the correct direction. – Minop Apr 21 '23 at 11:28

0 Answers0