draft
This commit is contained in:
		
							parent
							
								
									a86f928f36
								
							
						
					
					
						commit
						9753a8aaca
					
				
					 5 changed files with 54 additions and 24 deletions
				
			
		|  | @ -8,8 +8,8 @@ class Event implements Comparable<Event> { | |||
| 	// 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; | ||||
|  |  | |||
|  | @ -13,8 +13,8 @@ public class EventList { | |||
| 
 | ||||
| 	// This is a helper function to get the correct queue | ||||
| 	private PriorityQueue<Event> 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 | ||||
| 		}; | ||||
| 	} | ||||
|  |  | |||
|  | @ -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(); | ||||
| } | ||||
|  | @ -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); | ||||
| 	} | ||||
| } | ||||
|  | @ -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)); | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus