COSC1003/1903 Assignment
COSC2002/2902 Computational Modelling Assignment — due Sunday May 26th, 11:59pm
The assignment consists of two questions (COSC2002) or three questions (COSC2902). It is worth 10% of your total assessment for this Unit of Study. You will be marked on the correctness and quality of the code, as well as your written responses to the questions. You may submit your solutions independently or as a pair. If you submit as a pair you will both receive the same mark. Both members of the pair must be in the same stream (COSC2002 or COSC2902).
You should submit your assignment as a jupyter notebook through the Canvas submission portal. Your notebook should be named using your SID (e.g. cosc 012345678.ipynb), or both SIDs if you are submitting as a pair (e.g. cosc 012345678-111222333.ipynb).
Written responses to assignment questions should also be included in the jupyter notebook, using the ‘markdown’ option for a cell (so that your whole notebook can still be run without error). Check that your notebook runs before submission.
Cooperation between students is encouraged. If submitting as a pair please both submit the note- book (with both your SIDs in the file name), so that Canvas records a submission for both members of the pair. If you submit this assignment with another student, you certify that you have both made a fair contribution to it, and are happy to both receive the same mark.
Assignments that have simply been copied will not be accepted. Copying the work of another person without acknowledgement is plagiarism and contrary to University policies. By uploading your submission to Canvas, you are certifying that you have read and understood the University Academic Dishonesty and Plagiarism in Coursework Policy.
1
COSC1003/1903 Assignment
Question 1 (10 marks)
Once you know how to run simulations, the obvious thing to do is see what would happen in a zombie apocalypse.
Conceptually, the zombie apocalypse works as follows. We begin with a 2-dimensional grid of squares (the “world”). A number of healthy people are randomly distributed throughout the world, as well as one zombie. Each day, each person either stays still or moves one square up, down, right, or left, or one square diagonally. If a zombie spends a day on the same square as a healthy person, the healthy person becomes infected (i.e., becomes a zombie).
Your implementation should have four parts. First, specify the parameters of the simulation. Second, initialise the people who will live (and die) in the simulation. Third, run the simulation (i.e., at each timestep, update the locations of each person and whether or not they are a zombie). Finally, plot and output the results.
Parameters
The simulation should be initialized with four parameters: the size of the 2-dimensional world people inhabit (sidelength, default 40 squares per side), the maximum time to run the simulation for (maxtime, default 1000 days), the number of people (npeople, default 100), and the rate at which zombies spontaneously enter remission and return to being healthy humans (remission, default 0).
People
Each person will be associated with a number of pieces of information: (i) their x coordinate (an integer between 1 and the size of the world), (ii) their y coordinate (same), and (iii) whether or not they’re a zombie.
Simulation
The “simulation” consists of the calculations that occur inside the loop over time.
First, we need to figure out where people move to. For this, we’ll need an inner loop over people. At each timestep there are nine options, corresponding to the eight immediate neighbours of a person, and their current position (meaning they don’t move). If people move outside the world (x, y < 0 or x, y > sidelength-1), they come through the other side of the world (i.e. we shall assume we have periodic boundary conditions). So, a person who moves to x = −1 will instead move to
x =sidelength-1.
Second, we need to figure out which people are zombies and which are not zombies.
Third comes the tricky part: we need to handle infections. To do this, we can loop over just the
zombies, then loop over all the non-zombies. For each zombie/non-zombie pair, we need to check whether the non-zombie is occupying the same square as the zombie (i..e., their x and y coordinates are identical), and if so, infect the non-zombie (i.e. turn the non-zombie into a zombie).
Finally, for each zombie, we need to check if they spontaneously return to being a human: i.e., with probability remission at each time step the zombie may turn back into a non-zombie.
Plotting
Construct a scatter plot, where a dot shows the position of a zombie or a non-zombie on the xy plane. Plot healthy people in blue and zombies in green.
2
COSC1003/1903 Assignment
If you’ve done everything correctly, the simulation should take about 1 second of actual time for every 10 days of simulated time, and you should get an output like this:
Questions
(1.1) What is the average length of time until the last human gets infected? Provide also an estimate of the uncertainty in your value.
(1.2) Averaging over many runs: Plot the number of zombies as a function of time.
(1.3) Averaging over many runs: Plot the number of infections per day (the infection rate) as a func- tion of the fraction of zombies in the population.
(1.4) Given your results in (1.3), postulate a discrete map for the zombie fraction (Z), i.e. Zn+1 = f(Zn,tn).
(1.5) Ifyouhalvethesizeoftheworld(i.e.,setsidelength=20),howdoesthischangethetimeit takes until the last human is infected? Briefly discuss the real-world implications of this result, e.g. in terms of how diseases such as tuberculosis spread in crowded hospitals and prisons.
(1.6) Using the original size of the world (sidelength=40), change the spontaneous zombie re- mission rate from 0 to 0.01 (i.e., every 100 days on average a given zombie will spontaneously return to being human). Discuss the new dynamics of the model, with reference to steady states and/or equilibrium points (if you wish, you can also include a graph of the number of zom- bies over time to illustrate your point). Be careful to identify all possible types of behaviour (a “type” being a qualitatively distinct behaviour).
3
COSC1003/1903 Assignment
Question 2 (10 marks)
Looking at a bus timetable it is mysterious that often we seem to be waiting much longer than we should be. Fortunately with the development of real-time bus tracking the need to look at bus timeta- bles is a thing of the past. However, we still might like to know how long we expect to wait at a bus stop if we arrive randomly and just hope for the best. In this question we shall simulate waiting for buses, under various assumptions about bus arrival times, and try to determine the average wait time experienced by people at a bus stop. We shall compare our simulated bus statistics with example real-world data, and seek to conclude how accurate our assumptions and conclusions are.
We shall look at three different bus models, all with the same average time of 10 minutes between bus arrivals:
Model 1: Model 2:
Model 3:
We assume there are N buses in time TN and that TN /N = 10 minutes per bus, i.e. on average the buses are 10 minutes apart, but the buses could arrive any time between T = 0 and T = TN ;
We assume the buses are scheduled to arrive every 10 minutes, but that they are uniformly distributed between being up to x minutes early, and up to x minutes late, i.e. ‘lateness’ is a uniform distribution from −x to +x;
We assume the buses are scheduled to arrive every 10 minutes, but that the nth bus is offset from the scheduled arrival time by dn minutes, where dn is chosen from a normal distribution with mean 0 and standard deviation σ.
Note that in Model 2 and Model 3 it is possible for a scheduled bus to arrive before an earlier scheduled bus. The models thus allow for the possibility of a bus overtaking another bus that left the depot earlier, which is possible for bus stops towards the end of a bus route (where there’s the most variability in bus arrival times). Practically, this means we should make sure the bus arrival times are sorted to appear in increasing order, before we seek to determine how long a particular passenger has to wait.
A basic strategy to determining passenger wait times would be the following:
• Set up bus arrival times, in order of increasing time;
• Set up all passenger arrival times, in order of increasing time;
• For each passenger, identify which bus the passenger will be getting on; • For each passenger, determine the passenger wait time.
To proceed we should also decide on the number of buses and the number of passengers. The more buses, the better will be your statistics. For the number of passengers this will be determined by the average number being picked up at the bus stop. An average of between 5 and 10 would seem to be reasonable, so total passengers will be 5 (or 10) times the number of buses.
(2.1) For each Model carry out the following (for Model 2, take x = 5, and for Model 3, take σ = 4):
(i) Confirm that the average interval between buses in each case is 10 minutes; (ii) Determine the average waiting time for people;
(iii) Plot a histogram of the interval between bus arrivals;
(iv) Identify the nature of the distribution of bus intervals for each model.
4
COSC1003/1903 Assignment
(2.2) How does the estimate of average wait time depend on x (for Model 2) and σ (for Model 3)? A short explanation of the main observations is sufficient. How do the distributions of bus intervals change with these parameters (again a short paragraph of the main observations is sufficient).
(2.3) Real-world bus data is available on Canvas, “Route 1 minutes late.csv” and “Route 2 minutes late.csv”. These files contain a list of times for buses compared to their scheduled arrival times. A negative
number means the bus was late.
(i) Read in the data and plot histograms of the minutes late for each route;
(ii) Compare the data to predictions from your simulations. Which model seems to be the best fit for the real-world data?
(2.4) What can you conclude about the wait time for passengers? From the timetable, it is expected to be 5 minutes. From your simulations, and comparison with real-world data, is this reasonable, or can we expect a different wait time? A comment with reference (i.e. supported by) your earlier results is sufficient.
5
COSC1003/1903 Assignment
Question 3 (COSC2902 only) (5 marks)
(3.1) An alternative to traditional methods of numerical integration such as the trapezoidal rule is Monte Carlo integration. In this method, the estimate of the integral of a function f (x) over an interval [a, b] on x is given by
where
b
fdx ≈ (b − a)⟨f⟩ (1)
a
1 N
⟨f⟩ = N It can be calculated using the following steps:
(a) Choose N random points on the interval [a, b]
(b) Calculate the value of f (x) at each of these points
(c) Use the average value of f (x) in the estimate of the integral
Write a function integratemonte() to implement Monte Carlo integration for a given number of points (N).
(3.2) Using your function, calculate the integral of
f (x) = x3 sin x + x2 + 50 (3)
using 50 points over the range 0 ≤ x ≤ 5. Compare your answer to the analytic solution.
(3.3) The error term for Monte Carlo integration is given by
f(xi) (2)
i=1
where
⟨f2⟩−⟨f⟩2
E = (b − a) N (4)
1 N
⟨f2⟩ = N
f2(xi) (5)
Write a function to calculate the error E. Plot the error for a varying number of points between 1 and 10, 000. Use your plot to discuss how the error changes with number of points N . Plot your results using a log scale and extrapolate (or extend your plotting range) to work out how many points are needed to calculate the value of the integral to one decimal place.
(3.4) Briefly discuss (with examples) an advantage and disadvantage of Monte Carlo integration as compared to a traditional technique such as the trapezoidal rule.
6
i=1