Traffic Flow Help Page (last update: 05/15)

\(\boxdot\) General Aspects
  • Here is a link to the Traffic Flow project instruction sheet: Traffic Flow Project

  • Assessment Benchmarks:

Functions or Tasks Points
10 points per function (maximum of 9) 90
Task 2 10
9 functions and Task 2 Completed by 05/07 10
5 points per Task 3 and Task 4 by 05/18 10



  • Here is a link to a good journal article on traffic flow modeling by Paul James Wright of the University of Glasgow: Traffic Flow Modeling - Wright

  • Use short lists for the development and testing of your code. In fact, I suggest you use the example lists in the project outline for this purpose.

  • Skip the reaction step during development as well because randomization can cause frustration in the testing process if something is amiss randomization prevents repeating the same conditions over numerous test runs used to diagnose a fix a bug.

\(\boxdot\) Hand-In Instructions:
  • I will place a Project Submission item on the Katie Course page. Please upload your single Python file (having the suffix .py) that includes all completed functions and/or Tasks by 4:00 PM Friday, May 18.

  • Your file must be named using your Norse user name. For example, my Norse user name is bernatzr, so my submission file would be named proj_bernatzr.py This naming convention applies to all files, even those who previously submitted work by the early hand-in date.

  • If you submitted a file for extra credit by May 8:

    • If you have completed additional tasks since your early hand-in of May 8, please submit your current, single file that includes all your completed work (including work you completed by the early-submission date).

    • DO NOT submit another file if you have not completed any additional work since you submitted a file by the early submission date.

  • Your main() function must have the lines shown in the example below.

    def main():
        rho = 0.3
        p = 0.5
        n_cells = 200
        t_steps = 200
        plot_flow(rho, p, n_cells, t_steps)  
        # p_l = [0.0, 0.1, 0.3, 0.7, 0.9]
        # d_rho = 0.01
        # plot_ave_vel_v_rho(p_l, n_cells, t_steps, d_rho)
        # plot_gflow_v_rho(p_l, n_cells, t_steps, d_rho)
