Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop until steady-state of a complex data structure in Python

Tags:

python

I have a more-or-less complex data structure (list of dictionaries of sets) on which I perform a bunch of operations in a loop until the data structure reaches a steady-state, ie. doesn't change anymore. The number of iterations it takes to perform the calculation varies wildly depending on the input.

I'd like to know if there's an established way for forming a halting condition in this case. The best I could come up with is pickling the data structure, storing its md5 and checking if it has changed from the previous iteration. Since this is more expensive than my operations I only do this every 20 iterations but still, it feels wrong.

Is there a nicer or cheaper way to check for deep equality so that I know when to halt?

Thanks!

like image 445
jozmos Avatar asked Dec 27 '22 11:12

jozmos


1 Answers

Take a look at python-deep. It should do what you want, and if it's not fast enough you can modify it yourself.

It also very much depends on how expensive the compare operation and how expensive one calculation iteration is. Say, one calculation iteration takes c time and one test takes t time and the chance of termination is p then the optimal testing frequency is:

(t * p) / c

That is assuming c < t, if that's not true then you should obviously check every loop.

So, since you can dynamically can track c and t and estimate p (with possible adaptions in the code if the code suspects the calculation is going to end) you can set your test frequency to an optimal value.

like image 199
orlp Avatar answered Jan 18 '23 22:01

orlp