From 9753a8aacab0a37f5321991bafa569b4319ace02 Mon Sep 17 00:00:00 2001 From: Imbus Date: Fri, 29 Sep 2023 09:09:11 +0200 Subject: [PATCH] draft --- app/src/main/java/simulation/Event.java | 4 +- app/src/main/java/simulation/EventList.java | 4 +- .../java/simulation/GlobalSimulation.java | 3 +- .../main/java/simulation/MainSimulation.java | 9 +-- app/src/main/java/simulation/State.java | 58 ++++++++++++++----- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/simulation/Event.java b/app/src/main/java/simulation/Event.java index e5ed402..bf8db4c 100644 --- a/app/src/main/java/simulation/Event.java +++ b/app/src/main/java/simulation/Event.java @@ -8,8 +8,8 @@ class Event implements Comparable { // 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(int type, double eventTimeStamp) throws IllegalArgumentException { - if (type > 3 || type < 1) - throw new IllegalArgumentException("Event type must be one of ARRIVAL, READY or MEASURE"); + // if (type > 3 || type < 1) + // throw new IllegalArgumentException("Event type must be one of ARRIVAL, READY or MEASURE"); eventType = type; eventTime = eventTimeStamp; diff --git a/app/src/main/java/simulation/EventList.java b/app/src/main/java/simulation/EventList.java index 1836cc4..c42c340 100644 --- a/app/src/main/java/simulation/EventList.java +++ b/app/src/main/java/simulation/EventList.java @@ -13,8 +13,8 @@ public class EventList { // This is a helper function to get the correct queue private PriorityQueue getQueue() { - return switch(high_pq.size()) { - case 0 -> pq; // If no priority events, return normal queue + return switch (high_pq.size()) { + case 0 -> pq; // If no priority events, return normal queue default -> high_pq; // If priority events, return priority queue }; } diff --git a/app/src/main/java/simulation/GlobalSimulation.java b/app/src/main/java/simulation/GlobalSimulation.java index 6d12e27..0f29b31 100644 --- a/app/src/main/java/simulation/GlobalSimulation.java +++ b/app/src/main/java/simulation/GlobalSimulation.java @@ -4,7 +4,8 @@ import java.util.Random; // This class is only created to be able to store the global time. 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 Random rand = new Random(); } \ No newline at end of file diff --git a/app/src/main/java/simulation/MainSimulation.java b/app/src/main/java/simulation/MainSimulation.java index 717d604..d07dc32 100644 --- a/app/src/main/java/simulation/MainSimulation.java +++ b/app/src/main/java/simulation/MainSimulation.java @@ -9,8 +9,8 @@ public class MainSimulation extends GlobalSimulation { Event actEvent; State actState = new State(myEventList); - myEventList.insertEvent(new Event(ARRIVAL, 0)); - myEventList.insertEvent(new Event(MEASURE, 5)); + myEventList.insertEvent(new Event(NEW_ARRIVAL, 0)); + myEventList.insertEvent(new Event(MEASURE, 100)); while (MainSimulation.time < 200000) { actEvent = myEventList.popEvent(); @@ -18,7 +18,8 @@ public class MainSimulation extends GlobalSimulation { actState.TreatEvent(actEvent); } - System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements); - System.out.println("Number of measurements done: " + actState.noMeasurements); + actState.summary(); + // System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements); + // System.out.println("Number of measurements done: " + actState.noMeasurements); } } \ No newline at end of file diff --git a/app/src/main/java/simulation/State.java b/app/src/main/java/simulation/State.java index 08f2488..d81cb51 100644 --- a/app/src/main/java/simulation/State.java +++ b/app/src/main/java/simulation/State.java @@ -1,7 +1,7 @@ package simulation; class State extends GlobalSimulation { - public int numberInQueue = 0, accumulated = 0, noMeasurements = 0; + // public int numberInQueue = 0, accumulated = 0, noMeasurements = 0; private EventList eventList = null; State(EventList eventList) { @@ -11,35 +11,63 @@ class State extends GlobalSimulation { // Dispatches the events to the right handler with a java 20 switch statement public void TreatEvent(Event event) { switch (event.getEventType()) { - case ARRIVAL -> arrival(); - case READY -> ready(); + // case ARRIVAL -> arrival(); + // case SERVE -> serve(); 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", // 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 private static double generateMean(double mean) { return 2 * mean * rand.nextDouble(); } - private void arrival() { - if (numberInQueue == 0) - eventList.insertEvent(new Event(READY, time + generateMean(1))); - eventList.insertEvent(new Event(ARRIVAL, time + generateMean(2))); - numberInQueue++; + // private void arrival() { + // if (numberInQueue == 0) + // eventList.insertEvent(new Event(SERVE, time + generateMean(1))); + // eventList.insertEvent(new Event(ARRIVAL, time + generateMean(2))); + // 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() { - numberInQueue--; - if (numberInQueue > 0) - eventList.insertEvent(new Event(READY, time + generateMean(1))); + private void handle_A() { + queue_a--; + queue_b++; + 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() { - accumulated = accumulated + numberInQueue; - noMeasurements++; - eventList.insertEvent(new Event(MEASURE, time + generateMean(5))); + accum_a += queue_a; + accum_b += queue_b; + measure_amount++; + eventList.insertEvent(new Event(MEASURE, time + 100)); } } \ No newline at end of file