knitr::knit_hooks$set(margin = function(before,options,envir) { if(before) par(mgp=c(1.5,0.5,0),bty="n",plt=c(.105,.97,.13,.97)) else NULL }) knitr::opts_chunk$set(margin=T,prompt=T,comment="",collapse=T,cache=T, dev.args=list(pointsize=11),fig.height=3.5, fig.width=4.24725,fig.retina=2,fig.align="center") dir.create("tmp") knitr::opts_knit$set(root.dir=paste0(getwd(),"/tmp"))
This vignette shows how to install and use the gamar
package.
gamar
The package is currently on the GitHub repository. To install it from this
repository you can use the install_github
function from the devtools
package. If this package is not installed on your system, install it
install.packages("devtools")
Then you can install the gamar
package by typing:
devtools::install_github("choisy/gamar")
Note that the gamar
package depends on the XML
package. The installation of
gamar
package automatically installs the XML
too, if not already installed
on your system. Then, you can load and attach the gamar
library:
gamar
library(gamar)
Note that you need the GAMA platform (version > 1.7.0) to be installed on your machine. For now, you have to set the path to the GAMA folder yourself:
defpath("path_to_your_GAMA_folder")
On most Mac it would be:
defpath("/Applications/Gama.app")
There are a number of example files in the examples
directory of the library:
(f <- system.file("examples",package="gamar")) dir(f)
Loading the sir
experiment defined in the sir.gaml
file:
dir() experiment1 <- getmodelparameter(paste0(f,"/sir.gaml"),"sir") dir() class(experiment1)
The experiment:
experiment1
Note that the function getmodelparameter
loads GAMA and creates the
workgamar
folder:
dir()
This folder contains at this stage an XML file containing the specifications of the newly created experiment:
dir("workgamar")
The parameters of the model:
getparameternames(experiment1)
The variables monitored during the simulation:
getoutputnames(experiment1)
Setting the values of the parameters (i.e. initial values of state variables and model paramters):
experiment1 <- setparametervalue(experiment1,"S0",990) experiment1 <- setparametervalue(experiment1,"I0",10) experiment1 <- setparametervalue(experiment1,"R0",0) experiment1 <- setparametervalue(experiment1,"beta",.3) experiment1 <- setparametervalue(experiment1,"gamma",.1)
Setting the frame rates of the monitored variables:
experiment1 <- setoutputframerate(experiment1,"susceptibles",1) experiment1 <- setoutputframerate(experiment1,"infected",1) experiment1 <- setoutputframerate(experiment1,"recovered",1)
Setting the number of steps to simulate:
experiment1 <- setfinalstep(experiment1,200)
Making a second experiment, modified from the first experiment (with new starting values of susceptibles and infected):
experiment2 <- setparametervalue(experiment1,"S0",995) experiment2 <- setparametervalue(experiment2,"I0",5)
Putting the two experiment in one single experiment plan:
experimentplan <- addtoexperimentplan(experiment1) experimentplan <- addtoexperimentplan(experimentplan,experiment2)
Running the experiment plan on two cores:
dir("workgamar") output <- runexpplan(experimentplan,2)
Note the creation of another XML file and a folder
dir("workgamar")
This later folder contains one XML file per experiment, containing the experiment's output, and another folder:
dir("workgamar/out_sir_1")
This later folder may contain the snapshots of the simulations.
At the end, we can also remove this workgamar
folder:
dir() cleaning() dir()
Plotting the dynamics of infected of the first experiment of the plan:
with(output[[1]],{ plot(step,I,type="l",lwd=2,col="red", ylab="number of infected",ylim=c(0,1000)) lines(step,S,lwd=2,col="blue") lines(step,R,lwd=2,col="green") })
Loading the experiment prey_predator
from the predator_prey
model:
experiment1 <- getmodelparameter( paste0(system.file("examples",package="gamar"), "/predator_prey/models/predator_prey.gaml"),"prey_predator")
The parameters of the model:
getparameternames(experiment1)
The monitored variables of the model:
getoutputnames(experiment1)
Setting the model parameters to values:
experiment1 <- setparametervalue(experiment1,"Initial number of preys: ",990) experiment1 <- setparametervalue(experiment1,"Initial number of predators: ",100) experiment1 <- setparametervalue(experiment1,"Predator probability reproduce: ",0.1)
Specifying the number of steps:
experiment1 <- setfinalstep(experiment1,100)
Adding the experiment to an experiment plan:
experimentplan <- addtoexperimentplan(experiment1)
Running the experiment (only one here) of the plan:
output <- runexpplan(experimentplan,1)
Plotting the dynamics of the number of preys:
with(output[[1]],plot(step,`Number of preys`,type="l",lwd=2,col="red", ylim=c(0,1050)))
We can also generate a movie of the simulation of one experiment:
dir() makemovie(output[[1]]) dir()
setwd("..") knitr::opts_knit$set(root.dir=".")
unlink("tmp",T,T)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.