NetLogo Bag of Tricks

From Backspaces Wiki

Jump to: navigation, search
NetLogo Tutorial

This section presumes you are now fluent enough with NetLogo that you can look at models, read their code (Procedures pane) and included documentation (Information pane), and understand them .. and indeed borrow the good ideas you see there!

So we'll fly through a group of models we include in the tutorial. In each case, look at the Information and Procedures, getting an overall understanding of what the model is doing. Look for ideas you may need in your models.

Contents

Downloading NetLogoTut.zip

When you downloaded NetLogo on the first page of the tutorial, you also (should have!) downloaded NetLogoTut.zip. If not, go ahead and do so now, just click the link.

Your browser should automatically download it and uncompress it to a folder named NetLogoTut.

If it doesn't download for you, control-click or right-click the link, and ask for the file to be downloaded. The file name is NetLogoTut.zip, and double clicking on it will generally uncompress it. If you run into problems, ask a friend for help.

What's in NetLogoTut.zip

You can place the NetLogoTut folder anywhere you'd like on your computer.

It contains several samples (*) we've used previously in the tutorial, as well as several new models:

 Ants.nlogo         *ButtonsOrig.nlogo       Flock.nlogo       *Logistics.nlogo
 Ants0.nlogo        *CmdCenter.nlogo         Flock0.nlogo      *Lorenz3D.nlogo
*Bifurcation.nlogo  *ExitTutBehavior.nlogo  *GridPath.nlogo     NBody.nlogo
 Bounce.nlogo       *ExitTutGo.nlogo        *GridPath0.nlogo    NBody3D.nlogo
*Buttons.nlogo      *ExitTutSetup.nlogo      LinkTravel.nlogo  *WiggleTest.nlogo

