All colors are stored in an ordered list {c(0),...c(n-1)}. For each color in the list, there exists a corresponding rule - either turn right ("R") or turn left ("L"). When the ant encounters a patch with color c(i) it executes the corresponding rule r(i), changes the color of the patch to the next color c(i+1) in the ordered list of colors, and then moves forward one step. If the patch has the last color of the list it is changed to the first color in the list: c(n-1) -> c(0) (iow, the list wraps around). The behavior of the multicolor ant is defined by it's rule list and the length of the rule list determines the number of colors to use.
You can DOWNLOAD the .nlogo program HERE.
Implementation
In this program we use lists as a natural way to store colors and rules. Since there are only so many well distinguishable colors in netlogo we use the following list of 16 colors:set all-colors [ black violet lime orange red brown
yellow green turquoise magenta cyan
sky gray blue pink white]
and for each concrete instance of Langton's multicolor ant we use a subset of this color list depending on the length of its rule list. Therefore, the rule list is limited to a length of 16. The user enters the rule list in the input box "rules", so we check for and cap the length:
let len length rules if len >= 16 [ set rules substring rules 0 16 set len 16 ]
then we use that length to create the subset of the color list:
set colors sublist all-colors 0 len
Instead of checking whether the list wraps we simply use a second list which is rotated by one color and arrive at the output color by using the color from the second list. First we cut the first element from the front and then we add it as the last item to the second list:
set col2 butfirst colors ; rotated output color list set col2 lput item 0 colors col2
In demo mode the program plays a number of different multicolor instances, each for a certain number of time ticks, and displays a name for each. For this we use 3 lists - one with the rules for each instance, one with the times, and one with the names to display.
let rule-list ["LLRR" "LRRLLRRLLRRL" "LLRRL" ... ]
let time-list [ 3000000 1000000 500000 ... ]
let name-list ["1 - Symmetric" "2 - Square 'La Mariposa'" "3 - Ball" ... ]
and then we cycle through the lists as follows:
(foreach rule-list time-list name-list [ clear-output output-print ?3 set rules ?1 init repeat ?2 [ go ] ])
One final little detail: when the program is running and the user types in a new rule set that is shorter than the one that's already running this would result in an error. Therefore, we buffer the input field into another variable in our init function:
set rules2 rules ; buffer rules so typing in new ones while go is running ; does not create an error message

Keine Kommentare:
Kommentar veröffentlichen