October 10th, 2012, 10:00 AM
Python app for simulation
My first post!
I ask you an opinion on how you would do an application. This is a simulation.
I have to simulate a series of objects that enter in a queue (for example), with a certain speed (random assigned to each object). On the basis of this speed, will come out from the queue after a certain time. In addition, at any time, 20% of the elements in this queue have to do an operation.
So, what I need is:
1) Bringing the objects in the queue, indicating how long shall come forth;
2) See if the time of the object in the queue has expired;
3) Take 20% of the objects to make them do a task.
I would have thought of a multithreaded approach ... 3 threads, one to insert objects, one to control their expire time, and one to take the 20% and let him do the operation ...
I have not played a lot in python, so well accepted all your comments, and maybe some shortcut!
Thanks in advance!
October 10th, 2012, 01:52 PM
think it out
basically you shouldn't try and slap everything together at once, if you are told to keep track of multiple things at once try and make a few separate programs bringing in how you will handle each problem then try combine them together
also try writing out your format for the code like this:
while a is in queue
time -= 1
if time is equal to 0
something like that, i'm not going to do any of the code just this is how you should sort your problems
October 10th, 2012, 04:21 PM
Originally Posted by slyv
- help determine the important features (concentrate effort effectively, discover problems/improvements);
- predict outcomes without building the actual system (test GUT theories, build a working system cheaply without so many errors);
- explore alternatives.
Simulation should be faster than doing the real thing.
I'm certain this particular simulation has an analytic answer as in mean and standard deviation rate of growth of the queue as a function of the number of tellers (given distributions for the random events). Still, the simulation is ok. Your concept of multiple threads and timers appears easy to implement and workable.
Instead, I'd write a single thread using a loop counter as my clock.
TELLERS = 1 # is this many tellers enough?
raise(NotImplementedError('implement sample in subclasses'))
self.time = now+self.sample()
if not isinstance(other,int):
'will not compare %s with %s.'%(
return self.time < other
def sample(self): # how long does it take for the 20% task?
return 40*random.betavariate(2,5) # time to service a customer
self.__init__(now) # Whoot! never before tried this
return random.gauss(mu=10,sigma=3) # interval to next customer queuing
minute = 0
tellers = [Teller(minute) for i in range(TELLERS)]
customer = Customer(minute)
queue = 
print('minutes queue length')
for minute in range(1,60*8): # 8 hour day
if customer < minute:
customer = Customer(minute)
for teller in tellers:
if queue and teller < minute:
queue = queue[1:]
[/code] are essential for python code and Makefiles!