Overview

Objective

For this homework assignment you'll be running a simulation. The goal is to implement the simulation in a smart way, using Python data structures, NumPy, and Matplotlib effectively.

Suppose that M students each have N songs on their smartphone. For the sake of simplicity, assume that each student has the same N songs on their phone as every other student does. Furthermore, for each student, each song has a number between 0 and 1 associated with it. For each student, these sum of those numbers must be 1. They can be thought of as the probability that the student plays that song. For each student, the song with the largest number associated with it will be referred to as the student's 'favorite' song.

The simulation takes place over a series of time steps $t_0, t_1,\dots, t_k$. At each time step,

  1. Two students are chosen at random.
  2. Each student plays their favorite song to the other student.
  3. After hearing the other person’s favorite song, each person’s probability value for the other person’s favorite song goes up by a small amount, and the probability values for the other N-1 songs are adjusted down (to insure that for each student, the probabilities still sum to 1).

What happens after a long time?

Grading

This assignment is worth twenty points. When complete, you should email the script to Dr. Johnson. The due date is Wednesday, June 22, by 9:00AM. Late assignments will be penalized 1 point per day (that's 0.5 full letter grades per day late). Submission anytime after 9:00AM on June 22 counts as 1 day late.

Working together

You may discuss and work on this assignment with your peers. However, you must submit your own work, copying is not permitted. You will be graded individually, and if your work appears to be a copy of someone else's, I may ask for you to demonstrate in person and on the spot your ability to write the code to solve a similar problem.

Format

Submit your work as a Python script file (.py). I should be able to run the script and see the output. I should not need to make any changes to the file.

Instructions

Implement the simulation. Your implementation must:

  1. (5 points) Allow for the simulation to be run with an arbitrary number of students and an arbitrary number of songs. However, it must initialize with sensible defaults.
  2. (3 points) Initialize the song probabilities at time $t_0$ for each student randomly.
  3. (7 points) Run correctly. This means insuring that no song ever has probability greater than 1 and that for each student, the sum of the song probabilities is always one. Check this and appropriately handle any exceptions.
  4. (5 points) It must include an option to generate a sample plot when run. The default should be to output the plot. The sample plot should show the evolution of song probabilities over time for a randomly selected student (or better yet, set of students). (Hint: this means you need to store the probabilities for each song, for each student, for each time step in the simulation - 3d arrays might be handy here!).
  5. Bonus (3 points) Write this in a way that allows you to resume the simulation from where you left off if the number of time steps is not sufficient to determine what happens over a 'long time'.

In [ ]: