Subj : Re: intersection design help To : comp.programming From : Thad Smith Date : Sun Sep 18 2005 06:18 pm zaebos@gmail.com wrote: > I am trying to implement a simulation of a traffic intersection with > threads, from a textbook exercise, however I have run into some > problems, and seek assitance with the design..not so much at the > algorithm level, just as to which classes and basic methods I would > have first..I have tried a few ideas but none seem correct. Hmmm. I am not a Java or OO programmer, but I have had some experience with traffic light controllers. In general, you have the cars, which are constrained by the traffic signals and each other. You have the intersection, which constrains the the cars to a certain volume based on your transit time and traffic density, then you have your controller, which bases its behavior on the sensors and timers. You could have a class for the cars, which make decisions, and the controller, which makes decisions. The intersection is passive and the sensor logic is a simple function of the vehicles, probably not worth a class. > · The program should include the following timing requirements (all > the constants below should be easy to change). > o After the traffic lights have changed to a certain colour (green, > yellow or red), they must stay that way for a minimum of 2 seconds. > o If there are cars waiting in one direction (e.g. north-south), then > the lights in the "other" direction (e.g. east-west) must change > within approximately 10 seconds, even if there are more cars > approaching/entering the intersection from this other direction. Real traffic light controllers have similar constraints, but usually with different timing values. The minimum green is usually longer and 3 seconds is considered minimum for yellow (it is longer where speeds limits are higher, dur to greater braking time). One second is typical for red clearance (between yellow and opposing green). Maximum green times are usually 20 or more seconds. Simulations, of course, let you play to your hearts content. > o After entering an intersection, cars must spend between 1-3 seconds > in the intersection (the exact time must be randomly determined when > the car enters the intersection). You do not need to worry about how > cars avoid hitting each other in the intersection. > · Cars, which must be implemented as threads, should be > suspended until the light in the direction from which they have > approached has turned green. and the car in front has moved out of the way. ;-) > · Instead of three sensors for each direction, there are only two: > the approach and enter sensors are combined into a single sensor. Many real intersections have only one. When traffic is moving, the gap sensor ("enter" sensor in your description) monitors for traffic, allowing green to terminate after a given delay without sensing a vehicle. Since your intersection is 4 phase (no independent left turns), you wouldn't terminate until both opposing phases have timed out (or max green time has elapsed). The gap time is typically around 2 seconds. Sometimes it decreases as the traffic continues (since the traffic is moving faster after initial movement when the light turned green). > · Cars must enter the intersection in the order of arrival and there > must be at least 1 second between successive cars entering the > intersection from the same direction (cars from opposite directions may > enter the intersection at approximately the same > time). Your simulation may define a maximum number of cars, in which > case this maximum should be at least 8 and easy to change. The main > events under the control of the user should be when in the simulation a > car approaches the intersection, from what direction it approaches, and > in which direction it leaves. After a car approaches the intersection, > a car must enter the intersection when the appropriate light is green or yellow? Real vehicles have non-zero braking times. > and then leave the intersection after > between 1-3 seconds (no further user input should be required for > this). > I am unsure of any ideas for the sensors at this stage, If I were writing the code, the sensor would be a boolean function indicating whether a vehicle is currently over the sensor. To simulate that, you need to know where each vehicle is and where the sensor is. You might consider the vehicle entering and leaving the sensor area to be events, as well as entering and leaving the intersection, if you are using event-based time simulation. That means that you need to model vehicle length, spacing, and speed. The sensor data only affects the controller operation. Thad .