-
Notifications
You must be signed in to change notification settings - Fork 3
/
agents-methods.R
119 lines (100 loc) · 3.34 KB
/
agents-methods.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#' Agents in epiworldR
#'
#' These functions provide read-access to the agents of the model. The
#' `get_agents` function returns an object of class [epiworld_agents] which
#' contains all the information about the agents in the model. The
#' `get_agent` function returns the information of a single agent.
#' And the `get_state` function returns the state of a single agent.
#'
#' @param model An object of class [epiworld_model].
#' @param x An object of class [epiworld_agents].
#' @seealso agents
#' @export
#' @aliases epiworld_agents
#' @return
#' - The `get_agents` function returns an object of class [epiworld_agents].
#' @examples
#'
#' model_sirconn <- ModelSIRCONN(
#' name = "COVID-19",
#' n = 10000,
#' prevalence = 0.01,
#' contact_rate = 5,
#' transmission_rate = 0.4,
#' recovery_rate = 0.95
#' )
#'
#' run(model_sirconn, ndays = 100, seed = 1912)
#'
#' x <- get_agents(model_sirconn) # Storing all agent information into object of
#' # class epiworld_agents
#'
#' print(x, compressed = FALSE, max_print = 5) # Displaying detailed information of
#' # the first 5 agents using
#' # compressed=F. Using compressed=T
#' # results in less-detailed
#' # information about each agent.
#'
#' x[0] # Print information about the first agent. Substitute the agent of
#' # interest's position where '0' is.
get_agents <- function(model) {
res <- get_agents_cpp(model)
structure(
res,
class = "epiworld_agents",
model = model
)
}
#' @param x An object of class [epiworld_agents]
#' @param i Index (id) of the agent (from 0 to `n-1`)
#' @export
#' @rdname get_agents
#' @return
#' - The `[` method returns an object of class [epiworld_agent].
#' @aliases epiworld_agent
`[.epiworld_agents` <- function(x, i) {
structure(
get_agent_cpp(x, i),
class = "epiworld_agent",
model = attr(x, "model")
)
}
`[.epiworld_agents` <- function(x, i) {
structure(
get_agent_cpp(x, i),
class = "epiworld_agent",
model = attr(x, "model")
)
}
#' @export
#' @param compressed Logical scalar. When FALSE, it prints detailed information
#' about the agent.
#' @param ... Ignored
#' @returns
#' - The `print` function returns information about each individual agent of
#' class [epiworld_agent].
#' @rdname get_agents
print.epiworld_agent <- function(x, compressed = FALSE, ...) {
invisible(print_agent_cpp(x, attr(x, "model"), compressed))
}
#' @export
#' @param max_print Integer scalar. Maximum number of agents to print.
#' @rdname get_agents
print.epiworld_agents <- function(x, compressed = TRUE, max_print = 10, ...) {
model <- attr(x, "model")
cat(sprintf("Agents from the model \"%s\":\n", get_name(model)))
n <- size(model)
for (i in 1L:min(max_print, n)) {
print(x[i - 1L], compressed)
}
if (n > max_print)
cat(sprintf("... %i more agents ...\n", n - max_print))
invisible(x)
}
#' @export
#' @returns
#' - The `get_state` function returns the state of the [epiworld_agents] object.
#' @rdname get_agents
get_state <- function(x) {
get_state_agent_cpp(x)
}