fpom / snakes

SNAKES is the Net Algebra Kit for Editors and Simulators
https://snakes.ibisc.univ-evry.fr
Other
89 stars 29 forks source link

Non deterministic drawing of petri nets #27

Closed maxhoerstr closed 1 year ago

maxhoerstr commented 1 year ago

Hello,

if i execute the following dummy code to draw the created petri net, the result seems to be different in every run.

def main():
    net = PetriNet("petri_net")
    net.add_place(Place("1", []))
    net.add_place(Place("2", []))
    net.add_place(Place("3", []))
    net.add_transition(Transition("4"))
    net.add_transition(Transition("5"))
    net.add_input("1", "4", Value(1))
    net.add_input("2", "5", Value(1))
    net.add_input("3", "4", Value(1))
    net.add_input("3", "5", Value(1))
    net.draw("petri_net.png", "dot")

I need the drawing to be deterministic, so on every run, the same petri net is drawn. Is this possible in the current version of the library?

Below, 2 possible images for the petri net above are shown.

image image

Thank you in advance!

fpom commented 1 year ago

I suspect you're using an old version of Python (2.x) in which dict iteration order varies from one execution to another. This may influence the order in which GraphViz considers them for computing a layout. On my recent version of Python, the result is deterministic with dot layout. However, note that GraphViz may use randomized algorithms for other layouts, so the result may be non-deterministic by construction.

I've just committed a patch to plugin gv that sorts the nodes provided to GraphViz, this should solve your issue if it indeed caused by iteration orders. Let me know if it's better now.

maxhoerstr commented 1 year ago

Thank you very much for the quick response! This indeed solved my problem and now all my nets are drawn the same on every run, nice! I'm using Python 3.10 at the moment, and python version 3.x before, so i dont think Python 2.x was the source of problem. My team members also had the same problems.

Could you update the pip package so we can use the updated version? Thanks in advance!

fpom commented 1 year ago

My bad, I tested without restarting Python, and indeed iteration order may vary from one execution to another, even with newer Python versions. Anyway, sorting fixes the problem. I'll push a new release on PyPi.