package student.activities import ksl.modeling.entity.KSLProcess import ksl.modeling.entity.ProcessModel import ksl.modeling.entity.ResourceWithQ import ksl.modeling.variable.Counter import ksl.modeling.variable.IndicatorResponse import ksl.modeling.variable.RandomVariable import ksl.modeling.variable.Response 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 import ksl.utilities.random.rvariable.UniformRV /** * Staffing the production system with queueing theory: predict each station's * utilization and wait with the KSL's GGcWhittApproximation, choose a staffing * plan, and check the plan by simulation. The model is the production system * of the process view activities, without the palletizer. The activity writes * the parts marked TODO. */ fun main() { // TODO 2: set the demand and the staffing plan for each run val mtba = 5.0 // mean minutes between arrivals val staffing = Staffing(prep = 2, line1 = 2, line2 = 3, pack = 2) val simulateToo = true // false to print only the predictions predict(mtba, staffing) if (simulateToo) { simulate(mtba, staffing) } } /** The number of workers at each of the four stations. */ data class Staffing(val prep: Int, val line1: Int, val line2: Int, val pack: Int) { val total: Int get() = prep + line1 + line2 + pack } /** * TODO 1: Return the expected wait in queue at a station with [servers] * workers, using the KSL's GGcWhittApproximation, or infinity if the station's * utilization is 1 or more. */ fun stationWait(mtba: Double, meanST: Double, servers: Int, arrivalSCV: Double, serviceSCV: Double): Double { return Double.NaN } /** Prints each station's predicted utilization and wait, and the predicted time in the system. */ fun predict(mtba: Double, s: Staffing) { val pLine1 = 0.3 // means and squared coefficients of variation of the service times val prepST = 8.0 val prepSCV = 1.0 // exponential val buildST = (2.0 + 6.0 + 8.0) / 3.0 // both lines: 5.33 minutes val line1SCV = (4.0 + 36.0 + 64.0 - 12.0 - 16.0 - 48.0) / 18.0 / (buildST * buildST) val line2SCV = (9.0 + 36.0 + 49.0 - 18.0 - 21.0 - 42.0) / 18.0 / (buildST * buildST) val packST = 2.0 + 1.5 // wrapping and filling val packSCV = (4.0 + 1.0 / 12.0) / (packST * packST) // each station's arrivals are assumed to be Poisson: squared coefficient of variation 1 data class Row(val name: String, val c: Int, val mtba: Double, val st: Double, val scv: Double, val share: Double) val rows = listOf( Row("Preparation", s.prep, mtba, prepST, prepSCV, 1.0), Row("Line 1", s.line1, mtba / pLine1, buildST, line1SCV, pLine1), Row("Line 2", s.line2, mtba / (1.0 - pLine1), buildST, line2SCV, 1.0 - pLine1), Row("Packaging", s.pack, mtba, packST, packSCV, 1.0) ) println("Mean time between arrivals = $mtba minutes; ${s.total} workers") println("Station c rho Wq (min)") var time = 0.0 for (r in rows) { val rho = r.st / (r.mtba * r.c) val wq = stationWait(r.mtba, r.st, r.c, 1.0, r.scv) println("%-12s %3d %6.3f %9.3f".format(r.name, r.c, rho, wq)) time += r.share * (wq + r.st) } println("Predicted time in the system = %.2f minutes".format(time)) println() } /** Simulates 100 replications of 30 days with the given demand and staffing. */ fun simulate(mtba: Double, s: Staffing) { val m = Model("StaffingModel", autoCSVReports = false) StaffedProductionSystem(m, mtba, s, name = "Production") m.numberOfReplications = 100 m.lengthOfReplication = 30.0 * 24.0 * 60.0 // 30 days, in minutes m.simulate() val keep = listOf("System Time", "System Time < 60", "Parts Completed") + listOf("Prep", "Line1", "Line2", "Pack").flatMap { listOf("${it}Workers:InstantaneousUtil", "${it}Workers:Q:TimeInQ") } println("Simulation, 100 replications of 30 days") for (name in keep) { val stat = m.simulationReporter.acrossReplicationStatisticsList().first { it.name == name } println("%-28s %10.4f +/- %8.4f".format(name, stat.average, stat.halfWidth)) } } /** The production system of the process view activities, with the demand and staffing as parameters. */ class StaffedProductionSystem( parent: ModelElement, mtba: Double, staffing: Staffing, name: String? = null ) : ProcessModel(parent, name) { private val timeBetweenArrivalsRV = ExponentialRV(mtba, 1) private val prepTimeRV = RandomVariable(this, ExponentialRV(8.0, 2)) private val line1ChoiceRV = RandomVariable(this, BernoulliRV(0.3, 3)) private val build1TimeRV = RandomVariable(this, TriangularRV(2.0, 6.0, 8.0, 4)) private val build2TimeRV = RandomVariable(this, TriangularRV(3.0, 6.0, 7.0, 5)) private val wrapTimeRV = RandomVariable(this, ExponentialRV(2.0, 6)) private val fillTimeRV = RandomVariable(this, UniformRV(1.0, 2.0, 7)) private val prepWorkers = ResourceWithQ(this, capacity = staffing.prep, name = "PrepWorkers") private val line1Workers = ResourceWithQ(this, capacity = staffing.line1, name = "Line1Workers") private val line2Workers = ResourceWithQ(this, capacity = staffing.line2, name = "Line2Workers") private val packWorkers = ResourceWithQ(this, capacity = staffing.pack, name = "PackWorkers") private val systemTime = Response(this, "System Time") private val sysTimeLT60 = IndicatorResponse({ x -> x < 60.0 }, systemTime, "System Time < 60") private val partsCompleted = Counter(this, "Parts Completed") private val generator = EntityGenerator(::Part, timeBetweenArrivalsRV, timeBetweenArrivalsRV) private inner class Part : Entity() { val productionProcess: KSLProcess = process(isDefaultProcess = true) { use(prepWorkers, delayDuration = prepTimeRV) if (line1ChoiceRV.value == 1.0) { use(line1Workers, delayDuration = build1TimeRV) } else { use(line2Workers, delayDuration = build2TimeRV) } val packer = seize(packWorkers) delay(wrapTimeRV) delay(fillTimeRV) release(packer) systemTime.value = time - createTime partsCompleted.increment() } } }