Posts tagged ‘netlogo’
| Gabriel |
NetLogo has pretty good documentation but sometimes I’d like to see a bit more example code. Fortunately “.nlogo” files are stored as text, which means you can grep them to find examples of how the function is used in the model library. For instance this command will give you a list of models in the model library that use the function “layout-circle.”
grep -l -r 'layout-circle' "/Applications/NetLogo 4.1/models/Sample Models/"
Note that NetLogo itself only allows one open file at a time which is why I like to have the example code open in a text editor while the program I’m working on is open in NetLogo itself. Personally, I use TextMate for this, in part because I like it in general but also because the Logo bundle works pretty well with NetLogo and features like code-folding are especially useful for NetLogo coding style.
| Gabriel |
A few days ago, I posted a diffusion model written in NetLogo. I’ve now uploaded a copy to the NetLogo Community Models, which means you can run it in your browser.
| Gabriel |
In an earlier post I posted a simple NetLogo model of the Bass model. Of course the whole point of NetLogo is that it makes it easy to build in network structure. As such I’ve revised the model to also allow a a network contagion effect (with a preferential attachment network structure). Here’s a screenshot of the model, mid-run. [Update, click on the screenshot to go to a live Java version of the simulation hosted by the NetLogo Community Models].
I still haven’t figured out how to embed a live NetLogo sim in a post, so to run it you’ll need to download the code below and save it as a text file with the “.nlogo” extension.
| Gabriel |
As part of my prep work for attending James Kitts’ “Modeling Emergence” workshop at ASA I’ve been playing with NetLogo and there’s some really cool stuff in there. The basic “models library” includes biology models like wolf-sheep as well as some social science models that I already lecture on but didn’t have a dynamic illustration for, most notably Barabasi’s preferential attachment model and the team assembly model Uzzi et al developed on Broadway musicals.
It’s also pretty easy to learn. After just a few hours of reading the tutorial, I was able to write the script below, which is kind of a replication of an older Stata script. The really exciting thing though is that NetLogo makes it much easier to do simulations with network and/or spatial structure so I could refine the model to have structure. (Actually the model library already has this in the form of the “virus on a network” model but the point is that it’s flexible). So while this sim doesn’t do anything new it’s nice that I can extend it to do new things.
Note that in theory you can embed a NetLogo simulation in a webpage but I don’t know how to get WordPress to do this. Even doing it locally on my mac, the embed doesn’t works in Safari (but does work in Chrome, Firefox, and Opera). In lieu of embedding the whole simulation, here’s the “procedure” source code, which will only work if you hook the variables “constant-hazard” and “endogenous-hazard” and the plot-pen “Totals:adoption” to GUI elements.
to setup clear-all setup-turtles end to setup-turtles crt 1000 ask turtles [set color white] ask turtles [ setxy random-xcor random-ycor ] end to decide-adopt let nadopt count turtles with [color = red] ask turtles [ if color = white [ if random 100 <= constant-hazard + ( (nadopt / 1000 ) * endogenous-hazard ) [set color red ] ;; adopt if random number (0-100) is lower than ;; constant hazard + proportion adopted * endogenous hazard ] ] end to go if ticks >= 100 [stop] decide-adopt tick do-plots end to do-plots set-current-plot "Totals" set-current-plot-pen "adoptions" plot count turtles with [color = red] end
Update, I realized that a .nlogo file is just a text file so I can put the whole thing in the post, including the GUI elements. To use it just save what’s below as a text file with the extension to “.nlogo”. Not quite as nice as embedding, but it’ll work. Code below the jump.