package student.activities import ksl.modeling.queue.Queue import ksl.modeling.variable.Counter import ksl.modeling.variable.RandomVariable import ksl.modeling.variable.Response import ksl.modeling.variable.TWResponse import ksl.observers.ControlVariateDataCollector import ksl.simulation.KSLEvent import ksl.simulation.Model import ksl.simulation.ModelElement import ksl.utilities.random.rvariable.BernoulliRV import ksl.utilities.random.rvariable.ExponentialRV import ksl.utilities.random.rvariable.TriangularRV /** * Control variates for the time in the system at the airport checkpoint. The * model is given; the activity writes the parts marked TODO. As given, the file * compiles and runs, and reports the usual half-width summary. */ fun main() { val numReps = 30 // 30 days val model = Model("AirportControlVariates") val airport = AirportCheckpoint(model, name = "Airport") model.numberOfReplications = numReps model.lengthOfReplication = 600.0 // 10 hours, in minutes // TODO 1: make a ControlVariateDataCollector for the model, add the airport's // systemTime as its response, and add the time between arrivals and the // inspection time as controls, each with its mean. model.simulate() model.print() // TODO 2: get the collector's data for the response, compute the regression // results, and print them. } /** * The identification inspection of the event modeling activities, with one * inspector. Its random variables and its time in the system are public, so * that a collector can observe them. */ class AirportCheckpoint( parent: ModelElement, private val numInspectors: Int = 1, name: String? = null ) : ModelElement(parent, name) { // the random variables val timeBetweenArrivalsRV = RandomVariable(this, ExponentialRV(2.0, 1), "TimeBetweenArrivals") val inspectionTimeRV = RandomVariable(this, TriangularRV(0.75, 1.5, 3.0, 2), "InspectionTime") val passRV = RandomVariable(this, BernoulliRV(0.93, 3), "PassInspection") // the responses private val myNumBusy = TWResponse(this, "NumBusy") private val myInspectionQ: Queue = Queue(this, "InspectionQ") private val myNumCleared = Counter(this, "Num Cleared") private val myNumDenied = Counter(this, "Num Denied") val systemTime = Response(this, "System Time") // the events override fun initialize() { schedule(this::arrival, timeBetweenArrivalsRV) } private fun arrival(event: KSLEvent) { myInspectionQ.enqueue(QObject()) if (myNumBusy.value < numInspectors) { myNumBusy.increment() schedule(this::endOfInspection, inspectionTimeRV, myInspectionQ.removeNext()) } schedule(this::arrival, timeBetweenArrivalsRV) } private fun endOfInspection(event: KSLEvent) { myNumBusy.decrement() if (myInspectionQ.isNotEmpty) { myNumBusy.increment() schedule(this::endOfInspection, inspectionTimeRV, myInspectionQ.removeNext()) } val passenger = event.message!! if (passRV.value == 1.0) { myNumCleared.increment() } else { myNumDenied.increment() } systemTime.value = time - passenger.createTime } }