patternpythonMinor
Discrete event simulation with variable intervals
Viewed 0 times
simulationwithdiscreteintervalsvariableevent
Problem
I've built a discrete event simulation system, similar to the bank problem presented on the wikipedia page but with a key difference.
Let's say, that a
Consider the following example, where the servicing time for a single customer is of 4 time intervals and is doubled when the teller is servicing two customers at the same time:
The times are calculated using this formula:
Which gives these results for the values used above:
I came up with the following code in Python; is there something you would have done differently?
``
event ca
Let's say, that a
TELLER can service two CUSTOMERS at the same time, and that the CUSTOMER-DEPARTURE time depends upon the number of CUSTOMERS a TELLER is servicing concurrently; I need to manage the dynamic CUSTOMER-DEPARTURE event scheduling when the number of concurrently served CUSTOMERS changes.Consider the following example, where the servicing time for a single customer is of 4 time intervals and is doubled when the teller is servicing two customers at the same time:
t=0 CUSTOMER-ARRIVAL (1)
t=1 TELLER-BEGINS-SERVICE (1) --> schedules CUSTOMER-DEPARTURE (1) at t_a=5
...
t=3 CUSTOMER-ARRIVAL (2)
t=4 TELLER-BEGINS-SERVICE (1) --> teller is now servicing two customers:
- reschedule CUSTOMER-DEPARTURE (1) to t_b=6
- schedule CUSTOMER-DEPARTURE (2) at t_c=12
...
t=6 CUSTOMER-DEPARTURE (1) --> teller is now servicing only one customer:
- reschedule CUSTOMER-DEPARTURE (2) at t_d=9
...
t=9 CUSTOMER-DEPARTURE (2)The times are calculated using this formula:
t = t_current + (4 - completed) * customer_countWhich gives these results for the values used above:
- t_a = 1 + (4 - 0) * 1 = 5
- t_b = 4 + (4 - 3) * 2 = 6
- t_c = 4 + (4 - 0) * 2 = 12
- t_d = 6 + (4 - 1) * 1 = 9
I came up with the following code in Python; is there something you would have done differently?
``
import heapq
class Simulator(object):
def __init__(self):
self.queue = []
self.time = 0
def schedule(self, time, callback, *args, **kwargs):
"""
Schedules an event to be executed at a later point in time.
callback is a callable which executed the event-specific behavior;
the optional args and kwargs` arguments will be passed to theevent ca
Solution
Given the problem scope as I understand it (need to execute events in particular sequence, with ability to rearrange sequence at any point) I think the design seems clean and direct. I caveat that with: I don't know python, and I seem to be missing the part where you are ensuring sequence of your queue by the event's time member. I understand the event.time needs to be the sequencing key for your queue, or else the reschedule will have erratic behavior as self.time goes forward and backward if the queue isn't in order
The design wholesale seems clean though, to my eyes.
The design wholesale seems clean though, to my eyes.
Context
StackExchange Code Review Q#3670, answer score: 3
Revisions (0)
No revisions yet.