Traffic Project: Part 2
Many roads, routes and lights

Sample animation for deliverable 2

For the second deliverable, you will build on what you implemented previously. Still working with a single vehicle, your efforts here will support multiple road-segments, routes through a network of road-segments, and traffic lights managing the movement of cars at intersections.

Traffic Lights and Intersections

We are all familiar with traffic lights in the physical world, but how should we model such things in our simulation? Clearly, a light should have a state for the current color that is lit. But a single light at an intersection must coordinate with the state of the lights controlling other directions of travel through an intersection. Thinking ahead, we realize that the collective state of the lights at an intersection should be influenced by the level of traffic at all of the incoming roads. This reflection suggests that what we often think of as a traffic light, is but one component of several that are associated with a given intersection. For reasons we will discuss in class, we will put off incorporating these insights in our simulation until the final deliverable.

For our current purposes, we will radically simplify our treatment of intersections. First, traffic lights will only have red and green lights (i.e., no yellow lights). Second, our traffic lights will all operate in unison; that is, they will all be red or they will all be green. Obviously, this would be a disaster in heavy traffic, but since we still are modeling only a single vehicle we are fine for now.

Travel Routes

Of course, a traffic simulation needs more than a single road-segment; in this deliverable, you will implement support for multiple roads and for a vehicle to travel over a sequence of segments. We will modify our representation of vehicles by replacing the road field with a route field. We define a route to be a list of roadsegments in the order they are to be traversed. The origin location will be the start of the first roadsegment in the route and the destination will be end of the last roadsegment. At this stage, it is your responsibility to choose an origin and destination pair for which there exists a path through the roadsegments that you create; remember, roadsegments are all one-way. While traveling a route, a vehicle that reaches the end-point of its current roadsegment should switch to the start-point of the next roadsegment in the route. The vehicle (and the simulation) should stop when it reaches the end of the last roadsegment.

An origin can be the start of any roadsegment that does not have another roadsegment ending at it. We will also call an origin a source. Destinations may be selected from among roadsegment end points for which there is no roadsegment continuing (i.e., dead-ends). We will call a destination a sink. In subsequent deliverables, you will be randomly creating vehicles at sources with routes taking them to sinks. For this deliverable, we still have a single vehicle and you should hand-code the route between a valid source and sink.

Stop and Go Traffic

If the lights are red when your vehicle nears the end of its current road-segment having an associated light, the vehicle should stop a short distance before the very end of the road. Once the light turns green, the vehicle can proceed to the end of the road and switch to the beginning of the next road-segment in its route.

A basic traffic light has a timing cycle divided into phases. For our purposes, a traffic lights will stay red for a fixed ‘red-phase’ followed by a ‘green-phase’. We will introduce two state-variables to support the timing phase and the traffic-light color. The phase-clock will start at the predefined red-phase (or green-phase) value and count down towards zero on each simulation tick. When the phase-clock reaches zero, the lights should change and the clock should be reset to next phase value. The color of the lights will be represented by a second state-variable, light-status, which will take on the symbol values 'red or 'green.

Requirements

This is an individual project; working together must be limited to asking specific questions about Racket functionality.

As always, follow the design recipe and make sure your code conforms to the specifications. Function names and argument order must be followed precisely. I may test your program with code of my own so errors at testing time will result in significant penalties even if your code ‘works’ with your own tests.

  1. Revisions, Additions, and Templates. (i) Adjust the structure definition for vehicle to reflect the change from road to route and (ii) add a structure definition for traffic lights. A traffic-light (TL) is a structure: (make-light r) where r is a roadsegment. (iii) Add a template for functions that process TL structures. (iv) Modify your place-roadsegment so that the graphical representation includes an arrow pointing in the direction of travel (see image above for an example). (v) Modify init-vehicle to the change from road to route. (vi) Write definitions for the two state-variables introduced above. (vii) Modify your structure definition (and data-definition and template) for roadsegment so that it has a third field, light; this boolean field is true when there is a light associated with the roadsegment and false otherwise.
  2. place-light: TL image -> image. Write this function that places a graphical representation of a traffic light to the right of the roadsegment associated with the traffic light near the roadsegment's end. Hint: this may be one of the more challenging parts of the assignment. The light should be placed to the right in alignment with the roadsegment regardless of the orientation of the roadsegment. Look up the normal vector of a line segment for suggestions. Once you determine what you need to do, this can be accomplished using only the utilities you were to write for the first deliverable.
  3. switch-lights: -> void. Write this function to manipulate our two state-variables. The function consumes no arguments and returns void. It should be called when phase-clock has reached zero and should have the side-effects of resetting the clock to the proper value for the next phase, and should also change the value of light-status to the appropriate color.
  4. update-vehicle: vehicle -> vehicle. Modify your definition of this function so that (i) the vehicle switches from one segment to the next in its route, and (ii) stops a short distance (the width of the road is a plausible value) before the end when the lights are red. Vehicles should not stop while traversing roadsegments for which there is no corresponding light.
  5. do-updates: vehicle -> vehicle. Add the function, do-updates, that in addition to calling update-vehicle checks the phase-clock, switching the lights if necessary, and decrements the clock's value. This function would appear in the on-tick clause of your big-bang expression.
  6. Defined constants. You should define and use constants to control the size of your scene, the width of roadsegments, the size of traffic lights, the distance at which vehicles stop, the green-phase and red-phase values, etc., etc.
  7. Animate with big-bang. You should define a network of roadsegments; you should include at least ten. Within your network, determine a route having at least three roadsegments, starting at a source and ending at a sink, and involving at least one traffic light. Initialize your vehicle at its start location, and animate its movement along the route. You should tune your parameters (vehicle speed, green-phase, and red-phase) so that the vehicle must stop at at least one of the lights. For this second deliverable, the world-state for big-bang is still simply a single vehicle. The simulation should stop when the vehicle reaches the end of its route.

Submission

Do not forget to include the standard acknowledgments header at the top of your file. To grade this deliverable, I will review your code (including signatures, purpose statements, indentation, etc.), and then call big-bang with your your update-vehicle and place-vehicle functions, together with a roadsegment of my choosing.

Submit a single file with your username followed by “P2.rkt” as the name of the file, which should contain your code fulfilling the requirements as well as your acknowledgments.