Simulation of traffic queues in a junction with lights
It has been determined that the traffic flow in the junction below is not satisfactory, there are
often long queues.
The junction is controlled by the four light signals s1, s2, s3 and s4.
A significant part of the traffic that comes from E, to the right in the figure, is to turn left towards
S in the junction but they are often blocked by vehicles that comes from W. To solve this
problem, an additional lane for vehicles that comes from E respective W that are
to turn left in the junction, i.e. we would like to have a junction like this:
This junction does have additional lanes controlled by the light signals s2 and s5.
To determine the length of these lanes, we would like to have a program that simulates the traffic
flow in the crossing at different lengths of these turning" lanes, different traffic intensity and
different cycles of the light signals. To do this it is enough to study the flow in one direction, i.e.
in a system like this:
Since traffic from E aiming to both N and W are controlled by the same light signal (s1) we dont
need to separate these. We treat this traffic as aiming to W. We dont need to track the vehicles
when they have left the crossing.
Computer model
We represent a lane with an array where each element is either empty (null) or a vehicle (all
vehicles are assumed to have the same size).
The system consists of the lanes (r0, r1 and r2) and two light signals (s1 and s2):
In a time step one or more of the following events may occur:
_ a signal changes color.
_ a vehicle passes a signal (if it shows green).
_ a vehicle advances one step in a lane (if the place before it is free).
_ the vehicle immediately before X (i.e. in link list r0[0]) is moved to r1 or r2 depending on its
destination (W or S),
_ a vehicle arrives to the system at the point E.
A simulation consists of a time stepping where the events above occur.
The function of the light signals
A signal is green or red (no yellow light is needed). A signal is characterized by two parameters:
_ a period i.e. the number of time steps from the start of one green cycle to the start of the next.
_ a greenperiod i.e. the number of timesteps the signal is green.
These parameters are assigned when the light signal is built (ie as parameters to the constructor).
A signal needs an internal clock that is ticked by a step-method. It is a good idea (but not
necessary) to let the clock be circular, ie when it reaches the end of the period, it is reset to zero.
You also need a method that decides whether the signal is green or not.
Both signals should have the same period and start as green.
Example: If the period is 7 and the green period for s1 is 3 and fore s2 is 2, the table below
shows how the internal clock should tick and what color they show
Simulation
The program is controlled by the following parameters:
_ The length of the lanes. The lanes r1 and r2 always have the same length.
_ Probability of an arrival, i.e. th.
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Simulation of traffic queues in a junction with lightsIt has been .pdf
1. Simulation of traffic queues in a junction with lights
It has been determined that the traffic flow in the junction below is not satisfactory, there are
often long queues.
The junction is controlled by the four light signals s1, s2, s3 and s4.
A significant part of the traffic that comes from E, to the right in the figure, is to turn left towards
S in the junction but they are often blocked by vehicles that comes from W. To solve this
problem, an additional lane for vehicles that comes from E respective W that are
to turn left in the junction, i.e. we would like to have a junction like this:
This junction does have additional lanes controlled by the light signals s2 and s5.
To determine the length of these lanes, we would like to have a program that simulates the traffic
flow in the crossing at different lengths of these turning" lanes, different traffic intensity and
different cycles of the light signals. To do this it is enough to study the flow in one direction, i.e.
in a system like this:
Since traffic from E aiming to both N and W are controlled by the same light signal (s1) we dont
need to separate these. We treat this traffic as aiming to W. We dont need to track the vehicles
when they have left the crossing.
Computer model
We represent a lane with an array where each element is either empty (null) or a vehicle (all
vehicles are assumed to have the same size).
The system consists of the lanes (r0, r1 and r2) and two light signals (s1 and s2):
In a time step one or more of the following events may occur:
_ a signal changes color.
_ a vehicle passes a signal (if it shows green).
_ a vehicle advances one step in a lane (if the place before it is free).
_ the vehicle immediately before X (i.e. in link list r0[0]) is moved to r1 or r2 depending on its
destination (W or S),
_ a vehicle arrives to the system at the point E.
A simulation consists of a time stepping where the events above occur.
The function of the light signals
A signal is green or red (no yellow light is needed). A signal is characterized by two parameters:
_ a period i.e. the number of time steps from the start of one green cycle to the start of the next.
_ a greenperiod i.e. the number of timesteps the signal is green.
These parameters are assigned when the light signal is built (ie as parameters to the constructor).
A signal needs an internal clock that is ticked by a step-method. It is a good idea (but not
necessary) to let the clock be circular, ie when it reaches the end of the period, it is reset to zero.
2. You also need a method that decides whether the signal is green or not.
Both signals should have the same period and start as green.
Example: If the period is 7 and the green period for s1 is 3 and fore s2 is 2, the table below
shows how the internal clock should tick and what color they show
Simulation
The program is controlled by the following parameters:
_ The length of the lanes. The lanes r1 and r2 always have the same length.
_ Probability of an arrival, i.e. the probability that a vehicle arrives at E at a time step.
_ The probability that a created vehicle has S as its destination.
_ The parameters of the light signals (period and green period).
For the purpose of this assignment it is sufficient that these parameters are set in the code in a
way so it is easy to change them.
The result of a run
_ Average time (number of time steps) and maximal time for a vehicle to pass the light signal s1
resp. s2
_ Number of time steps when the queue at either light signal is longer than r1 and r2, i.e. the time
that the junction at X has been blocked by a queue.
_ A simple illustration of the system.
Example:
Design and sequence of work
You must follow the design (classes, methods- and name of attributes) that is found in the code
skeleton attached.
Solve the problem stepwise:
1. Create a folder for the files and copy the code skeleton into it.
2. Implement the Vehicle- and the Lane-classes.
NOTE: Dont forget the toString() methods!
3. Implement the Light-class.
4. Implement the class TrafficSystem that defines a traffic system as described above.
Leave the collection of statistics until the complete system is running.
5. Add the collection of the statistics.
public class Lane {
private Vehicle[] theLane;
public Lane(int n) {
3. }
public void step() {
}
public Vehicle removeFirst() {
return null;
}
public Vehicle getFirst() {
return null;
}
public boolean lastFree() {
return true;
}
public void putLast(Vehicle v) {
}
public String toString() {
return null;
}
}
public class Simulation {
public static void main(String [] args) {
TrafficSystem tf = new TrafficSystem();
while (true) {
try {
// If the printouts are done each timestep, a pause is needed
Thread.sleep(100);
}
catch (InterruptedException e) {
}
tf.step();
tf.print();
}
}
}
4. public class Light {
private int period;
private int green;
private int time;
public Light(int p, int g) {
}
public void step() {
}
public boolean isGreen() {
return true;
}
public String toString() {
return null;
}
}
public class Vehicle {
private int bornTime;
private char destination;
public Vehicle(int b, char d) {
}
public String toString()
{
return null;
}
}
/** TrafficSystem *
* Defines the lanes and signals that is to be studied. Collects statistics
*
* Model for traffic simulation
* ============================
*
5. * The following classes are used:
*
* Vehicle Represents a vehicle
* Time of arrival and destination are set when create.
*
* Light Represents the light signals
* See below
*
* Lane Represents a piece of a road
* A lane is represented by an array where each element
* either is empty or contain a reference to a
* vehicle-object.
*
* TrafficSystem
* Defines the components, ie the lanes and signals that
* build the system. See below
*
* Simulation
* main-method the controls the simulation
*
*
* The situation that is to be simulated looks schematically like
*
* C X E
* W s1<----r1-----<---------r0---------------------
* S s2<----r2-----<
*
* A lane (a piece of a road) r0 split into two files r1 and r2 at X.
* The signal s1 controls the lane r1 and the signal s2 the lane r2.
*
* Vehicles are create at E. The probability that a vehicle arrives to E
* at a certain time is called "the intensity of arrival".
*
* At a time step the vehicles move one step forward (if possible).
* At C, the vehicles are removed from the system if the resp signal is green.
* At X, vehicles are move from r0 to either r1 or r2 depending of its
6. * destination (if there are space for them).
*
*
*/
public class TrafficSystem {
// Attributes that describe the elements of the system
private Lane r0;
private Lane r1;
private Lane r2;
private Light s1;
private Light s2;
private int time = 0;
public TrafficSystem() {
}
public void step() {
}
public void printStatistics() {
}
public void print() {
}
}
" (1) | s (C)
begin{tabular}{|l|c|c|c|c|c|c|c|c|c|c|c|c|} hline Timestep: & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8
& 9 & 10 & hline Internal clock: & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 0 & 1 & 2 & 3 & hline
Color s1: & G & G & G & R & R & R & R & G & G & G & R & hline Color s2: & G & G
& R & R & R & R & R & G & G & R & R & hline end{tabular}
R:[WWW[WW[SWWSWWSSSWS]R:[SSSSSSSS]R:[WWWW][SWWSSWSSSWSW]R:[Ssss
SSSS]