#### Deliverables: There are 3 deliverables for this final project.
#### 1. The usual text file with R code and output that accompanies Q1 and Q2.
#### 2. A .RMD file for generating the report in Q3.
#### 3. The PDF or HTML file of the report that you make for Q3.
#### If in a question I refer to a function that we have not seen in class,
#### then use the help facility to find out about it.
#### Insert your answers under each question (Q1 and Q2 only)
#### Submit your R code solutions to Canvas as a plain text file.
#### If a question asks for a printed result, don’t just write a “print” statement;
#### include the output itself!
#### You may use any of the code published in the solutions to Assignment 5, once they are available.
# Smallville has undergone rapid development from a 26 block square to 36 blocks.
# The goal of these questions is to estimate the number of drivers (level of effort) that need to be available in order
# to meet a pre-specified level of service in terms of how long a passanger would have
# to wait at before being picked up. We will assume that there are at least as many
# drivers as passangers waiting to be picked up.(no of drivers >= no of passangers).
# As a reminder, here is the preamble about the town:
# smallville is an interesting town because it is laid out in a perfect city grid and has exactly
# 36 blocks in both the north and east directions. Roads going north-south are called Avenues,
# and roads east-west are called streets. All the drivers are waiting at street and avenue intersections.
# Passanger pickups and drop can be viewed as only happening at street and avenue intersections.
# This means that every driver, passanger pick up and drop location can be represented as a pair (i,j),
# i for the street they are on, and j for the avenue, where both i and j take integer values between 1 and 36.
# The city planners used the convention that the intersection at the south west corner of the grid is (1,1)
# and the intersection in the north east corner is (36,36).
#Q1. (41pts.) Write a function called “run_sim”, that runs a simulation, where the function takes the following four arguments
# (no default values required, but you can add them if you want).
#
# 1. num_cabs — the number of cabs/drivers initially available.
# 2. num_orders — the number of ride orders initially waiting for pick-up.
# 3. grid_size — the size of the Smallville city grid.
# 4. num_its — the number of iterations in the Monte Carlo simulation.
# The function should return a one column matrix, with the number of rows equal to num_its.
# The elements in this matrix should be the average time that a passanger wait to be picked up (where the average
# is taken over all ride orders (not drivers) within an iteration).
# Because grid_size is now a variable you will want to add an extra argument to your “newdriver” and “neworder”
# functions from Assignment 5, to reflect this fact. Grid size can default to 36.
# The problem you will face in this question is that in Assignment 5, the number of drivers equalled the number of orders,
# whereas now that is not necessarily true (At times more drivers).
# If the number of ride orders equals the number of drivers then the
# distance matrix is square (the same number of rows as columns).
# The optimization function, “lp.assign” in fact requires a square matrix as input and will fail to converge otherwise.
# The trick here, when the number of drivers does not equal the number of orders, is to pad the distance matrix
# with extra *zeroes* (not NAs), so that it becomes square.
# If we have drivers as rows and ride orders(passangers) as columns in the distance matrix,
# and lets say there were 20 drivers and 10 rider orders,
# then you would need to add 10 columns of 20 rows each to make the distance
# matrix square. Because these extra columns (phantom orders) are constructed with all zero entries they don’t change
# the solution to the optimization problem.
# Equivalently, you could create a distance matrix as square and full of zeroes,
# and then just compute the elements that correspond to actual orders.
# You can cosider drivers, who got assigned to these phantom ride orders, are waiting for (real) orders.
# Bottom line: you have to add in an extra step as compared to Assignment 5, where you ensure that the
# distance matrix is square if necessary, before calling “lp.assign”.
#a. Check all the arguments to make sure that they are non-negative numerics.
# The functions “stopifnot” and “is.numeric” will help you do this.
#b. The other thing to check, and stop if it is not true,
# is that the number of drivers is greater than or equal to the number of rider orders.
#c. Paste the code for your run_sim function. It will implicitly include your answers to parts a and b.
# Run the function with these arguments and report the estimated mean waiting time for each:
#d. run_sim(num_drivers = 30, num_orders = 30, grid_size = 36, num_its = 100)
#e. run_sim(num_drivers = 40, num_orders = 30, grid_size = 36, num_its = 100)
#f. run_sim(num_drivers = 60, num_orders = 20, grid_size = 36, num_its = 100)
#g. run_sim(num_drivers = 15, num_orders = 20, grid_size = 36, num_its = 100)
#h. run_sim(num_drivers = 20, num_orders = 20, grid_size = 36, num_its = TRUE)
#Q2 (16 pts.) We now assume that there are 30 ride orders waiting, the grid size is 36 and num.its = 1000.
# In this question you will explore the average order wait time as the
# number of driver varies between 30 and 80 in increments of 5.
# As you *develop* your code, it will be a good idea to work with a num_its much smaller than 1000,
# but the final results should use 1000. It took 13 minutes to run the complete simulation
# on my laptop.
#a. Create an empty container to hold the results of all the simulations.
# This should be a matrix of dimensions num_its by 11.
# Provide columns names for this matrix that describe the level of effort (represnted by no of drivers)
# associated with each column.
# Show the code you used to create this matrix.
#b. Set the random number seed to your birthday seed and show the code.
#c. Use a “for” loop to execute the run_sim command as the number of driver varies.
# Each pass through the loop should populate one column of the results container.
# Show the code that implements the for loop.
#d. Estimate the mean wait time for each level of effort (number of drivers)
# and print them below.
#e. Use the “write.csv” command to save the results container to disk and paste the command
# you used to do it below. Using the argument “row.names = FALSE” to write.csv, can save
# a little bit of pain later.
#Q3. (28 pts.) Using RMarkdown, create a markdown document that summarizes your simulation and render the
# document as either a powerpoint or PDF slide presentation.
# Your markdown document will read in the results file that you saved
# in Q2e to answer this question.
#a. The document should contain the following elements:
# A title slide with a project title, your name and PennID.
# To add penn id you can use following command in title slide
#b. A slide that presents the date on which the document was rendered (not just created),
# the name of the simulation results file and the number of iterations in the simulation
# (calculated after having read in the file that you saved in Q2e, not hardcoded).
#c. A plot that shows the level of effort (on the x-axis) against estimated average wait time on the y-axis.
# It should also have a horizontal line added at height y = 5, so that an optimal level of effort can
# be visualized. We want to know the situation when average passanger wait time drops below 5 minutes.
# Axes should be labeled appropriately (use arguments xlab and ylab). The commands “plot” and “abline”
# are enough to make the plot. Make the y-axis go between 0 and 15 by using the argument to plot, “ylim=c(0,15)”.
# Add a text comment to this slide that states what the least number of driver is, to meet the effort goal.
# You can just eyeball this answer from the plot.
#d. A plot that shows the level of effort (on the x-axis) against the 90% percentile of the distribution of the
# mean wait time. That is, apply the “quantile” command to the columns of your results matrix and plot the
# result. Axes should be labeled appropriately. It should also have a horizontal line added at height y = 5.
# Add a text comment to this slide that states what the least number of drivers is to meet the effort goal, when
# expressed in terms of the 90-th percentile of the distribution of the mean.
# Again, you can just eyeball this answer from the plot.