Project 3

"The Road Less Traveled By"

This project studies the use of computers to model the physical world. The focus is on how real systems are represented in computers, how completely the physical system is modeled, how the physical quantities of space and time are treated, and how reliable are the conclusions one can draw from a computer model. Additionally, there will be further opportunities to write small procedures.

>> Though the questions and tasks to be solved are interspersed throughout the following text, it is advisable to read the entire document before starting to answer any of them.

Resources: The main resource for this project is the "U District Traffic Simulation" program, available on the course web site on the File Archive page. Download all of the files of the UDTS to your computer, and then transfer them to your floppy. You will be making modifications to this system using this copy of the system.

Introduction To UDTS: The purpose of the U District Traffic Simulation is to study the effects of the traffic lights on commuting from the campus to north bound I-5. The primary window of the UDTS is shown above. The black lines are a schematic representation of the avenues from the north campus entrance (17th) to I-5 that cross 45th Street and 50th Street. Generally, it is not necessary to create a visual representation of a simulation in order to model the physical world, but it is common to do so as an assist to working with the system and to help others to understand the results. The visual representation becomes the interface to the actual simulation which, being embodied in the program and data, is not directly visible.

Three types of traffic lights are shown schematically: two-way, 1-way left and 2-way left. (The lights have been set with random initial configurations.) The UDTS is capable of simulating the progress of two cars from the time they depart the north campus gate to I-5. Each car takes a different route, one on 45th and one on 50th, which end at a merge point (green box) at the end of the 50th Street entrance ramp. The cars move along the streets at a constant speed, stopping when necessary for traffic lights. The time to reach the merge point is determined by how many lights are red and how long the car must wait at each of those.

Experiments involving a pair of cars are initiated and run to completion. The time of the trip from campus to I-5 is shown for each. The cars experience different running times on different experiments because the lights are not perfectly synchronized, and so have different relative behaviors. (Notice that a car traveling on 45th must pass 8 lights, while one traveling via 50th encounters 9. The sequence of lights on 45th, however, contains more of the complex, 2-way left lights.)

Question 3.1: How is this simulation oversimplified? List features or characteristics of driving from UW to I-5 that are not treated in this simulation.

Using The UDTS: There are buttons in the lower right corner of the form. Quit is self-explanatory. Start initiates the simulation and once started is converted to a stop button. Starting the simulation causes the first two simulated vehicles to leave the university. The purple car takes 50th Street and the yellow car takes 45th Street. Customize is used to set the lights’ configurations. The Create Car button starts a new pair of cars at the north entrance to campus. There is a limit of 10 experiments in any execution of the program, i.e at most 10 pairs of cars can be started. Also, there must be a sufficient separation between the cars.

The configuration of any traffic light can be set by clicking on the Customize button, to bring up the Customize window. Separate controls are provided for each street’s lights. To set a light on, say 50th, pull down the 50th Street’s light list and select the appropriate light. This will then be shown at the top of the form. By "stepping" through the configurations, it is possible to select the appropriate setting. It is also possible to set the "start time," which is defined to be the amount of time this configuration will remain in effect before proceeding to the next state. When the accept button is clicked, the state of the simulation is updated. Other lights can then be customized. The Proceed button returns to the main simulation page.

How It Works: The UDTS has a somewhat more complex structure than the Visual Basic 6.0 programs written so far. There is a module called mdlMain. A module is essentially a windowless form. The mdlMain module is used here to initialize all of the lights and to declare the few program variables needed. If you need to add declarations and initializations to global variables to the UDTS, plan to place them in the mdlMain.

There are also two Class Modules, clsCar and clsLight. These are not part of Visual Basic, but rather were programmer-created as part of writing the UDTS program. A class is a template or blueprint for an object. (These concepts are explained in Chapter 8 of Beginning Visual Basic 6.0.) The objects of a class have properties (like the controls) and methods that perform useful operations (like controls). An example method is DrawLight. Among the other similarities: An object is an instance of a class, and it is possible to create an array of objects of a class. For example, the UDTS uses an array of clsLight objects for the lights along each street. Object instances are created using the Dim declaration. (See the mdlMain module.) Inspect the clsCar and clsLight Class Modules to see how they work.

 

 

 

Question 3.2: Find the properties of a traffic light: Locate the definition of the clsLight and give the properties of a Light object. Also, find the definition for the light at Roosevelt and 50th and give the program’s values of these properties for that light.

Finally, there are two forms, frmTraff4 and frmCustom. The frmCustom is used exclusively for configuring the experiments. The frmTraff4 contains two procedures for advancing the cars, as well as the logic for the buttons and logic to drive the simulation. The simulation is advanced using a timer as was described for animations in the last project.

Experimenting With The UDTS: Run some experiments to familiarize yourself with the simulator’s operation.

Task 3.3: Run 10 trials, and record the elapsed times. What are the largest and smallest elapsed times observed for each route? Run 10 more trials and record the results. Were the records for the first 10 trials broken? Can you adjust the initial configurations of the lights to get larger or smaller extremes?

Task 3.4: Write a function to find the average of the transit times for the cars that have completed the trip. The function will have two parameters, the car object array and the number of completed experiments. Place the function in the frmTraff4, and use the header

Private Function average(aCar() As clsCar, completed As Integer)

Notice that the fact that the formal parameter aCar has parentheses following it tells VB6 that this is an array. The type of the items in the array is clsCar. The elapsed time for each car is the value of sTime when the car reaches the destination. Of course, to reference elapsed time of the ith element of that array requires a reference of the form,

aCar(i).sTime

The average is the sum of the elapsed times divided (\) by the number. The number of completed cars is one less than lead45car or lead50car, depending on the street. The average of these times is to be displayed for each car to the left of the Create Car button at the bottom of the form. The procedure is called from the "case 9" code for each car, at the site indicated.

Task 3.5: Change the simulation from an animation into a continuous simulation. This is accomplished by changing the timer-based driver for the simulator into a loop that is executed in the cmdStart event handler. (A Do Loop While will work best for this.) In the loop, the logic is the same as in the tmrTicker, except that there is no point in using tmrTicker to wait for a given amount of time to elapse, since the only goal is to simulate the cars. It is also unnecessary to animate them. (We will leave the screen painting in the simulation, just to get a relative idea of how much faster the simulation can be.) The Do Loop should continue while the

(lead45car <= num45car) Or (lead50car <= num50car)

condition is true. Remember to disable the timer, i.e. do not set it to a nonzero value in cmdStart.

Task 3.6: Randomize the simulation. The given version of the UDTS advances the cars at the constant rate of "65 units per tick", but driving through the U-District at a constant rate isn’t possible, except perhaps at 4:00 AM. So, randomize the speeds between each light by finding the places in the "advance car" routines where the position (.posish) state variable is set. This is the point at which the car starts towards the next light. The speed should be set (refer to the speed as .speed) to a base speed of 30 units plus a random (integer) amount between 0 and 50.

Recall from Project 2, the splatter painting example, how to call the random number function, scale the random number to the interval desired and convert it to an integer. Remember that the Randomize procedure must be called first, and the mdlMain module would be a good place to do this.

Question 3.7: Run a few batches of experimenst, e.g. 100 trials, in order to get a sense of the simulation’s behavior. Report the averages. Write a short (no more than 1 page) evaluation of the U-District Traffic Simulator and what you can learn from it about commuting from campus to I-5 North. Say which way you think is the fastest route. Comment on whether the UDTS is believable, partly believable or not believable at all, and support your position.