StarLogo is an agent-based simulation language developed by Mitchel Resnick, Eric Klopfer, and others at MIT Media Lab and MIT Scheller Teacher Education Program in Massachusetts. It is an extension of the Logo programming language, a dialect of Lisp. Designed for education, StarLogo can be used by students to model the behavior of decentralized systems.
The first StarLogo ran on a Connection Machine 2 parallel computer. A subsequent version ran on Macintosh computers; this version became known later as MacStarLogo (and now is called MacStarLogo Classic). The current StarLogo is written in Java and works on most computers.
Now StarLogo is available in a version called OpenStarLogo. The source code for OpenStarLogo is available online, although the license under which it is released is not an open source license according to the Open Source Definition, because of restrictions on the commercial use of the code.
Also StarLogo is available in a version StarLogoT, which is a programmable modeling environment for building and exploring multi-level systems. StarLogoT allows you to control the behavior of thousands of objects in parallel. Each object has its own variables and state. This allows one to model the behavior of distributed and probabilistic systems, often systems that exhibit complex dynamics.
Syntax and language structure
At the top of the procedures window, each agent (Turtles, Patches, and the Observer) declares its variables for the model. Each agent has some default variables; for example, every Turtle agent has the variables "color" and "heading". However, many models require giving agents new variables. For example, each turtle might have an energy value that is incremented each time it eats and decremented each time it moves. At the top of such a model we would declare this variable with the following text:
You can give each turtle more variables by adding to the list:
Turles-own [energy gender]
Now each turtle has a gender variable, which might be useful in a reproduction model. For patches the syntax is the same:
This variable could be used if each turtle eats some grass from the patch that it is on. The grass-length variable for the patch could then be decremented using the tset-grass-length command. Each turtle would then decrement the grass-length variable of the patch that it is on. See the Setting Variables section below.
Declaring arrays requires setting the length of the array. This determines how many elements, or variables, an array can hold. Arrays are declared just like other agent variables. So, if in addition to the variables "engery" and "gender" you want each turtle to have an array named "colors" with four elements, the array declaration has the following syntax:
turtles-own [energy colors gender]
An agent can change a variable that it owns by using the "set" syntax. For example, all turtles have a color variable, so the following code tells each turtle that has an energy level of at least 12 to set its color to red.
if (energy >= 12) [setcolor red]
However, sometimes you will want an agent to access and change the variable of another agent. The following code snippet tells each turtle to set the energy of any turtle directly above it to 12.
setengery-at 0 1 12
An agent can also set the values for variables that are owned by other types of agents. In a model where turtles eat grass, each turtle agent will need to decrement the value of the grass-length for the patch it is on. The syntax for this is the following:
tsetgrass-length (grass-length - 1)
Spacing and Syntax
There are no reserved symbols in StarLogo, therefore"+/-=" is a valid variable name. Given this freedom, there is a required coding convention regarding the use of spaces. All variables, operators, and primitives must have spaces between them (i.e, (16 / 12) + 3) . In all the StarLogoT code examples, you will notice this convention followed rigorously. The only exceptions are brackets and parentheses. Similarly, names for procedures and variables must NOT have spaces. Hence the liberal use of dashes to create legible compound words with no spaces, such as patches-own, grass-length, etc.
The comma is an often-misunderstood StarLogo operator. It is used to synchronize the parallel operations of turtles and patches. In effect, it enforces a "wait point" in the StarLogoT parallel execution. Turtles and patches will "wait" at a comma until all other turtles and patches have reached that point before continuing execution. For an example of why the comma operator can be important, look in at the "Patch Communications Demo" sample model in the included code examples.
Constants are like Global variables, but they do not change. Every agent has access to a constant. Constants are declared, like the variables, at the top of a model's procedures. In a model where it would be useful, for example, to have the value of gravity used by different agents in calculating their behavior, we can define the constant "gravity" with the following syntax:
constants [gravity [9.81]]
Now, the term "gravity" can be used throughout the model to substitute for the value "9.81"
Arrays are ordered lists of variables whose contents are individually indexed. Like regular lists, arrays can be owned by any agent. To create an array, include the array name, followed by the maximum index of the array (in brackets) within the variable list for a particular agent. For example:
globals [ global-array  ] turtles-own [ turtle-array  ]
To retrieve the value of a particular element in an array, use the following syntax:
arrayname-ref(-at, -of) index
To set the value of a particular element in an array, use this syntax:
setarrayname-ref(-at, -of) index value
The index is the spot in the array that is being accessed. Indexing starts at 0, so the value of index ranges from 0 to (array length - 1). The value is the value to which an array item will be set. For example:
; to set the first item in global-array to 100 setglobal-array-ref 0 100 ; to set the last item in global-array to 100 setglobal-array-ref 50 100 ; this command is illegal setglobal-array-ref 51 100
A string is a set of characters that makes up a word or a sentence. For example, "M", "Miss", and "Miss Muffet" are all strings. To create a one-word string in StarlogoT, use the quote (") character. To create a multiple-word string, or a string with special characters in it (such as the quote character itself), use the quote character followed by a vertical bar (|), and end the string once again with a vertical bar. For example:
globals [my-string] setmy-string "M setmy-string "Miss setmy-string "|Miss Muffet|
Notice that Starlogo does not require a concluding quote character. In fact, a concluding quote character will generate an error. Notice also that the quote character alone will not preserve capitalization. For best results, use "|string| notation all the time.
Inputs to Procedures
Procedures in StarLogo can take inputs. An input is specified by creating a temporary variable that begins with a colon. For example:
to walk :speed :direction setheading :direction fd :speed end
When walk is called from another procedure, it needs to be called with two arguments that will satisfy the input requirements "speed" and "direction".
to go walk 5 (random 360) end