Loading repository data…
Loading repository data…
TheMegistone4Ever / repository
This project simulates and optimizes traffic flow at a four-way intersection, employing Petri nets (Java) for simulation and a genetic algorithm (Java) to determine optimal traffic light phase durations. Python scripts are included for visualizing simulation statistics, fitness landscapes, and the algorithm's evolution, aiming to minimize traffic.
This project simulates and optimizes traffic flow at an adjustable crossroads using Petri nets and a modified genetic algorithm. The simulation models traffic movement in four directions, considering traffic light phases and arrival rates. The genetic algorithm optimizes the duration of traffic light phases to minimize the maximum average number of waiting cars. This approach aims to reduce traffic congestion and improve overall traffic flow efficiency at the intersection.
The increasing number of vehicles in urban areas leads to frequent traffic congestion, especially at intersections. Inefficiently configured traffic lights can exacerbate these issues. This project aims to address the challenge of optimizing traffic light timings to minimize the average queue lengths at a four-way intersection. The traffic light cycle consists of four phases: green for one set of directions, followed by a yellow phase, then green for the opposite set of directions, and a final yellow phase. The goal is to determine the optimal durations for the green phases to minimize traffic congestion, using a combination of Petri net modeling and a genetic algorithm.
git clone https://github.com/your-username/adjustable-crossroads.git1. Compile the Java code: `javac *.java` (within the respective `EVOLUTIONARY_SELECTION` and `POM` directories)
java LibTest.TERM_PAPER.POM.AdjustableCrossroadsjava LibTest.TERM_PAPER.EVOLUTIONARY_SELECTION.TrafficLightOptimizerjava AdjustableCrossroadsCSVExportVISUALIZATION directory (requires Python and
necessary libraries like matplotlib, pandas, opencv-python). For example: python visualization.py,
python stability_crossroads_simulation.py, and python fitness_video_generator.pyThe Java code utilizes the PetriObj
library (https://github.com/StetsenkoInna/PetriObjModelPaint),
developed by Inna Stetsenko, for modeling the crossroads as a Petri net. This library provides the necessary
functionalities for creating and simulating Petri net models. The AdjustableCrossroads class simulates the
traffic flow, while TrafficLightOptimizer uses a genetic algorithm to find optimal phase times. The
Individual class represents a solution (chromosome) in the genetic algorithm, and the Population class manages a
collection of these solutions.
The Python scripts handle the visualization of the simulation and optimization data. They use libraries like
matplotlib, pandas, and opencv-python to generate plots, charts, and animations.
visualization.py generates plots of fitness data over generations, including the best fitness, mean fitness, and box plots.
stability_crossroads_simulation.py creates plots of simulation statistics such as mean queue lengths and car counts over time for different iterations.
results_3d.py generates a 3D surface plot visualizing the fitness landscape, showing how different combinations of phase durations affect the maximum average queue length.
fitness_video_generator.py creates an animated 3D surface plot showing the evolution of the genetic algorithm over generations, which visually demonstrates the optimization process as the algorithm searches for the best phase timings.
The animation can also be viewed on YouTube: https://youtube.com/shorts/hXU6NNPJgXo.
This section interprets the results obtained from the simulation and optimization process, providing insights into the behavior of the traffic model and the effectiveness of the optimization approach.
Experimental results demonstrate that average queue lengths in different directions are dependent on the duration of the first and third phases of the traffic light cycle. Increasing the duration of the first phase (green light for directions 1 and 2) from 20 seconds to 40 seconds resulted in a 25% reduction in queue lengths for directions 1 and 2 but increased queues for directions 3 and 4 by 40%. Similarly, increasing the third phase from 30 seconds to 60 seconds reduced queues in directions 3 and 4 by 30% while increasing queues in directions 1 and 2 by 50%. These findings indicate the importance of balance in the time allocated to each direction.
The modified evolutionary algorithm demonstrated its capability to converge to optimal phase durations, which minimizes the average queue lengths at the intersection. The algorithm's use of tournament selection, elitism, mutation, and crossover allowed it to effectively explore the solution space. Comparison with combinatorial search results confirmed the correctness of the evolutionary algorithm's findings, showing a divergence of less than 2%.
The modeling results indicated that the optimal durations for the first and third phases are 25 and 10 seconds, respectively. With these settings, the maximum average number of waiting cars at any direction is approximately 0.91 cars, which is the lowest recorded among all the tested combinations.
The optimized parameters enable the traffic lights to operate more efficiently, reducing the average queues at the intersection by 40-50% compared to the initial (20 and 30 seconds) settings and reducing the delay for vehicles.
Analysis of the model's stability at the optimal phase durations showed that the simulation model is stable, with the queue lengths at each direction not deviating more than 10% from their steady-state values, which indicates the efficiency of the optimized parameters.
The simulation results showed the advantage of an optimized traffic light system over imbalanced configurations. A non-optimized system doesn't guarantee minimized delay, with a 30-40% increase in queue lengths for directions with shorter green times, compared to directions with longer green times. Adaptive traffic management systems, while potentially more effective, are more computationally intensive and complex to implement.
The project is licensed under the CC BY-NC 4.0 License.