mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-09 13:03:32 +00:00
slight sim change and engine code fomat
This commit is contained in:
@@ -41,12 +41,14 @@ public class SimulationEngine {
|
|||||||
private final PriorityQueue<Event> eventQueue;
|
private final PriorityQueue<Event> eventQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A map storing all intersections in the simulation, keyed by their ID (e.g., "Cr1").
|
* A map storing all intersections in the simulation, keyed by their ID (e.g.,
|
||||||
|
* "Cr1").
|
||||||
*/
|
*/
|
||||||
private final Map<String, Intersection> intersections;
|
private final Map<String, Intersection> intersections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responsible for creating new vehicles according to the configured arrival model.
|
* Responsible for creating new vehicles according to the configured arrival
|
||||||
|
* model.
|
||||||
*/
|
*/
|
||||||
private final VehicleGenerator vehicleGenerator;
|
private final VehicleGenerator vehicleGenerator;
|
||||||
|
|
||||||
@@ -144,8 +146,7 @@ public class SimulationEngine {
|
|||||||
id + "-" + direction,
|
id + "-" + direction,
|
||||||
direction,
|
direction,
|
||||||
greenTime,
|
greenTime,
|
||||||
redTime
|
redTime);
|
||||||
);
|
|
||||||
|
|
||||||
intersection.addTrafficLight(light);
|
intersection.addTrafficLight(light);
|
||||||
}
|
}
|
||||||
@@ -157,7 +158,8 @@ public class SimulationEngine {
|
|||||||
/**
|
/**
|
||||||
* Configures how vehicles should be routed between intersections.
|
* Configures how vehicles should be routed between intersections.
|
||||||
* This hardcoded logic defines the "map" of the city.
|
* This hardcoded logic defines the "map" of the city.
|
||||||
* * For example, `intersections.get("Cr1").configureRoute("Cr2", "East");` means
|
* * For example, `intersections.get("Cr1").configureRoute("Cr2", "East");`
|
||||||
|
* means
|
||||||
* "at intersection Cr1, any vehicle whose *next* destination is Cr2
|
* "at intersection Cr1, any vehicle whose *next* destination is Cr2
|
||||||
* should be sent to the 'East' traffic light queue."
|
* should be sent to the 'East' traffic light queue."
|
||||||
*/
|
*/
|
||||||
@@ -208,7 +210,8 @@ public class SimulationEngine {
|
|||||||
*
|
*
|
||||||
* @param light The {@link TrafficLight} that will change state.
|
* @param light The {@link TrafficLight} that will change state.
|
||||||
* @param intersectionId The ID of the intersection where the light is located.
|
* @param intersectionId The ID of the intersection where the light is located.
|
||||||
* @param delay The time (in seconds) from {@code currentTime} when the change should occur.
|
* @param delay The time (in seconds) from {@code currentTime} when the
|
||||||
|
* change should occur.
|
||||||
*/
|
*/
|
||||||
private void scheduleTrafficLightChange(TrafficLight light, String intersectionId, double delay) {
|
private void scheduleTrafficLightChange(TrafficLight light, String intersectionId, double delay) {
|
||||||
double changeTime = currentTime + delay;
|
double changeTime = currentTime + delay;
|
||||||
@@ -220,7 +223,8 @@ public class SimulationEngine {
|
|||||||
* Schedules the next {@link EventType#VEHICLE_GENERATION} event.
|
* Schedules the next {@link EventType#VEHICLE_GENERATION} event.
|
||||||
* The time of the next arrival is determined by the {@link VehicleGenerator}.
|
* The time of the next arrival is determined by the {@link VehicleGenerator}.
|
||||||
*
|
*
|
||||||
* @param baseTime The time from which to calculate the next arrival (usually {@code currentTime}).
|
* @param baseTime The time from which to calculate the next arrival (usually
|
||||||
|
* {@code currentTime}).
|
||||||
*/
|
*/
|
||||||
private void scheduleNextVehicleGeneration(double baseTime) {
|
private void scheduleNextVehicleGeneration(double baseTime) {
|
||||||
// Get the absolute time for the next arrival.
|
// Get the absolute time for the next arrival.
|
||||||
@@ -278,7 +282,8 @@ public class SimulationEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Main event processing logic.
|
* Main event processing logic.
|
||||||
* Delegates the event to the appropriate handler method based on its {@link EventType}.
|
* Delegates the event to the appropriate handler method based on its
|
||||||
|
* {@link EventType}.
|
||||||
*
|
*
|
||||||
* @param event The {@link Event} to be processed.
|
* @param event The {@link Event} to be processed.
|
||||||
*/
|
*/
|
||||||
@@ -308,7 +313,8 @@ public class SimulationEngine {
|
|||||||
* at its first destination intersection.
|
* at its first destination intersection.
|
||||||
* 4. Schedules the *next* {@link EventType#VEHICLE_GENERATION} event.
|
* 4. Schedules the *next* {@link EventType#VEHICLE_GENERATION} event.
|
||||||
* (Note: This line is commented out in the original, which might be a bug,
|
* (Note: This line is commented out in the original, which might be a bug,
|
||||||
* as it implies only one vehicle is ever generated. It should likely be active.)
|
* as it implies only one vehicle is ever generated. It should likely be
|
||||||
|
* active.)
|
||||||
*/
|
*/
|
||||||
private void handleVehicleGeneration() {
|
private void handleVehicleGeneration() {
|
||||||
Vehicle vehicle = vehicleGenerator.generateVehicle("V" + (++vehicleCounter), currentTime);
|
Vehicle vehicle = vehicleGenerator.generateVehicle("V" + (++vehicleCounter), currentTime);
|
||||||
@@ -344,7 +350,8 @@ public class SimulationEngine {
|
|||||||
* current intersection using {@link Intersection#receiveVehicle(Vehicle)}.
|
* current intersection using {@link Intersection#receiveVehicle(Vehicle)}.
|
||||||
* 5. Attempts to process the vehicle immediately if its light is green.
|
* 5. Attempts to process the vehicle immediately if its light is green.
|
||||||
*
|
*
|
||||||
* @param event The arrival event, containing the {@link Vehicle} and intersection ID.
|
* @param event The arrival event, containing the {@link Vehicle} and
|
||||||
|
* intersection ID.
|
||||||
*/
|
*/
|
||||||
private void handleVehicleArrival(Event event) {
|
private void handleVehicleArrival(Event event) {
|
||||||
Vehicle vehicle = (Vehicle) event.getData();
|
Vehicle vehicle = (Vehicle) event.getData();
|
||||||
@@ -379,7 +386,8 @@ public class SimulationEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add vehicle to the appropriate traffic light queue based on its next destination
|
// Add vehicle to the appropriate traffic light queue based on its next
|
||||||
|
// destination
|
||||||
intersection.receiveVehicle(vehicle);
|
intersection.receiveVehicle(vehicle);
|
||||||
|
|
||||||
// Try to process the vehicle immediately if its light is already green
|
// Try to process the vehicle immediately if its light is already green
|
||||||
@@ -463,7 +471,8 @@ public class SimulationEngine {
|
|||||||
* Handles {@link EventType#CROSSING_END}.
|
* Handles {@link EventType#CROSSING_END}.
|
||||||
* 1. Updates intersection and vehicle statistics.
|
* 1. Updates intersection and vehicle statistics.
|
||||||
* 2. Checks the vehicle's *next* destination.
|
* 2. Checks the vehicle's *next* destination.
|
||||||
* 3. If the next destination is the exit ("S"), call {@link #handleVehicleExit(Vehicle)}.
|
* 3. If the next destination is the exit ("S"), call
|
||||||
|
* {@link #handleVehicleExit(Vehicle)}.
|
||||||
* 4. Otherwise, schedule a {@link EventType#VEHICLE_ARRIVAL} event at the
|
* 4. Otherwise, schedule a {@link EventType#VEHICLE_ARRIVAL} event at the
|
||||||
* *next* intersection, after some travel time.
|
* *next* intersection, after some travel time.
|
||||||
*
|
*
|
||||||
@@ -489,7 +498,8 @@ public class SimulationEngine {
|
|||||||
String nextDest = vehicle.getCurrentDestination();
|
String nextDest = vehicle.getCurrentDestination();
|
||||||
if (nextDest != null && !nextDest.equals("S")) {
|
if (nextDest != null && !nextDest.equals("S")) {
|
||||||
// Route to the *next* intersection
|
// Route to the *next* intersection
|
||||||
// Travel time varies by vehicle type: tmoto = 0.5 × tcarro, tcaminhão = 4 × tmoto
|
// Travel time varies by vehicle type: tmoto = 0.5 × tcarro, tcaminhão = 4 ×
|
||||||
|
// tmoto
|
||||||
double travelTime = calculateTravelTime(vehicle.getType());
|
double travelTime = calculateTravelTime(vehicle.getType());
|
||||||
double arrivalTime = currentTime + travelTime;
|
double arrivalTime = currentTime + travelTime;
|
||||||
Event arrivalEvent = new Event(arrivalTime, EventType.VEHICLE_ARRIVAL, vehicle, nextDest);
|
Event arrivalEvent = new Event(arrivalTime, EventType.VEHICLE_ARRIVAL, vehicle, nextDest);
|
||||||
@@ -519,7 +529,8 @@ public class SimulationEngine {
|
|||||||
/**
|
/**
|
||||||
* Handles {@link EventType#TRAFFIC_LIGHT_CHANGE}.
|
* Handles {@link EventType#TRAFFIC_LIGHT_CHANGE}.
|
||||||
* 1. Toggles the light's state (RED to GREEN or GREEN to RED).
|
* 1. Toggles the light's state (RED to GREEN or GREEN to RED).
|
||||||
* 2. If the light just turned GREEN, call {@link #processGreenLight(TrafficLight, Intersection)}
|
* 2. If the light just turned GREEN, call
|
||||||
|
* {@link #processGreenLight(TrafficLight, Intersection)}
|
||||||
* to process any waiting vehicles.
|
* to process any waiting vehicles.
|
||||||
* 3. Schedules the *next* state change for this light based on its
|
* 3. Schedules the *next* state change for this light based on its
|
||||||
* green/red time duration.
|
* green/red time duration.
|
||||||
@@ -592,7 +603,8 @@ public class SimulationEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility method to get the configured crossing time for a given {@link VehicleType}.
|
* Utility method to get the configured crossing time for a given
|
||||||
|
* {@link VehicleType}.
|
||||||
*
|
*
|
||||||
* @param type The type of vehicle.
|
* @param type The type of vehicle.
|
||||||
* @return The crossing time in seconds.
|
* @return The crossing time in seconds.
|
||||||
@@ -623,6 +635,7 @@ public class SimulationEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current simulation time.
|
* Gets the current simulation time.
|
||||||
|
*
|
||||||
* @return The time in virtual seconds.
|
* @return The time in virtual seconds.
|
||||||
*/
|
*/
|
||||||
public double getCurrentTime() {
|
public double getCurrentTime() {
|
||||||
@@ -632,6 +645,7 @@ public class SimulationEngine {
|
|||||||
/**
|
/**
|
||||||
* Gets a map of all intersections in the simulation.
|
* Gets a map of all intersections in the simulation.
|
||||||
* Returns a copy to prevent external modification.
|
* Returns a copy to prevent external modification.
|
||||||
|
*
|
||||||
* @return A {@link Map} of intersection IDs to {@link Intersection} objects.
|
* @return A {@link Map} of intersection IDs to {@link Intersection} objects.
|
||||||
*/
|
*/
|
||||||
public Map<String, Intersection> getIntersections() {
|
public Map<String, Intersection> getIntersections() {
|
||||||
@@ -640,6 +654,7 @@ public class SimulationEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the statistics collector instance.
|
* Gets the statistics collector instance.
|
||||||
|
*
|
||||||
* @return The {@link StatisticsCollector}.
|
* @return The {@link StatisticsCollector}.
|
||||||
*/
|
*/
|
||||||
public StatisticsCollector getStatisticsCollector() {
|
public StatisticsCollector getStatisticsCollector() {
|
||||||
|
|||||||
@@ -46,54 +46,54 @@ simulation.arrival.fixed.interval=2.0
|
|||||||
# === TRAFFIC LIGHT TIMINGS ===
|
# === TRAFFIC LIGHT TIMINGS ===
|
||||||
# Format: trafficlight.<intersection>.<direction>.<state>=<seconds>
|
# Format: trafficlight.<intersection>.<direction>.<state>=<seconds>
|
||||||
|
|
||||||
# Intersection 1
|
# Intersection 1 (Entry point - balanced)
|
||||||
trafficlight.Cr1.North.green=30.0
|
trafficlight.Cr1.North.green=20.0
|
||||||
trafficlight.Cr1.North.red=30.0
|
trafficlight.Cr1.North.red=40.0
|
||||||
trafficlight.Cr1.South.green=30.0
|
trafficlight.Cr1.South.green=20.0
|
||||||
trafficlight.Cr1.South.red=30.0
|
trafficlight.Cr1.South.red=40.0
|
||||||
trafficlight.Cr1.East.green=30.0
|
trafficlight.Cr1.East.green=20.0
|
||||||
trafficlight.Cr1.East.red=30.0
|
trafficlight.Cr1.East.red=40.0
|
||||||
trafficlight.Cr1.West.green=30.0
|
trafficlight.Cr1.West.green=20.0
|
||||||
trafficlight.Cr1.West.red=30.0
|
trafficlight.Cr1.West.red=40.0
|
||||||
|
|
||||||
# Intersection 2
|
# Intersection 2 (Main hub - shorter cycles, favor East-West)
|
||||||
trafficlight.Cr2.North.green=25.0
|
trafficlight.Cr2.North.green=12.0
|
||||||
trafficlight.Cr2.North.red=35.0
|
trafficlight.Cr2.North.red=36.0
|
||||||
trafficlight.Cr2.South.green=25.0
|
trafficlight.Cr2.South.green=12.0
|
||||||
trafficlight.Cr2.South.red=35.0
|
trafficlight.Cr2.South.red=36.0
|
||||||
trafficlight.Cr2.East.green=35.0
|
trafficlight.Cr2.East.green=18.0
|
||||||
trafficlight.Cr2.East.red=25.0
|
trafficlight.Cr2.East.red=30.0
|
||||||
trafficlight.Cr2.West.green=35.0
|
trafficlight.Cr2.West.green=18.0
|
||||||
trafficlight.Cr2.West.red=25.0
|
trafficlight.Cr2.West.red=30.0
|
||||||
|
|
||||||
# Intersection 3
|
# Intersection 3 (Path to exit - favor East)
|
||||||
trafficlight.Cr3.North.green=30.0
|
trafficlight.Cr3.North.green=15.0
|
||||||
trafficlight.Cr3.North.red=30.0
|
trafficlight.Cr3.North.red=30.0
|
||||||
trafficlight.Cr3.South.green=30.0
|
trafficlight.Cr3.South.green=15.0
|
||||||
trafficlight.Cr3.South.red=30.0
|
trafficlight.Cr3.South.red=30.0
|
||||||
trafficlight.Cr3.East.green=30.0
|
trafficlight.Cr3.East.green=20.0
|
||||||
trafficlight.Cr3.East.red=30.0
|
trafficlight.Cr3.East.red=25.0
|
||||||
trafficlight.Cr3.West.green=30.0
|
trafficlight.Cr3.West.green=15.0
|
||||||
trafficlight.Cr3.West.red=30.0
|
trafficlight.Cr3.West.red=30.0
|
||||||
|
|
||||||
# Intersection 4
|
# Intersection 4 (Favor East toward Cr5)
|
||||||
trafficlight.Cr4.North.green=30.0
|
trafficlight.Cr4.North.green=15.0
|
||||||
trafficlight.Cr4.North.red=30.0
|
trafficlight.Cr4.North.red=30.0
|
||||||
trafficlight.Cr4.South.green=30.0
|
trafficlight.Cr4.South.green=15.0
|
||||||
trafficlight.Cr4.South.red=30.0
|
trafficlight.Cr4.South.red=30.0
|
||||||
trafficlight.Cr4.East.green=30.0
|
trafficlight.Cr4.East.green=20.0
|
||||||
trafficlight.Cr4.East.red=30.0
|
trafficlight.Cr4.East.red=25.0
|
||||||
trafficlight.Cr4.West.green=30.0
|
trafficlight.Cr4.West.green=15.0
|
||||||
trafficlight.Cr4.West.red=30.0
|
trafficlight.Cr4.West.red=30.0
|
||||||
|
|
||||||
# Intersection 5
|
# Intersection 5 (Near exit - favor East)
|
||||||
trafficlight.Cr5.North.green=30.0
|
trafficlight.Cr5.North.green=15.0
|
||||||
trafficlight.Cr5.North.red=30.0
|
trafficlight.Cr5.North.red=30.0
|
||||||
trafficlight.Cr5.South.green=30.0
|
trafficlight.Cr5.South.green=15.0
|
||||||
trafficlight.Cr5.South.red=30.0
|
trafficlight.Cr5.South.red=30.0
|
||||||
trafficlight.Cr5.East.green=30.0
|
trafficlight.Cr5.East.green=22.0
|
||||||
trafficlight.Cr5.East.red=30.0
|
trafficlight.Cr5.East.red=23.0
|
||||||
trafficlight.Cr5.West.green=30.0
|
trafficlight.Cr5.West.green=15.0
|
||||||
trafficlight.Cr5.West.red=30.0
|
trafficlight.Cr5.West.red=30.0
|
||||||
|
|
||||||
# === VEHICLE CONFIGURATION ===
|
# === VEHICLE CONFIGURATION ===
|
||||||
@@ -118,4 +118,4 @@ vehicle.travel.time.heavy.multiplier=2.0
|
|||||||
# === STATISTICS ===
|
# === STATISTICS ===
|
||||||
|
|
||||||
# Interval between dashboard updates (seconds)
|
# Interval between dashboard updates (seconds)
|
||||||
statistics.update.interval=10.0
|
statistics.update.interval=1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user