/users/rlr/Courses/ICPSR-03/Handouts/netlogo-1.txt -------------------------------------------------- This is a short overview of the parts from which NetLogo models are built. This is gleaned and condensed from the NetLogo online manual. NB: Truth in Teaching Statement: As of July 2003, I am just learning NetLogo myself, so there is an even higher than normal chance that there are some errors in this! (E.g., i may accidently mix in syntax from c, java, objectiveC, etc...) The basic model parts are: - patches in 2D space: attributes, procedures, reporters - turtles: attributes, procedures, reporters - observer: procedures, reporters - global variables - Agentsets (eg turtles, patches) - built-in (primitive) commands, variables, actions - GUI components - Input: buttons, sliders, switches - Output: plotters, monitors - Graphics window -- main display of patches and turtles - Command Center -- user enter commands, various text output There are other constructs in the NetLogo language...see the User Manual for details. - Patches There is a 2D grid (torus) of discrete "patch" objects which forms the main graphical display. Each patch has some attributes, i.e., individual variables which contain information about each particular patch. Patches have some built-in attributes, e.g, pxcor, pycor -- x,y location of the patch. pcolor -- color of patch in display Other attributes can be added by using the patches-own statement in the Procedures section of the model specification. - Procedures and built-in Actions Like other object types, patches can do certain built-in actions (as specified in the Users Manual) and can do other procedures that the user defines. One way to think about these actions and procedures is that they are a block of commands that can be carried out by *each individual patch*. For example, a patch procedure might do: if (pxcor > 0) and (pycor > 0) [set pcolor green] Each patch could be asked to execute that statement, and thus patches in the upper-right quadrant would satisfy the condition and set their own color attributes to green. Procedures are defined like this (for the "setup" procedure): to setup ... end in the Procedures section of the model definition. The "..." can be actions from the built-in primitives of NetLogo, or they can be other procedures and reporters. Reporters are procedures that return a value to-report avgWealth ... [report avg-wealth] end Procedures and Reporters can take parameters...see the User Manual for details. - Turtles (Agents) These are usually the focal "agents" in the model, usually moving around in the 2D world, "on top of" the patches. Note that multiple agents can be in the same patch. Turtles have some built-in attributes, e.g., xcor, ycor -- location in 2D world. color -- color of turtle in display Other attributes can be added with the turtles-own statement. . NB: Any patch attribute can also be accessed by any turtle in a patch! So this works: ask turtles [set pcolor red] Every patch with one or more turtles will be set to red. Note that for xcor,ycor (and for pxcor,pycor) the center of the grid is 0,0, and all cells are in the ranges [0 - screen-edge-x, screen-edge-x) and similarly for the y axis. - Agentsets An "agentset" is just that, a set of agents. There are predefined agentset's called turtles and patches, which respectively have all turtles and all patches in them. For example, one can "ask" all turtles to set their color via ask turtles [set color red] ;; this is a comment after the ;; We can also "filter" agents out of an agentset, to create subsets based on some criteria: set rich-turtles turtles with [weath >= 0.95 * maxWealth] To deconstruct that: - the basic form of the statement is: set variable value where in this case: variable is rich-turtles value is the rest of the line. - The value part of that line is thus turtles with [weath >= 0.95 * maxWealth] which is of the form: agentset with [reporter] where in this case agentset turtles in this case (all turtles) reporter avgWealth >= 0.95 * maxWealth which "reports" (returns) the subset of turtles who have attribute wealth within 5% of the maxWealth (probably a global variable). - Observer This is a special "object" that controls the GUI objects and that does other jobs that aren't the responsibility of *particular* turtles or patches, e.g., the observer does the "setup" procedure, to create the initial turtles, initialize the turtles and patches, etc. - Global variables These are variables that can be accessed by all agents, and which have a single value. (Compare to attributes of agents like turtles xor,ycor, which are at least potentially different values for each turtle.) These are useful for aggregate variables, e.g. to describe agentset properties like avg-Wealth, max-Size, and so on. - GUI components The GUI (Graphical User Interface) in NetLogo can be built from a number of components in the interface "library" as seen on the Interface section of the overall NetLogo GUI. Common parts include: - Graphics window This is the main display, where patches and turtles are seen. - Buttons These can be used to control the model. Each button is defined by specifying: - its name - what procedure to execute when the button is pressed - whether it is a "forever" or a "once only" button, i.e., should that procedure be done once on the click, or keep being done over and over (until some other event stops it). For instance, most models will have at least setup - a button to tell the model to initialize the model. go - a button to tell the model to do the "go" procedure "forever". - Sliders These can be used to set global variables, e.g., selecting from a range of integer or real values. When a slider is defined, a global variable that contains the chosen value is also defined. - Switches The can be used to define and set global binary (on/off) variables. - Plots These can be defined to plot values vs model time steps. (They can do more than that, but that's all we'll consider here.) Thus to set one up, one specified the lines to be plotted and the source of the date, e.g., a "reporter" procedure that returns a value to be plotted each step. - Monitors This display single (current) values of varaibles. - Command Center This can be used to enter commands to the model, and to see printed output (if any is produced). Its useful for trying out commands to then be incorporated in procedures, or to enter commands to further explore the state of a model in the middle of a run. Note the menu selector just to the left of the command entry slot which selects the "context" of the command, i.e., Observer, Turtles or Patches. That is, that selects which agent(s) will receive the command entered in the command center. A note on recipients of commands: Procedures can only be executed by the type of agent that "knows how" to execute the commands in the procedure. For instance, if the setup procedure has to setup cct 40 ;; create custom turtles 40 of them end that cannot be sent to turtles, because turtles cannot do the cct command. However, there seem to be some times when the NetLogo semantics is a bit loose, in that this works when sent to the Observer: ask turtles [set pcolor red] This sets the color of the patches that have >= 1 turtles in them to red! On the other hand, this does not work: ask patches [set color green] The error is "You can't use color in a patch context, because color is turtle only." I guess this makes some sense, in that every turtle must be in a patch, so the first command can be interpreted as ask each turtle to tell the patch its in to set its color red whereas the second might run into problems, because not every patch has a turtle in it. In fact, I have learned that any attribute defined for patches can be accessed by any turtle in the patch!! That also means that you cannot define attributes with the same name for turtles and patches.