CS 177 CSIM Programming Assignment

Modeling Cars moving through Intersectons

DUE DATE: FRIDAY, MARCH 21, 2008 at 11:59PM

In this assignment, you will build a CSIM model of cars traveling through an intersection controlled by traffic lights. Later on, we will define several intersections with different levels of complexity, but for now let's just look at how cars move.

Representation of a Single Lane from the Roadway

Each lane will composed of a linear sequence of discrete "cells", each representing a short distance (11 feet, say) that is half the length of a car. Hence, a stopped car occupies two consecutive cells, whereas we say that a moving car occupies three cells at a time to account for the two partially-occupied cells from which the car's tail of the car is leaving and its nose is entering. To make sure that only one car at a time can occupy a particular cell, we will represent them as individual CSIM facilities.

For example, suppose cells are numbered 0, 1, 2, etc while cars are labelled A, B, C, etc. Initially, let's assume that both cars are stopped so that cells 2 and 3 reserved by car A, and cells 0 and 1 reserved by car B. In order for car A to start moving, it must immediately reserve cell 4, then after moving ahead by one cell, car A reserves cell 5 and immediately releases cell 2, and so on.

In order for prevent crashes, a moving car monitors the status of the roadway up to 4 car-lengths (or 8 cells) ahead of its current position, depending on its speed. To allow this, each car sets the following variables associated with each of its currently-reserved cells:

Behavior of a Car

If the car is not forced to stop by a traffic light and there are no other occupied cars within monitoring range, a car will always try to go as fast as possible. Cars always go at constant speed for one car length, and then can increase their speed by one step when accelerating, or decrease their speed by up to 2 steps when stopping.

Cars Accelerating

To accelerate from speed=0 to speed=5, a car needs 6.5 seconds and advances by 9 cells, as shown below for car A in the above example:

Time
Reserve
Set
Clear and Release
0
cell 4
cell 2: speed=1, exit time=1.5

1.5
cell 5
cell 3: speed=1, exit time=3
cell 2
3
cell 6
cell 4: speed=2, exit time=3+11/12 cell 3
3+11/12
cell 7
cell 5: speed=2, exit time=4+5/6 cell 4
4+5/6 cell 8
cell 6: speed=3, exit time=5+1/3 cell 5
5+1/3 cell 9
cell 7: speed=3, exit time=5+5/6 cell 6
5+5/6 cell 10
cell 8: speed=4, exit time=6+1/6 cell 7
6+1/6 cell 11
cell 9: speed=4, exit time=6+1/2 cell 8
6+1/2 cell 12
cell 10: speed=5, exit time=6+3/4 cell 9


Notice that the nose of the car has advanced from cell 3 to cell 12 and it is now moving at its maximum speed.
staircase chart showing movement of 2 cars

Figure 1 shows a plot of occupied cells by cars A and B as a function of time, assuming the initial conditions consist of car A stopped in cells 2 and 3, and car B stopped in cells 0 and 1, wishing to start accelerating at time 0 seconds. (The full details of the calculation are can be found in the following MS Excel spreadsheet.) Notice that car B does not begin moving until car A has moved far enough to leave two unoccupied cells between the two cars because a moving car cannot change its speed until it has advanced by one car length (or two cells). Thus, the actions of car B trail the corresponding actions of car A by 3 seconds, which results in a gap of 5.5 car lengths (or 11 cells) when both cars have attained speed=5.

Cars Slowing Down and Stopping

While a car is slowing down, its speed can drop by up to two steps when it crosses the next car-length boundary. Therefore, a car moving at speed=5 needs only 4 seconds to stop and advances by 4 cells, since it skip directly to speed =3 for one car length and then to speed=1 for one car length before has stopped completely.

The effect of different initial speeds is shown below in Figure 2. In most cases, stops from lower speeds take less time and distance. However, the figure also includes the effect of a one-second reaction time -- which can sometimes delay the onset of braking for more than one second because speed changes can only occur once per car length (or once every second cell). The assumption of a one second reaction time is a pretty standard for modeling drivers, for example see: http://www.visualexpert.com/Resources/reactiontime.html
After 1 second reaction time, stops from higher speeds take longer.

