In this post we have already considered Langton's ant. Today, let's have a bit of fun and look at the interaction of several Langton's ants (
DOWNLOAD the .nlogo file here). The interactions can be quite interesting and colorful.
Implementation
In this program, we add a slider to choose the number of ants. Each ant gets its own color with which it draws but they all react to colored patches of any color (iow they don't care if a patch has a color that's different from their own, the only important thing is that whether it has any color at all). This is easily implemented - all we have to do is check if a patch is non-white. The only thing that's left to define is what happens when two or more turtles sit on the same patch at the same time. In this situation, all turtles would decide the same - if the patch is white they would color it and if the patch has color they would delete it (set the color to white). Since agent commands are executed one after the other in Netlogo we need to make sure that a second turtle does not override the actions of the first turtle. An easy way to do this is as follows:
to go
ask turtles [
ifelse not any? turtles-here with [done? = true] [
ifelse pcolor = white [
rt 90
set pcolor color
set black-patches black-patches + 1
set done? true
]
[ ; colored patch
lt 90
set pcolor white
set black-patches black-patches - 1
set done? true
]
]
[ ; other turtle has been here before
; patch has already changed color
; apply rules "in reverse"
ifelse pcolor = white [ lt 90 ] [ rt 90 ]
]
]
ask turtles [
fd 1
set done? false
]
tick
end
Turtles have a
done? variable which is set to true when it has already worked on a patch. So all a turtle has to check for is whether there is another turtle on its patch which has already set
done? - if not, then the current turtle is the first one to work on that patch and should do so, but if yes, the patch has already be worked on and its color has already been changed - therefore, no further color change should be done and also the rules have to be interpreted "in reverse" - turn left on a white patch and turn right on a colored patch. We still use the
black-patches variable to performance optimize counting the number of colored patches for our output plot as in the single
Langton's ant example.
Keine Kommentare:
Kommentar veröffentlichen