In the last article, I took an example using NetLogo's anonymous procedure (Lambda expressions). This time, as a continuation, I show another simple example. The problem is to find out pairs of right parenthesis and left parenthesis in multiple parentheses structures, as shown in Fig.1. The parentheses structure is given in a text format to the variable paren as shown in (a). The actual structure is as shown in (b). The result of detection of the pairs of left and right parenthesis is illustrated in (c).
This problem can be easily solved using a stack, as is well known. Here, referring to the documents [1] and [2], as shown in Fig. 2, the stack is represented by a list, and the operations of push and pop for it are given by lambda expressions (line 4-line 5). In pop, use the procedure getLast to get (and then to remove) the top element from the list. So far, it is still in preparation.
The actual parentheses check is performed by the foreach in line 5. First, please pay attention to "runresult paren". The variable paren is given the text as described above, but this input form has the ability to create this into a lambda expression for converting text to list. Therefore, runresult is applied. As a result, this part becomes a list. Foreach works on each element of this list, that is, on "(" or on ")". The variable "n" in the lambda expression in line 5 corresponds to one element of the list.
In the procedure "tinit", if the list element is "(", the value obtained by counting up the variable lpc is given to it as a label, and that element is pushed to the stack. On the other hand, if the list element is ")", get the label attached to the "(" at the top of the stack, and then set that label to the ")". This process determines the pair of parentheses. Since push is a lambda expression that does not return a value, it is evaluated by run, whereras, pop is a lambda expression that returns a value, so it is evaluated by runresult.
Well, "(" and ")" are both turtle agents. In order to display it in an easy-to-understand manner, the shape was defined independently. NetLogo provides a shape editor that allows users to freely define necessary shapes as shown in Fig.3.
References
[1] Alan G. Isaac, https://subversion.american.edu/aisaac/notes/netlogo-intro.xhtml#tasks-vs-procedures
[2] NetLogo Dictionary, https://ccl.northwestern.edu/netlogo/docs/
I am a professor emeritus of CS at Kanagawa Institute of Technology, Japan. Originally my specialty was parallel and distributed systems. My current interests include machine learning, natural language processing, creating mobile apps with MIT App Inventor, and quantum computing. In the web version of this blog, clicking the icon on the right (a plastic sphere) will take you to the "List of Quantum Computing Articles". - Fujio Yamamoto (for e-mail, add "@ieee.org" after "yamamotof")
2019年3月30日土曜日
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.
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.
登録:
投稿 (Atom)





