2019年3月27日水曜日

An example of anonymous procedure (lambda expression) in NetLogo (1)

Let's take advantage of the anonymous procedure in NetLogo programming. This anonymous procedure is called lambda expression in other languages ​​such as Java. An example is shown in Fig.1. Thirty turtles are randomly arranged. Their types are red, pink and white. The problem is to connect the same colored turtles with a line, under the condition that only turtles of the same color in the range of radius 3 are targeted. The result is shown in the figure on the right.



There should be various NetLogo code to achieve this solution. Here, I created a source program like Fig.2.  I added sight as a new property of turtle (Line 1). This is to give turtles the ability to detect the existence of other turtles around them. This sight is given detection capabilities by calling procedure "getSight 3" (line 9). Here, "3" means to detect within the range of radius 3. This sight is quite different from other properties, such as color. The value of color is a constant like "red + 2", but the value of sight is an anonymous procedure (or lambda expression) as shown in line 14.

That is, in the setup procedure below, the value of sight is not determined, but instead a method is given to determine it. The specific value of sight is determined in the go procedure. The command "runresult sight" (line 18) evaluates the lambda expression sight here. The result should be a set of turtles of the same color, within a radius of 3. Then they are connected in a straight line by the command "create-links-with".



Find out all the properties that turtles have. For example, Fig. 3 shows properties for the turtle whose id (who) is 8. As mentioned above, you can confirm that a "procedure (reporter)" is set to sight (at the last row) unlike other properties. In this way, you can handle the procedure as if it were a value, enabling flexible processing in various situations.


0 件のコメント:

コメントを投稿