1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power

    Python app for simulation

    Hi all,
    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!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Rep Power

    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
    leave queue

    something like that, i'm not going to do any of the code just this is how you should sort your problems
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Originally Posted by slyv
    I ask you an opinion on how you would do an application. This is a simulation.
    • 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.
    import random
    TELLERS = 1                             # is this many tellers enough?
    class c:
        def sample(self):
            raise(NotImplementedError('implement sample in subclasses'))
        def __init__(self,now):
            self.time = now+self.sample()
        def __lt__(self,other):
            if not isinstance(other,int):
                raise TypeError(
                    'will not compare %s with %s.'%(
            return self.time < other
    class Teller(c):
        def sample(self):        # how long does it take for the 20% task?
            return 40*random.betavariate(2,5) # time to service a customer
        def reset(self,now):
            self.__init__(now)           # Whoot!  never before tried this
    class Customer(c):
        def sample(self):
            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('      report')
    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]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo