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

Add support for loading resources from disk #34

Merged
merged 2 commits into from
Nov 14, 2020
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
26 changes: 23 additions & 3 deletions R/resource_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' \item{\code{remove_resource()}}{Removes a resource from the manager}
#' \item{\code{get_resource_index()}}{Retrieves the index of the resource}
#' \item{\code{get_states()}}{Returns a list of states}
#' \item{\code{load()}}{Loads a csv file of resources and adds them to the manager.}
#' }
resource_manager <- R6::R6Class("resource_manager",
public = list(resources = NULL,
Expand Down Expand Up @@ -66,9 +67,28 @@ resource_manager <- R6::R6Class("resource_manager",
#'
#' @return A list of data frames
get_states = function() {
vector_states = vector(length = length(self$resources))
# Create a data frame to hold the states
for (res in self$resources)
vector_states <- append(vector_states, res$as_tibble())
state_tibble <- tibble::tibble()
for (i in seq_along(self$resources)) {
if (i ==1) {
state_tibble <- self$resources[[i]]$as_tibble()
}
else {
state_tibble <- rbind(state_tibble, self$resources[[i]]$as_tibble())
}
}
return (state_tibble)
},

#' Loads a csv file of resources into the manager
#'
#' @param file_name The path to the csv file
#' @return None
load = function(file_name) {
resources <- read.csv(file_name)
for(i in 1:nrow(resources)) {
resource_row <- resources[i,]
self$add_resource(resource$new(name=resource_row$name, quantity=resource_row$quantity))
}
}
))
5 changes: 5 additions & 0 deletions tests/testthat/test-files/test-resources.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name,quantity
maize,10
salmon,5
trout,1
cashews,0
19 changes: 19 additions & 0 deletions tests/testthat/test-resource_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ test_that("the manager removes resources", {
resource_mgr$remove_resource(resource_1_name)
testthat::expect_length(resource_mgr$resources, 2)
})

test_that("the manager can load resources from disk", {
resource_mgr <- resource_manager$new()
file_path = "test-files/test-resources.csv"
resource_mgr$load(file_path)

# Test that the resources exist with the expected quantities
corn <- resource_mgr$get_resource("maize")
testthat::expect_equal(corn$quantity, 10)

corn <- resource_mgr$get_resource("salmon")
testthat::expect_equal(corn$quantity, 5)

corn <- resource_mgr$get_resource("cashews")
testthat::expect_equal(corn$quantity, 0)

corn <- resource_mgr$get_resource("trout")
testthat::expect_equal(corn$quantity, 1)
})
73 changes: 56 additions & 17 deletions tests/testthat/test-village.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,87 @@ test_that("propagate doesn't copy the initial state on year 1", {
# Check that the initial state is passed into the user's model on the first year
# This makes sure that models can set initial states inside their code

test_model <- function(currentState, previousState, modelData, population_manager) {
test_model <- function(currentState, previousState, modelData, population_manager, resource_mgr) {
if(currentState$year == 1)
currentState$carryingCapacity <- 999
resource_mgr$add_resource(resource$new(name="corn", quantity=5))
resource_mgr$add_resource(resource$new(name="salmon", quantity=6))

}
new_state <- VillageState$new()
new_village <- BaseVillage$new(initialState=new_state, models=test_model)
simulator <- Simulation$new(length = 2, villages = list(new_village))
simulator$run_model()

# Check that the initial state is 999
testthat::expect_equal(simulator$villages[[1]]$StateRecords[[1]]$carryingCapacity, 999)
last_record <- simulator$villages[[1]]$StateRecords[[1]]$resource_states
# Check that the initial state of corn is 5
corn_row <- match("corn", last_record$name)
corn_row<- last_record[corn_row,]

testthat::expect_equal(corn_row$quantity, 5)
# Check that it was copied to the second day's state
testthat::expect_equal(simulator$villages[[1]]$StateRecords[[1]]$carryingCapacity, 999)
salmon_row <-match("salmon", last_record$name)
salmon_row<- last_record[salmon_row,]
testthat::expect_equal(salmon_row$quantity, 6)
})

test_that("propagate runs a custom model", {
random_crop_stock_model <- function(currentState, previousState, modelData, population_manager) {
currentState$cropStock <- 11
corn_model <- function(currentState, previousState, modelData, population_manager, resource_mgr) {
if(currentState$year == 1) {
resource_mgr$add_resource(resource$new(name="corn", quantity=5))
}
else {
if (currentState$year == 3) {
# On the third year add 5 corn
corn_resource <- resource_mgr$get_resource("corn")
corn_resource$quantity <- corn_resource$quantity + 5
}
}
}

new_state <- VillageState$new()
new_village <- BaseVillage$new(initialState=new_state, models=random_crop_stock_model)
simulator <- Simulation$new(length = 2, villages = list(new_village))
new_village <- BaseVillage$new(initialState=new_state, models=corn_model)
simulator <- Simulation$new(length = 3, villages = list(new_village))
simulator$run_model()
testthat::expect_equal(simulator$villages[[1]]$StateRecords[[2]]$cropStock, 11)

last_record <- simulator$villages[[1]]$StateRecords[[3]]$resource_states

corn_row <- match("corn", last_record$name)
corn_row<- last_record[corn_row,]
testthat::expect_equal(corn_row$quantity, 10)
})

test_that("propagate runs multiple custom models", {
random_crop_stock_model <- function(currentState, previousState, modelData, population_manager) {
currentState$cropStock <- 11
corn_model <- function(currentState, previousState, modelData, population_manager, resource_mgr) {
if(currentState$year == 1) {
resource_mgr$add_resource(resource$new(name="corn", quantity=5))
}
else {
corn <- resource_mgr$get_resource("corn")
corn$quantity <-corn$quantity + 1
}
}

random_fish_stock_model <- function(currentState, previousState, modelData, population_manager) {
currentState$fishStock <- 3
salmon_model <- function(currentState, previousState, modelData, population_manager, resource_mgr) {
if(currentState$year == 1) {
resource_mgr$add_resource(resource$new(name="salmon", quantity=1))
}
else {
salmon <- resource_mgr$get_resource("salmon")
salmon$quantity <-salmon$quantity + 1
}
}
new_state <- VillageState$new()
new_village <- BaseVillage$new(initialState=new_state, models=list(random_crop_stock_model, random_fish_stock_model))
new_village <- BaseVillage$new(initialState=new_state, models=list(corn_model, salmon_model))

simulator <- Simulation$new(length = 2, villages = list(new_village))
simulator$run_model()
testthat::expect_length(simulator$villages, 1)
testthat::expect_equal(new_village$StateRecords[[2]]$cropStock, 11)
testthat::expect_equal(new_village$StateRecords[[2]]$fishStock, 3)

last_record <- simulator$villages[[1]]$StateRecords[[2]]$resource_states
corn_row <- match("corn", last_record$name)
corn_row<- last_record[corn_row,]
salmon_row <- match("salmon", last_record$name)
salmon_row<- last_record[salmon_row,]
testthat::expect_equal(corn_row$quantity, 6)
testthat::expect_equal(salmon_row$quantity, 2)
})