Supermarket Simulation Write a Python program that simulates

Supermarket Simulation

Write a Python program that simulates a check-out line at a supermarket.

Customers arrive in random integer intervals of 1 to 4 minutes.

Each customer is serviced in random integer intervals of 1 to 4 minutes.

The rates need to be balanced. If the average arrival rate is larger than the average service rate, the queue will grow infinitely. Even with balanced rates, randomness can still cause long lines.

Run the supermarket simulation for a 12 hour day (720 minutes) using the following algorithm.

1. Choose a random integer between 1 and 4 to determine the minute at which the first customer arrives.

2. At the first customer\'s arrival time: Determine the customers service time (random integer from 1 to 4) Begin servicing the customer Schedule the arrival time of the next customer (random integer 1 to 4 added to the current time)

3. For each minute of the day:

If the next customer arrives:

Say so (display a message, with the customer count starting with 1)

Enqueue the customer

Schedule the arrival time of the next customer

If the service was completed for the last customer

Say so (display a message with the customer count)

Dequeue the next customer to be serviced

Determine customer\'s service completion time

Run the simulation for 720 minutes and answer each of the following:

1) Total number of customers serviced.

2) Maximum number of customers in queue at any one time.

3) What is the longest wait any one customer experienced?

Solution

#import statements

from random import randint
from collections import deque

#customer class with arrival time and customer number as attributes

class Customer:
    arr_time = 0
    arr_count = 0
    def __init__(self, arr_time, arr_count):
        self.arr_time = arr_time
        self.arr_count = arr_count

#initialising timer

t = 0

#initialising customers queue

customers = deque()

#initialising the number of customers in queue

arr_count = 0

arrival_time = randint(1, 4)

departure_time = arrival_time + randint(1, 4)

max_wait = 0

max_size = 0

#Simulation

while t<720:

    if arrival_time == t: #Arrival of a customer
        arr_count += 1
        customers.append(Customer(t, arr_count))
        print \"Customer\", arr_count, \"has arrived\"
        arrival_time = randint(1, 4)+t

    if departure_time == t: #departure of a customer
        if len(customers)>0:
            customer_departed = customers[0].arr_count
            wait_time = t - customers[0].arr_time
            if wait_time > max_wait:
                max_wait = wait_time
            customers.popleft()
            print \"Service was completed for customer\", customer_departed
            departure_time = randint(1, 4)+t

    queue_size = len(customers)
    if max_size < queue_size:
        max_size = queue_size
    t += 1

print \"Total Number of customers serviced: \", arr_count

print \"Maximum number of customers in queue at any time: \", max_size

print \"Longest wait any one customer experienced: \", max_wait

Supermarket Simulation Write a Python program that simulates a check-out line at a supermarket. Customers arrive in random integer intervals of 1 to 4 minutes.
Supermarket Simulation Write a Python program that simulates a check-out line at a supermarket. Customers arrive in random integer intervals of 1 to 4 minutes.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site