Courses/CS 461/Winter 2006/NetLogo
From CSWiki
While reading the NetLogo mailing list, I realized that the stop command does not stop the system. It simply stops the forever button in which it occurs. Since in most systems, the go button is the only forever button, and the go button is what drives the system to continue to take steps, stopping the go button effectively stops the system. But that isn't necessary. You can have multiple forever buttons. Whenever a forever button encounters a stop, it stops repeating itself. Encountering a stop does not stop the entire system.
Also note that buttons may be assigned to either the observer or to agents (patches or turtles). (See the Biology > termites model.) The go button is a turtles button. Its commands apply to an individuals turtle. However, when pressed, it is run in each of the turtles. In other words, this is a way to run agents as individual threads. Each thread runs on its own and without synchronization with the other threads. Presumably, they time-share the processor.
In experimenting with the Biology Wolf-Sheep Predation model, I did a couple of things. (All with grass off.)
- Prevent the last wolf or sheep from dieing. Add a guard to die.
if (count turtles with [breed = breed-of self] > 1) [die]
- Prevent wolves or sheep from reproducing if there are too many turtles overall.
to-report room-available? report count sheep + count wolves < (count patches) / 2 end to reproduce-wolves ;; wolf procedure if random-float 100 < wolf-reproduce and room-available? [ ;; throw "dice" to see if you will reproduce …
That limited the number of wolves and sheep but even so, eventually the sheep would take over. What happened is that the sheep would use up all the space and not let the wolves reproduce. There was nothing to kill them off except an occasional wolf catch. But since there were so many more sheep than wolves, the sheep would reproduce faster than the wolves could kill them. Since the total population of wolves and sheep together is limited, there were always many sheep and very few wolves!
The following was an attempt to prevent sheep from reproducing if they were too crowded. Don't reproduce unless this condition holds.
sum values-from neighbors [count turtles-here] < 2
But it didn't seem to help.
[edit] Logo, the NetLogo scripting language
One thing that I find difficult about NetLogo is figuring out how to parse it. The lack of required parentheses make reading NetLogo script difficult—at least for the uninitiated. Basically, it seems to be prefix or infix operator notation. Consider the previous example. It is read as follows.
(sum (values-from (neighbors) [count (turtles-here)])) < 2
The operators, in order of precedence from loosest binding to tightest binding are:
<, sum, values-from, count, neighbors, turtles-here
The final two, neighbors and turtles-here are 0-ary operators, which take as their argument the implicit object self. In other words, every element of the preceding expression is an operator, which returns a value. In addition, the square brackets [ and ] are also an operator that take their content and return a function, which is applied to elements of neighbors. (See more below.) In other words, NetLogo is a very sophisticated language.
The most significant portion is to the left of the '<' sign. The function sum takes the sum of a list. Most interesting is the values-from function, which illustrates how sophisticated the NetLogo language is.
The values-from function
values-from neighbors [count turtles-here]
takes two arguments: an AgentSet and a function. It applies the function to all elements of the AgentSet. That's a very functional programming-like construct because the function is defined on the fly. In this case, the function is
count turtles-here
That's an anonymous function which is applied to each element in the AgentSet neighbors.
Then the values-from function returns a list of the results that the function returns for each element in the AgentSet. In this case it returns a list consisting of the number of turtles in each neighboring patch.
Then the sum function is applied to that list, which it adds up. Finally, the result is compared to 2.
Let's look at the Primitives Dictionary as described in the NetLogo user documentation. From the above link, click on Primitives Dictionary.
[edit] Language syntax
It's worth noting that the language itself is minimal. Other than surrounding methods with to (or to-report) and end and requiring [ and ] in certain contexts, there isn't much to the language. Most of the control structures are defined as operators rather than as part of the language. The control structures are defined by in terms of the operators in the Control/Logic cataegory of primitives.

