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.
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.
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.
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.
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.
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.