Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross village interaction #97

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v1

Expand All @@ -37,11 +37,11 @@ jobs:
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v1
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: rcmdcheck

- uses: r-lib/actions/check-r-package@v1
- uses: r-lib/actions/check-r-package@v2

- name: Show testthat output
if: always()
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ docs/

# translation temp files
po/*~

results/*
tests/testthat/test_results/
tests/testthat/results*
vignettes/results/*
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: villager
Title: A Framework for Designing and Running Agent Based Models
Version: 1.2.2
Version: 2.0.0
Authors@R: c(
person(
given = "Thomas",
family = "Thelen",
role = c("aut", "cre"),
email = "[email protected]"
email = "[email protected]"
),
person(
given = "Gerardo",
Expand All @@ -31,7 +31,7 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: false
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.3.1
Depends: R (>= 3.5.0)
Imports:
readr,
Expand All @@ -46,7 +46,8 @@ Suggests:
remotes,
rmarkdown,
testthat,
roxygen2,
pandoc,
URL: https://github.com/zizroc/villager/
BugReports: https://github.com/zizroc/villager/issues/
VignetteBuilder: knitr

1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(resource)
export(resource_manager)
export(simulation)
export(village)
export(village_manager)
export(village_state)
importFrom(R6,R6Class)
importFrom(readr,write_csv)
Expand Down
10 changes: 5 additions & 5 deletions R/simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
simulation <- R6::R6Class("simulation",
public = list(
length = NA,
villages = NA,
village_mgr = NA,
writer = NA,

#' Creates a new Simulation instance
Expand All @@ -24,7 +24,7 @@ simulation <- R6::R6Class("simulation",
initialize = function(length,
villages,
writer = villager::data_writer$new()) {
self$villages <- villages
self$village_mgr <- village_manager$new(villages)
self$length <- length
self$writer <- writer
},
Expand All @@ -33,16 +33,16 @@ simulation <- R6::R6Class("simulation",
#'
#' @return None
run_model = function() {
for (village in self$villages) {
for (village in self$village_mgr$get_villages()) {
village$set_initial_state()
}
# Loop over each village and run the user defined initial condition function. Index off of 1 because the
# initial condition is set at 0
current_step <- 1
while (current_step <= self$length) {
# Iterate the villages a single time step
for (village in self$villages) {
village$propagate(current_step)
for (village in self$village_mgr$get_villages()) {
village$propagate(current_step, self$village_mgr)
self$writer$write(village$current_state, village$name)
}
current_step <- current_step + 1
Expand Down
8 changes: 5 additions & 3 deletions R/village.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' }
village <- R6::R6Class("village",
public = list(
#' @field name Unique identifier for the village
identifier = NA,
#' @field name An optional name for the village
name = NA,
#' @field initial_condition A function that sets the initial state of the village
Expand Down Expand Up @@ -47,7 +49,7 @@ village <- R6::R6Class("village",
self$initial_condition <- initial_condition
self$agent_mgr <- agent_manager$new(agent_class)
self$resource_mgr <- resource_manager$new(resource_class)

self$identifier <- uuid::UUIDgenerate()
# Check to see if the user supplied a single model, outside of a list
# If so, put it in a vector because other code expects 'models' to be a list
if (!is.list(models) && !is.null(models)) {
Expand All @@ -69,7 +71,7 @@ village <- R6::R6Class("village",
#' to set initial conditions. See the set_initial_state method.
#' @param current_step The current time step
#' @return None
propagate = function(current_step) {
propagate = function(current_step, village_mgr) {
# Create a new state representing this slice in time. Since many of the
# values will be the same as the previous state, clone the previous state
self$current_state <- self$previous_state$clone(deep = TRUE)
Expand All @@ -79,7 +81,7 @@ village <- R6::R6Class("village",
for (model in self$models) {
# Create a read only copy of the last state so that users can make decisions off of it
self$previous_state <- self$current_state$clone(deep = TRUE)
model(self$current_state, self$previous_state, self$model_data, self$agent_mgr, self$resource_mgr
model(self$current_state, self$previous_state, self$model_data, self$agent_mgr, self$resource_mgr, village_mgr
)
}
self$current_state$agent_states <- self$agent_mgr$get_states()
Expand Down
52 changes: 52 additions & 0 deletions R/village_manager.R
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, though haven't tested it.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' @export
#' @title Village Manager
#' @docType class
#' @description This object manages all of the villages. It acts as an interface to them
#' @section Methods:
#' \describe{
#' \item{\code{initialize()}}{Creates a new manager}
#' \item{\code{get_villages()}}{Gets all of the villages that the manager has}
#' \item{\code{get_village()}}{Retrieves a specific village from the manager, by name}
#' \item{\code{add_village()}}{Adds a village to the manager}
#' }
village_manager <- R6::R6Class(
"village_manager",
public = list(
#' @field villages A list of village objects
villages = NULL,
#' Creates a new, village manager
#' @description Get a new instance of a village_manager
initialize = function(villages) {
self$villages <- villages
},

#' Gets all of the managed villages
#'
#' @return A list of resources
get_villages = function() {
return(self$villages)
},

#' Gets a village given a village name
#'
#' @param name The name of the requested village
#' @return A village object
get_village = function(name) {
for (village in self$villages) {
if (village$name == name) {
return(village)
}
}
},

#' Adds a village to the manager.
#'
#' @param ... The villages to add
#' @return None
add_resource = function(...) {
for (new_village in list(...)) {
self$villages <- append(self$villages, new_village)
}
}
)
)
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test_model <- function(current_state, previous_state, model_data, agent_mgr, res
Agents are created by instantiating the `agent` class. There are a number of agent properties that can be passed to the constructor.

```{r}
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr) {
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr, village_mgr) {
mother <- agent$new(first_name="Kirsten", last_name="Taylor", age=9125)
father <- agent$new(first_name="Joshua", last_name="Thompson", age=7300)
daughter <- agent$new(first_name="Mariylyyn", last_name="Thompson", age=10220)
Expand All @@ -56,7 +56,7 @@ test_model <- function(current_state, previous_state, model_data, agent_mgr, res
To add agents to the simulation, use the provided `agent_mgr` object to call `add_agent`. Because the classes are R6, the object can be modified after being added to the manager and the changes will be persisted without needing to re-add the villager. For example, setting a daughter's mother and her father below. Note that the standard way is to modify the properties _beforehand_, although not strictly necessary.

```{r}
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr) {
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr, village_mgr) {
agent_mgr <- agent_manager$new()
agent_mgr$add_agent(mother, father, daughter)
daughter$mother_id <- mother$identifier
Expand All @@ -74,7 +74,7 @@ agent_mgr$agent_mgr$connect_agents(mother, father)
Resources are similar to agents in that they're both R6 classes, are instantiated similarly, and are also managed by an object passed into the model. An example of creating resources and adding them to the simulation is given below.

```
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr) {
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr, village_mgr) {
corn_resource <- resource$new(name="corn", quantity = 10)
fish_resource <- resource$new(name="fish", quantity = 15)
corn_resource$quantity=5
Expand Down Expand Up @@ -161,7 +161,7 @@ initial_condition <- function(current_state, model_data, agent_mgr, resource_mgr
resource_mgr$add_resource(corn_resource, fish_resource)
}

test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr) {
test_model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr, village_mgr) {
print(paste("Step:", current_state$step))
for (agent in agent_mgr$get_living_agents()) {
agent$age <- agent$age+1
Expand All @@ -185,9 +185,16 @@ To demonstrate programatically creating villagers, consider the model below that
- Every odd day, one villager dies

```
library(villager)
initial_condition <- function(current_state, model_data, agent_mgr, resource_mgr) {
for (i in 1:10) {
name <- runif(1, 0.0, 100)
new_agent <- agent$new(first_name <- name, last_name <- "Smith")
agent_mgr$add_agent(new_agent)
}
}

model <- function(current_state, previous_state, model_data, agent_mgr, resource_mgr, village_mgr) {
current_day <- current_state$step
print(current_day)
if((current_day%%2) == 0) {
# Then it's an even day
# Create two new agents whose first names are random numbers
Expand All @@ -206,7 +213,6 @@ library(villager)
coastal_village <- village$new("Test village", initial_condition, model)
simulator <- simulation$new(4, villages = list(coastal_village))
simulator$run_model()
mgr <- simulator$villages[[1]]$agent_mgr
```

## Advanced Usage
Expand Down
18 changes: 9 additions & 9 deletions man/Simulation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions man/agent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading