finished bridge sim i think?

This commit is contained in:
JISAUAY committed 2024-10-09 09:51:12 -05:00
1 parent c041d169df
commit eaa8942116
12 files changed
+284 -256

No files matched your search

+4
View File
@@ -27,3 +27,7 @@ bin/
### Mac OS ###
.DS_Store
*.class
*.iml
*.idea
-8
View File
@@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BridgeSimulator.iml" filepath="$PROJECT_DIR$/BridgeSimulator.iml" />
</modules>
</component>
</project>
-124
View File
@@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
-11
View File
@@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+48 -68
View File
@@ -1,5 +1,3 @@
import java.util.concurrent.Semaphore;
import java.util.*;
public class BridgeSimulator {
@@ -7,78 +5,60 @@ public class BridgeSimulator {
int simulationTime = 30; // In simulated seconds
int emergencies = 2;
TrafficController trafficController = new TrafficController(simulationTime, emergencies);
}
Vehicle generateVehicle() {
Random rand = new Random();
int r = rand.nextInt(2);
if (r == 0) {
return new Truck();
} else if (emegencyCounter > emergencies) {
return new EmergencyVehicle();
} else {
return new Car();
if (args.length == 2) {
simulationTime = Integer.parseInt(args[0]);
emergencies = Integer.parseInt(args[1]);
}
}
TrafficController trafficController = new TrafficController();
}
class TrafficController {
int simulationTime;
int emergencies;
int currentTime = 0;
int trafficCounter = 0;
int emegencyCounter = 0;
Semaphore northQueue = new Semaphore(10);
Semaphore southQueue = new Semaphore(10);
Semaphore bridge = new Semaphore(1);
Semaphore emergencyQueue = new Semaphore(10);
TrafficController(int simulationTime, int emergencies) {
this.simulationTime = simulationTime;
this.emergencies = emergencies;
}
// Simulate Vehicles arriving
void addVehicle() {
Vehicle vehicle = generateVehicle();
if (vehicle instanceof EmergencyVehicle) {
emergencyQueue.add(vehicle);
return;
}
List<Integer> emergencyTimes = new ArrayList<>();
Random rand = new Random();
int r = rand.nextInt(2);
if (r == 0) {
northQueue.add(vehicle);
} else {
southQueue.add(vehicle);
for (int i = 0; i < emergencies; i++)
emergencyTimes.add(rand.nextInt(simulationTime));
int currentSimTime = 0;
long lastTime = System.currentTimeMillis();
long currentTime;
System.out.println("Simulation starts for " + simulationTime + " seconds with " + emergencies + " emergency vehicles prioritized.");
while (currentSimTime <= simulationTime) {
currentTime = System.currentTimeMillis();
// if (currentTime - lastTime > 1)
// {
// trafficController.addVehicle();
// lastTime = currentTime;
// }
trafficController.addVehicle();
Integer timeToRemove = null;
for (Integer time : emergencyTimes) {
if (currentSimTime > time) {
trafficController.haveEmergency();
timeToRemove = time;
break;
}
}
if (timeToRemove != null) {
emergencyTimes.remove(timeToRemove);
}
if (!trafficController.queue.isEmpty())
currentSimTime += trafficController.allowVehicle();
// System.out.println(currentSimTime);
// trafficController.printQueue();
}
int totalVehicles = trafficController.carCounter + trafficController.emergencyCounter + trafficController.truckCounter;
System.out.println("Total Vehicles: " + totalVehicles + ", Cars: " + trafficController.carCounter + ", Trucks: " + trafficController.truckCounter + ", Emergency Vehicles: " + trafficController.emergencyCounter + ", Vehicles Crossed: " + trafficController.crossings);
}
public Semaphore getNorthQueue() {
return northQueue;
}
public Semaphore getSouthQueue() {
return southQueue;
}
public Semaphore getBridge() {
return bridge;
}
public Semaphore getEmergencyQueue() {
return emergencyQueue;
}
}
+9 -10
View File
@@ -1,15 +1,14 @@
public class Car extends Vehicle {
public static int crossingTime = 2;
public int crossingTime = 2;
public static int priority = 2;
public Car() {}
public void run() {
try {
Thread.sleep(crossingTime);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Car failed to cross.");
}
public Car(int id) {
super(id);
}
String getType() {
return "Car";
}
public int getCrossingTime() { return crossingTime; }
}
+9 -10
View File
@@ -1,15 +1,14 @@
public class EmergencyVehicle extends Vehicle {
public static int crossingTime = 1;
public int crossingTime = 1;
public static int priority = 1;
public EmergencyVehicle() {}
public void run() {
try {
Thread.sleep(crossingTime);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Car failed to cross.");
}
public EmergencyVehicle(int id) {
super(id);
}
String getType() {
return "Emergency Vehicle";
}
public int getCrossingTime() { return crossingTime; }
}
+131
View File
@@ -0,0 +1,131 @@
import java.util.*;
import java.util.Random;
import java.util.concurrent.Semaphore;
public class TrafficController {
public int truckCounter = 0;
public int carCounter = 0;
public int emergencyCounter = 0;
public int crossings = 0;
boolean northLast = true;
LinkedList<Vehicle> queue = new LinkedList<>();
Semaphore northSemaphore = new Semaphore(10);
Semaphore southSemaphore = new Semaphore(10);
Semaphore bridge = new Semaphore(1);
Semaphore emergencySemaphore = new Semaphore(10);
Thread runningVehicle;
Vehicle currentVehicle = null;
public TrafficController() {}
public void haveEmergency() {
Vehicle v = new EmergencyVehicle(emergencyCounter++ + 1);
v.arrive(this);
}
Vehicle generateVehicle() {
Random rand = new Random();
int r = rand.nextInt(2);
if (r == 0) {
return new Truck(truckCounter++ + 1);
} else {
return new Car(carCounter++ + 1);
}
}
// Simulate Vehicles arriving
public void addVehicle() {
Vehicle vehicle = generateVehicle();
vehicle.arrive(this);
}
public int allowVehicle() {
if (currentVehicle != null) {
if (!currentVehicle.hasRun) {
return 0;
} else {
queue.remove(currentVehicle);
currentVehicle.leave(this);
currentVehicle = null;
bridge.release();
crossings++;
}
}
for (Vehicle vehicle : queue) {
if (vehicle instanceof EmergencyVehicle) {
currentVehicle = vehicle;
break;
// North is ZERO so look for South going car
} else if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
currentVehicle = vehicle;
northLast = false;
break;
} else if (vehicle.direction == 0 && (!northLast || southSemaphore.availablePermits() == 10)) {
currentVehicle = vehicle;
northLast = true;
break;
}
}
if (currentVehicle == null) {
if (!queue.isEmpty()) {
System.out.println("Queue is NOT empty");
printQueue();
System.exit(0);
}
System.out.println("No vehicle to cross.");
return 0;
}
try {
bridge.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " could not cross.");
}
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " starts crossing the bridge.");
runningVehicle = new Thread(currentVehicle);
runningVehicle.start();
return currentVehicle.getCrossingTime();
}
public Semaphore getNorthSemaphore() {
return northSemaphore;
}
public Semaphore getSouthSemaphore() {
return southSemaphore;
}
public Semaphore getBridge() {
return bridge;
}
public Semaphore getEmergencySemaphore() {
return emergencySemaphore;
}
public void queueVehicle(Vehicle vehicle) {
this.queue.add(vehicle);
}
public void printQueue() {
System.out.println("Queue:");
for (Vehicle vehicle : queue) {
System.out.println(vehicle.getType() + ' ' + vehicle.id + " " + vehicle.direction);
}
}
}
+9 -10
View File
@@ -1,15 +1,14 @@
public class Truck extends Vehicle {
public static int crossingTime = 3;
public int crossingTime = 3;
public static int priority = 2;
public Truck() {}
public void run() {
try {
Thread.sleep(crossingTime);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Truck failed to cross.");
}
public Truck(int id) {
super(id);
}
String getType() {
return "Truck";
}
public int getCrossingTime() { return crossingTime; }
}
+74 -1
View File
@@ -1,4 +1,77 @@
import java.util.Random;
import java.util.concurrent.Semaphore;
public abstract class Vehicle implements Runnable {
public static int crossingTime;
public int crossingTime;
public static int priority;
int direction;
int id;
boolean hasRun = false;
public Vehicle(int id) {
Random rand = new Random();
this.direction = rand.nextInt(2);
this.id = id;
}
public void arrive(TrafficController trafficController) {
Semaphore queue;
if (this instanceof EmergencyVehicle) {
queue = trafficController.getEmergencySemaphore();
} else if (direction == 0) {
queue = trafficController.getNorthSemaphore();
} else {
queue = trafficController.getSouthSemaphore();
}
if (this.direction == 0)
System.out.println(this.getType() + " " + id + " approaches the bridge. Waiting (North): " + (10 - queue.availablePermits()));
else
System.out.println(this.getType() + " " + id + " approaches the bridge. Waiting (South): " + (10 - queue.availablePermits()));
if (queue.tryAcquire())
trafficController.queueVehicle(this);
else
System.out.println(this.getType() + " " + id + " saw too many cars and left.");
//
// try {
//
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// System.out.println(this.getType() + " " + id + " saw too many cars and left.");
// }
}
public void leave(TrafficController trafficController) {
Semaphore queue;
if (this instanceof EmergencyVehicle) {
queue = trafficController.getEmergencySemaphore();
} else if (direction == 0) {
queue = trafficController.getNorthSemaphore();
} else {
queue = trafficController.getSouthSemaphore();
}
queue.release();
}
@Override
public void run() {
try {
Thread.sleep(this.crossingTime);
System.out.println(this.getType() + " " + id + " crosses the bridge.");
hasRun = true;
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(this.getType() + " " + id + " couldn't cross for some reason.");
}
}
abstract String getType();
abstract int getCrossingTime();
}