


Go to source code. Goal of simulation: Use CLOS (Common Lisp Object System) to simulate traffic flow where the basic rule of speedupifcaraheadisfaraway and slowdownifitisclose is applied with random degrees for all the cars in the simulation. 



where a is the acceleration, p is the power, s is the separation, cd is the criticaldistance and m is the mass. The acceleration is set the result of this calculation, unless the separation is less than the 'supercriticaldistance', 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 'policecar'. If the policecar is in a state of 'on', all the cars are forced to travel at the legal speed limit (as opposed to the speedlimit determined by their engines). The policecar 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 runsimul. The output will then be of the following form 



where s_{n} and a_{n} represent the speed and acceleration of car_{n} in miles/hour and miles/hour^{2}, respectively, and d is the distance in meters between car_{n} and car_{n1}. (Note that the first car, car_{n}, 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 runsimul. In this output mode, the simulation will print out car_{n}, followed by a number of spaces, then car_{n1}, followed by a number of spaces, then car_{n2}, etc. Each space represents 5 meters. 

Class structure for simulation
There are 4 objects in this simulation: vehicle, automobile, engine and policecar. The vehicle, engine and policecar 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 trafficflow 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. 