Look-Ahead to Avoid Crashing

If the car detects an obstruction (i.e., a slower moving, or stopped, car) within its monitoring range, the car continues at its present speed for 1 second to allow for the driver's reaction time, then reduces its speed as it crosses the next car-length boundary. Note that the car only experiences a single reaction-time delay per slow-down event, since the driver should be paying close(r) attention to the situation after seeing a potential traffic hazard ahead. The new speed is set to the maximum of: (a) two steps below its current speed, or (b) the speed of the car ahead.

Including the effect of a one-second reaction time, Figure 2 shows that a car moving at speed 5 requires 5 seconds and 3.5 car lengths to stop.Thus, we should be able to avoid crashes with a monitoring range of 4 car lengths at speed 5. On the other hand, if the car is moving at speed 4, then we see that the stop only requires about 3.2 seconds and 2.5 car lengths, so a monitoring range of 3 car lengths is sufficient. Similarly, a car moving at speed 2 or 3 can use a monitoring range of 2 car lengths because stopping from speed 3 requires 4 seconds and 1.5 car lengths, and stopping from speed 2 requires 2.75 seconds and 1 car length.

Cars moving at speed 1 are treated as a special case where we will use a monitoring range of 1 car length. This is sufficient to avoid crashes, since a car moving at speed 1 can stop in exactly 3 seconds and 1 car length. It also allows a car line of stopped cars to begin moving as soon as the inter-car gap has reached one car length (see Figure 1).

Experiments

Here is a list of experiments that you can use to test your traffic simulator. Everyone should make sure that their program can handle experiments 1 and 2. The remaining experiments are specifically designed to be progressively more complex and hence they will require substantially more time and effort to complete. Given the amount of time available for you to work on the problem, I do not expect everyone to complete all the experiments. However, you will not be able to get an "A" on the assignment if you only complete experiments 1 and 2.
  1. Circulating cars without crashing. Start with a one-lane, unidirectional circle of roadway that is L cells long (for example, try L=20 cells long).
    Create C cars (for example, try C=2 or C=3) and place them at random initial locations on the road, with instructions for car j to move a total distance of Dj cells before stopping (where Dj is chosen uniformly between L and 1.5L), unless it encounters a stopped car along its path.  In that case, the car should stop right behind the stopped car. Test your program for various combinations of C and L to make that the cars can move around the circle without crashing into each other.
  1. What is the capacity of a circle? Start with the same one-lane unidirectional circle of roadway from experiment 1. This time, however, start with a row of C stopped cars and let them keep going until you can determine the steady-state throughput of the system -- defined as the average number of cars per second passing through cell 1 -- as a function of C.  Test your program for (at least) L=10, L=20 and L=50. Plot a graph of how throughput varies as a function of C for all C between 1 and L/2. Don't forget to calculate confidence intervals for each throughput value!
  1. Repeat the experiment 2 assuming the car speeds have a small random variation around the "official" value rather than being deterministic. More specifically, for each speed, assume that the time required to move 1 car length is normally distributed with the same mean and standard deviation set to 10% of the mean. This random value is recalculated once for each car length of travel. Thus, if the car is "officially" moving at speed 5, then its actual time to travel one car length will be T/2 seconds, where T=normal (0.5, 0.05) and it is recalculated each time the car has moved two cells forward. Do these random speed variations affect the capacity of the circle?
  1. Cars traveling around a "Figure 8" with a traffic light at the intersection point. This time, we split the one-lane unidirectional roadway into two "loops" of length L/2, which meet at a traffic light in the middle. To make things more concrete, assume that the intersection is a square whose boundaries are labeled by the four compass directions: "N", "E", "S" and "W", where the first roadway segment exits the intersection through its "S" boundary and re-enters through its "E" boundary, and the second roadway segments exits the intersection through the "W" boundary and re-enters through its "N" boundary.  Also assume that the intersection is split diagonally into two triangular cells called "SW" and "NE": therefore, no matter which way the car travels through the intersection it always moves through cell "SW" followed by cell "NE".