Transcript NetLogo Workshop - Wayne State University
Slides and model files will be at www.is.wayne.edu/drbowen > NetLogo workshop
NetLogo Workshop
Complexity And Business Analytics October 10, 2008, UM-Dearborn David Bowen, WSU Physics [email protected]
As a result of this workshop, (I hope) you will be able to:
12:30 – 2:10 • Describe agent-based modeling • Download & install NetLogo • Find & use existing models installed with NetLogo & on NetLogo website 2:30 – 4:10 • Modify an existing NetLogo model • Build a new NetLogo model 10/10/08 NetLogo Workshop 2
NetLogo Workshop
Part 1 • Agent-based modeling • Download and Install • Find and run
Agent-Based Modeling
• What is agent-based modeling?
Simulating a set of acting/interacting agents Examples of agents: people, organs, molecules, planets, family units, corporations, ribosomes Modeler assigns (programs): o Characteristics or properties of agent Homogenous or diverse agents o Rules or methods of interaction Example: Preferentially link to the most popular agent (the one that already has the most links) 10/10/08 NetLogo Workshop 4
Agent-Based Modeling
• What is agent-based modeling?
Agent-based modeling environment/toolkit: o Steps agents through actions/interactions o Displays agents, charts indicators, saves data NetLogo has four basic types of agents A: always o A Observer: sees all o A Turtles (agents), modeler can create subtypes o S Links between turtles, can have subtypes o A Patches or pieces of background Can be “invisible” but sometimes important (grass in Sheep and Wolves) 10/10/08 NetLogo Workshop 5
Agent-Based Modeling
• Where is agent-based modeling used?
Everywhere, e.g. all academic departments Natural systems (science, engineering etc.) o Many existing simulation techniques, question is purpose, strengths, weaknesses Social systems (humanities, social science, business/marketing etc.) o Often the best or only laboratory available 10/10/08 NetLogo Workshop 6
Agent-Based Modeling
• Types of use Real-world simulation, optimization Research Classroom: learning, demo, homework • Alternatives to making your own models Models Library downloads with installation Publicly available models Modify and existing model 10/10/08 NetLogo Workshop 7
Agent-Based Modeling
• About NetLogo Uri Wilensky et al. at Center for Connected Learning and Computer-Based Modeling at Northwestern University “Low floors, high ceilings” Free download Java-based (run anywhere) but private Java copy Discussion forums, technical support, continuous development 10/10/08 NetLogo Workshop 8
Download and Install
• URL: ccl.northwestern.edu/netlogo/ • Notice: mail lists, Resources (more models) • Download on the left • Save to desktop (because of rights on these computers, elsewhere you can accept the default) • Double-click on downloaded file to install (NetLogo4.0.3Installer.exe) • Location: up-arrow to desktop, add
\
NetLogo > Next • Accept all other defaults 10/10/08 NetLogo Workshop 9
Run
• Start > All Programs > NetLogo > NetLogo 4.0.3
• Notice: File menu item (top left) – Models Library, etc Interface – “the model” Information – How to use & change the model Procedures – the code 10/10/08 NetLogo Workshop 10
Models Library
• File > Models Library Checked except for (unverified) • Biology > Heatbugs (famous) Double-click model name or picture to open Setup then Go (Go again pauses) Information – what the model is doing Procedures o Turtles-own establishes properties (characteristics) o Button name starts “to” procedure with that name o ; for comment, can be whole or last part of line 10/10/08 NetLogo Workshop 11
Models Library
• File > Models Library > Computer Science > Cellular Automata > Life • Cellular Automaton Here, patches = cells, no turtles o Dark = cell alive, background color = dead Cell with 3 live neighbors comes to life Live cell with two live neighbors continues to live o Otherwise cell dies 10/10/08 NetLogo Workshop 12
Models Library
• Social Science > Party (famous in another form) Schelling model – two types of people o Here, men and women o Originally, Black and White o Otherwise the same – properties and methods How comfortable are these agents with those of the other type – slider Even a slight preference can lead to complete segregation 10/10/08 NetLogo Workshop 13
Models Library
• File > Models Library > Networks > Small Worlds (famous – six degrees of separation) Uses links Path along links from one agent to another Setup = ring, links to 2 nearest on each side Average path length for all pairs of agents Even a few links across the ring reduces the average path length significantly Significant in organizations, e.g. businesses 10/10/08 NetLogo Workshop 14
Models Library
• File > Open > Biology > Wolf Sheep Predation • Different kinds of agents, including patches • Click grass? Switch to On • Boom-and-bust cycles very common 10/10/08 NetLogo Workshop 15
Models on NetLogo site
• URL: ccl.northwestern.edu/netlogo/ > Community Models • Possible problems if models are written for earlier versions of NetLogo My Two-color gas works with 4.0.3
My Brownian motion doesn’t 10/10/08 NetLogo Workshop 16
NetLogo Models on the Web
• • Solves problems with old versions since Java Virtual Machine (VM) downloaded also (but different versions can coexist) www.is.wayne.edu/drbowen/ Models Brownian motion works Big Bang model > Classroom
10/10/08 NetLogo Workshop 17
Extensions
• Array, Table, GoGo, Profiler, GIS, Sample, and Sound are in the standard installation • In Extensions subfolder of NetLogo installation folder • Each is explained in Help > NetLogoUser Manual > Extensions • Some (GIS, Table) come with existing models 10/10/08 NetLogo Workshop 18
NetLogo Workshop
Part 2 • Modify • Roll your own
Programming Resources
• Help > User Manual Tutorials Interface Guide – World, Menu items, Controls o World - Settings: size, wrapping Vs reflecting Programming Guide – big picture o Types: observer (starts off), turtle, patch, link o What can I do with agents?
NetLogo Dictionary – the nitty gritty o Alphabetical list o By type 10/10/08 NetLogo Workshop 20
Modify One
• File > Model Library > Computer Science > Cellular Automata > Life • Procedures – scan to numbers 3 and 2 • On the way patches-own – creates properties for patches o Built-in: pxcor, pycor, pcolor o living? - ? makes this logical (true/false only) • 3 4, 2 3 • File > Save As a new name, e.g. LifeDB on desktop 10/10/08 NetLogo Workshop 21
Modify Life
• Add a slider to vary number on interface • Click on down-arrow to select slider Global variable init-number, 1 to 5, value 2 Then right-click to Select, squeeze it in • In Procedures, 2 3 init-number, init-number + 1 / Save, Setup then Go 10/10/08 NetLogo Workshop 22
Modify Life
• You now load this modified version from the desktop or by double-clicking the desktop icon “Back home” you can keep the .NLOGO file in any folder you choose 10/10/08 NetLogo Workshop 23
Modify Small Worlds
• File > Models Library > Networks > Small Worlds • On interface, put in a slider: Global variable init-links Minimum 1 Increment 1 Maximum 3 Value 2 • Save on desktop under a new name Add initials?
10/10/08 NetLogo Workshop 24
Modify Small Worlds
• Procedures, scroll through (bigger than Life) Find to setup, then see make-turtles then wire-them (this creates the links) About 75% down (Find?) find to wire-them What we want to do: o Always link to n+1 o If init-links = 2 or 3 then also link to n + 2 o If init-links = 3 then also link to n + 3 Dictionary: if condition [ commands ] 10/10/08 NetLogo Workshop 25
Modify Small Worlds
• Error when running with init-links = 1; divide by zero Divide by clustering-coefficient-of-lattice divide by dum, protect dum from being zero o We might eventually want a better fix 10/10/08 NetLogo Workshop 26
Modifying Models
• Find a model that does something close to what you want, and modify it to suit • For these modified models, we should go back and update the information • Structured and commented code helps – do this for yourself, too 10/10/08 NetLogo Workshop 27
Roll Your Own
• Decisions needed System to be modeled What do the agents represent?
What are the rules of action and interaction?
How will you approximate these rules for entry into the modeling environment?
What does a tick (cycle) represent?
10/10/08 NetLogo Workshop 28
Other
• You might want to change the World settings Size Location of 0, 0 Wrapping or box Patch size (also determines size of world screen) 10/10/08 NetLogo Workshop 29
Programming in NetLogo
• Case-insensitive • To set a equal to b: set a b • Arithmetic operators must have space on either side: set a 3 * b • Can use parentheses to control order • Use [ ] to block code statements • White space ignored after minimum space • Routines start with to, end with end • No number types • Logical variables end in ?
10/10/08 NetLogo Workshop 30
Programming in NetLogo
• Context: who is acting?
ask turtles [ commands ] sets context to turtles o Agents can ask other agents or even other types of agents ask turtles [ … ask links [ … ]… ] Only that particular agent can find the value or change a value that it owns If one agent (e.g. a turtle) is acting, other turtles is set of all other turtles, one-of other turtles is a random other stop to stop asking • breed to subtype an agent: breed [ cats cat ] 10/10/08 NetLogo Workshop 31
Simple Model
• Model will: Setup o Create turtles with random headings (this is the default), positions, speed Go o Let turtles go with speed and headings o Randomly change speed and headings Sliders to control speed range and size of heading changes 10/10/08 NetLogo Workshop 32
Simple Model
• Built-in turtle variables (each turtle has its own) breed (sub-type) Color (defaults to random for each turtle) Heading (degrees, 0 = up, 90 = right) hidden?
label, label-color pen-mode, pen-size shape size Who (a number) xcor, ycor (by default 0, 0) 10/10/08 NetLogo Workshop 33
Network Model
• Model will: Setup o Create a set of turtles with random positions o Create a random set of links between the turtles Each turtle has same chance of having links Go Once o Pick a random turtle and a random one of its links, and replace it within one attached preferentially Higher chance of linking to a turtle with the most links 10/10/08 NetLogo Workshop 34
Network Model
• Start new model, save it to desktop as Network • Go to Model Library, open Network > Preferential Attachment, copy layout and limit-magnitude to clipboard and then into Procedures window, and resave Will make a better World plot 10/10/08 NetLogo Workshop 35
Network Model
• Agentset – a list set of agents Defaults to a random order but can be sorted Can be “traversed” – go through all agents in the list by asking Can also pick a random agent from the list If we want angentset to persist, must set it to a name we have previously declared in globals or locally • If we have picked a turtle, other turtles is an
of all of the other turtles 10/10/08 NetLogo Workshop 36
Agentsets and Lists
• Agentset: an unordered set of agents of any type, including breeds Order is randomized on each use Can ask agentset [ list of commands ] Current agent in “ask” sequence: self • List: an ordered list of any objects, including agents Order is always the same unless you change it Can be sorted, etc.
Current list item in “for each” sequence: ?
10/10/08 NetLogo Workshop 37
Jumpers
• Transport • Agents jump by patches left to right • Have to wait at each patch for its individual wait count, can’t jump on top of another • Setup: Initialize patches with wait counts, color-code • Go: Turtles jump to right if wait count reached and that patch is empty After jumping off last patch on right, turtle dies 10/10/08 NetLogo Workshop 38
And…
• 3D • Numerical modeling (modeling number of agents, etc.)
• Flying behind a turtle
10/10/08 NetLogo Workshop 39