tests/testthat/test-sim_farming.R

library("testthat");
library("resevol");
context("Simulation run tests");

test_that("Simulations are run and output is produced", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 40, ydim = 40, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 9, min_age_move = 0, max_age_move = 9,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 0, max_age_feed = 9,
                              food_consume = 0.25, pesticide_consume = 0.1,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.25, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 1, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 1, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 2, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0);
    expect_equal(length(sim[[1]]), 174);
    expect_equal(dim(sim[[2]]), c(40, 40, 21));
})


test_that("Movement with a reflective boundary", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 40, ydim = 40, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 9, min_age_move = 0, max_age_move = 9,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 0, max_age_feed = 9,
                              food_consume = 0.25, pesticide_consume = 0.1,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.25, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 1, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 1, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 2, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0, land_edge = "reflect");
    expect_equal(length(sim[[1]]), 174);
    expect_equal(dim(sim[[2]]), c(40, 40, 21));
})


test_that("Movement with a leaky boundary", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 40, ydim = 40, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 9, min_age_move = 0, max_age_move = 9,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 0, max_age_feed = 9,
                              food_consume = 0.25, pesticide_consume = 0.1,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.25, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 1, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 1, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 2, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0, land_edge = "leaky");
    expect_equal(length(sim[[1]]), 174);
    expect_equal(dim(sim[[2]]), c(40, 40, 21));
})

test_that("Movement with a sticky boundary", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 40, ydim = 40, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 9, min_age_move = 0, max_age_move = 9,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 0, max_age_feed = 9,
                              food_consume = 0.25, pesticide_consume = 0.1,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.25, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 1, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 1, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 2, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0, land_edge = "sticky");
    expect_equal(length(sim[[1]]), 174);
    expect_equal(dim(sim[[2]]), c(40, 40, 21));
})

test_that("Geometric crop growth", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 10, ydim = 10, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 30, min_age_move = 0, max_age_move = 30,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 20, max_age_feed = 23,
                              food_consume = 0.0, pesticide_consume = 0.0,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.0, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 100, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 100, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 1, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0, crop_growth = 0.1,
                              crop_growth_type = "geometric");
    crop_growth_value <- sim[[2]][1, 1, 2];
    expect_equal(crop_growth_value, 1.331);
})

test_that("Linear crop growth", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim       <- run_farm_sim(mine_output = mg, N = 100, xdim = 10, ydim = 10, 
                              repro = "sexual", neutral_loci = 1000, 
                              max_age = 30, min_age_move = 0, max_age_move = 30,
                              min_age_reproduce = 0, max_age_reproduce = 9, 
                              min_age_feed = 20, max_age_feed = 23,
                              food_consume = 0.0, pesticide_consume = 0.0,
                              rand_age = FALSE, move_distance = 1, 
                              food_needed_surv = 0.0, 
                              pesticide_tolerated_surv = 0.1, 
                              food_needed_repr = 0,
                              pesticide_tolerated_repr = 0,
                              reproduction_type = "lambda", mating_distance = 1,
                              lambda_value = 1,
                              movement_bouts = 1,
                              selfing = TRUE, feed_while_moving = FALSE,
                              mortality_type = 0, age_food_threshold = 0,
                              age_pesticide_threshold = 0, farms = 4,
                              time_steps = 4, mutation_pr = 0,
                              crossover_pr = 0, mutation_type = 0, 
                              net_mu_layers = 0, net_mu_dir = 0, 
                              mutation_direction = 0, crop_rotation_type = 2,
                              crop_rotation_time = 100, 
                              pesticide_rotation_type = 2, 
                              pesticide_rotation_time = 100, crop_per_cell = 1,
                              pesticide_per_cell = 1, crop_sd = 0, 
                              pesticide_sd = 0, crop_min = 0, crop_max = 1000,
                              pesticide_min = 0, pesticide_max = 1000,
                              crop_number = 1, pesticide_number = 1, 
                              print_inds = FALSE, print_gens = FALSE,
                              print_last = FALSE, K_on_birth = 1000,
                              pesticide_start = 0, immigration_rate = 0,
                              get_f_coef = FALSE, get_stats = FALSE,
                              metabolism = 0, crop_growth = 0.1,
                              crop_growth_type = "linear");
    crop_growth_value <- sim[[2]][1, 1, 2];
    expect_equal(crop_growth_value, 1.3);
})


test_that("Pest thresholds applied", {
    skip_on_cran();
    gmt       <- matrix(data = 0, nrow = 2, ncol = 2);
    diag(gmt) <- 1;
    mg        <- mine_gmatrix(gmatrix = gmt, loci = 4, layers = 2, indivs = 100, 
                              npsize = 100, max_gen = 4, prnt_out = FALSE);
    sim <- run_farm_sim(mine_output = mg_n1, repro = "asexual", 
                        pesticide_number = 1, pesticide_init = "random", 
                        pesticide_consume = c("T1"), farms = 5,
                        pesticide_rotation_time = 2, 
                        pesticide_rotation_type = 3, get_stats = FALSE,
                        pesticide_tolerated_surv = 0, pesticide_per_cell = 1,
                        crop_rotation_time = 4, crop_number = 1, 
                        crop_per_cell = 8, food_consume = 1, 
                        reproduction_type = "food_based", food_needed_surv = 1, 
                        food_needed_repr = 1, max_age = 4, min_age_feed = 0, 
                        max_age_feed = 2, min_age_move = 3, max_age_move = 4, 
                        min_age_reproduce = 4, print_gens = FALSE,
                        max_age_reproduce = 4, age_pesticide_threshold = 2, 
                        rand_age = TRUE, move_distance = 2, print_inds = FALSE,
                        immigration_rate = 10, time_steps = 4, 
                        print_last = FALSE, xdim = 18, ydim = 18,
                        trait_means = c(1, 1, 1, 1), land_edge = "torus",
                        pesticide_threshold = c(10000, 0, 0, 3, 10000), 
                        pesticide_delay = c(1, 1, 10, 1, 10));
    F1 <- sim[[2]][1,  1,  12];
    F2 <- sim[[2]][1,  10, 12];
    F3 <- sim[[2]][1,  18, 12];
    F4 <- sim[[2]][18, 1,  12];
    F5 <- sim[[2]][18, 10, 12];
    F6 <- sim[[2]][18, 18, 12];
    expect_equal(F1, 0);
    expect_equal(F2, 1);
    expect_equal(F3, 0);
    expect_equal(F4, 0);
    expect_equal(F5, 0);
    expect_equal(F6, 0);
})

Try the resevol package in your browser

Any scripts or data that you put into this service are public.

resevol documentation built on June 8, 2025, 10:35 a.m.