The NetLogo Primer: Introduction

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=Logo.png

Some Quick Questions

What is NetLogo?

It is a computer programming tool specifically designed to:

So is it like other computer 'languages'?

Yes .. It is an 'interpreted' programming language -- that provides a number of functions and commands that can tell the computer to do something, without us having to actually 'speak computer' (i.e. 001 00100010 010010 10110100 00 ... you get the picture).

Other examples of interpreted languages are R, MATLAB and Java. Most interpreted languages don't need compiling -- a step which translates our 'simple' written instructions into computer speak. .. An example of a language that does need compiling is C (and its variants).

You can think of this as a spectrum:

                                THE COMPUTER LANGUAGE SPECTRUM

Hard to write/understand    | <----------------> | Easy to understand/write
Very very fast              | <----------------> | Slower
Communicates directly with  | <----------------> | Needs interpretation to speak to the CPU
the CPU                     | <----------------> | (no compiling)
Serious 'geek' territory    | <----------------> | Entry level, to moderate enthusiast level
Extremely flexible          | <----------------> | Somewhat constrained to very constrained

What can I do with it?

You can program an infinite number of models. The models will traditionally have agents who move around a world which is divided into little squares called patches.

For instance, you could write a small program to simulate how birds flock in the sky, or how ants find your sugar bowl, or how humans exit a building that is on fire, or how traders' actions give rise to a price for certain goods, or ...

What can't I do with it?

Write an operating system (e.g. Windows). Actually, a lot of things .. because it has been built with a specific use in mind (agent-based modelling), it will not work very well for things like controlling a nuclear power plant, or managing financial records of a large company, or storing and manipulating masses of scientific data, or ...

Where does it come from?

The benevolent scientists at NorthWestern University in the US. They have a real interest (and the funding from the US Government) to bring complex systems and agent-based modelling into the classroom. Hence they have created NetLogo .. they have also built pictorial programming languages such as 'StarLogo' which are specifically for primary to lower high-schoolers to program with. Very neat.

Getting Started

NetLogo and Tabs

The Interface Tab

The first screen you see in NetLogo is called the Interface Tab. It is where the user will change various dials and switches, and press buttons to make the program run. The output from the program will appear in the 'world' -- the square box to the right, along with other plots and reporters that the program updates (none shown here).

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=InterfaceTab.png

Important components

  1. Control Tabs: This gives you access to the three main tab components of NetLogo -- the 'Interface', 'Information' and 'Procedures' tabs;

  2. Add Elements: The Add button is used often to add a new component, such as a variable slider (this creates a new global variable), or a plot, or a button, or a reporter etc.;

  3. Speed Slider: This allows you to change the speed of computation -- yes, you may actually want to slow it down!

  4. The World: the NetLogo world is like a patch-work quilt, everything happens here;

  5. Interactive Command Line: This is an advanced feature allowing the user to actually interact with the model at any time -- issuing commands.

The Information Tab

In any good coding practice, you must exhibit good coding hygiene -- that is, you must do a few little jobs along the way to make sure that your code/program is healthy, and useful for others.

The Information Tab is all about this -- it has an easy edit tab to click and add information under the pre-formed headings -- these headings can be changed, of course, although the ones that are there are an excellent start.

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=InformationTab.png

The Procedures Tab

This is where the main 'grunt' work of the program is -- notice that NetLogo have helpfully put the 'code' part of it at the 'back' .. this is a good way to think about it -- although we sometimes obsess about our code, it really only drives the outputs and interaction area for the user, and so ideally should always be 'back of house' and perhaps even never seen.

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=ProceduresTab.png

Some Quick Tips before going On

Two Quick Tips:

  1. If ever things are not looking right, or clicking buttons or menu items is not working for you, then select the halt item from the 'Tools' menu -- this halts any computation that is going on and can be a real life-saver!

  2. Code re-use: .. you know the 'reduce, re-use, recycle' .. the same is true in coding -- you should always try to write small, efficient code, and use other good code where it is available, adapting it for your particular context -- the Models Library is exactly what you need. There are lots of examples of other models that you can draw code from.

Halt the Computation!

The Models Library

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=Halt.png

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=ModelsLibrary.png

The Fundamental Elements of NetLogo: Patches and Turtles

Since NetLogo is designed for agent-based modelling it has as its key elements, well, agents and a world that they live and do their thing in. For NetLogo (for historical reasons), the agents are called Turtles (yes!) and the world is made up of what netlogo calls Patches.

Patches

Turtles

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=Patches.png

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=Turtles.png

How did you do that? (set patches to random colours)

  • To make the patches change colour we just ask them:

    • observer> ask patches [set pcolor random 100]

    • What?
      1. ask <turtles,agents> [<to do this>]

      2. set <property> <to this>

      3. pcolor -- means 'patch color', syntax is pcolor <color> .. in NL, colours are numbers, and the operation random <number> will create random numbers from 0 up to 100 which correspond to colours

One more things about patches -- the 'world' can either have hard edges, or continous edges. The choice depends on the application -- for example, in the flocking example, you don't want the birds to hit the edge of the world and stop (ouch!) -- instead, you'd like them to fly right over and perhaps re-appear back on the other side, hence you want a continous world. However, if you are thinking of a room that people have to escape because you've put a fire in there, then, well, you don't really want them to be able to go through walls .. you want hard edges.

To set this, click on Settings.png and from there either check or uncheck 'World wraps horizontally' (and/or vertically).

How did you do that? (make turtles appear)

  • NL has a cute way of making an agent/turtle appear, it uses the syntax sprout, as if they are born

  • Now, since they must be born somewhere, we actually ask the patches to sprout their own turtle
    • observer> ask patches [sprout 1]

  • What?
    1. ask we already know

    2. sprout <how many> .. sprout 1 gives us 1 turtle at each site

  • But when I do it, I get triangles, not turtles! ... that's right, because the default shape is a triangle, to make them have another shape we just ask them:

    • observer> ask turtles [set shape "turtle"]

  • But mine are random colours?! .. sorry, yes, you can set them to green .. can you think how to do that? (hint: observer> ask turtles [set <something> <something>]

What can I set or Change about a Patch or a Turtle

Each patch and turtle (when created) carries around with it a bunch of numbers and text .. sort of like we have a name, an address, perhaps a hair type, eye colour and maybe even favourite band etc. To get access to these, you simply have to right-click on the patch or turtle you are interested in, and select from the menu 'Inspect Patch X X' or 'Inspect Turtle X X'

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=PatchCursor.png

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=TurtleCursor.png

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=PatchInfo.png

/~sangus/cgi-bin/mointeach.cgi/MonashU/ECC3855ComputationalEconomics/NetlogoPrimer?action=AttachFile&do=get&target=TurtleInfo.png

For a Patch:

For a turtle

Examples

  • observer> ask turtle 100 [set size 5]

  • observer> ask turtle 100 [set color red]

  • observer> ask turtle 100 [set heading 0]

  • observer> ask turtle 100 [forward 5]

The Next Step

OK .. now you are orientated in the world of NL .. you should go and do the tutorials online:

... Happy programming!

MonashU/ECC3855ComputationalEconomics/NetlogoPrimer (last edited 2008-10-21 04:23:48 by Supervisor2012)