Advertisement
Guest User

Untitled

a guest
Apr 17th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. from __future__ import division     # Always produce floating point results
  2. import random
  3.  
  4.  
  5. gobase = 300    # prob of someone going to bathroom on given tick => 1/gobase
  6. nr_people = 25  # at least 2 or prog will crash, as we check rooms 0 and 1
  7. nr_rooms = 3
  8. bticks = 4  # how many ticks people stay in bathroom
  9.  
  10. def init():
  11.     # Set or reset starting states of global vars
  12.     global person, room_occ, freq, r0_occ, r1_occ, r1_given_r0
  13.    
  14.     # Is room n occupied?
  15.     room_occ = [False]*nr_rooms
  16.    
  17.     # freq[i] => How many times i rooms were occupied
  18.     freq = [0]*(nr_rooms+1)
  19.    
  20.     # How many times r0 occupied
  21.     r0_occ = 0
  22.    
  23.     # How many times r2 occupied
  24.     r1_occ = 0
  25.    
  26.     # How many times r1 occupied if r0 occupied
  27.     r1_given_r0 = 0
  28.    
  29.     # Person is a list of lists, where for each sublist p...
  30.     # p[0] = True/False - Is person in bathroom?
  31.     # p[1] = nr of bathroom they are in, or None of not in any
  32.     # p[2] = clock tick on which person went, or None if not in any
  33.    
  34.     person = []
  35.     for i in range(nr_people):
  36.         person.append([False,None,None])
  37.    
  38.  
  39. def goes():
  40.     # Randomly decide whether person will go to bathroom
  41.     # Prob of given person going on any given tick is 1/gobase
  42.     return random.uniform(0.0,1.0) < 1/gobase
  43.  
  44. def countgoes(n):
  45.     # For validating goes() prob dist only
  46.     cg = 0
  47.     for i in range(n):
  48.         if goes():
  49.             cg +=1
  50.     return cg
  51.  
  52. def ordertried():
  53.     # Randomly pick order in which to try rooms
  54.     return random.sample(range(nr_rooms),nr_rooms)
  55.  
  56. def gone(p):
  57.     # Has person p gone to the bathroom?
  58.     return p[0]
  59.    
  60. def timeup(p,tick):
  61.     # Time for person to come back from bathroom?
  62.     return (p[2] + bticks - 1) < tick
  63.  
  64.    
  65.  
  66. def cycle(tick):
  67.     # Run one clock cycle
  68.     for p in person:
  69.         if not gone(p):
  70.             if goes():
  71.                 goroom(p,tick)
  72.         else:
  73.             if timeup(p,tick):
  74.                 comeback(p)
  75.     examine(tick)
  76.    
  77. def examine(tick):
  78.     # Examine what is occupied and count frequencies
  79.     global r0_occ, r1_occ, r1_given_r0
  80.     occnr = sum(room_occ)
  81.     freq[occnr] += 1
  82.     if room_occ[0]:
  83.         r0_occ += 1
  84.         if room_occ[1]:
  85.             r1_given_r0 += 1
  86.     if room_occ[1]:
  87.         r1_occ += 1
  88.     #print "Tick %4d: %d rooms occupied" % (tick,occnr)
  89.            
  90. def comeback(p):
  91.     #print "Person came back from Room %d" % (p[1])
  92.     room_occ[p[1]] = False
  93.     p[0] = False
  94.     p[1] = None
  95.     p[2] = None
  96.  
  97.    
  98. def goroom(p,tick):
  99.     # Try rooms in a random order and go to first empty one
  100.     ot = ordertried()
  101.     for r in ot:
  102.         #print "Checking room %d" % r
  103.         if not room_occ[r]:
  104.             room_occ[r] = True
  105.             p[1] = r
  106.             p[0] = True
  107.             p[2] = tick
  108.             #print "Person went to Room %d" % (r,)
  109.             break
  110.     else:
  111.         # All occupied
  112.         pass
  113.    
  114. def simul(t1,t2):
  115.     # Run a simul for clock ticks t1 to t2 inclusive
  116.     for t in range(t1,t2+1):
  117.         cycle(t)
  118.     sf()
  119.        
  120. def sr():
  121.     # Show state of people and rooms; used to validate prog only
  122.     print "person"
  123.     for i,p in enumerate(person):
  124.         print i,p
  125.     print
  126.     print "room_occ"
  127.     print room_occ
  128.    
  129. def sf():
  130.     # Print counts and probs
  131.     print "sum freq = %d" % sum(freq)
  132.     print "freq"
  133.     print freq
  134.     print
  135.     for (i,f) in enumerate(freq):
  136.         print "p(c%d) = %f" % (i,f/sum(freq))
  137.     print
  138.     print "r0_occ = %d, r1_given_r0 = %d, r1_occ = %d" % (r0_occ, r1_given_r0,
  139.                                                           r1_occ)
  140.     print
  141.     print "p(r1_occ) = %f" % (r1_occ / sum(freq))
  142.     print "p(r1_given_r0) = %f" % (r1_given_r0 / r0_occ)
  143.     print "ratio = %f" % ((r1_given_r0 / r0_occ)/(r1_occ / sum(freq)))
  144.    
  145. def runsim(n=100000):
  146.     # A quick way to run a standard sim
  147.     init()
  148.     print "%d people, %d rooms, gobase=%d, bticks=%d" % (
  149.             nr_people, nr_rooms, gobase, bticks)
  150.     simul(1,n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement