package student.activities import ksl.modeling.entity.KSLProcess import ksl.modeling.entity.ProcessModel import ksl.modeling.entity.ResourceWithQ import ksl.modeling.spatial.DistancesModel import ksl.modeling.spatial.MovableResource import ksl.modeling.spatial.MovableResourcePoolWithQ import ksl.modeling.variable.Counter 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.ConstantRV import ksl.utilities.random.rvariable.ExponentialRV import ksl.utilities.random.rvariable.TriangularRV import ksl.utilities.random.rvariable.UniformRV /** * The production system with the distances between its stations, and carts * that carry the parts from preparation to the build lines and from the build * lines to packaging. The model, the distances, and the carts are given; the * activity writes the parts marked TODO. As given, the file compiles and runs, * and the parts move between the stations in no time. */ fun main() { val numCarts = 2 // the number of carts val m = Model("CartsModel") CartsBetweenStations(m, numCarts, name = "Production") m.numberOfReplications = 30 m.lengthOfReplication = 30.0 * 24.0 * 60.0 // 30 days, in minutes m.simulate() m.print() } class CartsBetweenStations( parent: ModelElement, private val numCarts: Int = 2, name: String? = null ) : ProcessModel(parent, name) { private val timeBetweenArrivalsRV = ExponentialRV(5.0, 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 = 2, name = "PrepWorkers") private val line1Workers = ResourceWithQ(this, capacity = 2, name = "Line1Workers") private val line2Workers = ResourceWithQ(this, capacity = 3, name = "Line2Workers") private val packWorkers = ResourceWithQ(this, capacity = 2, name = "PackWorkers") private val systemTime = Response(this, "System Time") private val partsCompleted = Counter(this, "Parts Completed") // the stations' locations, and the distances between them in feet private val dm = DistancesModel() private val prep = dm.Location("Prep") private val line1 = dm.Location("Line1") private val line2 = dm.Location("Line2") private val pack = dm.Location("Pack") private val cartSpeed = 150.0 // feet per minute private val loadTime = ConstantRV(0.5) // minutes to load a part on a cart private val unloadTime = ConstantRV(0.5) // minutes to unload it init { dm.addDistance(prep, line1, 100.0, symmetric = true) dm.addDistance(prep, line2, 150.0, symmetric = true) dm.addDistance(prep, pack, 200.0, symmetric = true) dm.addDistance(line1, line2, 60.0, symmetric = true) dm.addDistance(line1, pack, 120.0, symmetric = true) dm.addDistance(line2, pack, 80.0, symmetric = true) dm.defaultVelocity = ConstantRV(cartSpeed) spatialModel = dm } // the carts, which all start at preparation, and the pool that dispatches them private val carts = List(numCarts) { MovableResource(this, prep, ConstantRV(cartSpeed), "Cart${it + 1}") } private val cartPool = MovableResourcePoolWithQ(this, carts, ConstantRV(cartSpeed), name = "Carts") private val generator = EntityGenerator(::Part, timeBetweenArrivalsRV, timeBetweenArrivalsRV) private inner class Part : Entity() { val productionProcess: KSLProcess = process(isDefaultProcess = true) { currentLocation = prep use(prepWorkers, delayDuration = prepTimeRV) val line1Chosen = line1ChoiceRV.value == 1.0 val buildLine = if (line1Chosen) line1 else line2 // TODO: move the part to buildLine if (line1Chosen) { use(line1Workers, delayDuration = build1TimeRV) } else { use(line2Workers, delayDuration = build2TimeRV) } // TODO: move the part to pack val packer = seize(packWorkers) delay(wrapTimeRV) delay(fillTimeRV) release(packer) systemTime.value = time - createTime partsCompleted.increment() } } }