An example of some work I have done using LISP.

Go to source code.

Goal of simulation: Use CLOS (Common Lisp Object System) to simulate traffic flow where the basic rule of speed-up-if-car-ahead-is-far-away and slow-down-if-it-is-close is applied with random degrees for all the cars in the simulation.

To run program: The user inputs the number of cars desired for the simulation, and the time desired for the simulation to run (in seconds), and the output type desired. The function to use is called 'run-simul', and the first parameter is the time to run the simulation (in seconds), the second parameter is the number of cars to use in the simulation, and the final parameter is the type of output desired (1 for text, anything else for 'graphical').

Design and assumptions of simulation: The simulation is designed to be as realistic as is realistic. It uses SI units (meters, kilogram, seconds) in its calculations, but translates the output to miles/hour for speed and miles/(hour2) for acceleration (it keeps distance in units of meters for the output). It assumes that traffic flow is one-way, and that no passing is allowed.

initialization: It initializes each car to be separated from the car in front of it by a distance of 5 to 7 times its own length and to have an acceleration of zero. All other parameters (speed, engine and braking power and length) are initialized to random quantities within a certain realistic range.

The simulation occurs in one second steps. At each step it calculates each car's new acceleration based on how far that car is from the car in front of it (this distance is referred to as the separation). Note that the leading car always has an acceleration of zero. The acceleration is proportional to the car's engine power and the difference between the separation and the 'critical-distance', and inversely proportional to the mass of the car. The critical-distance is defined as six times the car's length. Mathematically, this is expressed as

where a is the acceleration, p is the power, s is the separation, cd is the critical-distance and m is the mass. The acceleration is set the result of this calculation, unless the separation is less than the 'super-critical-distance', which is defined as two car lengths, in which case the speed and the acceleration of the car is set to that of the car in front of it. Once the acceleration is known, we calculate the car's new speed and position in the normal manner. This process then repeats itself for the amount of time requested by the user.

perturbations: The traffic is perturbed at random times (on average, once every 1000 seconds) by a 'police-car'. If the police-car is in a state of 'on', all the cars are forced to travel at the legal speed limit (as opposed to the speed-limit determined by their engines). The police-car stays on for one second, then is turned off, at which point the cars (including the lead car) are given new speeds, again at random (but within a certain range), and the simulation continues.

output: The simulation prints out results every ten seconds. It may output its results in two formats, called the text format and the graphical format. To see the text format, the user inputs a '1' as the third input parameter to the function run-simul. The output will then be of the following form
where sn and an represent the speed and acceleration of carn in miles/hour and miles/hour2, respectively, and d is the distance in meters between carn and carn-1. (Note that the first car, carn, always has an acceleration of zero.) To use the graphical format, the user simply enters a value other than '1' for the third parameter of run-simul. In this output mode, the simulation will print out carn, followed by a number of spaces, then carn-1, followed by a number of spaces, then carn-2, etc. Each space represents 5 meters.
Class structure for simulation

There are 4 objects in this simulation: vehicle, automobile, engine and police-car. The vehicle, engine and police-car objects are all derived from the standard object. The automobile object is derived from vehicle object. We may add other types vehicles, such as trucks or motorcycles, to this simulation in the future. The class hierarchy is shown below.

Figure 1) Class hierarchy for traffic-flow simulation.
The following diagram shows the instances, initial values and slot allocations for the objects described above in Figure 1.

The source code for the simulation may be downloaded here.

LISP source