Mittwoch, 30. April 2014

Netlogo tutorial: turtle interacting with patches - Langton's ant

Langton's ant needs more than just patches - it is an interaction between a mobile agent (called a turtle in Netlogo) and the patches that make up the floor. The ant follows 2 very simple rules:

  • on a white patch, turn to the right 90°, color the patch black, then move forward 1 step
  • on a black patch, turn to the left 90°, color the patch white, then move forward 1 step
This is a well-known very simple example of a 2D-Turing machine in computer science.

You can download my implementation HERE.

Implementation

We implement the ant as a turtle. The interesting part is in the go procedure where the ant has to interact with the patch it stands on. This is actually very simple - every turtle can access the variables of any patch and vice versa (in OO-speak you'd say they are all public). So, it is only a matter of getting access to the right patch. In our example, we are interested in the patch the turtle is currently standing on which turns out to be the default. All we need to know is that patches store their color in a variable called pcolor and voilà, here is our go procedure:
to go   
  
  ask turtles [ 
    ifelse pcolor = white [ 
      rt 90
      set pcolor black
    ]
    [ ; black
      lt 90
      set pcolor white
    ]

    fd 1    
  ]
    
  tick
end

We also want to have a nice plot window that keeps track of how many black patches there are already. If we were to simply define the plot update command (accessible via the Edit menu for the plot) as
plot count patches with [pcolor = black]

this would be extremely slow. For performance reasons, it is much better to keep track of the number of black patches in a global variable. So we define
globals [black-patches]

at the beginning of the program, initialize that to 0 in the setup procedure and change our go procedure to this:
to go   
  
  ask turtles [ 
    ifelse pcolor = white [ 
      rt 90
      set pcolor black
      set black-patches black-patches + 1
    ]
    [ ; black
      lt 90
      set pcolor white
      set black-patches black-patches - 1
    ]

    fd 1    
  ]
...

And then we simply plot (in the Edit menu of the plot)
plot black-patches

One interesting aspect of Langton's ant is that after a period of seemingly chaotic behavior the ant starts to draw a very regular "highway". This happens shortly after 10000 ticks, so it might be a good idea to have some kind of stopping point in the program so we can, for example, fast forward to this point and then go slow to observer the process in detail. One easy way to make a stop point is to insert this at the end of the go procedure:
... 
 tick
 if ticks = 10000 [stop]
 
end

This will stop the go procedure and also depress the go button. When you hit go again, the simulation continues at tick 10001. You can also make a list of interesting points to stop at and the simulation will stop at each of them like this:
if member? ticks [10000 19000 29500 39000 47000 50000] [stop]

Conclusion

Turtles can interact with any patches by simply reading and writing their variables. The default patch is always the one the turtle currently sits on. Other patches can be accessed with functions which work relative to the turtle like patch-at, patch-ahead, patch-at-heading-and-distance, patch-left-and-ahead, patch-right-and-ahead or with the function patch which takes absolute coordinates and then using ask to instruct the patch to set the desired variable. For more info, see the Netlogo Dictionary. Eg. a go procedure could contain a construct like the following:
...
  ask turtles
    ifelse [pcolor = white] of patch-ahead 1 [ 
       ask patch-ahead 1 [set pcolor black]
...

Keine Kommentare:

Kommentar veröffentlichen