3.6 A Simple Inspection Process
The following example illustrates how to model a simple inspection process that has individual items being defective and includes the random selection of items for inspection.
Example 3.11 (Simple Inspection Process) A manufacturing process has a simple inspection process. The process produces items and places them in boxes with four items per box. The manufacturing process has a defect rate of 15%. That is, the probability of an individual item being defective is 0.15. Assume that the chance of an item being defective is independent of any other item being defective. An inspector tests each box by randomly sampling one item from the box. If the selected item is good, the box is passed; if the selected item is defective, the box is rejected. We are interested in estimating the expected number of boxes that the inspector will look at before the first box is rejected.
This situation could be modeled using the concepts of probability theory; however, we will simulate the process to illustrate the use of KSL constructs. The probability that an item is defective can be modeled with a Bernoulli random variable with probability of success equal 0.15. In the following code, we model the inspection process with a function that counts the number of boxes that are inspected until the first rejected box.
fun main() {
val itemRV = BernoulliRV(probOfSuccess = 0.15)
val itemsPerBox = 4
val stat = Statistic("Num Until Rejection")
val sampleSize = 100
for (i in 1..sampleSize) {
val countBoxes = numUntilFirstRejection(itemRV, itemsPerBox)
stat.collect(countBoxes)
}
print(String.format("%s \t %f %n", "Count = ", stat.count))
print(String.format("%s \t %f %n", "Average = ", stat.average))
print(String.format("%s \t %f %n", "Std. Dev. = ", stat.standardDeviation))
print(String.format("%s \t %f %n", "Half-width = ", stat.halfWidth))
println((stat.confidenceLevel * 100).toString() + "% CI = " + stat.confidenceInterval)
}
/**
* This function counts the number of boxes inspected until the first
* box is found that has a randomly selected item that is defective.
*/
fun numUntilFirstRejection(itemRV: BernoulliRV, itemsPerBox: Int): Double {
require(itemsPerBox >= 1) { "There must be at least 1 item per box" }
var count = 0.0
do {
count++
// randomly generate the box
val box = itemRV.sample(itemsPerBox)
// randomly sample from the box
val inspection = KSLRandom.randomlySelect(box)
// stops if bad = 1.0 is found, continues if good = 0.0 is found
} while (inspection != 1.0)
return count
}
Notice that first a sample from the Bernoulli random variable is obtained. The size of this sample is the number of items per box (equal to 4 in this example). The returned sample array contains elements with values 1.0 or 0.0, with 1.0 indicating a defective item. Then, the KSLRandom
functionality is used to randomly select from the sample “box” an element (item). If the item is good, the sampling continues with a new box. If the selected item is bad, then the sampling stops and returns the number of sample boxes inspected until the first bad box is found.
Count = 100.000000
Average = 5.710000
Std. Dev. = 4.688897
Half-width = 0.930379
95.0% CI = [4.779621063279056, 6.640378936720944]
The results indicate that on average about 5.71 boxes are inspected until the first box is rejected.