NetLogo Fooling Around
From Backspaces Wiki
| NetLogo Tutorial |
Its easy to fall into the trap of thinking NetLogo models are "serious" and you have to spend a lot of time on them.
It turns out you can (and should!) "sketch" as well as make masterpieces. By that I mean use NetLogo's facilities for simple explorations. You'll be surprised how quickly you can try things once you get started.
We'll try a few simple explorations on this page to give you an idea what we mean.
Contents |
Logistic Map
When Liz Bradley discussed the Logistic Map at the Santa Fe Institute's summer school, she mentioned something I wasn't aware of: that as chaotic as the Logistic Map is for high values of r (beyond 3.57), there are "islands of stability".
So while listening to the lecture, I wrote a really simple model in NetLogo:
globals [x0] to calc ca set x0 random-float 1 set-plot-x-range 0 2000 repeat 2000 [set x0 (R * x0 * (1 - x0)) plot x0] end
The interface looks like this:
| | |
- The View (patches, turtles) is hidden by select, shrink, and put plot on top!
- The R slider - Min: 1, Incr: .01, Max: 4
- Combine setup & go into one procedure by simply choosing an arbitrary (2000) repeat value.
- The Command Center is eliminated by the up/down arrow icon on its top right.
- To "zoom into" a specific area, change the slider. Example: Min=8.2, Incr=.001, Max=8.4.
Lorenz Attractor: 3D
In another of Liz's lectures, she presented the Lorenz Attractor. Using the 3D version of NetLogo, it was quite easy to setup the equations and UI for setting the parameters. Some fussing about was required to get the coordinate system right, see the settings panel below, right.
to setup
clear-all
crt 1
[ set size 4 setxyz x0 y0 z0
set shape "circle" set color red pen-down ]
end
to go
tick
ask turtle 0 [step]
if ticks = stop-ticks [stop]
end
to step ;; turtle proc
let deltax dt * (a * ( ycor - xcor))
let deltay dt * ((r * xcor) - ycor - (xcor * zcor))
let deltaz dt * ((xcor * ycor) - (b * zcor))
setxyz xcor + deltax ycor + deltay zcor + deltaz
end
Again, the code is minimal. A significant NetLogo 3D advantage is the ability to "fly around" the equation, seeing it from all angles. Here's what it looks like:
| | |
- 3D NetLogo is quite similar to 2D. The only difference above is setxyz. Here are the 3D primitives.
Wiggling
This is embarrassing, but true! It shows how important testing (and good math!) is to modeling.
In one of our models, a turtle would randomly turn a random angle. We had used:
right random wiggle-angle / 2 - random wiggle-angle / 2
I.e. the turtle would choose the turn angle using two random calls, one positive, the other negative.
We realized that a single random call would be fine:
right (wiggle-angle / 2) - random wiggle-angle
I.e. subtract a random number from a constant half of its max random value.
Well, something seemed fishy: are these the same? We built a test in less than a couple of minutes. And as we should have remembered from Probability 101, they are different. Here's the program:
to rand2 hist n-values 10000 [random wiggle-angle / 2 - random wiggle-angle / 2] end to rand1 hist n-values 10000 [(wiggle-angle / 2) - random wiggle-angle] end to hist [l] ca set-plot-x-range min l max l histogram l end
And here are the tests:
| | |
Blush!
Knuth
This model was inspired by Donald Knuth's Selected Papers on Computer Science, and a conversation on the Friam mailing list. The section of the book of interest was Counting the Paths on a Grid.
When I saw Knuth's grid, and how it was used, I though it'd be interesting to see how well it could be implemented in NetLogo. This is admittedly a more complicated program (it took 2-3 hours to get it good enough to post to friends), but its a good example of using NetLogo for calculations.
globals [prob]
patches-own [ok? occupied?]
to setup
ca
set prob 1
ask patches [ set pcolor 5 + random-float 2 set plabel-color white set occupied? false ]
crt 1
[ set color red set shape "circle" set size .4 set pen-size 3
setxy 0 0 stamp set occupied? true pd ]
draw-grid black
end
to go
tick
flood-fill
ask turtle 0 [
let p neighbors4 with [ok?]
draw-num count p
move-to one-of p
stamp
set occupied? true
]
if done? [stop]
end
to draw-num [n] set plabel word n " " set prob prob / n end
to setup-n-go setup while [not done?] [go] end
to-report done? report [occupied?] of patch max-pxcor max-pycor end
to draw-grid [c]
crt 1 [
set color c
set heading 0 foreach n-values world-width [?] [setxy ? 0 pd fd max-pycor pu]
set heading 90 foreach n-values world-height [?] [setxy 0 ? pd fd max-pxcor pu]
die
]
end
to flood-fill
ask patches [set ok? false]
let pset patch-set patch max-pxcor max-pycor
while [count pset > 0]
[ ask pset [set ok? true]
set pset patch-set [neighbors4 with [not ok? and not occupied?]] of pset ]
end
Points of interest:
- The path starts at the lower left and terminates at the upper right. To make this easier, we used the settings on the left below.
- There are only two turtles used: one to traverse the path, and a second to draw-grid. The latter "dies" after the grid is drawn.
- We are using the Drawing Layer for the grid, the path, and the circles.
- The patches are used as a background, and to post the number of possible path segments at that point for the next move.
- A flood-fill is used each tick to determine the "illegal moves", i.e. moves that will make it impossible to reach our goal. This is similar to the graph-of reporter in the Largest Bunch section of the Buttons model: both start with a single node (graph-of) or patch (flood-fill) and progress "outwards" to find all connected nodes or patches. Specifically, flood-fill marks all the patches "not ok", then starting at the upper right, finds all the accessible patches and makes them ok.
- Three monitors (1's 2's 3's) do not show a global variable, rather they calculate a value: how many of the path segments have a branch choice of 1 (i.e. a plabel of "1 "), 2, or 3. See below right.
| | |
- Although we have a one button "setup-n-go", we also include the usual tipple "setup, go, step" for looking at details. One use of these is looking at the flood-fill. Use setup, then step several times, then in the Command Center:
observer> ask patches with [ok?] [set pcolor sky]
| | |
Homework
This is the first section that we did not go into considerable detail, rather we just showed you some interesting examples of "fooling around" with NetLogo. Think seminar rather than class. So we'll suggest some do-it-yourself homework derived from the above examples.
- All examples: Look through all the examples for commands we haven't used yet. Look them up.
- Logistic Map: Use NetLogo to build a Bifurcation Diagram
- Lorenz Attractor: Replace the simple linear interpolation with the Runge Kutta method
- Wiggling: Why is the rand2 distribution "peaked" while the rand1 distribution uniform?
- Knuth: Add two additional path styles: one that takes the shortest step at each turn, and one that takes the longest. The flood-fill will now use distances rather than the ok? boolean. Here's what I used:
to flood-fill
ask patches [set dist -1]
let pset patch-set patch max-pxcor max-pycor
let d 0
while [count pset > 0]
[ ask pset [set dist d]
set d d + 1
set pset patch-set [neighbors4 with [dist = -1 and not occupied?]] of pset ]
end
Notes
- Of interest in the NetLogoTut download folder are: Bifurcation.nlogo, GridPath0.nlogo, GridPath.nlogo, Logistics.nlogo, Lorenz3D.nlogo.
