CSE 331 - Data Structures - Fall 2004
Programming Assignment # 4 - Traffic Simulation


DUE: Tuesday, November 2, 2004, by MIDNIGHT

This assignment will be on the Linux machines.

We will be simulating the traffic flow through the intersection at the south end of campus where Angela Blvd. becomes Edison Rd. and where Eddy St. becomes Juniper Rd. East/west traffic is on Angela/Edison and north/south traffic is on Juniper/Eddy.

You will conduct actual ten (10) minute surveys of the traffic flow through each lane. These surveys will be done at three times of day (morning rush, late morning or early afternoon, and afternoon rush). As a class or as groups you may conduct and share this actual frequency data. From these counts you will determine the probabilities that cars will arrive during a single 3 second interval destined for each lane passing through the intersection.

Here is a map of the intersection.

                                Juniper Rd.

                             ***/(16)|    \**
                         *****/      |     \**
                     ******/         |    (6)\*
                  ******/        |   |   |\   \*      NOTRE
               ******/     /*|       | y6|*\   \**     DAME
             *****/RIGHT/****|   | L |   |**\   \****
          *****/     /*******|     E |   |***\   \******
        ****/     /**********|   | F |   |****\   \***************
     ****/     /*************|     T |   |*****\   +-----------+****
  ****/(9)  /****************|(8)|(7)|   |******\   RIGHT       \*****
 ***+     +------------------+           +-------+   ---   ---   +------
**/        y9                             (5)                     (15)
+      +---------------------+           ---   ---   ---   ---   ---
     /             LEFT  (10)             (4) LEFT               Edison Rd.
---+     ---   ---   ---   ---           +------------------------------
 (17) Angela Blvd.       (11) 
