Traffic Simulator Applet
I wrote this traffic simulation, following the article "Und nun die Stauvorhersage"
of the German Magazine Die Zeit, June 7, 1996. The article describes the work
of Professor Michael Schreckenberger of the University of Duisburg and unnamed
collaborators at the University of Cologne and Los Alamos National Laboratory.
These researchers model traffic flow according to simple rules, such as the following:
- A freeway is modeled as a sequence of grid points.
- Every car occupies one grid point. Each grid point occupies at most one car.
- A car can have a speed of 0 - 5 grid points per time interval.
- A car with speed of less than 5 increases its speed by one unit in each time interval,
until it reaches the maximum speed.
- If a car's distance to the car in front is d grid points, its speed is
reduced to d-1 if necessary to avoid crashing into it.
- With a certain probability, in each time interval some cars slow down one unit for no good reason
whatsoever.
This applet models these rules. Each line shows an image of the same stretch of road.
Each square denotes one car. The first scrollbar lets you adjust the probability
that some cars slow down. If the slider is all the way to the left, no car slows down.
If it is all the way to the right, every car slows down one unit. A typical setting is
that 10% - 20% of the cars slow down. The second slider controls the arrival rate of
the cars. When it is all the way to the left, no new cars enter the freeway. If it is
all the way to the right, a new car enters the freeway every time interval, provided
the freeway entrance is not blocked. (There is a bug in Netscape that sometimes
kills the applet thread when you click on the scrollbars. Click on Start to restart
the simulation.)
Try out the following experiments. Decrease the
probability of slowdown to 0. Crank up the arrival rate to 1. That means, every
time unit, a new car enters the road. Note how the road can carry this load.
Now crank up the probability that some cars slow down. Note how traffic jams
occur almost immediately.
The moral is: If it wasn't for the rubberneckers, the cellular phone users
and the makeup-appliers who can't keep up a constant speed, we'd all get to
work more quickly.
Notice how the traffic jam is stationary or even moves backwards, even though
the individual cars are still moving. In fact, first the car causing the jam has long
left the scene by the time the jam gets bad.
The source for the applet.
For more information about applets, graphics programming and multithreading in
Java, see
Core Java by Gary Cornell and
Cay Horstmann, published by SunSoft Press/Prentice-Hall