This description section is not mandatory but very helpful. Start by naming and describing the model using the symbol # in front of each line to “comment” the information. Save it under the name of the model in an appropriate folder.
#FOR LOOP NETLOGO CODE#
Open a new R script (using RStudio or an adapted text editor like Tinn-R) to write the code of the model in it. System.file ( "examples", package = "NetLogoR" )
#FOR LOOP NETLOGO MANUALS#
Please refer to their manuals for any related questions. Users can also create any other figures ( e.g., the number of turtles through time) on the R plot interface or generate outputs directly on the screen ( e.g., show the current time step).Īll these procedures need to be coded in the model, either using the plot() or Plot() functions adapted to the NetLogoR classes for the world visualization, or using R functions and of different packages for any other figures. These functions work the same way as the plot() ones, except when adding turtles on a world, use Plot(nameTurtles, addTo = "nameWorld") instead of points(nameTurtles).
Plot() functions (with a capital “P”) from quickPlot give a better rendering when plotting multiple time steps. Turtles can be plotted alone with plot(nameTurtles) or on a world already plotted with points(nameTurtles). To visualize only one layer of a worldArray, use plot(nameWorldArray]) or plot(nameWorldArray]). plot(nameWorld) works with both worldMatrix and worldArray. However, you can use different plot functions for visualizations similar to the “View” of NetLogo. There is no interface built-in as part of NetLogoR. For example, when moving turtles around using the fd function, the output of the function ( i.e., the turtles with new coordinates) needs to be re-assigned to the turtles object. Functions in R create new objects, they do not directly modify the ones given as arguments. The result/output of a function needs to be assigned to an object to be kept and re-used later in the model.
#FOR LOOP NETLOGO PATCH#
For example, in the NLdist function, the argument agents takes one patch coordinates, the argument agents2 takes another patch coordinates, the argument world takes a worldMatrix or a worldArray object to indicate in which world agents and agents2 are located and the argument torus indicate that the world is wrapped. In NetLogoR, the function arguments indicate which world and/or agents ( i.e., patches and/or turtles) are involved in the function. # Create a world according to a given extent w1 <- createWorld (minPxcor = 0, maxPxcor = 10, minPycor = 0, maxPycor = 10 ) # Report the distance between the patch and the patch pDist <- NLdist (agents = cbind (pxcor = 0, pycor = 0 ),Īgents2 = cbind (pxcor = 1, pycor = 1 ), world = w1, torus = TRUE ) In all cases of similar object types, we have implemented functions to convert between the types.
agentMatrix is based on matrices and has no coordinate reference system, both of which make operations dramatically faster than with SpatialPointsDataFrame objects. The key difference in this case is for speed. agentMatrix is similar to SpatialPointsDataFrame from sp package, with a few differences. In worldMatrix and worldArray, integer coordinates are in the middle of the patch or cell, i.e., (0, 0) is in the middle of the patch in a worldMatrix that has 1m resolution and integer coordinates, whereas, (0, 0) would be at the edge of a cell in a Raster* that has 1 m resolution and integer coordinates. In worldMatrix and worldArray, (0, 0) is at the bottom left, like a mathematical graph, whereas it is in the top left in Raster* objects, as in geographic coordinates. Two important differences are the mapping of X and Y coordinates and whether integer locations are in the center or at the corners of integer coordinate systems. WorldMatrix and worldArray are similar to RasterLayer and RasterStack in the raster package. Patches coordinates are always integers but turtles coordinates can have decimals ( i.e., a turtle can be positioned anywhere on a patch). Turtles are defined by their coordinates xcor and ycor and data associated in agentMatrix objects. See help("worldMatrix-class") for more details on the patch coordinates system. Patches are represented as matrix objects with two columns pxcor and pycor, representing the spatial location ( i.e., coordinates) of the center of the patches. Patches cannot move whereas turtles can move. There are two types of agents in NetLogoR: the patches and the turtles. It represents a two dimensional landscape, divided into a grid of square patches ( i.e., the matrix cells) of resolution 1. In NetLogoR, the world is either a worldMatrix object (similar to a matrix) or a worldArray object (similar to an array which is a collection of matrices).