---+     ---   ---   ---   ---           ---   ---   ---   ---   ---
****\        (18)        (12) 
 ****+------+ ---   ---  +---+               +--------------------------
  ***********\  RIGHT     \**|   |(1)|(2)|(3)|*************************
     *********+-------+    \*|   | L       R |**** 
       ****************\    \+   | E |   | I |**
                ********\(13)    | F       G |**
                     ****\       | T |   | H |**
                      ****\      |         T |**
                         ***\    |   |   |   |**
                          ***\   +           +*
                            **\   \         /**
                             **\   \       /**
                               *\   \ (14)/*
                                 
                                  Eddy St.

General description

Eddy St. brings northbound traffic to the intersection. At a point approximately 10 car lengths before the intersection one lane splits into three lanes (left turn only, straight only, and right turn only).

Edison Rd. brings westbound traffic to the intersection. At a point approximately 10 car lengths from the intersection two lanes split into three lanes (left turn only, straight only, and right turn lane around island to yield sign).

Juniper Rd. brings southbound traffic to the intersection. At a point approximately 10 car lengths from the intersection one lane splits into 3 lanes (left turn only, straight only, and right turn lane around island to yield sign).

Angela Blvd. brings eastbound traffic to the intersection. At a point approximately 10 car lengths from the intersection one lane splits into 3 lanes (left turn only, straight only, and straight/right turn). At a point approximately 5 car lengths from the intersection the straight/right turn lane splits into two lanes (straight and right turn lane around mini-island to yield sign).

The result is 13 lanes of traffic entering the intersection environment. However, two of those lanes lead to yield signs that are at least one time interval PAST the intersection. This will require us to track cars in at least two lanes for one time interval AFTER they have cleared the intersection.

Also, on all 4 approaches the main road splits adding additional lanes. The simulation will be queued up in this fashion.

13 queues, one for each lane entering the intersection
 5 queues, one for each lane that will split prior to the intersection
 2 booleans, for each lane past the intersection that may force a yield
--
18 queues total

Each of the 5 queues containing traffic leading to a split point must contain two data values (entry time & destination lane). The 13 queues of entering traffic at the intersection need to contain only the original entry time. The two booleans past the intersection will reflect simply whether a car is at this point or not.

Rules for queuing cars

We will simulate one hour of traffic flow, using 3 second intervals. Three seconds is an estimate of the time required for a car to move though the intersection unimpeded. All arrivals will occur at random with a probability determined by your actual traffic counts. Arrivals are determined at the beginning of each 3 second interval and are queued up in the outermost arrival queues (q4, q14, q15, q16, and q17) along with their initial arrival time and the intersection lane (queue) for which they are destined.

The main simulation proceeds under the control of a timing loop.

for (t=0 to 3600 seconds, in 3 second increments)
   determine arrivals at outermost queues (random with surveyed 
      frequencies)
   determine movement of leading car(s) from outermost queues
      into split queue(s)
   determine movement of cars at yield signs
   determine movement of cars forcing yields (y6 and y9)
   determine movement of cars at intersection (processing those that 
      must yield to oncoming traffic first)
   increment time t
   if lights used
      if it is time for a light change then change lights
      advance light state timer

Queues may be numbered and used as shown on the map.

q1 left turn only from Eddy St.
q2 straight from Eddy St.
q3 right turn only from Eddy St.

q4 left turn only from Edison Rd.
q5 straight from Edison Rd.
q6 right turn lane around island from Edison Rd.

q7 left turn only from Juniper Rd.
q8 straight from Juniper Rd.
q9 right turn lane around island from Juniper Rd.

q10 left turn only from Angela Blvd.
q11 straight from Angela Blvd. (the left lane of pair)
q12 straight from Angela Blvd. (the right lane of pair)
q13 right turn lane around mini-island from Angela Blvd.

q14 approach queue on Eddy St. (traffic splits to q1, q2 &q3)
q15 approach queue for right lane on Edison Rd. (traffic splits to q5 & q6)
q16 approach queue on Juniper Rd. (traffic splits to q7, q8 & q9)
q17 approach queue on Angela Blvd. (traffic splits to q10, q11 & q18)
q18 rightmost lane on Angela Blvd. (traffic splits to q12 & q13)

y6 (bool) indicates presence of car forcing yield by q6
y9 (bool) indicates presence of car forcing yield by q9

Note: We will not try to simulate this intersection without traffic lights, but we will try it with different timing sequences and both with and without advanced left turn arrows. The term "held" refers to either a light signal or other traffic preventing the lead car in a lane (queue) from proceeding through the intersection.

U-Turns are NOT allowed.

Basic rules for yielding to oncoming traffic are as follows.

// right turns on red or at yield signs
if !y6 then 
   q6 may proceed
if !y9 then 
   q9 may proceed
if q12 is empty or held then
   q3 may proceed
if q4 and q8 are both empty or held then
   q13 may proceed

// left turns yielding to oncoming traffic
if q8, q4, q5, q10, q11 and q12 are all empty or held then
   q1 may proceed (and set y9 for next interval)
if q1, q2, q7, q8, q11 and q12 are all empty or held then
   q4 may proceed
if q2, q4, q5, q10, and q11 are all empty or held then
   q7 may proceed
if q1, q2, q7, q8 and q5 are all empty or held then
   q10 may proceed

NOTE: if advanced left turn arrow is in effect, then presence of cars
in other lanes does not matter.

// straight through intersection
if q4, q5, q10, q11 and q12 are all empty or held then
   q2 may proceed (and set y6 true for next time interval) and
   q8 may proceed
if q1, q2, q7 and q8 are all empty or held then
   q5 may proceed (and set y9 true for next time interval) and
   q11 and q12 may proceed

The rules above pertain in all situations.

ONLY one car (the front one) in each lane is allowed to move forward through the intersection in a single time interval. Waiting time is calculated from the current time t and the queued entry time of the car passing through the intersection. When a car leaves one of the outer arrival queues and enters one of the inner queues (split in traffic lanes) the original arrival time at the outer queue is placed in the inner queue. This way the TOTAL wait for any car includes both outer queue and inner queue waits.

NOTE: It is possible that multiple cars may queue up in a single approach queue during a single time interval. If the frequencies are high and this approach leads into 3 other lanes, then 2 or 3 cars may arrive at the approach at the "same" time. Don't worry about the physical ramifications of this. It's OK. On the other hand, you should not be generating multiple arrivals bound for the same inner queue (lane) during a single time interval. Each inner queue should have a probability of arrival between 0.0 and 1.0, and only ONE arrival will occur (or not) per time interval. As an example, you might have 3 cars arrive at q14 (Eddy St. approach) in a single time interval, but each one is headed for a different lane (q1, q2 & q3).

Approaching cars first queue up in q4, q14, q15, q16 and q17. In all cases except q4 the data in the queue must indicate time t at time of arrival AND destination queue when split occurs in this or future time interval. The sizes shown below (5, 10, 15) are indications of the total number of cars that can be in a given lane before that lane backs traffic up past the approach queue. The splits occur according to the following rules.

Eddy St.
--------
if q14.front() is left turn and q1.size() < 10 then
   move q14.front() to q1
if q14.front() is straight and q2.size() < 10 then
   move q14.front() to q2
if q14.front() is right turn and q3.size() < 10 then
   move q14.front() to q3

Edison Rd.
----------
NOTE: cars destined for q4 are never put into q15, so the only
split here is to q5 and q6.
if q15.front() is right turn and q6.size() < 15 then
   move q15.front() to q6
if q14.front() is straight and q5.size() < 10 then
   move q15.front() to q5

Juniper Rd.
-----------
if q16.front() is left turn and q7.size() < 10 then
   move q16.front() to q7
if q16.front() is straight and q8.size() < 10 then
   move q16.front() to q8
if q16.front() is right turn and q9.size() < 15 then
   move q16.front() to q9

Angela Blvd.
------------
// first split
if q17.front() is left turn and q10.size() < 10 then
   move q17.front() to q10
if q17.front() is straight and q11.size() < 10 or q18.size() < 5 then
   move q17.front() to either q11 or q18 (randomly choose which)
if q17.front() is right turn and q18.size() < 5 then
   move q17.front() to q18
// second split
if q18.front() is straight and q12.size() < 5 then
   move q18.front() to q12
if q18.front() is right turn and q13.size() < 5 then
   move q18.front() to q13

Light Setups

Simple Lights (Setup # 1)
-------------------------
No advanced left turn arrows.
GO - East/West
--------------
lanes 4,5,10,11,12 have green for 45 seconds
lanes 4,5,10,11,12 have yellow for 6 seconds
lanes 1,2,3,7,8 have red for 51 seconds

GO - North/South
----------------
lanes 4,5,10,11,12 have red for 51 seconds
lanes 1,2,3,7,8 have green for 45 seconds
lanes 1,2,3,7,8 have yellow for 6 seconds

Simple Lights (Setup # 2)
-------------------------
Same as setup # 1 except that the total GO time (green & yellow lights)
for East/West is 60 seconds and the total GO time for North/South 
is 42 seconds.

Advanced Lights (Setup # 3)
---------------------------
GO - East/West (45 seconds total)
---------------------------------
 9 seconds - 4,10(green arrow) 5,11,12(red)
 6 seconds - 4,10(yellow arrow) 5,11,12(red)
30 seconds - 4,10(red) 5,11,12(green)
 6 seconds - 4,10(red) 5,11,12(yellow)
lanes 1,2,3,7,8 have red for whole 51 seconds

GO - North/South (45 seconds total)
---------------------------------
 9 seconds - 1,7(green arrow) 2,3,8(red)
 6 seconds - 1,7(yellow arrow) 2,3,8(red)
30 seconds - 1,7(red) 2,3,8(green)
 6 seconds - 1,7(red) 2,3,8(yellow)
lanes 4,5,10,11,12 have red for whole 51 seconds

Advanced Lights (Setup # 4)
---------------------------
Same as setup # 3 except that the total GO time for East/West is
60 seconds and the total GO time for North/South is 42 seconds.

Your variations (Setup # 5 and setup # 6)
-----------------------------------------
You may allow variation of light timing based on numbers of cars in
queues. You may change left turn signal so it cycles through the 
sequence (left-green, left-yellow, green, yellow, red). You can even 
check out what the light actually does, which is to vary time AND
light sequencing based on sensing of cars in the lanes (and probably
the time of day).

WHAT YOU ARE TO DO AND TURN IN.

Create the program so it simulates the intersection, allowing the user to select ...

  1. time of day frequency data (morning, midday, evening)
  2. different light setups (all 6)
  3. run a simulation and determine stats (listed below)
  4. exit

I would also recommend some form of textual display of # of cars in queues waiting times, etc, as the program runs. This will help you decide whether the program actually works correctly or not.

Having a single step mode that allows you to see what arrives and what departs in each time interval is another good way to test and verify that the program works.

Graphical display is NOT necessary.

Run the program yourself using all six light setups on each of your times of day. That is 18 runs. Collect the following stats for each run.

for each numbered lane (excluding approaches)
   # cars through intersection in this lane
   # cars left in queue for this lane
   average waiting time for this lane
   longest waiting time for this lane

for entire simulation
   # cars through intersection
   # cars left waiting at end of simulation
   average waiting time overall (all lanes)
Summarize this information in some logical way (spreadsheet, text file, ...) and write a brief recommendation justifying which light setup is the most appropriate for this intersection.


DUE: Tuesday, November 2, 2004, by MIDNIGHT

The following are to be placed in the prog4 folder in your dropbox.