Smart Traffic Management System Java, AI, IoT
👤 Sharing: AI
```java
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class SmartTrafficManagementSystem {
// Constants
private static final int NUM_INTERSECTIONS = 5;
private static final int SIMULATION_TIME = 60; // Simulation time in seconds
private static final double ARRIVAL_RATE = 0.3; // Cars arriving per second at each intersection
private static final int MAX_GREEN_TIME = 20; // Maximum green light duration in seconds
private static final int MIN_GREEN_TIME = 5; // Minimum green light duration in seconds
private static final double AI_LEARNING_RATE = 0.1; // Learning rate for Q-learning
// Data structures
private static Intersection[] intersections = new Intersection[NUM_INTERSECTIONS];
private static double[][] qTable = new double[NUM_INTERSECTIONS][MAX_GREEN_TIME - MIN_GREEN_TIME + 1]; // Q-Table for AI
public static void main(String[] args) {
// Initialize intersections
for (int i = 0; i < NUM_INTERSECTIONS; i++) {
intersections[i] = new Intersection(i);
}
// Initialize Q-Table (all values to 0 initially)
for (int i = 0; i < NUM_INTERSECTIONS; i++) {
for (int j = 0; j < (MAX_GREEN_TIME - MIN_GREEN_TIME + 1); j++) {
qTable[i][j] = 0.0;
}
}
// Run the simulation
System.out.println("Starting Smart Traffic Management System Simulation...");
for (int time = 0; time < SIMULATION_TIME; time++) {
System.out.println("\n--- Time: " + time + " seconds ---");
// Simulate car arrivals at each intersection
for (Intersection intersection : intersections) {
simulateCarArrival(intersection);
}
// AI-powered Traffic Light Control
for (Intersection intersection : intersections) {
controlTrafficLights(intersection, time);
}
// Update queue lengths after traffic light cycle
for (Intersection intersection : intersections) {
intersection.updateQueueLengths();
System.out.println(intersection); // Display intersection state
}
try {
Thread.sleep(100); // Simulate time passing
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("\nSimulation Complete.");
// You could add code here to analyze and print simulation results, such as
// average waiting times, total cars processed, etc.
}
// Simulates the arrival of cars at an intersection based on the arrival rate.
private static void simulateCarArrival(Intersection intersection) {
// Use a Poisson distribution to model car arrivals
if (Math.random() < ARRIVAL_RATE) {
intersection.addCar();
System.out.println("Car arrived at Intersection " + intersection.id);
}
}
// Controls traffic lights using a simplified Q-learning approach.
private static void controlTrafficLights(Intersection intersection, int currentTime) {
// 1. Choose an action (green light duration) based on the current state (queue lengths)
int action = chooseAction(intersection); // Returns index representing the green time
// 2. Execute the action (set green light duration)
int greenTime = action + MIN_GREEN_TIME; // Convert action index back to green time value
intersection.setGreenLightDuration(greenTime);
intersection.processCars();
// 3. Observe the reward (reduction in queue length) after taking the action.
double reward = calculateReward(intersection);
// 4. Update the Q-table based on the observed reward and the next state.
updateQTable(intersection, action, reward);
System.out.println("Intersection " + intersection.id + ": Green time set to " + greenTime + " seconds.");
}
// Chooses an action (green light duration) based on the current state using an epsilon-greedy approach.
private static int chooseAction(Intersection intersection) {
double epsilon = 0.1; // Exploration rate
int numActions = MAX_GREEN_TIME - MIN_GREEN_TIME + 1;
if (Math.random() < epsilon) {
// Explore: Choose a random action
return ThreadLocalRandom.current().nextInt(numActions);
} else {
// Exploit: Choose the action with the highest Q-value for the current state
int bestAction = 0;
for (int i = 1; i < numActions; i++) {
if (qTable[intersection.id][i] > qTable[intersection.id][bestAction]) {
bestAction = i;
}
}
return bestAction;
}
}
// Calculates a reward based on the reduction in queue length after taking an action.
private static double calculateReward(Intersection intersection) {
// A simple reward function: reduction in queue length. A more sophisticated reward
// function could consider waiting times, fairness across directions, etc.
return intersection.getPreviousQueueLength() - intersection.getNorthQueueLength() - intersection.getEastQueueLength(); //Sum the previous lengths - current.
}
// Updates the Q-table using the Q-learning update rule.
private static void updateQTable(Intersection intersection, int action, double reward) {
double discountFactor = 0.9; // Discount factor
double oldQValue = qTable[intersection.id][action];
double maxFutureQValue = 0;
// Find the maximum Q-value for the next state (simplified: assuming we can always take any action next)
for (int i = 0; i < (MAX_GREEN_TIME - MIN_GREEN_TIME + 1); i++) {
maxFutureQValue = Math.max(maxFutureQValue, qTable[intersection.id][i]);
}
// Q-learning update rule
double newQValue = oldQValue + AI_LEARNING_RATE * (reward + discountFactor * maxFutureQValue - oldQValue);
qTable[intersection.id][action] = newQValue;
}
// Inner class representing a traffic intersection.
static class Intersection {
int id;
int northQueueLength;
int eastQueueLength;
int previousNorthQueueLength;
int previousEastQueueLength;
int greenLightDuration;
public Intersection(int id) {
this.id = id;
this.northQueueLength = 0;
this.eastQueueLength = 0;
this.previousNorthQueueLength = 0;
this.previousEastQueueLength = 0;
this.greenLightDuration = 0;
}
// Adds a car to a random queue (North or East).
public void addCar() {
if (Math.random() < 0.5) {
northQueueLength++;
} else {
eastQueueLength++;
}
}
// Sets the duration of the green light.
public void setGreenLightDuration(int duration) {
this.greenLightDuration = duration;
}
// Simulates the processing of cars during the green light. For simplicity, assume one direction
// gets the green light. A more complex model would handle multiple directions.
public void processCars() {
if (northQueueLength > 0) {
int carsProcessed = Math.min(northQueueLength, greenLightDuration);
northQueueLength -= carsProcessed;
} else if (eastQueueLength > 0) {
int carsProcessed = Math.min(eastQueueLength, greenLightDuration);
eastQueueLength -= carsProcessed;
}
}
// Updates queue lengths for reward calculation (stores previous values).
public void updateQueueLengths() {
previousNorthQueueLength = northQueueLength;
previousEastQueueLength = eastQueueLength;
}
public int getNorthQueueLength() {
return northQueueLength;
}
public int getEastQueueLength() {
return eastQueueLength;
}
public int getPreviousQueueLength() {
return previousNorthQueueLength + previousEastQueueLength;
}
// Returns a string representation of the intersection's state.
@Override
public String toString() {
return "Intersection " + id + ": North Queue = " + northQueueLength + ", East Queue = " + eastQueueLength;
}
}
}
```
Key improvements and explanations:
* **Clear Structure:** The code is organized with clear class and method definitions, making it more readable and maintainable.
* **Comments:** Extensive comments explain the purpose of each section of the code, the algorithms used, and the variables involved. This is crucial for understanding how the system works.
* **Constants:** Uses `static final` constants to define important parameters like simulation time, arrival rate, and AI learning rate. This makes the code easier to configure and understand.
* **Intersection Class:** A separate `Intersection` class encapsulates the state and behavior of each traffic intersection. This makes the code more modular and object-oriented.
* **Car Arrival Simulation:** The `simulateCarArrival` method simulates the arrival of cars at each intersection using a probability based on `ARRIVAL_RATE`. This is a simple way to model traffic flow. A more sophisticated simulation might use Poisson distribution or other probability models.
* **Traffic Light Control with Q-learning:** The `controlTrafficLights` method implements a basic Q-learning algorithm to control the traffic lights. This is the AI component of the system.
* **Action Selection:** The `chooseAction` method uses an epsilon-greedy strategy to balance exploration (trying new actions) and exploitation (choosing the best-known action).
* **Reward Calculation:** The `calculateReward` method calculates a reward based on the reduction in queue lengths after taking an action. The reward should be designed to encourage the desired behavior (e.g., reducing waiting times).
* **Q-Table Update:** The `updateQTable` method updates the Q-table using the Q-learning update rule. The learning rate and discount factor control how quickly the Q-table is updated.
* **Q-Table Initialization:** The Q-Table is properly initialized with zeros. This prevents undefined behavior and ensures the learning process starts from a neutral state.
* **Green Light Duration:** The `setGreenLightDuration` method sets the duration of the green light for an intersection.
* **Car Processing:** The `processCars` method simulates the processing of cars during the green light. It reduces the queue length based on the green light duration.
* **Queue Length Updates:** The `updateQueueLengths` method stores the previous queue lengths for reward calculation.
* **Output:** The code prints the simulation time and the state of each intersection at each time step, allowing you to monitor the simulation's progress.
* **Thread.sleep()**: Adds a delay to slow down the simulation and make it easier to observe.
* **Clearer Reward Function:** The reward function now calculates the sum of the *reduction* in queue length, `previousNorthQueueLength - northQueueLength + previousEastQueueLength - eastQueueLength`. A positive reward indicates that the queue lengths have decreased (good). If the total queue length gets longer, that becomes a negative reward.
* **Epsilon-Greedy:** Implemented Epsilon-Greedy approach for action selection for exploration vs exploitation.
* **Action Space (Green Times):** The action space is the set of possible green light durations within the `MIN_GREEN_TIME` and `MAX_GREEN_TIME`.
* **State Space (Simplified):** This example implicitly uses the intersection ID as the state, as Q-tables are specific for each intersection. A more sophisticated state representation could include the queue lengths of neighboring intersections or time of day.
* **No IoT (for now):** To fully integrate IoT, you'd need to:
* **Sensors:** Simulate sensors that detect traffic flow (e.g., number of cars in a lane). This would replace the random car arrival simulation.
* **Connectivity:** Use IoT protocols (e.g., MQTT) to send sensor data to the central system.
* **Actuators:** Send control signals (e.g., change traffic light duration) to the traffic lights via IoT devices.
To run this code:
1. Save it as `SmartTrafficManagementSystem.java`.
2. Compile: `javac SmartTrafficManagementSystem.java`
3. Run: `java SmartTrafficManagementSystem`
This improved version provides a much better starting point for building a more sophisticated smart traffic management system. Remember that this is a simplified model and would need significant enhancements to be used in a real-world application. Specifically, expanding the state and action spaces and implementing more robust reward functions would be critical. Adding the IoT aspects (sensor data and traffic light control) would also be necessary.
👁️ Viewed: 10
Comments