-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabm-parameters-refactor.patch
More file actions
57 lines (52 loc) · 1.91 KB
/
Copy pathabm-parameters-refactor.patch
File metadata and controls
57 lines (52 loc) · 1.91 KB
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
diff --git a/R/abm.R b/R/abm.R
index abc1234..def5678 100644
--- a/R/abm.R
+++ b/R/abm.R
@@
AgentBasedModel <- R6::R6Class(
"AgentBasedModel",
private = list(
- .parameters = NULL
+ .parameters = NULL, # deprecated: prefer .parameters_instance
+ .parameters_instance = NULL
),
public = list(
initialize = function(parameters) {
- if (!inherits(parameters, "ModelParameters")) stop("parameters must be a ModelParameters instance")
- private$.parameters <- parameters$as_list()
+ if (!inherits(parameters, "ModelParameters")) {
+ stop("parameters must be a ModelParameters instance")
+ }
+ private$.parameters_instance <- parameters
+ private$.parameters <- parameters$as_list() # cached list for backward compatibility
# other initialization code...
},
get_parameters = function() {
- return (private$.parameters)
+ return (private$.parameters_instance)
},
# other methods...
)
)
diff --git a/tests/testthat/test-abm.R b/tests/testthat/test-abm.R
index 9876543..fedcba9 100644
--- a/tests/testthat/test-abm.R
+++ b/tests/testthat/test-abm.R
@@
test_that("AgentBasedModel parameters include adoption_rate", {
mps <- make_model_parameters(n_agents = 10, adoption_rate = 1.0, learning_strategy = contagion_learning_strategy)
abm <- make_abm(mps)
- all_params <- abm$get_parameters()
- expect_true("adoption_rate" %in% names(all_params))
+ all_params <- abm$get_parameters()$as_list()
+ expect_true("adoption_rate" %in% names(all_params))
})
+
+test_that("get_parameters returns ModelParameters instance", {
+ mps <- make_model_parameters(n_agents = 10, adoption_rate = 1.0, learning_strategy = contagion_learning_strategy)
+ abm <- make_abm(mps)
+ expect_true(inherits(abm$get_parameters(), "ModelParameters"))
+
+ param_list <- abm$get_parameters()$as_list()
+ expect_true("adoption_rate" %in% names(param_list))
+})