Here is a problem I am curious about.
Suppose there is a machine with 5 engines – all engines are running simultaneously. At time=0, there is a 0.5 probability of each engine failing. At time=1, there is a 0.51 probability of each engine failing, etc. The first time an engine fails, it permanently fails. This continues until all 5 engines have failed.
I wrote R code to represent this scenario:
engines <- paste0("engine_", 1:5)
prob_working <- 0.5
df <- data.frame(matrix(ncol = 11, nrow = 0))
colnames(df) <- c("turn", paste0("engine_", 1:5), paste0("engine_", 1:5, "_prob"))
max_turns <- 0
for (engine in engines) {
turn <- 1
while(TRUE) {
result <- ifelse(runif(1) < prob_working, "working", "failure")
df[turn, "turn"] <- turn
df[turn, engine] <- result
df[turn, paste0(engine, "_prob")] <- prob_working
if (result == "failure") {
break
}
prob_working <- prob_working - 0.01
turn <- turn + 1
}
max_turns <- max(max_turns, turn)
prob_working <- 0.5
}
df <- df[1:max_turns, ]
Here is an output of this simulation
turn engine_1 engine_2 engine_3 engine_4 engine_5 engine_1_prob engine_2_prob engine_3_prob engine_4_prob engine_5_prob
1 1 failure failure failure failure working 0.5 0.5 0.5 0.5 0.50
2 2 <NA> <NA> <NA> <NA> working NA NA NA NA 0.49
3 3 <NA> <NA> <NA> <NA> working NA NA NA NA 0.48
4 4 <NA> <NA> <NA> <NA> working NA NA NA NA 0.47
5 5 <NA> <NA> <NA> <NA> failure NA NA NA NA 0.46
Now, suppose there is a single mechanic. When the first engine fails, the mechanic starts trying to repair this engine. When the mechanic receives the first failed engine, there is a 0.5 probability that the mechanic repairs it right away. In the next time , there is a 0.51 probability that the mechanic repairs, 0.52 in the next time unit. When the mechanic is done repairing the engine and it goes back into the machine, this engine has a 0.5 probability of failing while all other active engines keep their existing failure probabilities.
However, the remaining engines now fail at increased rate. For example, when there are 4 engines, the probability of each remaining engine failing increases by 0.02 each time unit. When there are 3 engines, the probability of each remaining engine failing increases by 0.03 each time unit. When there are 4 engines, the probability of each remaining engine failing increases by 0.04 each time unit. Anytime an engine returns back to the machine, the failure rate of the remaining engine decreases (e.g. if there are 3 engines with an increase in probability of 0.03 per time unit, once another engine comes back and there are 4 engines – the probability increase goes down to 0.02 per time unit).
The mechanic can only work on one engine at a time. If the mechanic is working on an engine and another engine arrives, the mechanic will continue to work on the existing engine and then move to the next one.
These kinds of problems have always intimidated me from a coding perspective. How do I begin working on these? I know that I probably need to use something like a stack/queue to keep track of the order of which engines failed, the priority in which they will be repaired, the vector of failure probabilities, the number of active engines, etc.
Can someone guide me on how to work on these problems?