inst/examples/Wolf-Sheep-Predation/Wolf-Sheep-Predation-SpaDES.R

# Run the WolfSheepPredation SpaDES module
library(NetLogoR)
library(quickPlot)
library(SpaDES.core)
library(SpaDES.tools)

# Parameters values
wolfSheepParams <- list(.plotInitialTime = 0, .plotInterval = 10,
                        .saveInitialTime = 0, .saveInterval = 1,
                        grassOn = TRUE, grassTGrowth = 30,
                        nSheep = 100, gainFoodSheep = 4, reproSheep = 4,
                        nWolf = 50, gainFoodWolf = 20, reproWolf = 5)

# Model init
wolfSheepSim <- simInit(
  times = list(start = 0, end = 500),
  params = list(WolfSheepPredation = wolfSheepParams),
  modules = list("WolfSheepPredation"),
  paths = list(modulePath = system.file("examples/Wolf-Sheep-Predation", package = "NetLogoR"))
)
# Run the model
wolfSheepRun <- spades(wolfSheepSim) # use debug=TRUE for debugging

# Plot outputs
dev(6)
clearPlot()
timeStep <- 1:length(wolfSheepRun$numSheep)

maxSheep <- max(wolfSheepRun$numSheep)
maxWolves <- max(wolfSheepRun$numWolves)
maxGreen <- max(wolfSheepRun$numGreen / 4)

if (wolfSheepParams$grassOn == TRUE) {
  plot(timeStep, wolfSheepRun$numSheep, type = "l", col = "blue", lwd = 2,
       ylab = "Population size", xlab = "Time step",
       ylim = c(min = 0, max = max(c(maxSheep, maxWolves, maxGreen))))
  lines(timeStep, wolfSheepRun$numWolves, col = "red", lwd = 2)
  lines(timeStep, wolfSheepRun$numGreen / 4, col = "green", lwd = 2)

  legend("topleft", legend = c("Sheep", "Wolves", "Grass / 4"), lwd = c(2, 2, 2),
         col = c("blue", "red", "green"), bg = "white")
} else {
  plot(timeStep, wolfSheepRun$numSheep, type = "l", col = "blue", lwd = 2,
       ylab = "Population size", xlab = "Time step",
       ylim = c(min = 0, max = max(c(maxSheep, maxWolves))))
  lines(timeStep, wolfSheepRun$numWolves, col = "red", lwd = 2)

  legend("topleft", legend = c("Sheep", "Wolves"), lwd = c(2, 2),
         col = c("blue", "red"), bg = "white")
}

Try the NetLogoR package in your browser

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

NetLogoR documentation built on Aug. 17, 2022, 9:06 a.m.