Agent-Based Modelling In Practice

An Introduction to NetLogo

Dr Nick Malleson and Dr Andrew Evans
School of Geography, University of Leeds

http://mass.leeds.ac.uk/

[Full Screen]

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary
marmite
Photo attributed to Celeste Hodges (CC BY-NC 2.0)

Programming ...

Most ABM tools require some degree of programming

Programming in NetLogo has been simplified

Similar to other programming languages, but with shortcuts

Other popular packages (e.g. Repast Simphony or MASON) require Java knowledge

If you are inspired to learn more...

Try: Code Academy

And: code.org.

Software Tools / Platforms

Different types of ABM software

What are they?

Pieces of software to help people build models

Wide range of tools

Computer code ('libraries')

Entire graphical environment

Somewhere in the middle

Computer code ('libraries')

Researchers write software to perform useful functions:

Draw graphs

Visualise the model

Manage the schedule

Great for programmers

Less time spend worrying about admin, more time on modelling

Examples:

MASON

Repast Simphony

Mageo

Loads of others listed here

Graphical Environments

Repast behaviour editor

Entirely visual - no programming needed

Most useful for non-programmers

Examples

Agent Sheets

VisualBots

Repast Simphony

Modelling4All

Somewhere in the middle

NetLogo GUI

Some code writing, some visual development

More powerful than purely visual tools, but easier to use.

Save time having to learn to do simple tasks and concentrate on model behaviour

e.g. NetLogo

Base on Star Logo.

Popular teaching tool

Designed to be used by children

But also powerful

Developed by The Center for Connected Learning (CCL) and Computer-Based Modeling at Northwestern University

Free!

Uses Java in the background

Multi platform

Can be converted into applets (and embedded in websites)

Great for quickly putting a model together and thinking through ideas

Easy to build

Easy to interact with models

East to extract data and create plots

Excellent documentation: http://ccl.northwestern.edu/netlogo/docs/

Example - Segregation (Schelling)

Example - Ants

NetLogo Ants example

NetLogo for Crime Analysis

Example: Exploring crime theory

Birks (2012) model environment

Birks et al. (2012)

Randomly generated abstract environments

Theoretical 'switches'

Rational choice perspective

Routine activity theory

Geometric theory of crime

Example crime theories

Compare results to expected outcomes:

Spatial crime concentration

Repeat victimisation

Journey to crime curve

NetLogo for Crime Analysis

Example: Exploring crime theory

TheoryEnabledDisabled
Routine activities Agents assigned a 'home' and routine paths Random movements
Rational choice Victim attractiveness (based on risk, reward, effort) Homogeneous target attractiveness
Awareness space Dynamic awareness - alters offender decision-making Uniform environment awareness
Theoretical 'switches' in the model

Results:

All hypotheses are supported

Rational choice has lower influence

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary

The Program

NetLogo is "somewhere in the middle"

Graphical part (Interface) with sliders, graphs, buttons and a map

Scripting part (Procedures) which contains instructions (code)

NetLogo program

The Interface

NetLogo interface

Interface Components

Switch a NetLogo switch Slider a NetLogo slider
Button a NetLogo button Monitor a NetLogo monitor
Graph a NetLogo graph

The Information Tab

NetLogo info

The Program - Code

NetLogo code tab

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary

Turtles, Patches and the Observer

turtles and patches

There are two types of objects in NetLogo: turtles and patches.

Both are agents

They have rules that determine their behaviour

They can interact with other agents

Main differences:

Patches cannot move

You can create different types of 'turtle' (e.g. person, dog, cat, car, etc.)

 

Why turtles?

'Logo' language originally used to control robot turtles. It seems that the name 'turtle' has stuck..

Turtles, Patches and the Observer

turtles and patches

Also important: the observer

The 'god' of a model

Oversea everything that happens, give orders to turtles or patches, control other things like data input/output, virtual time, etc.

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary

Variables

In programming, variables are a way of storing information. E.g.

