From d2bed601d0d9bbb9e70478db171a8a0fd37aa25e Mon Sep 17 00:00:00 2001 From: thomascecil <thomascecil@thomass-air.ern.nps.edu> Date: Sun, 14 May 2023 21:11:31 -0700 Subject: [PATCH] HW 3 submit. --- .../homework3/Cecil/ArrivalProcess.java | 85 +++++++++++++++++++ .../Cecil/ExampleSimulationProgram.java | 30 ++++++- 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ArrivalProcess.java diff --git a/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ArrivalProcess.java b/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ArrivalProcess.java new file mode 100644 index 0000000000..97630234a6 --- /dev/null +++ b/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ArrivalProcess.java @@ -0,0 +1,85 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package MV3500Cohort2023MarchJune.homework3.Cecil; + +import simkit.SimEntityBase; +import simkit.random.RandomVariate; + +/** + * Specifies the cumulative number of arrivals with times between arrivals + * (deterministic or random). + * @author thomascecil + */ +public class ArrivalProcess extends SimEntityBase { + + private RandomVariate interarrivalTimeGenerator; + protected int numberArrivals; + + /** + * Constructor for the arrival process setting the interarrivalTimeGenerator. + * @param interarrivalTimeGenerator + */ + public ArrivalProcess(RandomVariate interarrivalTimeGenerator) { + this.setInterarrivalTimeGenerator(interarrivalTimeGenerator); + } + + /** + * Constructs the arrival process. + */ + public ArrivalProcess() { } + + /** + * Getter for the InterarrivalTimeGenerator. + * @return + */ + public RandomVariate getInterarrivalTimeGenerator() { + return interarrivalTimeGenerator; + } + + /** + * Setter for the interarrivalTimeGenerator. + * @param interarrivalTimeGenerator + */ + public void setInterarrivalTimeGenerator(RandomVariate interarrivalTimeGenerator) { + this.interarrivalTimeGenerator = interarrivalTimeGenerator; + } + + /** + * Getter for the number of arrivals. + * @return + */ + public int getNumberArrivals() { + return this.numberArrivals; + } + + /** + * Sets all state variables to their initial values. + */ + @Override + public void reset() { + super.reset(); + numberArrivals = 0; + } + + /** + * Fires a property change then schedules the first event, a run event. + */ + public void doRun() { + firePropertyChange("numberArrivals", getNumberArrivals()); + waitDelay("Arrival", interarrivalTimeGenerator); + } + + /** + * Saves the old value for numberArrivals and passes it to firePropertyChange + * and updates time-varying state variables. + */ + public void doArrival() { + int oldNumberArrivals = getNumberArrivals(); + numberArrivals = numberArrivals + 1; + firePropertyChange("numberArrivals", oldNumberArrivals, + getNumberArrivals()); + waitDelay("Arrival", interarrivalTimeGenerator); + } +} diff --git a/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ExampleSimulationProgram.java b/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ExampleSimulationProgram.java index 87bda3d015..1201f46aa3 100644 --- a/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ExampleSimulationProgram.java +++ b/assignments/src/MV3500Cohort2023MarchJune/homework3/Cecil/ExampleSimulationProgram.java @@ -16,6 +16,11 @@ import java.time.LocalDateTime; import java.util.logging.Level; import java.util.logging.Logger; +import simkit.Schedule; +import simkit.random.RandomVariate; +import simkit.random.RandomVariateFactory; +import simkit.util.SimplePropertyDumper; + /** The purpose of this inheritable class is to provide an easily modifiable * example simulation program that includes DIS-capable entities performing * tasks of interest, and then reporting activity via PDUs to the network. @@ -37,13 +42,13 @@ public class ExampleSimulationProgram protected PduFactory pduFactory; /** seconds per loop for real-time or simulation execution */ - private double simulationTimeStepDuration = 1.0; // seconds TODO encapsulate + private double simulationTimeStepDuration = 0.2; // seconds TODO encapsulate /** initial simulation time in seconds */ double simulationTimeInitial = 0.0; /** current simulation time in seconds */ double simulationTimeSeconds = simulationTimeInitial; /** Maximum number of simulation loops */ - int MAX_LOOP_COUNT = 4; + int MAX_LOOP_COUNT = 10; String narrativeMessage1 = new String(); String narrativeMessage2 = new String(); @@ -215,7 +220,26 @@ public class ExampleSimulationProgram // ============================================================================================= // * your own simulation code starts here! ***************************************************** // ============================================================================================= - + RandomVariate interarrivalTimeGenerator = RandomVariateFactory.getInstance( + "Exponential", 3.2); + ArrivalProcess arrivalProcess = new ArrivalProcess(interarrivalTimeGenerator); + + // Event listener for changes in numberArrivals + SimplePropertyDumper simplePropertyDumper = new SimplePropertyDumper(); + arrivalProcess.addPropertyChangeListener(simplePropertyDumper); + + System.out.println(arrivalProcess); + + Schedule.stopAtTime(15.0); + Schedule.setVerbose(true); + + // Initializes state variables and starts simulation + Schedule.reset(); + Schedule.startSimulation(); + + System.out.println("At time " + Schedule.getSimTime() + + " there have been " + arrivalProcess.getNumberArrivals() + + " arrivals"); // are there any other variables to modify at the beginning of your loop? // are your reading any DIS PDUs from the network? check for them here -- GitLab