Lab 4 Details: -- Automobile Traffic Simulation
COM1101 Winter 2001

(Version of 1/27/2001)

Professor Futrelle
College of Computer Science, Northeastern U., Boston, MA

This page explains the technical details of the simulation and code organization for this lab. The assignment, what you are to hand in, is describe in the assignment page. The lab will take place on Tuesday, January 30th and is due by 4pm on Monday, February 5th.

Overview: You will define a simulation class and a car class. Together they will allow you to simulate simple traffic flow. This must be a multi-file project (Separate Compilation, Savitch, Sec. 8.2). This first lab will focus on API specifications and ADT declarations using a multi-file project. The simulation will be simple, with all cars moving at a single constant speed. In the next lab, Lab 5, you'll extend and refine the simulation, using separate classes for objects such as the road, traffic light, report generator, etc. Also, the cars will interact, possibly leading to traffic jams, collisions, pileups, lawsuits and all that stuff. ;-)

How time-based simulations work

For traffic simulations such as this, each car has a position and a speed. In our case the position will be a real value (type double) as will the speed. All units will be in the metric system. (If you're concerned about the equivalences, just go to http://www.ccsiab.se/Indexes/Metric/SpeedConversion.html)

The cars move along a single dimension, basically one long street. Each car has a step() member function that changes its position by an amount equal to its speed times the time step. The time step is the argument to step().

Programmatically, all cars reside in an a large array of cars, say size 10 for tests and size 1000 for serious runs. Initially, none of the cars are active (a bool), and two pointers are used to denote the active portion of the car array. Initially these pointers, firstCar and nextAvailableCar, have the same value, indicating that none are active. Think of the array as a big "garage" or "home base" from which they all start. When each one starts, it's at position 0.0. When it reaches or exceeds 1000.0 meters, about 6/10ths of a mile, it is set to not active.

After each time step, Simulation should fill an array with blank characters, say 70 long, and then overwrite a non-blank symbol at the approximate position of each car and print the array to the screen. So the patterns will "march across the screen" as the cars move.

All the main() needs to do is to create the simulation instance and then call init(), run() and report() member functions on the instance. Simulation does the rest.

As for the multi-file organization, you'll need to carefully study Savitch, Sec. 8.2 to understand how this is done.

You will need to use additional member variables and functions that I haven't spelled out in detail here. They are yours to design and implement.

How to generate random numbers in C++: The basics are in Savitch, pg. 921. The random(int) function is not available in Visual C++. Here are examples of what does work in Visual C++,
double randomDouble(double max) {

	return max*(rand()/double(RAND_MAX));
}

int randomInt(int max) {

	return floor(max*(rand()/double(RAND_MAX)) + 0.5);
}

floor() and 0.5 are used to round the value, since otherwise, the max value would rarely be returned. You'll need to include <cstdlib> and <cmath> for these to compile.

EXTRA CREDIT

You will get extra credit for this lab if you produce a report which is an html page of little cars at the appropriate positions for each time step. Use tiny car images such as, or . You may want to limit yourself to only a few cars for these tests. See http://208.56.255.141/graphics/animated/cars/index.html for more car pics. Here's a site with an animated applet of a nice little traffic simulation that actually does more than I'm asking you in these two labs. It's a Java applet: http://www.orbicon.demon.co.uk/traffic/traffic.html