banner-nll

Lab 3 - It’s all a matter of procedure

Now that you have completed the first lab and are familiar with the NetLogo interface, and basic commands, its time to really dig into the software, by learning how to write your own procedures.

In NetLogo, commands and reporters tell agents what to do. Commands are actions for the agents to carry out. Reporters carry out some operation and report a result either to a command or another reporter.

Commands and reporters built into NetLogo are called primitives. The Primitives Dictionary has a complete list of built-in commands and reporters.

Commands and reporters you define yourself are called procedures. Each procedure has a name, preceded by the keyword to. The keyword end marks the end of the commands in the procedure. Once you define a procedure, you can use it elsewhere in your program.

Many commands and reporters take inputs -- values that the command or reporter uses in carrying out its actions.

Examples: Here are two command procedures:

    to setup
      ca             ;; clear the screen
      crt 10         ;; make 10 new turtles

    end

    to go

      ask turtles
       [ fd 1             ;; all turtles move forward one step
         rt random 10     ;; ...and turn a random amount
         lt random 10 ]

    end

Note the use of semicolons to add “comments” to the program. Comments make your program easier to read and understand. understand.

In this program:

setup and go are user-defined commands.

ca ("clear all"), crt ("create turtles"), ask, lt ("left turn"), and rt ("right turn") are all primitive commands.

random and turtles are primitive reporters. random takes a single number as an input and reports a random number that is less than the input (in this case, between 0 and 9). turtles reports the agentset consisting of all the turtles. (We'll explain about agentsets later.)

setup and go can be called by other procedures or by buttons. Many NetLogo models have a once-button that calls a procedure called setup, and a forever-button that calls a procedure called go.

In NetLogo, you must specify which agents -- turtles, patches, or the observer -- are to run each command. (If you don't specify, the code is run by the observer.) In the code above, the observer uses ask to make the set of all turtles run the commands between the square brackets.

ca and crt can only be run by the observer. fd, on the other hand, can only be run by turtles. Some other commands and reporters, such as set, can be run by different agent types.


For this lab, your task is to complete Tutorial # 3 from the NetLogo User’s Manual.

Some Hints to ease your learning:
1) When you get an error, check to see if your spelling is correct.  Did you transcribe the code from the tutorial properly?

2) Did you spell such common terms as turtles or patches properly?

3) While there is a natural tendency to want to cut-and-paste code from the tutorial, there is something to be said for writing the code yourself, in terms of learning the material and retention of ideas.

4) Take notes while you go through the tutorial. It isn’t a race to be finished first. Use the tutorial to learn how to create a NetLogo world.

 


With Support from:

The NetLogo Learning Lab is part of modelingcomplexity.org, the home of the Mesa State College Center for Agent-Based Modeling.

This website is copyright by Mesa State College, 2004. All rights are reserved.

Some materials are adapted from the NetLogo User manual, and are copyright Wilensky, U. (1999). NetLogo.  Center for Connected Learning and Computer-Based Modeling. Northwestern University, Evanston, IL.