For this computer assignment, you are to write and implement a Python program to simulate a typical grocery store. The purpose of this program is simply to collect statistical data during the simulation and printout the final statistics at the end of the simulation. In this model, customers enter the store by random inter-arrival times and then start picking up the grocery items, which takes a random amount of time. After picking up the grocery items, a customer gets in the checkout line to pay for his/her items. The duration of the time spent in the checkout line is also random.

Programming Notes:

1. All random numbers in simulation have integer values and the time unit for the simulation clock is in minutes. Before starting to generate random numbers, call the function seed ( ) with the argument SEED = 1 to initialize the RNG. To get a random number in the range [ low, high ], execute: randrange ( low, high + 1 ). For inter-arrival times, the range is [ MIN_INT_ARR = 1, MAX_INT_ARR = 5 ]; for the time to pick up the grocery items, the range is [ MIN_PICK = 1, MAX_PICK = 10]; and for the service time in the checkout line, the range is [ MIN_SERV = 1, MAX_SERV = 4 ], which excludes the waiting time in line.

2. To capture the data values of a customer as a single unit, define a dictionary with keys: 'atime, ptime', wtime and dtime, where atime is the arrival time, ptime is the time of completion of picking up the grocery items and then entering the checkout line, wtime is the waiting time in the checkout line, and dtime is the departing time from the store of a customer.

3. For customers who are in the process of picking up the grocery items, define a list of dictionaries, where each element of the list corresponds to a customer.

4. To capture the statistical values as a single unit, define a dictionary with keys: 'num', pick, wait, serv and shop, where num is total number of customers departed from the store, pick is the accumulated grocery picking times, wait is the accumulated waiting times, serv is the accumulated service times, and shop is the accumulated time spent in the store over all customers.

5. In the main ( ) function, initialize the simulation clock to 0, initialize the events: next_arr, next_pick, next_serv, next_dept to 0, None, None, None, correspondingly, and initialize all statistical values to 0, where next_arr is the arrival time of the next customer, next_pick is the time of a customer who is next to finish of picking up the grocery items and then enter in the checkout line, next_serv is the time of a customer who is next to be served in the checkout line, and next_dept is the departing time of a next customer from the store. The main ( ) function calls one of the following functions for a given event at the clock time and updates the clock to the time of the next event. The simulation stops if the clock >= SIMTIME, where SIMTIME = 30 * 24 * 60.

  • def Arrival ( clock, picking ): It's called when a new customer arrives in the store. It calls the RNG to generate two random numbers to determine the arriving time of the next customer and the completion of the grocery picking time of the current customer by adding the current clock time to these numbers. Creates a dictionary object for the current customer and enters the object in the list of picking, which is a list of dictionaries, and then searches the list to find out the minimum picking value for all customers in picking. At the end, it returns the arriving time of the next customer and the minimum picking value to the main ( ) function as a tuple.
  • def Shopping ( clock, picking, checkout ): It's called when a customer completes the picking up the grocery items and is ready to enter the checkout line at the clock time. It searches the list picking to find out the customer with the minimum picking value and then removes that customer from picking. If the list checkout is not empty, it calls the RNG to create a random number to determine the departing value of the customer from the store by adding the current clock time to this number and sets the waiting time of the customer to 0. At the end, it enters the removed customer from picking in the list checkout and returns the final states of the lists picking and checkout to the main ( ) function.
  • def Departure ( clock, checkout, total ): It's called when a customer departs from the store. It removes the first customer from the list checkout and calls the function update_stat ( ) to update the statistical values with the time values of this customer. If the checkout will still have more customers waiting for service, it assigns current clock time as the waiting time for the first customer of the remaining customers in checkout, it calls the RNG to create a random number to determine the departing value of the customer from the store by adding the current clock time to this number. At the end, it returns the departing value of the next customer to the main ( ) function, but if the checkout becomes empty after the departing customer, it returns None.
  • def update_clock ( next_arr, next_pick, next_dept ): It finds out minimum value from the values of its arguments next_arr, next_pick, and next_dept, and it returns that value to the main ( ) function. If the values of next_pick and/or next_dept is None, then the minimum value is determined from the remaining values. The main ( ) function simply use the return value to determine which of the three functions: Arrival ( ), Shopping ( ) or Departure ( ) will be called for a next event. If two or three events happen at the same time, then the main ( ) function chooses the next event in the priority order, where next_arr has the highest and next_dept has the lowest priority.
  • def update_stat ( cust, total ): It updates the values in the dictionary total with the values in the dictionary cust and it increases the number of departed customers by 1, where the values in cust are the clock time values, but the values in total are the time-difference values. For example, the shop time of a customer in the store is the difference between the departing time and the arrival time of a customer. At the end, this function returns the accumulate values in total to the print_stat ( ) function.
  • def print_stat ( total ): It prints out all statistical values on stdout, where each floating-point number should be printed with 3 digits after the decimal point (right aligned) and the number of customers should be printed with separating commas, like in business applications.
  • def rnd ( low, high ): It's called to generate a random integer in the range [ low, high ].

6. After every event, you need to print the type of the event and the clock time of its occurrence, but since the clock runs for a long simulation time, limit the printing for only up to events but no later than the N = 10 departing events. In case, your program fails to generate correct simulation results, you can trace the output of your program using these printed values or trace your program with any value of N >= 1.

7. One of possible techniques to find the minimum value corresponding to a key in a dictionary, in a list of dictionaries, is a list comprehension method. Using this technique, you can create a list of the dictionary values, and then use the min ( ) function on the resulting list to get the first minimum value in all dictionaries for a given key. If you need to remove the dictionary from a list, you can use the index ( ) function on the list to get the corresponding index value, and then remove the dictionary from the list using that index value.

8. There are several constant values that you need to use in your program. It's a good idea to define those values in a separate file, say header.py, and import the file in your program prog8.py.

9. Before starting to execute your program, make it executable by the statement: chmod u+x prog8.py. For a final test, you can execute your program as: Make N=8. When the execution is over, the file prog8.out will contain the output, as well as the error messages (if any). You can find the correct output in file prog8.out, which is in the same directory with the data files.

10. In your program, do not use any global variables and try not to use any redundant variables and characters.

11. Your program needs to be fully documented. Without a proper documentation, a program does not carry much value to its users. As specified in the course syllabus, your program will be graded by three individual criteria: correctness, efficiency, and documentation.

12. At the top of your program, insert the following lines:

#!/usr/bin/python3
from random import seed, randrange
from header import *
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.