Skip to content

Instantly share code, notes, and snippets.

@nullhack
Last active November 2, 2016 16:57
Show Gist options
  • Save nullhack/846aecbbd1116a825cc60a691af21493 to your computer and use it in GitHub Desktop.
Save nullhack/846aecbbd1116a825cc60a691af21493 to your computer and use it in GitHub Desktop.
Simulates a post office
"""Licensed under GPLv3 <http://www.gnu.org/licenses/>.
Authors:
Eric Lopes
"""
import random
import sys
class PostOffice:
def __init__(self):
self.queue = []
self.total_leave_time_spent = 0
self.num_customers_arrive = 0
self.num_customers_leave_without_service = 0
self.working_minutes = 0
self.idle_time = 0
self.serving_customers = []
self.num_leaves = 0
@property
def total_time_spent(self):
minute = self.working_minutes
qtime = sum([minute-customer+1 for customer in self.queue])
stime = sum([minute-customer+1 for customer in self.serving_customers])
return self.total_leave_time_spent + qtime + stime
def work(self, minutes=60*8):
for minute in range(minutes):
self.working_minutes += 1
r = random.random()
#New costumer arrives
if r<=0.6:
self.num_customers_arrive += 1
if len(self.serving_customers)<2:
self.serving_customers.append(minute)
else:
self.queue.append(minute)
#After arrives, check for idle clerks
if len(self.serving_customers)<2:
self.idle_time += 2-len(self.serving_customers)
#Each serving customer has 25% of chance to complete their business
for serving_customer in self.serving_customers:
r = random.random()
if r<=0.25:
self.num_leaves += 1
customer = self.serving_customers.pop(0)
self.total_leave_time_spent += minute-customer+1
if len(self.queue)>0:
new_costumer = self.queue.pop(0)
self.serving_customers.append(new_costumer)
r = random.random()
#At the end of minute, someone in line may give up and leave
if r<=0.05 and len(self.queue)>0:
self.num_leaves += 1
n = random.randrange(len(self.queue))
customer = self.queue.pop(n)
self.num_customers_leave_without_service += 1
self.total_leave_time_spent += minute-customer+1
if __name__== '__main__':
n = 480
if len(sys.argv)>1:
n = int(sys.argv[1])
post = PostOffice()
post.work(n)
print("avg time per customer", post.total_time_spent/post.num_customers_arrive)
print("#customers arrived", post.num_customers_arrive)
print("#cdustomers that leave without service",
post.num_customers_leave_without_service)
print("%customers that leave without service",
100*post.num_customers_leave_without_service/post.num_customers_arrive,
"%")
print("idle time", post.idle_time)
print("%idle time", 100*post.idle_time/(2*post.working_minutes), "%")
print("%leaves (with or without service)",
100*post.num_leaves/post.working_minutes, "%")
print("working minutes", post.working_minutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment