PIC 10A Section 2 – Homework #7 (due Friday, November 22, by 6 pm)
You should upload each .cpp (and .h file) separately and submit them to CCLE before the due date/time! Your work will otherwise not be considered for grading. Do not submit a zipped up folder or any other type of file besides .h and .cpp.
Be sure you upload files with the precise name that you used in your edi- tor environment otherwise there may be linker and other errors when your homeworks are compiled on a different machine. Also be sure your code com- piles and runs on Visual Studio 2019.
SIMULATING CUSTOMERS AT CHECKOUTS
This homework is about writing classes and functions. Read all the instructions care- fully and do EXACTLY what is asked. It is not that hard but you need to understand how it all fits together and deviating from the instructions is dangerous here. In a nutshell, we are interested in the following scenario:
A store has business hours during which time customers can enter the store. After en- tering the store, the customers form one single queue. As a cashier at a checkout becomes available, they go to the checkout to make their purchases (we ignore the possibility of them shopping in the store beforehand). No more customers come in once closing time is reached, but all remaining customers are processed before leaving. We would like to know how long, on average, a customer spends in the store (waiting in line time plus purchasing time at the checkout), along with the entry/exit times of all customers over the day. For simplicity, we model time as a discrete variable (doing things in time steps) rather than continuously.
To model this, we work with two classes: HourMin to describe time in the format of Hour:Min, and Customer to describe a customer (when they arrived, how long they take, etc.).
Provided for you is:
• Source.cpp, which gives a main routine and implements the logic of customers coming in/out, and printing the statistics (this file might be modified when your homeworks are being tested);
• Simulation.h, which declares a number of utility functions, some which you have 1
to define yourself, while others are given in
• Provided.cpp, giving some definitions of the functions for simulation. This file will be modified when your homework is tested!
• HourMin Interface.txt giving an interface (it is not a header file!!!), without any documentation or implementations, for HourMin. It is just a hint to help you get started. You will likely find its contents useful but then need to write a proper header file.
Note the code provided has two possible scenarios to help you with debugging/testing your code, depending on the lines you comment/uncomment.
Scenario (1) is simple: 1 customer always comes in per minute; there are always 20 check- outs; every customer always takes exactly 60 minutes at the checkout, and the day runs from 8:00-8:40.
Scenario (2) is more realistic: each minute there is only a probability 0.25 that a customer will enter; the time it takes a customer at a checkout once there has an exponential dis- tribution (just a mathematical way to model variations) with average of 5 minutes; and the number of checkouts is less between 12:00 and 13:00, only 2 compared to the normal of 6.
In the end, you will submit 3 or 4 files: Customer.h, HourMin.h, Simulation.cpp, and Definitions.cpp (optional).
Customer.h provides the definition of the Customer class. You have a choice about defining the member functions and constructor inside the class interface or defining the member functions in Definitions.cpp.
HourMin.h provides the definition of the HourMin class. You have a choice about defining the member functions and constructor inside the class interface or defining the member functions in Definitions.cpp.
Simulation.cpp should provide the definitions for the enter, exp rand, bernoulli rand, finish, and help functions.
Definitions.cpp only needs to be included if you choose to define your member func- tions/constructors outside of the class interfaces.
Okay, so let’s talk about the classes. The HourMin class should 2
• store 2 member variables, hour and min, both of type int to represent the hour and minute of the day.
• have a single constructor accepting values for hour and min, initializing them appropriately.
• have a private mutator function format that properly formats the HourMin object so that 8:62 would become 9:2, for example (this function may need to be called within the class).
• have an advance min mutator function that accepts an int for how many minutes to advance by, updating the time.
• have a before accessor function that accepts an HourMin object, call it other, returning true if the time comes before other and false otherwise.
• have a minutes to accessor function that accepts an HourMin object, call it other, returning the number of minutes from the time to other.
• have an as string accessor function that returns a std::string representation of the given object.
You should write the HourMin class first. The Customer class should
• store 4 member variables: start and end, of type HourMin, for when the customer enters/leaves; duration, of type int, for how many minutes their checkout takes; and being helped, of type bool, for whether they are at a checkout being helped.
• have a single constructor accepting an HourMin for when they arrived with start
and end initially set to this value, the duration being initialized from the get duration in min function; and being helped set to false.
• have a mutator help function, accepting an HourMin for the current time, such that if they are not currently being helped, it changes being helped to true and sets the end time to now advanced by duration.
• have an is being helped accessor function that returns true if they are being helped and otherwise false.
• have an is done accessor function that accepts an HourMin for the current time. If they are being helped and the current time is at or past end the function returns true and otherwise returns false.
• have a get minutes accessor function that returns the number of minutes from start to end.
• have a print accessor function that prints “Arrival: [START] Exit: [END]” where [START] and [END] are start and end.
3
By reading the Simulation.h file, you should know how enter, bernoulli rand, finish, and help should be defined.
For exp rand here is an important fact: to generate an exponential random variable with mean λ, first generate a random variable U that is uniformly distributed on [0, 1) (closed at 0, open at 1!). Then calculate −λ ln(1 − U ).
The desired format is below:
Today there were [SOME NUMBER] customers. They spent on average [SOME NUM- BER] minutes in the store. Their arrivals/exits were: Arrival: [ARRIVAL TIME] Exit: [EXIT TIME]
Arrival: [ARRIVAL TIME] Exit: [EXIT TIME]
…
Arrival: [ARRIVAL TIME] Exit: [EXIT TIME]
Outputs for case (1) and case (2) are below. Note that case (2) has random numbers involved so your results may differ.
Case 1:
Today there were 40 customers. They spent on average 80 minutes in the store.
Their arrivals/exits were:
Arrival: 8:0 Exit: 9:0
Arrival: 8:1 Exit: 9:1
Arrival: 8:2 Exit: 9:2
Arrival: 8:3 Exit: 9:3
Arrival: 8:4 Exit: 9:4
Arrival: 8:5 Exit: 9:5
Arrival: 8:6 Exit: 9:6
Arrival: 8:7 Exit: 9:7
Arrival: 8:8 Exit: 9:8
Arrival: 8:9 Exit: 9:9
Arrival: 8:10 Exit: 9:10
Arrival: 8:11 Exit: 9:11
Arrival: 8:12 Exit: 9:12
Arrival: 8:13 Exit: 9:13
Arrival: 8:14 Exit: 9:14
Arrival: 8:15 Exit: 9:15
Arrival: 8:16 Exit: 9:16
Arrival: 8:17 Exit: 9:17
Arrival: 8:18 Exit: 9:18
Arrival: 8:19 Exit: 9:19
Arrival: 8:20 Exit: 10:0
4
Arrival: 8:21 Exit: 10:1
Arrival: 8:22 Exit: 10:2
Arrival: 8:23 Exit: 10:3
Arrival: 8:24 Exit: 10:4
Arrival: 8:25 Exit: 10:5
Arrival: 8:26 Exit: 10:6
Arrival: 8:27 Exit: 10:7
Arrival: 8:28 Exit: 10:8
Arrival: 8:29 Exit: 10:9
Arrival: 8:30 Exit: 10:10
Arrival: 8:31 Exit: 10:11
Arrival: 8:32 Exit: 10:12
Arrival: 8:33 Exit: 10:13
Arrival: 8:34 Exit: 10:14
Arrival: 8:35 Exit: 10:15
Arrival: 8:36 Exit: 10:16
Arrival: 8:37 Exit: 10:17
Arrival: 8:38 Exit: 10:18
Arrival: 8:39 Exit: 10:19
Case 2:
Today there were 123 customers. They spent on average 7.27642 minutes in the store.
Their arrivals/exits were:
Arrival: 8:23 Exit: 8:24
Arrival: 8:24 Exit: 8:25
Arrival: 8:25 Exit: 8:27
Arrival: 8:27 Exit: 8:27
Arrival: 8:20 Exit: 8:32
Arrival: 8:33 Exit: 8:44
Arrival: 8:46 Exit: 8:48
Arrival: 8:56 Exit: 8:58
Arrival: 8:54 Exit: 9:0
Arrival: 9:0 Exit: 9:4
Arrival: 9:2 Exit: 9:4
Arrival: 9:15 Exit: 9:19
Arrival: 9:20 Exit: 9:23
Arrival: 9:18 Exit: 9:24
Arrival: 9:22 Exit: 9:25
Arrival: 9:29 Exit: 9:36
Arrival: 9:34 Exit: 9:39
Arrival: 9:30 Exit: 9:44
Arrival: 9:36 Exit: 9:48
Arrival: 9:44 Exit: 9:55
Arrival: 9:48 Exit: 9:56
5
Arrival: 9:53 Exit: 10:2
Arrival: 9:50 Exit: 10:3
Arrival: 10:1 Exit: 10:4
Arrival: 10:2 Exit: 10:5
Arrival: 10:6 Exit: 10:8
Arrival: 10:8 Exit: 10:13
Arrival: 10:10 Exit: 10:13
Arrival: 10:11 Exit: 10:14
Arrival: 10:12 Exit: 10:17
Arrival: 10:17 Exit: 10:18
Arrival: 10:14 Exit: 10:19
Arrival: 10:20 Exit: 10:21
Arrival: 10:33 Exit: 10:43
Arrival: 10:27 Exit: 10:45
Arrival: 10:42 Exit: 10:46
Arrival: 10:58 Exit: 10:59
Arrival: 10:57 Exit: 11:3
Arrival: 11:0 Exit: 11:4
Arrival: 11:1 Exit: 11:8
Arrival: 11:4 Exit: 11:10
Arrival: 11:12 Exit: 11:12
Arrival: 10:59 Exit: 11:15
Arrival: 11:24 Exit: 11:28
Arrival: 11:26 Exit: 11:29
Arrival: 11:38 Exit: 11:40
Arrival: 11:39 Exit: 11:41
Arrival: 11:36 Exit: 11:42
Arrival: 11:47 Exit: 11:51
Arrival: 11:48 Exit: 11:52
Arrival: 11:46 Exit: 11:54
Arrival: 11:53 Exit: 11:57
Arrival: 11:59 Exit: 11:59
Arrival: 12:0 Exit: 12:9
Arrival: 12:4 Exit: 12:10
Arrival: 12:7 Exit: 12:19
Arrival: 12:20 Exit: 12:26
Arrival: 12:16 Exit: 12:31
Arrival: 12:25 Exit: 12:33
Arrival: 12:26 Exit: 12:33
Arrival: 12:33 Exit: 12:33
Arrival: 12:31 Exit: 12:41
Arrival: 12:41 Exit: 12:45
Arrival: 12:43 Exit: 12:47
Arrival: 12:49 Exit: 12:50
6
Arrival: 13:8 Exit: 13:10
Arrival: 12:46 Exit: 13:14
Arrival: 13:10 Exit: 13:14
Arrival: 13:12 Exit: 13:14
Arrival: 13:17 Exit: 13:21
Arrival: 13:19 Exit: 13:21
Arrival: 13:20 Exit: 13:29
Arrival: 13:23 Exit: 13:38
Arrival: 13:33 Exit: 13:44
Arrival: 13:21 Exit: 13:47
Arrival: 13:39 Exit: 13:47
Arrival: 13:36 Exit: 13:50
Arrival: 13:44 Exit: 13:52
Arrival: 13:41 Exit: 13:58
Arrival: 13:48 Exit: 14:0
Arrival: 13:46 Exit: 14:5
Arrival: 13:50 Exit: 14:5
Arrival: 13:55 Exit: 14:11
Arrival: 14:6 Exit: 14:14
Arrival: 14:17 Exit: 14:19
Arrival: 14:20 Exit: 14:24
Arrival: 14:23 Exit: 14:25
Arrival: 14:28 Exit: 14:38
Arrival: 14:36 Exit: 14:42
Arrival: 14:38 Exit: 14:42
Arrival: 14:33 Exit: 14:45
Arrival: 14:42 Exit: 14:58
Arrival: 14:51 Exit: 15:5
Arrival: 15:0 Exit: 15:6
Arrival: 14:53 Exit: 15:10
Arrival: 15:2 Exit: 15:10
Arrival: 15:10 Exit: 15:11
Arrival: 15:12 Exit: 15:18
Arrival: 15:20 Exit: 15:23
Arrival: 15:19 Exit: 15:27
Arrival: 15:25 Exit: 15:32
Arrival: 15:33 Exit: 15:37
Arrival: 15:37 Exit: 15:43
Arrival: 15:51 Exit: 15:57
Arrival: 15:59 Exit: 16:1
Arrival: 16:4 Exit: 16:9
Arrival: 16:0 Exit: 16:11
Arrival: 16:5 Exit: 16:15
Arrival: 16:8 Exit: 16:17
7
Arrival: 16:20 Exit: 16:23
Arrival: 16:22 Exit: 16:28
Arrival: 16:28 Exit: 16:36
Arrival: 16:30 Exit: 16:38
Arrival: 16:34 Exit: 16:43
Arrival: 16:41 Exit: 16:43
Arrival: 16:42 Exit: 16:47
Arrival: 16:43 Exit: 16:52
Arrival: 16:36 Exit: 16:56
Arrival: 16:47 Exit: 16:57
Arrival: 16:48 Exit: 16:59
Arrival: 16:44 Exit: 17:3
Arrival: 16:56 Exit: 17:4
Arrival: 16:51 Exit: 17:9
Remarks on the mathematics: You can ignore this if you are not interested in the math behind this model. But some of you may be math students so this could be relevant/interesting.
0 ,
• If U ∼ uniform([0, 1]), the cumulative distribution function for U is F (u) = u,
u < 0
0 ≤ u ≤ 1, u > 1 .
0, w<0
will have a cumulative distribution function G(w) = 1 − exp(w/λ), 0 ≤ w. .
Choosing the value of an exponential random variable from a uniform random vari- able amounts to choosing a value that is “as extreme” as the uniform case. In particular, if U = u then the probability Pr(U ≤ u) = F(u). And a corresponding value for W bearing the same probability is Pr(W ≤ u) = G(w∗) for some w∗. This can be inverted to yield w∗ = G−1(u) = λ ln(1 − u), which is the value we choose for W given U.
• The requirement that U ∈ [0, 1), i.e. being in an interval that is open at 1 should not be too surprising. If U = 1, that translates to W = −λ ln 0, which isn’t a great value to have in a simulation...
• The choice of having one person enter with a probability p every dT minutes is a
cheap approximation to a better model, namely a Poisson distribution. There, we’d
need to compute terms in a probability mass function of the form e−λλn . I felt doing n!
that might be overkill.
• Also modelling the customer checkout time as an exponential random variable could be improved. In this model, we could kind of think that the number of items cus- tomers purchase is exponentially distributed (who knows what the actual distribu- tion is) and the time to process each item is roughly the same...
1 ,
Now suppose that W ∼ exp(λ) is an exponential random variable with mean λ. It
8
• The random numbers generated from std::rand are not that good, really. Modern C++ offers better approaches, even offering library functions to help generate all sorts of distributions: Poisson, exponential, Gaussian, etc. But it’s harder to set up so we don’t cover it in this class.
9