Advertisement
Guest User

Mefi Bathroom AskMe

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