Hi Anna,
this was new to me and the key is that it's using map/reduce to
construct a list of parts of a netlogo command. That gets evaluated
with run-result (which takes some text and executes is as a Netlogo
command), which in this case does some math to generate a number.
This is covered starting with page 391 of Wilensky's textbook.
Depending on your institution, you might be able to get access to
just that chapter here:
https://www.jstor.org/stable/j.ctt17kk851
The first few lines set things up:
Each turtle has a
strategy. There are also the two globals
that hold the possible
operators and
inputs.
The initial strategy gets set up here:
Breaking that down, let's go step by step (in the Command Center):
observer> show inputs
observer: ["heading " "xcor " "1 " "2 " "10 "]
observer> show (list one-of inputs)
observer: ["heading "]
observer> show (list one-of inputs)
observer: ["2 "]
To see how the rest works, I added a global variable
stratx
so we can walk through a version of that reporter.
observer> set stratx (list one-of inputs)
observer> show stratx
observer: ["10 "]
The next line has something like this that gets repeated 5 times:
observer> show (sentence stratx one-of operators one-of inputs)
observer: ["10 " "- " "1 "]
It's taken that original "10 " that got put into stratx (a list) and
added one element randomly from operators and one from inputs. When
it gets repeated 5 times and stored in stratx, it might look like:
observer> repeat 5 [ set stratx (sentence stratx one-of operators one-of inputs) ]
observer> show stratx
observer: ["10 " "* " "1 " "* " "10 " "- " "1 " "- " "heading " "+ " "xcor "]
So that shows how the inputs and operators get randomly built into a
strategy and if you ask the turtles their strategies after setup,
you'll see something like this:
observer> ask turtles [ show strategy ]
(turtle 11): ["xcor " "- " "2 " "+ " "2 " "- " "10 " "- " "2 " "+ " "xcor "]
(turtle 0): ["10 " "* " "1 " "+ " "1 " "- " "1 " "* " "xcor " "* " "heading "]
(turtle 12): ["heading " "- " "xcor " "* " "xcor " "* " "heading " "- " "1 " "- " "heading "]
(turtle 4): ["heading " "+ " "xcor " "* " "xcor " "* " "2 " "* " "xcor " "- " "heading "]
(turtle 14): ["2 " "+ " "xcor " "+ " "2 " "- " "2 " "+ " "1 " "+ " "heading "]
(turtle 18): ["1 " "- " "2 " "+ " "10 " "* " "1 " "+ " "xcor " "* " "xcor "]
(turtle 5): ["2 " "- " "2 " "- " "xcor " "- " "2 " "- " "heading " "- " "2 "]
(turtle 17): ["xcor " "* " "2 " "- " "heading " "+ " "1 " "+ " "2 " "+ " "heading "]
The next part we need to look at is this:
set heading run-result reduce word strategy
Looking back at the stratx I made above, it converts that list of
elements into a string that is a Netlogo expression:
observer> show reduce word stratx
observer: "10 * 1 * 10 - 1 - heading + xcor "
which is then evaluated with run-result. If this were a turtle's
strategy,
the expression is: 10 * 1 * 10 - 1 - [turtle's current heading] +
[turtle's current xcor]
Now for demonstration I've modified the code to only generate 1
turtle.
From that I can see its strategy:
When it does run-result, it's going to evaluate 1 - 81 * 81 * 10 +
10 + 2... to -65597
Then when the heading is outside the typical range, Netlogo wraps it
around to 0-360 appropriately (e.g. -65597 mod 360 = 283).
Note that go code has each turtle repeat the process 25 times, since
as it adjusts heading and moves, some of the values of inputs like
xcor
and
heading have changed. This is what causes some of the
turtles to move in arcs, etc.
Cheers!
Dale