CS 1723-001 Data Structures
Time-driven Simulation
A Line of Cars Waiting at a Traffic Light


In class we discussed a simple simulation: a line of cars waiting at a traffic light. The idea is that perhaps a city wants to install a new traffic light at an intersection. In one direction, the line of cars waiting for the red light might block a fire station entrance if it is too long, so the traffic engineers want to simulate the line to see if this might be a problem. The simulation presented here is much simpler than what would be used in a real situation.

Assumptions:

Here is a Java program to run the simulation:


// TimeSim: a simple time-driven simulation
public class TimeSim { 

   public static void main(String[] args) {
      final int MAX_TIME = 3600; // one hour of simulated time
      final double ARRIVAL_RATE = 20.0/60.0; // 20 cars/minute, on the average
      int greenLight = Integer.parseInt(args[0]); // this is assumed to be 25
      int length = 0; // the length of the line of cars at any second
      int maxLength = -1; // the maximum length during the simulation
      for (int time = 0; time < MAX_TIME; time++) { // let time proceed
         if (Math.random() < ARRIVAL_RATE) length++; // a car arrives
         if (time%60 < greenLight && length > 0) length--; // go through light
         if (length > maxLength) maxLength = length; // maximum length
      }
      System.out.println("Arrival Rate: " + ARRIVAL_RATE +
         ",\tGreen Light: " + greenLight +
         ",\tMax Length: " + maxLength);

   }
}
Here is the output of five separate runs of the simulation, using 25 as the number of seconds per minute that the light is green. Notice that there is a fair amount of variation in the outcome.
ten42% java TimeSim 25
Arrival Rate: 0.3333333333333333,       Green Light: 25,        Max Length: 18
ten42% java TimeSim 25
Arrival Rate: 0.3333333333333333,       Green Light: 25,        Max Length: 21
ten42% java TimeSim 25
Arrival Rate: 0.3333333333333333,       Green Light: 25,        Max Length: 18
ten42% java TimeSim 25
Arrival Rate: 0.3333333333333333,       Green Light: 25,        Max Length: 19
ten42% java TimeSim 25
Arrival Rate: 0.3333333333333333,       Green Light: 25,        Max Length: 17
Because if the variation in the output above, I wrote a slightly more complicated program that would run the simulation 10000 times and get the average of the maximum values produced by the runs. This program also lists the full range of maximum values. The program was run with a variety of times for the green light, all as the number of seconds of green out of each 60 seconds total. Here is a listing:


// Traffic: a simple time-driven simulation
public class Traffic { 
   final int MAX_TIME = 3600;
   double arrivalRate;
   int greenLight;

   public Traffic(int gLight, double arrival) {
      greenLight = gLight; arrivalRate = arrival;
   }

   public int runSim() {
      length = 0;
      maxLength = -1;
      for (int time = 0; time < MAX_TIME; time++) {
         if (Math.random() < arrivalRate) length++;
         if (time%60 < greenLight && length > 0) length--;
         if (length > maxLength) maxLength = length;
      }
      return maxLength;
   }

   public static void main(String[] args) {
      final int TRIALS = 10000;
      int greenLight = Integer.parseInt(args[0]);
      Traffic traffic = new Traffic(greenLight, 20.0/60.0);
      int maxLength;
      int maxMax = -1, minMax = 1000000;
      int sumMax = 0;
      double aveMax;
      for (int trials = 0; trials < TRIALS; trials++) { 
         maxLength = traffic.runSim();
         if (maxLength > maxMax) maxMax = maxLength;
         if (maxLength < minMax) minMax = maxLength;
         sumMax += maxLength;
      }
      aveMax = (double)sumMax/TRIALS;
      System.out.println("Range: (" + minMax + "-" + maxMax +
         "), Average of Maximums: " + aveMax);

   }
}
Here is a table showing the results of multiple runs of this simulation:

Length of
Green Light
Range of
Maximums
Average of
Maximums
19(23-189)81.62
20(19-128)44.72
21(16-77)29.73
22(16-46)24.10
23(16-40)21.42
24(15-34)19.83
25 (14-31)18.85
26(14-30)18.14
27(13-26)17.55
28(14-27)17.06
29(13-23)16.61
30(13-23)16.16
35(10-20)13.97
40(9-17)11.71

The original question was: how long can we expect the lines to be that are waiting at the light? The answer for 25 seconds of green light is "a maximum of almost 19 cars on the average but sometimes quite a bit longer". Increasing the length of the green light cycle, say to 35 or 40 seconds, does not help as much as I would have expected.


Revision date: 2001-10-09. (Please use ISO 8601, the International Standard.)