\(\boxdot\) Task Help
  1. Functions

    1. init_positions(n_cells, n_veh) -

    2. init_speeds(loc_c) -

    3. calc_accel(loc_c, spd_c) -

    4. calc_dist(loc_c) - This may be the “trickiest” function to write. I know of at least two ways to do this. One is to create a list of the vehicle cell locations. Using the lists of Figure 1 of the project handout, the list of locations would be x_list = [1,2,5,8]. Then, the distance between vehicle 1 and 2 is 2 - 1 = 1, between 2 and 3 is 5 - 2 = 3, between 3 and 4 is 8 - 5 = 3. What is left to determine is the distance between vehicle 4 and 1 because of the “circular” nature of the road. To accomplish this using the x_list, we must add one more element to the list. Its value is determined by adding the length of the track to the location of the first vehicle. In this example, the number 11 + 1 = 12 must be appended so that x_list = [1,2,5,8,12]. Now, the distance between vehicle 4 and vehicle 1 is 12 - 8 = 4. The resulting distances must be stored is a list of length n_cells in the location of the corresponding vehicle. For this example, distance between the vehicles for the current time step = dis_c = [0,1,3,0,0,3,0,0,4,0,0] is what would be returned by the function. The other way I know of is a little more complicated in that it uses cell location indicies and a “step counter” that counts the steps required to go from one occupied cell location to the next occupied cell.

      As a way of testing your function, if loc_c = [0,1,1,0,0,1,0,0,1,0,0], then calc_dist(loc_c) will return [0,1,3,0,0,3,0,0,4,0,0].

    5. calc_braking(loc_c, spd_c, dis_c) -

    6. calc_reaction(loc_c, spd_c, p) - ***NOTE : The probability expression is wrong in Part 2.3 of the handout! It should be if random.random() <= p, not >=.

    7. calc_loc_spd(loc_c, spd_c) - Use the speed list to determine how many cells forward a vehicle will move in this time step. The modulus operator is useful for this purpose. The corresponding speeds of the vehicles must be moved as well.

    8. simulate_flow(rho, p, n_cells, t_steps) - This function will use all of the previous functions to simulate the flow of traffic on the road over a specified number of time steps. The number of vehicles on the road is determined from the n_cells and rho parameters. For example, if n_cells = 100 and rho = 0.4, then n_veh = 0.4 * 100 = 40. Functions (c) - (g) will be included in a loop. Before the loop is enacted, the vehicle locations and speeds must be initialized using init_positions(n_cells, n_veh) and init_speeds(loc_c). Also, empty lists must be created to accumulate the location and speed lists for each time step.

      Each loop begins with the current location and speed lists. These lists are used to determine the acceleration, the distances between vehicles used to determine which vehicles must brake, the random braking done by vehicles and the new location and speed for each vehicle on the road. Before the loop is started for the next time step, the new location and speed lists must be referenced by the loc_c and spd_c names (they become the current location and speed lists) and the speed list must be appended to speed versus time list. BE SURE to use cloning as opposed to aliasing where appropriate.

      The function returns two lists of lists. The first is a list of location lists, and the other is a list of speed lists. These lists will be used in the plot_flow() function to complete Task 2. Additionally, the last entry in the list of speed lists is used to determine the average speed of the vehicles on the road. The average speed (speed and velocity are used interchangeably) is used by Tasks 3 and 4.

      NOTE: For sake of testing, begin with a single vehicle on a 10-cell track (so rho = 0.1) with the probability of reaction braking set to zero (p = 0). Regardless of the vehicle’s initial speed (1 - 5), the vehicle should eventually reach and maintain a speed of 5. The average speed of the vehicle in this case eventually is 5.

      THINK about this… how many cells must there be for each vehicle on a track (one cell for the vehicle plus the number of open cells in front of it) to reach and maintain a speed of 5? A correct answer to this will allow you to predict what would happen to the average speed if two vehicles are on the 10-cell track.

      WHAT is the minimum length (number of cells) that a track can have so that two vehicles will eventually reach and maintain a speed of 5 (so the average speed is 5)?

      Here are my results for once through the function sequence (c) - (g) with no reaction braking (p = 0.0):

      • loc_c = [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0] (initial positions)
      • spd_c = [0, 2, 1, 0, 0, 5, 0, 0, 3, 0, 0] (initial speeds)
      • spd_c = [0, 3, 2, 0, 0, 5, 0, 0, 4, 0, 0] (after calc_accel)
      • dis_c = [0, 1, 3, 0, 0, 3, 0, 0, 4, 0, 0] (after calc_dist)
      • spd_c = [0, 0, 2, 0, 0, 2, 0, 0, 3, 0, 0] (after calc_braking)
      • spd_c = [0, 0, 2, 0, 0, 2, 0, 0, 3, 0, 0] (after calc_reaction with p = 0.0, so no change)
      • loc_c = [1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0] (after calc_loc_spd)
      • spd_c = [3, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0] (after calc_loc_spd)

    9. plot_flow(rho, p, n_cells, t_steps) - This function will use the simulate_flow() function to generate lists of location lists and speed lists. It will use Turtle graphics to generate a plot like that shown in Figure 1 of the Wright article. Set up the window with dimensions that match the number of time steps and number of cells (sites) used to generate the simulation. Generate the lists of location and speed lists. Create a loop that traverses the lists to determine the location and speed of the vehicles for a given time step. Each time step represents a horizontal sequence of dots (points) in the Figure. The speed list is used to determine the color used by the turtle dot() method

    10. plot_ave_spd_v_rho(p_l, n_cells, t_steps, d_rho) - See Task 3 below for suggestions.

    11. plot_gflow_v_rho(p_l, n_cells, t_steps, d_rho) - See Task 4 below for suggestions.

  2. Location versus time plot - Invoke the plot_flow() described in Task 1(i).

  3. Average velocity versus road density plot - See the Wright article (link is above) and Figure 3 to see what this plot should look like for the cases of p = 0.0 and p = 0.5 . Suggestion: Devise an algorithm by “thinking in reverse order.” The plot for a given value of p requires ordered pairs (a list of sub-lists) of average velocity versus road density (rho). Fix a value of p. Set up a loop to iterate through rho values from 0.0 to 1.0 with a step size of d_rho (delta rho) of 0.1 (to begin). Invoke the simulate_flow() function for a sizeable number of t_steps (more than 100) for a given rho value. The simulate_flow function returns two lists of lists (one for location and one for speed). Use the last entry of the speed list to determine the average velocity for the specified rho value. Append the rho - average velocity pair to the list that will be plotted to generate a single curve shown in Figure 3 of the Wright article.

    Your resulting graph may look a little “jagged” because of the stochastic nature of simulation. The plot can be “smoothed” somewhat by adding a loop within the rho loop.

    n_veh = int(rho * n_cells)
    n_trials = 20
    a_spd_list = []
    for trial in range(n_trials):
          loc_l, spd_l = simulate_flow(rho, p, n_cells, t_steps)
          a_spd_list.append(sum(spd_l[-1])/n_veh )
    ave_spd = sum(a_spd_list)/len(a_spd_list)  
  4. Global flow versus road density plot - See the Wright article (link is above) and Figure 4 to see what this plot should look like. This Task has an almost identical framework of Task 3. The one difference is the plot list here has rho - global flow pairs. This type of graph is known as a “fundamental density-flow diagram.” The global flow value is given by the product of rho (road density) and average speed.