Two conventions:

  • A few models have a very simple version and a more complex one. The former's name will end in "0".
  • A model ending in "3D" will use the 3D version of NetLogo (and won't work in the standard version).

All the models have comments on their use and novel ideas in the Information pane. The code (Procedures pane) is commented to guide you, especially where there are fine points and subtleties.

The New Models

The 8 new models (5 models with 3 having slight variations) are:

Ants.nlogo        Ants0.nlogo
Bounce.nlogo
Flock.nlogo       Flock0.nlogo
LinkTravel.nlogo
NBody.nlogo       NBody3D.nlogo

Ants and Flock

These are two modeling classics: ants creating and following pheromone paths, and birds forming a flock by interacting with three simple rules.

The ants all start out in their home nest, the yellow square. They drop nest pheromone which diffuses as seen in the yellow lines radiating out from the nest. Initially they are looking for food pheromone, of which there is none yet, so they move randomly. Eventually they find food, the blue square, and head back to the nest, dropping blue food pheromone, which diffuses like the nest pheromone does. After several trips back and forth, a single path eventually dominates. The gray obstacle can be moved with the mouse to frustrate the ants and observe their creating a new dominate path.

In our flock, the "boids" start out flying in random directions. They obey three interactions rules:

  • Separate: avoid crowding neighbors
  • Align: steer towards average heading of neighbors
  • Cohere: steer towards average position of neighbors

The figure in the middle of the view represents the minimum separation (yellow circle), the maximum vision (red circle), and the maximum turning angle of the boids (the red "V"). "Neighbors" are boids within the vision radius.

As the boids interacts, they form a flock with greater common flight vector. The graph averages these vectors which can go from 0 (unordered) to 1 (ordered).

Bounce

This is yet another model predicated by the physics lectures. It occurred to me I should be able to simulate a bouncing ball! I tried several stunts, and came up with a surprise: use two spring layouts working together!

Look at the program and you'll see that we use one spring layout for the circle, and another for the "spokes" of the circle. They combine to give outward pressure as well as circular coherence. The model bounces the ball and you can see the deformation created when the ball hits the walls.

You can also grab any node and stretch it out from its original position, "throwing" the ball:

While you browse the code, note how simple the grab-a-node interaction is, inside the go procedure:

 if mouse-down?
 [ let t min-one-of turtles [distancexy mouse-xcor mouse-ycor]
   while [mouse-down?] [ ask t [setxy mouse-xcor mouse-ycor] ] ]

Also note the simple 1-liner procedures at the end of the program which make the code much simpler and compact.

to-report circle report turtles with [who > 0] end
to-report center report turtle 0 end
to-report circle-links report links with [not member? turtle 0 both-ends] end
to-report spokes report links with [member? turtle 0 both-ends] end
to-report radius report radius% * max-pxcor / 100 end
to-report circle-delta report (PI * 2 * radius) / circle-nodes end
to-report constrain [val minval maxval] report min list (max list val minval) maxval end
to-report constrainx [x] report constrain x min-pxcor max-pxcor end
to-report constrainy [y] report constrain y min-pycor max-pycor end

The point of this small, silly model is that NetLogo is a creative exercise, figuring out how to use its features in novel ways to achieve your goal.

LinkTravel

Generally agents travel on the NetLogo patches. But it is often useful for the agents to travel on a network or graph, using the links for roads or rivers or internet connections .. and many other possibilities.

LinkTravel is a tiny model which creates a graph, and places agents on the graph with a given velocity. The agents randomly switch to other links when they reach the end of their current one. We will use this idea in our Venice GIS model.

NBody

Considered by some as the beginning of Chaos theory, the n-body problem shows the quirky nature of a simple non-linear force: gravity. The force is inversely proportional to the distance, thus non-linear. For n, the number of bodies being considered, greater than 2, there is no closed form solution.

This model creates n gravitating bodies, with random velocity and mass. The body's size is in proportion to its mass.

Each step calculates the force between each body and all the others, as a vector sum. It then moves each "infinitesimally" according to Newtons laws. This results in apparently smooth motion.

As you can see, just 4 bodies create quite a mess! On the other hand, 2 bodies are stable. We also include a 3D version.

NetLogo GIS

As we have all learned over the last couple of years, GIS (Geographical Information Systems) have exploded upon the scene. Google Maps and Earth and tens of other web services have made Location Services a big deal. Our phones now are Location Aware.

Unfortunately, the core technologies are quite difficult. Projective transformations from a spherical earth to a flat map are very subtle and there are many. The "datum" of the earth is not a perfect sphere, creating a host of other tweaks to projections. Gathering all the layers of a given map and coordinating their transformations so that they align properly is not for the faint of heart!

Fortunately, recent releases of NetLogo have included a GIS extension. Although apparently simple, this extension creates one of the most approachable GIS systems we know of.


Like many other research groups, Redfish and the Santa Fe Complex have added GIS to our projects. We've used GIS to model wildfire evacuation problems, and studied water traffic for Venice's water transportation department. This section is a very brief tour of GIS modeling in NetLogo.

We'll present two models, one a simple example of getting started studying Santa Fe streets, the other a sophisticated prototype of Venice water traffic analysis.

Data

With GIS projects, Data Is Everything! It is often difficult to find, and after obtaining it, adapting it to your use. In our Venice model, we were fortunate to work with Fabio Carrera, an urban studies specialist. His vast repository of Venice data allowed us to use several shapefile layers (vector GIS data) along with their metadata. To study these and adapt them for our use, we used a freely available GIS package, uDig.

For our Santa Fe street study, we initially used publicly available US Census data. Their TIGER repository includes state and county data, including Santa Fe County.

Models

Because these models are quite large, ~10MB, due to their GIS data, we have a separate zip file, NetLogoTutGIS.zip. Click to download as before.

The SantaFeStreets model is quite simple, just using two shapefiles and manipulating them to support pan and zoom. It is designed for the starting point of several models:

  • Wildfire Evacuation: place cars on the map and have them evacuate away from a wildfire.
  • Traffic Flow: place cars on the map and examine their patterns of flow under crowded road situations.
  • Finding Bottlenecks: examine the street graph to find neighborhoods with restricted exit capacity for new road planning.

Here's the model zomed out so as to see the entire county. We also show a color elevation GIS raster as background to show the surrounding mountains. The city view is shown on the right, without a background. Use the Information tab for documentation. The code is fairly simple too, and will give you an idea of how to read and render GIS data. (Note: make sure to search the Model Library for GIS, it will give you other simple GIS models to look at.)

The Venice model is more elaborate. It is a prototype for a contract with the Venice city water traffic department, and is typical of initial "professional" investigations. Again, the Information pane gives quite a bit of detail. The chief novel idea here is creating a NetLogo graph (nodes and links) from the GIS data, and creating boat "agents" which travel along the links. The LinkTravel model above shows you how to travel along the graph created from the GIS data.

There is an odd problem with our model: we can't see the boats after clicking setup and go! Why? We are zoomed out so much that we see all of Venice, which makes any realistic boat too small to see. To help with this we have added a "toggle-boats" button which makes the boats unnaturally large but visible, mainly for debugging. Another solution is to use the 3D view and zoom in, giving the view on the right. The jaggies are due to zooming the Venice map far past its initial resolution. A finished model would add good pan and zoom controls directly to the model as we did with Santa Fe Streets above.

Personal tools