ラベル NetLogo の投稿を表示しています。 すべての投稿を表示
ラベル NetLogo の投稿を表示しています。 すべての投稿を表示

2019年3月30日土曜日

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

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/

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.


2019年2月27日水曜日

NetLogo関連のブログ記事

ある必要に迫られて、小生がNetLogo(エージェントベーストモデリング向け)の利用に関して、これまでに書いたブログの記事をまとめてみました。

他の方にはほとんど役に立たないかも知れませんが、小生自身が再度エージェント指向モデリングをやる場合の資料的価値はありそうです。

ブログ記事は、一般的に言って、他の人に迷惑にならなければ、参考になる場合があるかも知れません。



「このブログ(sparse-dense byFoYo)」に掲載の記事

◎卒研のレベルを高める手がかり:Multi-level Models In NetLogo 6(その2)
  http://sparse-dense.blogspot.com/2017/02/multi-level-models-in-netlogo-6_22.html
◎クリスマスプレゼントか、NetLogo 6.0リリース(日本語に恩恵)
  https://sparse-dense.blogspot.com/2016/12/netlogo-60.html
◎スマホでマルチエージェントモデリング!
  https://sparse-dense.blogspot.com/2017/07/blog-post.html
◎卒研のレベルを高める手がかり:Multi-level Models In NetLogo 6
  https://sparse-dense.blogspot.com/2017/02/multi-level-models-in-netlogo-6.html
◎Deep Learningのビジュアライゼーション
  https://sparse-dense.blogspot.com/2016/11/deep-learning_18.html
◎マルチエージェントでDeep Learningの基礎
  https://sparse-dense.blogspot.com/2016/12/deep-learning.html
◎Construct a neural network (multilayer perceptrons) using micro:bit
  https://sparse-dense.blogspot.com/2018/06/microbittwo-layer-perceptronxor.html
◎Java 8 ラムダ式の初歩(続き1)
  https://sparse-dense.blogspot.com/2017/05/java-8_81.html  
◎NetLogo
  https://sparse-dense.blogspot.com/2016/11/netlogo.html
◎マルチエージェントシステム NetLogo3D
  https://sparse-dense.blogspot.com/2017/03/netlogo3d.html
◎KAITシンポジウム2016で「現実感のある」エージェント指向モデリングを展示
  https://sparse-dense.blogspot.com/2016/12/kait2016.html

「神奈川工科大学 情報工学科 ブログ」に掲載の記事

◎NetLogoのネットワーク(社会的/電子的)
  http://blog.cs.kanagawa-it.ac.jp/2013/02/netlogo.html
◎Android端末をリモコンとして活用!
  http://blog.cs.kanagawa-it.ac.jp/2012/06/android.html
◎ホタルの季節です
  http://blog.cs.kanagawa-it.ac.jp/2015/06/blog-post_21.html
◎NetLogoの3D機能で再びサボテンを観察する
  http://blog.cs.kanagawa-it.ac.jp/2013/02/netlogod.html
◎情報学部棟(12階建て)からの階段による避難
  http://blog.cs.kanagawa-it.ac.jp/2012/06/blog-post_16.html
◎平成27年度第1回のi-Androidの会が開催されました[5/27]
  http://blog.cs.kanagawa-it.ac.jp/2015/05/271i-android527.html
◎春休みにWebでお勉強(複雑系)
  http://blog.cs.kanagawa-it.ac.jp/2014/03/web.html
◎百円ショップで買ったサボテンにフィボナッチ数列を見る
  http://blog.cs.kanagawa-it.ac.jp/2013/01/blog-post_14.html
◎(続)NetLogoプログラミングのススメ
  http://blog.cs.kanagawa-it.ac.jp/2012/06/netlogo.html
◎NetLogoプログラミングのススメ
  http://blog.cs.kanagawa-it.ac.jp/2012/05/netlogo.html
◎多忙にも余裕を、硬派にも癒しを
  http://blog.cs.kanagawa-it.ac.jp/2012/05/blog-post_04.html
◎i-Androidの会が開催されました(12/24)
  http://blog.cs.kanagawa-it.ac.jp/2015/01/i-android1224.html
◎蝶の山越え:3年セミナーの一場面
  http://blog.cs.kanagawa-it.ac.jp/2014/01/blog-post_30.html
◎(続)6月9日オープンキャンパス研究室公開
  http://blog.cs.kanagawa-it.ac.jp/2013/06/blog-post_10.html
◎通算20回目の情報工学科アンドロイドの会が行われました
  http://blog.cs.kanagawa-it.ac.jp/2013/06/blog-post_5.html
◎段階的に「交通シミュレータ」
  http://blog.cs.kanagawa-it.ac.jp/2013/05/blog-post_24.html

追記:小生による「NetLogoのチュートリアル」は以下にあります:

https://sites.google.com/site/yamlabnetlogo/home


2018年6月4日月曜日

Consturuct a neural network (multilayer perceptrons) using micro:bit

notice:
This article treats only forward propagation, whereas, backward propagation is also demonstrated in another article. -> Please see here.

Let's learn the basics of neural networks using micro:bit. Understanding deepens by actually programming. For neural network learning to solve various tasks, back propagation is generally required. Learning with this back propagation requires considerably long time calculations. It is not realistic to do this with such a tiny microbit with low computing power, even though it is not impossible. Therefore, here we will try forward calculation only, using the edge weights of the already learned neural network. Even so, you can experience some of the important points of the neural network.

Neural network with micro:bit

 this illustrates evaluation of z = XOR(1,1) = 0

One thing to notice here is that one microbit plays a role of one neuron. By doing so, you can have an image that is close to real neurons. Here, XOR (Exclusive OR) is the problem. z = XOR (x, y) shows 1 only if either x or y is exclusively 1. Otherwise the value of z is 0. In the microbit, JavaScript and MicroPython can be used, but here we use MicroPython. The reason is that microbit JavaScript currently does not support calculation of float type data. Float type operation is mandatory for calculation of Neural network. Also, neurons (microbit) exchange signals with each other. For that we use send / receive on radio. This is not Bluetooth. Additionally, as the activation function, we use the sigmoid function.

The weights of the edge used above are the results of being learned by the following NetLogo program that is based on the following one:
The red line denotes a negative value, the blue line denotes a positive value, and the thickness shows the magnitude of the absolute value.

Weights used in the above example were obtained by this NetLogo  simulation

Please see the video for an actual operation example.
https://youtu.be/PPUcsXgCnZ4

In the first half, z = XOR (x, y) is calculated where x = 1, y = 1, and then the value 0.0178... was obtained, so that "0" was finally displayed. On the other hand, the second half is the case of x = 1 and y = 0. Now that 0.9852... has been obtained, "1" was displayed as the final result of z.