This commit is contained in:
Imbus 2023-09-29 09:09:11 +02:00
parent a86f928f36
commit 9753a8aaca
5 changed files with 54 additions and 24 deletions

View file

@ -8,8 +8,8 @@ class Event implements Comparable<Event> {
// Constructor, creates an event of a certain type that should be put in the // Constructor, creates an event of a certain type that should be put in the
// event list at a certain time. Throws if type is out of bounds. // event list at a certain time. Throws if type is out of bounds.
Event(int type, double eventTimeStamp) throws IllegalArgumentException { Event(int type, double eventTimeStamp) throws IllegalArgumentException {
if (type > 3 || type < 1) // if (type > 3 || type < 1)
throw new IllegalArgumentException("Event type must be one of ARRIVAL, READY or MEASURE"); // throw new IllegalArgumentException("Event type must be one of ARRIVAL, READY or MEASURE");
eventType = type; eventType = type;
eventTime = eventTimeStamp; eventTime = eventTimeStamp;

View file

@ -13,8 +13,8 @@ public class EventList {
// This is a helper function to get the correct queue // This is a helper function to get the correct queue
private PriorityQueue<Event> getQueue() { private PriorityQueue<Event> getQueue() {
return switch(high_pq.size()) { return switch (high_pq.size()) {
case 0 -> pq; // If no priority events, return normal queue case 0 -> pq; // If no priority events, return normal queue
default -> high_pq; // If priority events, return priority queue default -> high_pq; // If priority events, return priority queue
}; };
} }

View file

@ -4,7 +4,8 @@ import java.util.Random;
// This class is only created to be able to store the global time. // This class is only created to be able to store the global time.
public class GlobalSimulation { public class GlobalSimulation {
public static final int ARRIVAL = 1, READY = 2, MEASURE = 3; public static final int ARRIVAL = 1, SERVE = 2, MEASURE = 3;
public static final int JOB_A = 11, JOB_B = 12, NEW_ARRIVAL = 13;
public static double time = 0; public static double time = 0;
public static Random rand = new Random(); public static Random rand = new Random();
} }

View file

@ -9,8 +9,8 @@ public class MainSimulation extends GlobalSimulation {
Event actEvent; Event actEvent;
State actState = new State(myEventList); State actState = new State(myEventList);
myEventList.insertEvent(new Event(ARRIVAL, 0)); myEventList.insertEvent(new Event(NEW_ARRIVAL, 0));
myEventList.insertEvent(new Event(MEASURE, 5)); myEventList.insertEvent(new Event(MEASURE, 100));
while (MainSimulation.time < 200000) { while (MainSimulation.time < 200000) {
actEvent = myEventList.popEvent(); actEvent = myEventList.popEvent();
@ -18,7 +18,8 @@ public class MainSimulation extends GlobalSimulation {
actState.TreatEvent(actEvent); actState.TreatEvent(actEvent);
} }
System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements); actState.summary();
System.out.println("Number of measurements done: " + actState.noMeasurements); // System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements);
// System.out.println("Number of measurements done: " + actState.noMeasurements);
} }
} }

View file

@ -1,7 +1,7 @@
package simulation; package simulation;
class State extends GlobalSimulation { class State extends GlobalSimulation {
public int numberInQueue = 0, accumulated = 0, noMeasurements = 0; // public int numberInQueue = 0, accumulated = 0, noMeasurements = 0;
private EventList eventList = null; private EventList eventList = null;
State(EventList eventList) { State(EventList eventList) {
@ -11,35 +11,63 @@ class State extends GlobalSimulation {
// Dispatches the events to the right handler with a java 20 switch statement // Dispatches the events to the right handler with a java 20 switch statement
public void TreatEvent(Event event) { public void TreatEvent(Event event) {
switch (event.getEventType()) { switch (event.getEventType()) {
case ARRIVAL -> arrival(); // case ARRIVAL -> arrival();
case READY -> ready(); // case SERVE -> serve();
case MEASURE -> measure(); case MEASURE -> measure();
case NEW_ARRIVAL -> arrival_A();
case JOB_A -> handle_A();
case JOB_B -> handle_B();
} }
// System.out.println(String.format("Event type: %d, Customers: %d, Time: %.1f", // System.out.println(String.format("Event type: %d, Customers: %d, Time: %.1f",
// event.getEventType(), numberInQueue, event.getEventTime())); // event.getEventType(), numberInQueue, event.getEventTime()));
} }
private int queue_a, queue_b, measure_amount, accum_a, accum_b;
public void summary() {
System.out.println(String.format("Mean number of customers in queue A: %.2f", (double) accum_a / measure_amount));
System.out.println(String.format("Mean number of customers in queue B: %.2f", (double) accum_b / measure_amount));
System.out.println(String.format("Total amount of measurements: %d", measure_amount));
}
// Helper to generate a random number with certain statistical properties // Helper to generate a random number with certain statistical properties
private static double generateMean(double mean) { private static double generateMean(double mean) {
return 2 * mean * rand.nextDouble(); return 2 * mean * rand.nextDouble();
} }
private void arrival() { // private void arrival() {
if (numberInQueue == 0) // if (numberInQueue == 0)
eventList.insertEvent(new Event(READY, time + generateMean(1))); // eventList.insertEvent(new Event(SERVE, time + generateMean(1)));
eventList.insertEvent(new Event(ARRIVAL, time + generateMean(2))); // eventList.insertEvent(new Event(ARRIVAL, time + generateMean(2)));
numberInQueue++; // numberInQueue++;
// }
private void arrival_A() {
queue_a++;
eventList.insertEvent(new Event(NEW_ARRIVAL, time + 1000.0/60.0));
eventList.insertEvent(new Event(JOB_A, time + 2));
} }
private void ready() { private void handle_A() {
numberInQueue--; queue_a--;
if (numberInQueue > 0) queue_b++;
eventList.insertEvent(new Event(READY, time + generateMean(1))); eventList.insertPriorityEvent(new Event(JOB_B, time + 4));
} }
private void handle_B() {
queue_b--;
}
// private void serve() {
// // numberInQueue--;
// // if (numberInQueue > 0)
// // eventList.insertEvent(new Event(SERVE, time + generateMean(1)));
// }
private void measure() { private void measure() {
accumulated = accumulated + numberInQueue; accum_a += queue_a;
noMeasurements++; accum_b += queue_b;
eventList.insertEvent(new Event(MEASURE, time + generateMean(5))); measure_amount++;
eventList.insertEvent(new Event(MEASURE, time + 100));
} }
} }