pq = pqueue()
pq$push(3,"Clear drains")
pq$push(4, "Feed cat")
pq$push(5, "Make tea")
pq$push(1, "Solve RC tasks")
pq$push(2, "Tax return")
while(!pq$empty())
print(pq$pop())
pq = new("PQueueRef")
pq$push(3,"Clear drains")
pq$push(4, "Feed cat")
pq$push(5, "Make tea")
pq$push(1, "Solve RC tasks")
pq$push(2, "Tax return")
while(!pq$empty())
print(pq$pop())
pq = new("EventQueue")
pq$push(3,"Clear drains")
pq$push(4, "Feed cat")
pq$push(5, "Make tea")
pq$push(1, "Solve RC tasks")
pq$push(2, "Tax return")
while(!pq$empty())
print(pq$pop())
DES = setRefClass("DES",
contains = "BaseDiscreteEventSimulation",
methods=list(
init=function() {
scheduleAt(3,"Clear drains")
scheduleAt(4, "Feed cat")
scheduleAt(5, "Make tea")
scheduleAt(1, "Solve RC tasks")
scheduleAt(2, "Tax return")
},
handleMessage=function(event) print(event)))
des = new("DES")
des$run()
if (FALSE) {
testRsimulation1 <- function() {
## A simple example
Simulation <-
setRefClass("Simulation",
contains = "BaseDiscreteEventSimulation")
Simulation$methods(
init = function() {
scheduleAt(rweibull(1,8,85), "Death due to other causes")
scheduleAt(rweibull(1,3,90), "Cancer diagnosis")
},
handleMessage = function(event) {
if (event %in% c("Death due to other causes", "Cancer death")) {
clear()
print(event)
}
else if (event == "Cancer diagnosis") {
if (runif(1) < 0.5)
scheduleAt(now() + rweibull(1,2,10), "Cancer death")
print(event)
}
})
Simulation$new()$run()
}
## An extension with individual life histories
testRsimulation2 <- function(n=100) {
Simulation <-
setRefClass("Simulation",
contains = "BaseDiscreteEventSimulation",
fields = list(state = "character", report = "data.frame"))
Simulation$methods(
init = function() {
report <<- data.frame()
state <<- "Healthy"
scheduleAt(rweibull(1,8,85), "Death due to other causes")
scheduleAt(rweibull(1,3,90), "Cancer diagnosis")
},
handleMessage = function(event) {
report <<- rbind(report, data.frame(state = state,
begin = attr(event,"sendingTime"),
end = currentTime,
event = event,
stringsAsFactors = FALSE))
if (event %in% c("Death due to other causes", "Cancer death")) {
clear()
}
else if (event == "Cancer diagnosis") {
state <<- "Cancer"
if (runif(1) < 0.5)
scheduleAt(now() + rweibull(1,2,10), "Cancer death")
}
},
final = function() report)
sim <- Simulation$new()
do.call("rbind", lapply(1:n, function(id) data.frame(id=id,sim$run())))
}
## reversible illness-death model
testRsimulation3 <- function(n=100) {
Simulation <-
setRefClass("Simulation",
contains = "BaseDiscreteEventSimulation",
fields = list(state = "character", everCancer = "logical",
report = "data.frame"))
Simulation$methods(
init = function() {
report <<- data.frame()
state <<- "Healthy"
everCancer <<- FALSE
scheduleAt(rweibull(1,8,85), "Death due to other causes")
scheduleAt(rweibull(1,3,90), "Cancer diagnosis")
},
handleMessage = function(event) {
report <<- rbind(report, data.frame(state = state,
everCancer = everCancer,
begin = attr(event,"sendingTime"),
end = currentTime,
event = event,
stringsAsFactors = FALSE))
if (event %in% c("Death due to other causes", "Cancer death")) {
clear()
}
else if (event == "Cancer diagnosis") {
state <<- "Cancer"
everCancer <<- TRUE
if (runif(1) < 0.5)
scheduleAt(now() + rweibull(1,2,10), "Cancer death")
scheduleAt(now() + 10, "Recovery")
}
else if (event == "Recovery") {
state <<- "Healthy"
scheduleAt(now() + rexp(1,10), "Cancer diagnosis")
}
},
final = function() report)
sim <- Simulation$new()
do.call("rbind", lapply(1:n, function(id) data.frame(id=id,sim$run())))
}
## cancer screening
testRsimulation4 <- function(n=1) {
Simulation <-
setRefClass("Simulation",
contains = "BaseDiscreteEventSimulation",
fields = list(state = "character", report = "data.frame"))
Simulation$methods(
init = function() {
report <<- data.frame()
state <<- "Healthy"
scheduleAt(rweibull(1,8,85), "Death due to other causes")
scheduleAt(rweibull(1,3,90), "Cancer onset")
scheduleAt(50,"Screening")
},
handleMessage = function(event) {
report <<- rbind(report, data.frame(state = state,
begin = attr(event,"sendingTime"),
end = currentTime,
event = event,
stringsAsFactors = FALSE))
if (event %in% c("Death due to other causes", "Cancer death")) {
clear()
}
else if (event == "Cancer onset") {
state <<- event
dx <- now() + rweibull(1,2,10)
scheduleAt(dx, "Clinical cancer diagnosis")
scheduleAt(dx + rweibull(1,1,10), "Cancer death")
scheduleAt(now() + rweibull(1,1,10), "Metastatic cancer")
}
else if (event == "Metastatic cancer") {
state <<- event
cancel(function(event) event %in%
c("Clinical cancer diagnosis","Cancer death")) # competing events
scheduleAt(now() + rweibull(1,2,5), "Cancer death")
}
else if (event == "Clinical cancer diagnosis") {
state <<- event
cancel(function(event) event == "Metastatic cancer")
}
else if (event == "Screening") {
switch(state,
"Cancer onset" = {
state <<- "Screen-detected cancer diagnosis"
cancel(function(event) event %in%
c("Clinical cancer diagnosis","Metastatic cancer"))
},
"Metastatic cancer" = {}, # ignore
"Clincal cancer diagnosis" = {}, # ignore
"Healthy" = {
if (now()<=68) scheduleAt(now()+2, "Screening")
})
}
else stop(event)
},
final = function() report)
sim <- Simulation$new()
do.call("rbind", lapply(1:n, function(id) data.frame(id=id,sim$run())))
}
## ticking bomb - toy example
testRsimulation5 <- function(n=1) {
Simulation <-
setRefClass("Simulation",
contains = "BaseDiscreteEventSimulation",
fields = list(report = "data.frame"))
Simulation$methods(
init = function() {
report <<- data.frame()
scheduleAt(rexp(1,1), "tick")
if (runif(1)<0.1)
scheduleAt(rexp(1,1), "explosion")
},
handleMessage = function(event) {
report <<- rbind(report, data.frame(begin = attr(event,"sendingTime"),
end = currentTime,
event = event,
stringsAsFactors = FALSE))
if (event == "explosion")
clear()
else {
clear() # queue
if (event == "tick") scheduleAt(currentTime+rexp(1,1), "tock")
else scheduleAt(currentTime+rexp(1,1), "tick")
if (runif(1)<0.1)
scheduleAt(currentTime+rexp(1,1), "explosion")
}
},
final = function() report)
sim <- Simulation$new()
do.call("rbind", lapply(1:n, function(id) data.frame(id=id,sim$run())))
}
}
Run the code above in your browser using DataLab