Traffic Simulation
Two different traffic simulations were implemented: A cyclical traffic light circuit and a traffic light simulation controlled by Unity's ml-agents.
Content
1. Project Summary and Structure
2. Spawn Manager and Car Behaviour
5. Conclusion
Project Summary and Structure
The traffic simulation is used to depict two different traffic light circuits. First, a cyclical approach changes the traffic light color regardless of environmental values. At the same time, the artificial intelligence implementation bases its decision on several factors, such as the sum of standing cars and the type of vehicle, examining whether the vehicle is tagged as special and has, therefore, a priority in traffic. These special vessels symbolize, for example, ambulances.
For the AI Unitys ml-agents were used and trained to fit these principles.
In general, the project setup is straightforward:
Cars are represented by cubes moving on a street, and the traffic lights are cuboids placed in their respective position at the crossroads.
The user can choose the simulated method by clicking on the button on the user interface. The following videos show the different methods of simulation.
The UML diagram below shows the dependencies and classes of the project:
Spawn Manager and Car Behaviour
Apart from the different types of traffic light simulation, the car behavior and the spawning and despawning of these had to be managed.
The class SpawnerController functions as spawn manager and as the parent class to four individual spawners that are each instantiated. To keep track of and control each vehicle, they are registered in a list on spawn and unregistered on despawn.
Before a car is spawned, a raycast is used to ensure no object is in the way.
As soon as this is provided, a timer is started to determine when to spawn the car. A random number sets the timer to diversify the traffic flow.
The type of the car is also defined by a random factor. In order to prevent too many special cars from being spawned, the possibility of the vehicle being a standard car is set higher.
The Unity events OnCarSpawn and OnCarDespawn are invoked on the respective events.
The car movement is kept relatively simple. Each vehicle has a random speed that is set on spawn. To prevent the car from crashing into another vessel, a raycast is used to maintain a certain distance.
Cyclical Traffic Light
The cyclic traffic light circuit is implemented in TrafficLightController. Hereby the class inhibits multiple events that are added when the car spawns and removed when it is despawned.
These events are called OnRedLight and OnGreenLight. OnRedLight is added to the vehicles' stop and OnGreenLight to the move function.
A timer controls the traffic light color. It is differentiated between the current light and the previous light since the material of the current light is always green. Therefore the material of the previous light is always red. The number of the current light is incremented in the end. The value of the previous light is set to the number of the current one.
Events are called according to the value of the previous light in CheckCase and the light is set accordingly.
ML-Agents Traffic Light
Unitys ml-agents manage the artificial intelligence based traffic light simulation. A total of eight parameters must be observed by ml-agents: The number of cars at each spawner and the vehicle type of each car. In order to ensure the least possible wait time for each vehicle, the number is required, and since special cars are to be prioritized in traffic, they must be observed too.
The shifting of the traffic light is controlled by ChangeLight, which is called in OnActionRecieved.
Ml-agents is rewarded and punished on multiple occasions.
In RewardOnCarType, the AI receives a reward if special cars move and is punished if they are standing. Their index does not matter.
If too many vehicles are standing still, ml-agents is punished in RewardOnCarCount. Cars that have already passed the traffic light are not considered here.
Furthermore, ml-agents is discouraged from allowing car collisions and receives a severe punishment should a crash occur. Since the AI had the tendency to allow a car crash in the early learning phase, the scene is reset in CarHitTimer. This is also due to the fact that cars were stuck in each other, and ml-agents was therefore being punished permanently with no option to be rewarded.
Ml-agents was trained over multiple occasions, the individual cases were each examined, and the training environment was adjusted to ensure the best possible outcome.
Conclusion
In conclusion, both applications are working and display different outcomes with various positive and negative factors. The cyclic traffic light simulation controls the traffic well since only one traffic light is green at once, regulated by time rather than external factors. But as a consequence, the possibility of cars having to wait long before being allowed to pass is very high. The lack of consideration of external factors does not allow for any special cases of regulation regarding the number of cars. To ensure that vehicles must not wait when there are no cars present at the green traffic light is to implement a trigger or collider that keeps track of whether there is a car.
Implementing the ml-agents based traffic light simulation was reasonably complex since the need to configure punishment and reward at the right place was given. It was also needed to identify the correct parameter and rate of punishment and reward. A too high or too low parameterization allowed ml-agents to find loopholes and act according to them. Furthermore, the time needed to learn rose exponentially, depending on the complexity of the desired outcome.
What went well
showcasing the different possibilities of traffic simulation
a significant learning experience in terms of the usage of ml-agents and machine learning
What went wrong
installing and configuring ml-agents was tedious and took a long time
creating the best possible learning environment for ml-agents needed to be done over multiple iterations