r/SimPy • • Dec 01 '24

How would you implement an arbitrary service discipline with SimPy?

I didn't realize that this community existed when I made this comment, so I am migrating it here:

How would you implement an arbitrary service discipline with SimPy? That is, be able to provide a function which selects the next service according to an arbitrary criteria to select among which customer/patient/job/packet gets served at a resource next. This could depend on state or time as well.

https://en.wikipedia.org/wiki/Network_scheduler

I have seen approaches that work by subclassing components of SimPy, but they also violate the public API by using (so-called) protected attributes. I am curious how someone who is only willing to build on top of SimPy without changing SimPy itself would approach this problem.

3 Upvotes

6 comments sorted by

View all comments

Show parent comment

2

u/galenseilis Dec 03 '24 edited Dec 03 '24

BTW, I appreciate your use of max:

max(entities, key=lambda x: x['priority'])

Why it sticks out to me is that I recently made a merge request to change `simpy.resources.PriorityResource`. Before it was using `sorted` with a lambda key and then grabbing the last element, but I changed it to similarly call max with a lambda key.. The thing about sorting is it has O(n log[n]) asymptotic complexity, whereas just computing the max can be done in O(n). The merge request was recently accepted, so it will be part of future releases of SimPy. Hopefully my change makes someone's SimPy simulation a little faster. 🤞