my-name = "Nick"

seconds-per-minute = 60

pi = 3.142

infected = True

Variables can belong to different objects in the model.

Examples:

Turtle variables: e.g. name, age, occupation, wealth, energy

Patch variables: e.g. height-above-sea, amount-of-grain, building-security, deprivation

Observer variables:total-wealth, weather, time-of-daypi

Different objects can have different variable values

Built-In Variables in NetLogo

Contexts and variables

NetLogo Commands

Commands are the way of telling NetLogo what we want it to do

Some examples

(don't worry, these will be explained properly in the first practical):

show "Hello World"Prints something to the screen
set my-age 13Sets the value of a variable
ask turtles [ ... ]Ask the turtles to do something
ask turtles [ set color blue ] Asks the turtles to turn blue

Commands are very well documented

Brackets

NetLogo uses both square [ ] and round ( ) brackets.

Round brackets are used to set the order of operations. E.g.:

 2 + 3  × 4 = 14

(2 + 3) × 4 = 20

Square brackets are used to split up commands. E.g.:

ask turtles [ ... ]

the ask command expects to find some more commands inside the brackets.

Contexts

Contexts and variables

Contexts are NetLogo's way of controlling where commands are sent.

There are three contexts:

  1. Observer
  2. Turtle
  3. Patch

Don't Panic: Lots of opportunity to understand these during the practicals..

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary

Flow Control

Programs are recipes

And computers are really, really stupid cooks.

Programmers need to tell the computer exactly what to do, and in what order

Geek joke:

Q: How do you keep a programmer in the shower forever?

A: Give him a bottle of shampoo that says "lather, rinse, repeat".

Flow Control and Logic

Usually, NetLogo will run through your code, one line after the other.

But! Sometimes there are two or more possibilities for what to do next.

if statements are one example:

... start here ...

if ( age < 18 )
  [ .. do something .. ]

if ( age > 18 )
  [ .. do something else .. ]

... now continue ...

Outline

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code
  8. Summary

Finally: Writing Nice Code

Computers don't care what code looks like

But there are some good conventions that we can use to make our code easier to understand by humans

Indentation

New blocks of code should be indented (moved to the right)

E.g. the if statements on previous slide

White space

Different sections of code can be separated by lots of white space

Comments

Comments are special parts of code that NetLogo will ignore.

Anything after a ; is ignored.

Use comments to explain what your computer code does.

Indentation

Good

if age = 15 [

  if count friends > 0 [
    set happiness ( happiness + 1 )
  ]

  if count friends > 5 [
    set happiness ( happiness + 5 )
  ]

]

Bad

if age = 15 [

if count friends > 0 [
set happiness ( happiness + 1 )
]

if count friends > 5 [
set happiness ( happiness + 5 )
]

]

Whitespace

Good

if age = 15 [

  if count friends > 0 [
    set happiness ( happiness + 1 )
  ]

  if count friends > 5 [
    set happiness ( happiness + 5 )
  ]

]

Bad (well, not too bad, but ..)

if age = 15 [
  if count friends > 0 [
    set happiness ( happiness + 1 )
  ]
  if count friends > 5 [
    set happiness ( happiness + 5 )
  ]
]

Comments

Good

if age = 15 [

   ; This happens if the agent is 15 years old
  if count friends > 0 [
   ; If at least 1 friend, then they're happy
    set happiness ( happiness + 1 )
  ]

  if count friends > 5 [
   ; If they have 5, then even more happy
    set happiness ( happiness + 5 )
  ]

]

Bad

if age = 15 [

  if count friends > 0 [
    set happiness ( happiness + 1 )
  ]

  if count friends > 5 [
    set happiness ( happiness + 5 )
  ]

]

Conclusion

  1. Tools for individual-level modelling
  2. Introduction to NetLogo
  3. The Program
  4. Turtles and Patches
  5. Variables
  6. Flow Control
  7. Writing Nice Code